I'm working on a daily login system however cannot get around one thing(been trying to learn how to read/store from a file). Using this as base btw: https://www.rune-server.ee/runescape...ml#post5123327
The idea is the reward is loaded from a file depending on the date of the month(1st April, 2nd April, 3rd April ect..)
The files are as such: http://prntscr.com/j30tcw
The contents of each file would look something like
Format = ITEM ID, AMOUNT
DailyLogin.java - handles the Daily Login
Code:
package com.ruse.world.content.DailyLogin;
import java.util.Calendar;
import com.ruse.world.entity.impl.player.Player;
public class DailyLogin {
static Calendar cal = Calendar.getInstance();
static int year = cal.get(Calendar.YEAR);
static int date = cal.get(Calendar.DAY_OF_MONTH);
static int month = cal.get(Calendar.MONTH)+1;
static int day = cal.get(Calendar.DAY_OF_MONTH);
private Player c;
public DailyLogin(Player p) {
this.c = p;
}
public static int getLastDayOfMonth(int month) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month -1, date);
int maxDay = calendar.getActualMaximum(date);
return maxDay;
}
public void IncreaseStreak() {
c.LastLoginYear = year;
c.LastLoginDate = date;
c.LastLoginMonth = month;
c.LoginDay = day;
DailyReward.GiveReward(c);
c.LoginStreak++;
}
public void RefreshDates() {
cal = Calendar.getInstance();
year = cal.get(Calendar.YEAR);
date = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH)+1;
day = cal.get(Calendar.DAY_OF_MONTH);
}
public void LoggedIn() {
RefreshDates();
if ((c.LastLoginYear == year) && (c.LastLoginMonth == month) && (c.LastLoginDate == date)) {
c.getPacketSender().sendMessage("You have already recieved your daily reward for today.");
return;
}
if ((c.LastLoginYear == year) && (c.LastLoginMonth == month) && (c.LastLoginDate == (date - 1)))
IncreaseStreak();
else if ((c.LastLoginYear == year) && ((c.LastLoginMonth + 1) == month) && (1 == date))
IncreaseStreak();
else if ((c.LastLoginYear == year-1) && (1 == month) && (1 == date))
IncreaseStreak();
else {
c.LoginStreak = 0;
IncreaseStreak();
}
}
}
DailyReward.java - handles reward
Code:
package com.ruse.world.content.DailyLogin;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import com.ruse.world.entity.impl.player.Player;
public class DailyReward {
private static final String FILE_DIRECTORY = "./data/daily/";
public int day = DailyLogin.day;
public String ItemReward;
private String[] file = new String[30];
public static void GetReward() {
}
public static void GiveReward(Player p) {
switch(p.LoginDay) {
case 1: //1st day
//Add reward for first login here
p.getPacketSender().sendMessage("You have logged in for the first time! log in tomorrow for a better reward");
break;
case 2: //2 logins in a row
//Add reward for 2 logins here
p.getPacketSender().sendMessage("You have logged in for the second time! log in tomorrow for a better reward");
break;
case 3: //3 logins
//Add reward for 3 logins here
p.getPacketSender().sendMessage("You have logged in for the third time! log in tomorrow for a better reward");
break;
}
}
String readFile(String fileName) throws IOException {
String GetDay = Integer.toString(day);
File file = new File(FILE_DIRECTORY + GetDay + ".txt");
BufferedReader br = new BufferedReader(new FileReader(file));
try {
StringBuilder sb = new StringBuilder();
ItemReward = br.readLine();
while (ItemReward != null) {
sb.append(ItemReward);
sb.append("\n");
ItemReward = br.readLine();
}
return sb.toString();
} finally {
br.close();
}
}
}
To sum it up I just need some help loading the reward from the file.
Any ideas would be highly appreciated.
(Before you comment on the PI looks of a lot of the code, I converted it however want to get it working before I fully rename everything)
EDIT: NVM found it easier to just use JSON instead of .txt