Thread: New players wont save to "character" file:(

Results 1 to 4 of 4
  1. #1 New players wont save to "character" file:( 
    Registered Member
    Join Date
    Aug 2015
    Posts
    24
    Thanks given
    0
    Thanks received
    0
    Rep Power
    11
    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;
    	}
    }
    Reply With Quote  
     

  2. #2  
    Registered Member

    Join Date
    Mar 2013
    Age
    11
    Posts
    509
    Thanks given
    18
    Thanks received
    30
    Rep Power
    89
    Make sure you have a character folder in Data, then players will save
    Reply With Quote  
     

  3. #3  
    Stand guard at the door of your mind

    Proto's Avatar
    Join Date
    Jul 2011
    Age
    29
    Posts
    1,234
    Thanks given
    152
    Thanks received
    413
    Rep Power
    462
    Half of your PlayerSave class is missing. Post your saveGame method. Btw its a nightmare to read your code. I recommend you work on your doc commenting (Java Documentation Comments) if youre going to do it, do it right. Study majors informative thread (http://www.rune-server.org/runescape...tter-java.html) to generally write better java code. Take a look at Asterias player saving for a well documented full functional system.




    Reply With Quote  
     

  4. #4  
    Banned New players wont save to &quot;character&quot; file:( Market Banned


    Join Date
    Jan 2011
    Age
    26
    Posts
    3,112
    Thanks given
    1,198
    Thanks received
    1,479
    Rep Power
    0
    can you elaborate on the issue
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 3
    Last Post: 08-27-2013, 05:35 AM
  2. Replies: 17
    Last Post: 04-19-2011, 01:02 AM
  3. Wont Save To File >.<
    By Mister Maggot in forum Application Development
    Replies: 6
    Last Post: 01-03-2010, 02:15 PM
  4. Replies: 31
    Last Post: 01-28-2009, 10:01 PM
  5. Replies: 7
    Last Post: 11-21-2008, 07:54 PM
Tags for this Thread

View Tag Cloud

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •