Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pl.mines.xcraftrayx.bungeeonlinetime;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.concurrent.TimeUnit;
- import net.md_5.bungee.api.plugin.Plugin;
- import net.md_5.bungee.api.scheduler.ScheduledTask;
- public class BungeeOnlineTime extends Plugin
- {
- public static final String incomingChannel = "lc:bungeecord";
- public static final String outgoingChannel = "lc:bungeecord";
- public static ArrayList<ScheduledTask> tasks = new ArrayList<ScheduledTask>(2);
- private static BungeeOnlineTime instance;
- public static ArrayList<OnlinePlayer> playerList = new ArrayList<OnlinePlayer>();
- public void reload()
- {
- Config.getVariables();
- BungeeOnlineTime.tasks.get(0).cancel();
- BungeeOnlineTime.tasks.get(1).cancel();
- runSavePlayersTime();
- runCheckerToClearWeekTOP();
- }
- public void onEnable()
- {
- //Registering channels to be able to communicate with bukkit-side
- getProxy().registerChannel(incomingChannel);
- getProxy().registerChannel(outgoingChannel);
- //Registering listeners
- getProxy().getPluginManager().registerListener(this, new ServerListener());
- getProxy().getPluginManager().registerListener(this, new PlayerListener());
- getProxy().getPluginManager().registerCommand(this, new CustomCommand());
- instance = this;
- Config.runConfigFile();
- MySQL.openConnection();
- MySQL.addServers(getProxy().getServers().values());
- runSavePlayersTime();
- if(Config.saveTOPWeekIsEnabled)
- {
- runCheckerToClearWeekTOP();
- }
- getLogger().info("BungeeOnlineTime - Loaded!");
- }
- public void onDisable()
- {
- // MySQL.closeConnection();
- }
- public static BungeeOnlineTime getInstance()
- {
- return instance;
- }
- public static OnlinePlayer getOnlinePlayer(String userName)
- {
- for(OnlinePlayer op : playerList)
- {
- if(op.getPlayerName().equals(userName))
- {
- return op;
- }
- }
- return null;
- }
- public static void removeOnlinePlayer(String userName)
- {
- for(Iterator<OnlinePlayer> it = playerList.iterator(); it.hasNext();)
- {
- OnlinePlayer op = it.next();
- if(op.getPlayerName().equals(userName))
- {
- it.remove();
- break;
- }
- }
- // for(OnlinePlayer op : playerList) //CONCURED
- // {
- // if(op.getPlayerName().equals(userName))
- // {
- // playerList.remove(op);
- // break;
- // }
- // }
- }
- public void runSavePlayersTime()
- {
- ScheduledTask task = getProxy().getScheduler().schedule(getInstance(), () ->
- {
- Iterator<OnlinePlayer> it = playerList.iterator();
- while(it.hasNext()) //concurret modification exception
- {
- OnlinePlayer op = it.next();
- String userName = op.getPlayerName();
- op.saveTime();
- for(String server : op.getTimes().keySet())
- {
- if(!MySQL.userAndServerIsExist(userName, server))
- {
- MySQL.addServerToUser(userName, server);
- }
- }
- MySQL.saveTimes(op.getPlayerName(), op.getTimes());
- //FOR WEEK SAVE
- if(Config.saveTOPWeekIsEnabled)
- {
- int allTimeInSec = 0;
- for(String key : op.getTimes().keySet())
- {
- allTimeInSec += op.getTimes().get(key);
- }
- MySQL.saveWeekTime(op.getPlayerName(), allTimeInSec);
- }
- op.resetTimes();
- }
- }, Config.saveTime, Config.saveTime, TimeUnit.SECONDS);
- tasks.add(task);
- }
- public void runCheckerToClearWeekTOP()
- {
- ScheduledTask task = getProxy().getScheduler().schedule(getInstance(), () ->
- {
- if(isDateToReset())
- {
- MySQL.clearTOPWeek();
- }
- }, 1, 1, TimeUnit.SECONDS);
- tasks.add(task);
- }
- public boolean isDateToReset()
- {
- Date date = new Date();
- if(date.getDay() == Config.resetDay && date.getHours() == 0 && date.getMinutes() == 0 && date.getSeconds() == 0)
- {
- return true;
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement