Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. package pl.mines.xcraftrayx.bungeeonlinetime;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.Iterator;
  6. import java.util.concurrent.TimeUnit;
  7.  
  8. import net.md_5.bungee.api.plugin.Plugin;
  9. import net.md_5.bungee.api.scheduler.ScheduledTask;
  10.  
  11. public class BungeeOnlineTime extends Plugin
  12. {
  13. public static final String incomingChannel = "lc:bungeecord";
  14. public static final String outgoingChannel = "lc:bungeecord";
  15.  
  16. public static ArrayList<ScheduledTask> tasks = new ArrayList<ScheduledTask>(2);
  17.  
  18. private static BungeeOnlineTime instance;
  19.  
  20. public static ArrayList<OnlinePlayer> playerList = new ArrayList<OnlinePlayer>();
  21.  
  22. public void reload()
  23. {
  24. Config.getVariables();
  25. BungeeOnlineTime.tasks.get(0).cancel();
  26. BungeeOnlineTime.tasks.get(1).cancel();
  27. runSavePlayersTime();
  28. runCheckerToClearWeekTOP();
  29. }
  30.  
  31. public void onEnable()
  32. {
  33. //Registering channels to be able to communicate with bukkit-side
  34. getProxy().registerChannel(incomingChannel);
  35. getProxy().registerChannel(outgoingChannel);
  36.  
  37. //Registering listeners
  38. getProxy().getPluginManager().registerListener(this, new ServerListener());
  39. getProxy().getPluginManager().registerListener(this, new PlayerListener());
  40. getProxy().getPluginManager().registerCommand(this, new CustomCommand());
  41.  
  42. instance = this;
  43.  
  44. Config.runConfigFile();
  45.  
  46. MySQL.openConnection();
  47.  
  48. MySQL.addServers(getProxy().getServers().values());
  49.  
  50. runSavePlayersTime();
  51.  
  52. if(Config.saveTOPWeekIsEnabled)
  53. {
  54. runCheckerToClearWeekTOP();
  55. }
  56.  
  57. getLogger().info("BungeeOnlineTime - Loaded!");
  58. }
  59.  
  60. public void onDisable()
  61. {
  62. // MySQL.closeConnection();
  63. }
  64.  
  65. public static BungeeOnlineTime getInstance()
  66. {
  67. return instance;
  68. }
  69.  
  70. public static OnlinePlayer getOnlinePlayer(String userName)
  71. {
  72. for(OnlinePlayer op : playerList)
  73. {
  74. if(op.getPlayerName().equals(userName))
  75. {
  76. return op;
  77. }
  78. }
  79. return null;
  80. }
  81.  
  82. public static void removeOnlinePlayer(String userName)
  83. {
  84. for(Iterator<OnlinePlayer> it = playerList.iterator(); it.hasNext();)
  85. {
  86. OnlinePlayer op = it.next();
  87. if(op.getPlayerName().equals(userName))
  88. {
  89. it.remove();
  90. break;
  91. }
  92. }
  93.  
  94. // for(OnlinePlayer op : playerList) //CONCURED
  95. // {
  96. // if(op.getPlayerName().equals(userName))
  97. // {
  98. // playerList.remove(op);
  99. // break;
  100. // }
  101. // }
  102. }
  103.  
  104. public void runSavePlayersTime()
  105. {
  106. ScheduledTask task = getProxy().getScheduler().schedule(getInstance(), () ->
  107. {
  108. Iterator<OnlinePlayer> it = playerList.iterator();
  109. while(it.hasNext()) //concurret modification exception
  110. {
  111. OnlinePlayer op = it.next();
  112. String userName = op.getPlayerName();
  113.  
  114. op.saveTime();
  115.  
  116. for(String server : op.getTimes().keySet())
  117. {
  118. if(!MySQL.userAndServerIsExist(userName, server))
  119. {
  120. MySQL.addServerToUser(userName, server);
  121. }
  122. }
  123. MySQL.saveTimes(op.getPlayerName(), op.getTimes());
  124.  
  125. //FOR WEEK SAVE
  126. if(Config.saveTOPWeekIsEnabled)
  127. {
  128. int allTimeInSec = 0;
  129. for(String key : op.getTimes().keySet())
  130. {
  131. allTimeInSec += op.getTimes().get(key);
  132. }
  133. MySQL.saveWeekTime(op.getPlayerName(), allTimeInSec);
  134. }
  135. op.resetTimes();
  136. }
  137. }, Config.saveTime, Config.saveTime, TimeUnit.SECONDS);
  138. tasks.add(task);
  139. }
  140.  
  141. public void runCheckerToClearWeekTOP()
  142. {
  143. ScheduledTask task = getProxy().getScheduler().schedule(getInstance(), () ->
  144. {
  145. if(isDateToReset())
  146. {
  147. MySQL.clearTOPWeek();
  148. }
  149. }, 1, 1, TimeUnit.SECONDS);
  150. tasks.add(task);
  151. }
  152.  
  153. public boolean isDateToReset()
  154. {
  155. Date date = new Date();
  156.  
  157. if(date.getDay() == Config.resetDay && date.getHours() == 0 && date.getMinutes() == 0 && date.getSeconds() == 0)
  158. {
  159. return true;
  160. }
  161.  
  162. return false;
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement