Heey rune-server, I need help trying to fix my playersave or playersaving files to make it so that it auto saves each new player. I got my account to save, however no new players characters have saved.
Playersave code:
Code:
public class PlayerSave {
/**
* Tells us whether or not the player exists for the specified name.
*
* @param name
* @return
*/
public static boolean playerExists(String name) {
File file = new File("./Data/characters/" + name + ".txt");
return file.exists();
}
/**
* Tells use whether or not the specified name has the friend added.
*
* @param name
* @param friend
* @return
*/
public static boolean isFriend(String name, String friend) {
long nameLong = Misc.playerNameToInt64(friend);
long[] friends = getFriends(name);
if (friends != null && friends.length > 0) {
for (int index = 0; index < friends.length; index++) {
if (friends[index] == nameLong) {
return true;
}
}
}
return false;
}
/**
* Returns a characters friends in the form of a long array.
*
* @param name
* @return
*/
public static long[] getFriends(String name) {
String line = "";
String token = "";
String token2 = "";
String[] token3 = new String[3];
boolean end = false;
int readMode = 0;
BufferedReader file = null;
boolean file1 = false;
long[] readFriends = new long[200];
long[] friends = null;
int totalFriends = 0;
try {
file = new BufferedReader(new FileReader("./Data/characters/"
+ name + ".txt"));
file1 = true;
} catch (FileNotFoundException fileex1) {
}
if (file1) {
new File("./Data/characters/" + name + ".txt");
} else {
return null;
}
try {
line = file.readLine();
} catch (IOException ioexception) {
return null;
}
while (end == false && line != null) {
line = line.trim();
int spot = line.indexOf("=");
if (spot > -1) {
token = line.substring(0, spot);
token = token.trim();
token2 = line.substring(spot + 1);
token2 = token2.trim();
token3 = token2.split("\t");
switch (readMode) {
case 0:
if (token.equals("character-friend")) {
readFriends[Integer.parseInt(token3[0])] = Long
.parseLong(token3[1]);
totalFriends++;
}
break;
}
} else {
if (line.equals("[FRIENDS]")) {
readMode = 0;
} else if (line.equals("[EOF]")) {
try {
file.close();
} catch (IOException ioexception) {
}
}
}
try {
line = file.readLine();
} catch (IOException ioexception1) {
end = true;
}
}
try {
if (totalFriends > 0) {
friends = new long[totalFriends];
for (int index = 0; index < totalFriends; index++) {
friends[index] = readFriends[index];
}
return friends;
}
file.close();
} catch (IOException ioexception) {
}
return null;
}
/**
* Loading
**/
public static int loadGame(Client p, String playerName, String playerPass) {
String line = "";
String token = "";
String token2 = "";
String[] token3 = new String[3];
boolean EndOfFile = false;
int ReadMode = 0;
BufferedReader characterfile = null;
boolean File1 = false;
try {
characterfile = new BufferedReader(new FileReader(
"./Data/characters/" + playerName + ".txt"));
File1 = true;
} catch (FileNotFoundException fileex1) {
}
if (playerExists(Misc.removeSpaces(playerName))
&& !playerExists(playerName)) {
p.disconnected = true;
return 3;
}
if (File1) {
new File ("./characters/"+playerName+".txt");
} else {
Misc.println(playerName + ": character file not found.");
p.newPlayer = false;
return 0;
}
try {
line = characterfile.readLine();
} catch (IOException ioexception) {
Misc.println(playerName + ": error loading file.");
return 3;
}
while (EndOfFile == false && line != null) {
line = line.trim();
int spot = line.indexOf("=");
if (spot > -1) {
token = line.substring(0, spot);
token = token.trim();
token2 = line.substring(spot + 1);
token2 = token2.trim();
token3 = token2.split("\t");
switch (ReadMode) {
Playersaving code:
Code:
public class PlayerSaving implements Runnable {
private ArrayList<Integer> requests = new ArrayList<Integer>();
private Thread thread;
private static PlayerSaving singleton;
private static long lastGroupSave;
private static final int SAVE_TIMER = 30;
public static PlayerSaving getSingleton() {
return singleton;
}
public static void initialize() {
singleton = new PlayerSaving();
singleton.thread = new Thread(singleton);
singleton.thread.start();
}
@Override
public synchronized void run() {
while (true) {
saveAllPlayers();
try {
Thread.sleep(300000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public synchronized void requestSave(int i) {
if (!requests.contains(i)) {
requests.add(i);
notify();
}
}
public void saveAllPlayers() {
lastGroupSave = System.currentTimeMillis();
// requests.clear();
long start = lastGroupSave;
for (Player p : PlayerHandler.players) {
if (p != null)
PlayerSave.saveGame((Client) p);
if (System.currentTimeMillis() - start >= (Server.getSleepTimer() - 5)) {
System.out.println("Aborted all saving to prevent lag.");
return;
}
}
System.out.println("Saved all games.");
}
public void saveRequests() {
int totalSave = 0;
for (int id : requests) {
if (PlayerHandler.players[id] != null) {
Client c = (Client) PlayerHandler.players[id];
PlayerSave.saveGame((Client) PlayerHandler.players[id]);
totalSave++;
}
}
System.out.println("Saved a total of: " + totalSave + " games.");
requests.clear();
}
public static boolean saveRequired() {
return System.currentTimeMillis() - lastGroupSave > SAVE_TIMER;
}
}