Add option of "uptimes" to servergroups

This commit is contained in:
xXVevzZXx 2016-07-08 02:33:51 +02:00 committed by cnr
parent daddaa477e
commit 654ab281ea
6 changed files with 281 additions and 4 deletions

View File

@ -159,7 +159,7 @@ public class PersonalServerManager extends MiniPlugin
}
}
final ServerGroup serverGroup = new ServerGroup(serverName, serverName, host.getName(), ram, cpu, 1, 0, UtilMath.random.nextInt(250) + 19999, true, "arcade.zip", "Arcade.jar", "plugins/Arcade/", minPlayers, maxPlayers,
final ServerGroup serverGroup = new ServerGroup(serverName, serverName, host.getName(), ram, cpu, 1, 0, UtilMath.random.nextInt(250) + 19999, null, true, "arcade.zip", "Arcade.jar", "plugins/Arcade/", minPlayers, maxPlayers,
true, false, false, games, "", "", "Player", true, event, false, true, false, true, true, false, false, false, false, true, true, true, false, false, "", _us ? Region.US : Region.EU, "", "", "", "");
getPlugin().getServer().getScheduler().runTaskAsynchronously(getPlugin(), new Runnable()

View File

@ -24,6 +24,8 @@ public class ServerGroup
private int _requiredTotalServers;
private int _requiredJoinableServers;
private String _uptimes;
private boolean _arcadeGroup;
private String _worldZip;
private String _plugin;
@ -78,6 +80,7 @@ public class ServerGroup
_requiredTotalServers = Integer.valueOf(data.get("totalServers"));
_requiredJoinableServers = Integer.valueOf(data.get("joinableServers"));
_portSection = Integer.valueOf(data.get("portSection"));
_uptimes = data.get("uptimes");
_arcadeGroup = Boolean.valueOf(data.get("arcadeGroup"));
_worldZip = data.get("worldZip");
_plugin = data.get("plugin");
@ -120,7 +123,7 @@ public class ServerGroup
parseServers(serverStatuses);
}
public ServerGroup(String name, String prefix, String host, int ram, int cpu, int totalServers, int joinable, int portSection, boolean arcade, String worldZip, String plugin, String configPath
public ServerGroup(String name, String prefix, String host, int ram, int cpu, int totalServers, int joinable, int portSection, String uptimes, boolean arcade, String worldZip, String plugin, String configPath
, int minPlayers, int maxPlayers, boolean pvp, boolean tournament, boolean tournamentPoints, String games, String modes, String boosterGroup, String serverType, boolean noCheat, boolean worldEdit, boolean teamRejoin
, boolean teamAutoJoin, boolean teamForceBalance, boolean gameAutoStart, boolean gameTimeout, boolean rewardGems, boolean rewardItems, boolean rewardStats
, boolean rewardAchievements, boolean hotbarInventory, boolean hotbarHubClock, boolean playerKickIdle, boolean staffOnly, boolean whitelist, String resourcePack, Region region
@ -134,6 +137,7 @@ public class ServerGroup
_requiredTotalServers = totalServers;
_requiredJoinableServers = joinable;
_portSection = portSection;
_uptimes = uptimes;
_arcadeGroup = arcade;
_worldZip = worldZip;
_plugin = plugin;
@ -232,6 +236,7 @@ public class ServerGroup
public String getServerNpcName() { return _npcName; }
public String getPortalBottomCornerLocation() { return _portalBottomCornerLocation; }
public String getPortalTopCornerLocation() { return _portalTopCornerLocation; }
public String getUptimes() { return _uptimes; }
public Set<MinecraftServer> getServers() { return _servers; }
@ -358,6 +363,7 @@ public class ServerGroup
_dataMap.put("totalServers", _requiredTotalServers + "");
_dataMap.put("joinableServers", _requiredJoinableServers + "");
_dataMap.put("portSection", _portSection + "");
_dataMap.put("uptimes", _uptimes);
_dataMap.put("arcadeGroup", _arcadeGroup + "");
_dataMap.put("worldZip", _worldZip);
_dataMap.put("plugin", _plugin);

View File

@ -216,6 +216,7 @@ public class Arcade extends JavaPlugin
config.ServerType = _serverConfiguration.getServerGroup().getServerType();
config.MinPlayers = _serverConfiguration.getServerGroup().getMinPlayers();
config.MaxPlayers = _serverConfiguration.getServerGroup().getMaxPlayers();
config.Uptimes = _serverConfiguration.getServerGroup().getUptimes();
config.Tournament = _serverConfiguration.getServerGroup().getTournament();
config.TournamentPoints = _serverConfiguration.getServerGroup().getTournamentPoints();
config.TeamRejoin = _serverConfiguration.getServerGroup().getTeamRejoin();

View File

@ -173,7 +173,8 @@ public class ArcadeManager extends MiniPlugin implements IRelation
private ProgressingKitManager _progressionKitManager;
private BoosterManager _boosterManager;
private GameSpectatorManager _spectatorManager;
private ServerUptimeManager _serverUptimeManager;
private IncognitoManager _incognitoManager;
private TaskManager _taskManager;
@ -331,7 +332,8 @@ public class ArcadeManager extends MiniPlugin implements IRelation
_kitProgressionManager = new KitProgressionManager(getPlugin(), clientManager);
_progressionKitManager = new ProgressingKitManager(this);
_serverUptimeManager = new ServerUptimeManager(this);
if (GetHost() != null && !GetHost().isEmpty())
{
Bukkit.getScheduler().runTaskLater(plugin, () -> Portal.transferPlayer(GetHost(), _serverStatusManager.getCurrentServerName()), 80L);

View File

@ -12,6 +12,9 @@ public class GameServerConfig
public String BoosterGroup = null;
public int MinPlayers = -1;
public int MaxPlayers = -1;
public String Uptimes;
public ArrayList<GameType> GameList = new ArrayList<GameType>();
public ArrayList<String> GameModeList = new ArrayList<String>();

View File

@ -0,0 +1,265 @@
package nautilus.game.arcade.managers;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import mineplex.core.common.Rank;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilTime;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import nautilus.game.arcade.ArcadeManager;
import nautilus.game.arcade.game.Game.GameState;
/**
* ServerUptimeManager
*
* @author xXVevzZXx
*/
public class ServerUptimeManager implements Listener
{
private static final long SHUTDOWN_DELAY = 10000;
private static final long KICK_AFTER_SENDING = 20*10; // 10 Seconds
public ArcadeManager Manager;
private HashMap<Date, Date> _dates;
private long _informed;
private boolean _closeServer;
private boolean _enabled;
private boolean _closed;
public ServerUptimeManager(ArcadeManager manager)
{
Manager = manager;
_dates = new HashMap<>();
_enabled = true;
updateDates();
Manager.getPluginManager().registerEvents(this, Manager.getPlugin());
}
@EventHandler
public void updateDates(UpdateEvent event)
{
if (!_enabled)
return;
if (event.getType() != UpdateType.MIN_30)
return;
if (isOpen())
return;
updateDates();
}
@EventHandler
public void changeState(UpdateEvent event)
{
if (!_enabled)
return;
if (event.getType() != UpdateType.SLOWEST)
return;
if (isOpen())
{
if (!_closed)
return;
System.out.println("Opening server!");
UtilServer.getServer().setWhitelist(false);
Manager.setRequiredRank(null);
_closed = false;
}
else
{
if (_closed)
return;
System.out.println("Closing server!");
_informed = System.currentTimeMillis();
_closeServer = true;
Manager.setRequiredRank(Rank.HELPER);
UtilServer.getServer().setWhitelist(true);
UtilServer.broadcast(F.main("Server", "Servertime expired! I will shutdown soon."));
}
}
@EventHandler
public void closeServer(UpdateEvent event)
{
if (!_enabled)
return;
if (event.getType() != UpdateType.SLOWER)
return;
if (!_closeServer)
return;
if (Manager.GetGame() == null)
return;
if (Manager.GetGame().GetState() != GameState.Recruit)
return;
if (!UtilTime.elapsed(Manager.GetGame().GetStateTime(), SHUTDOWN_DELAY)
|| !UtilTime.elapsed(_informed, SHUTDOWN_DELAY))
return;
if (_closed)
return;
_closeServer = false;
for (Player player : UtilServer.getPlayers())
{
Manager.GetPortal().sendToHub(player, "Servertime has expired!");
}
Manager.runSyncLater(new Runnable()
{
@Override
public void run()
{
while (UtilServer.getPlayers().length > 0)
{
UtilServer.getPlayers()[0].kickPlayer(F.main("Server", "Servertime has expired!"));
}
}
}, KICK_AFTER_SENDING);
_closed = true;
}
public boolean isOpen()
{
for (Date start : _dates.keySet())
{
Date end = _dates.get(start);
Date current = new Date(System.currentTimeMillis());
if (current.before(end) && current.after(start))
return true;
}
return false;
}
public void updateDates()
{
updateDates(Manager.GetServerConfig().Uptimes);
}
public void updateDates(String uptimes)
{
System.out.println(uptimes);
if (Manager.GetServerConfig().Uptimes == null)
{
_enabled = false;
return;
}
_dates.clear();
String[] pairs = uptimes.split(",");
for (String pair : pairs)
{
GregorianCalendar firstDate = new GregorianCalendar();
GregorianCalendar secondDate = new GregorianCalendar();
int i = 0;
for (String part : pair.split("-"))
{
i++;
GregorianCalendar current = i == 1 ? firstDate : secondDate;
String[] date = part.split(":");
for (String token : date)
{
if (token.endsWith("Y"))
{
current.set(GregorianCalendar.YEAR,
Integer.parseInt(token.substring(0, token.length() - 1)));
}
else if (token.endsWith("MO"))
{
current.set(GregorianCalendar.MONTH,
Integer.parseInt(token.substring(0, token.length() - 2)));
}
else if (token.endsWith("W"))
{
current.set(GregorianCalendar.WEEK_OF_MONTH,
Integer.parseInt(token.substring(0, token.length() - 1)));
}
else if (token.endsWith("D"))
{
current.set(GregorianCalendar.DAY_OF_WEEK,
Integer.parseInt(token.substring(0, token.length() - 1)));
}
else if (token.endsWith("H"))
{
current.set(GregorianCalendar.HOUR_OF_DAY,
Integer.parseInt(token.substring(0, token.length() - 1)));
}
else if (token.endsWith("M"))
{
current.set(GregorianCalendar.MINUTE,
Integer.parseInt(token.substring(0, token.length() - 1)));
}
else if (token.endsWith("S"))
{
current.set(GregorianCalendar.SECOND,
Integer.parseInt(token.substring(0, token.length() - 1)));
}
}
}
int[] types =
{ GregorianCalendar.SECOND, GregorianCalendar.MINUTE, GregorianCalendar.HOUR_OF_DAY,
GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.WEEK_OF_MONTH,
GregorianCalendar.MONTH, GregorianCalendar.YEAR };
int e = 0;
for (int type : types)
{
e++;
if (e == 7)
e = 6;
if (firstDate.get(type) > secondDate.get(type))
{
int time = secondDate.get(types[e]) + 1;
secondDate.set(types[e], time);
}
if (secondDate.get(type) > Calendar.getInstance().getMaximum(type))
{
secondDate.set(type, secondDate.get(type - 1));
int time = secondDate.get(types[e]) + 1;
secondDate.set(types[e], time);
}
}
_dates.put(firstDate.getTime(), secondDate.getTime());
}
}
}