Guest User

Untitled

a guest
May 20th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. package server.model.players;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import server.Server;
  6.  
  7. /**
  8. * @author Sanity
  9. */
  10.  
  11. public class PlayerSaving implements Runnable {
  12.  
  13. private ArrayList<Integer> requests = new ArrayList<Integer>();
  14. private Thread thread;
  15. private static PlayerSaving singleton;
  16. private static long lastGroupSave;
  17. private static final int SAVE_TIMER = 300000;
  18.  
  19. public static PlayerSaving getSingleton() {
  20. return singleton;
  21. }
  22.  
  23. public static void initialize() {
  24. singleton = new PlayerSaving();
  25. singleton.thread = new Thread(singleton);
  26. singleton.thread.start();
  27. }
  28.  
  29. public synchronized void run() {
  30. while(true) {
  31. saveAllPlayers();
  32. try {
  33. thread.sleep(300000);
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39.  
  40. public synchronized void requestSave(int i) {
  41. if (!requests.contains(i)) {
  42. requests.add(i);
  43. notify();
  44. }
  45. }
  46.  
  47. public void saveAllPlayers() {
  48. lastGroupSave = System.currentTimeMillis();
  49. //requests.clear();
  50. long start = lastGroupSave;
  51. for (Player p : PlayerHandler.players) {
  52. if (p != null)
  53. PlayerSave.saveGame((Client)p);
  54. if (System.currentTimeMillis() - start >= (Server.getSleepTimer() - 5)) {
  55. System.out.println("Aborted all saving to prevent lag.");
  56. return;
  57. }
  58. }
  59. System.out.println("Saved every player's file.");
  60. }
  61.  
  62. public void saveRequests() {
  63. int totalSave = 0;
  64. for (int id : requests) {
  65. if (PlayerHandler.players[id] != null) {
  66. Client c = (Client)PlayerHandler.players[id];
  67. PlayerSave.saveGame((Client)PlayerHandler.players[id]);
  68. totalSave++;
  69. }
  70. }
  71. System.out.println("Saved a total of: " + totalSave + " player files.");
  72. requests.clear();
  73. }
  74.  
  75. public static boolean saveRequired() {
  76. return System.currentTimeMillis() - lastGroupSave > SAVE_TIMER;
  77. }
  78. }
Add Comment
Please, Sign In to add comment