Mineplex2018-withcommit/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/GameCreationManager.java

237 lines
5.9 KiB
Java
Raw Normal View History

2013-08-27 17:14:08 +02:00
package nautilus.game.arcade.managers;
import java.lang.reflect.InvocationTargetException;
2013-08-27 17:14:08 +02:00
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import mineplex.core.common.util.UtilMath;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilTime;
2015-06-27 03:40:40 +02:00
import mineplex.core.itemstack.ItemStackFactory;
2014-06-25 21:06:24 +02:00
import mineplex.core.timing.TimingManager;
2013-08-27 17:14:08 +02:00
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import mineplex.minecraft.game.core.combat.CombatManager.AttackReason;
2013-08-27 17:14:08 +02:00
import nautilus.game.arcade.ArcadeManager;
import nautilus.game.arcade.GameType;
import nautilus.game.arcade.game.Game;
import nautilus.game.arcade.game.Game.GameState;
2014-09-06 14:01:05 +02:00
import nautilus.game.arcade.stats.StatTracker;
2013-08-27 17:14:08 +02:00
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
public class GameCreationManager implements Listener
{
ArcadeManager Manager;
private ArrayList<Game> _ended = new ArrayList<Game>();
private GameType _nextGame = null;
private String _lastMap = "";
private ArrayList<GameType> _lastGames = new ArrayList<GameType>();
2014-05-02 06:35:41 +02:00
public String MapPref = null;
2013-08-27 17:14:08 +02:00
public GameCreationManager(ArcadeManager manager)
{
Manager = manager;
Manager.getPluginManager().registerEvents(this, Manager.getPlugin());
2013-08-27 17:14:08 +02:00
}
public String GetLastMap()
{
return _lastMap;
}
public void SetLastMap(String file)
{
_lastMap = file;
}
@EventHandler
public void NextGame(UpdateEvent event)
{
if (event.getType() != UpdateType.FAST)
return;
if (Manager.GetGameList().isEmpty())
return;
while (_lastGames.size() > Manager.GetGameList().size() - 1)
_lastGames.remove(_lastGames.size()-1);
2013-08-27 17:14:08 +02:00
if (Manager.GetGame() == null && _ended.isEmpty())
{
CreateGame(null);
}
//Archive Game
if (Manager.GetGame() != null)
{
2013-08-27 17:14:08 +02:00
if (Manager.GetGame().GetState() == GameState.Dead)
{
HandlerList.unregisterAll(Manager.GetGame());
//Schedule Cleanup
_ended.add(Manager.GetGame());
//Lobby Display
Manager.GetLobby().DisplayLast(Manager.GetGame());
Manager.SetGame(null);
}
}
//Clean Archived Games
Iterator<Game> gameIterator = _ended.iterator();
while (gameIterator.hasNext())
{
Game game = gameIterator.next();
2014-09-06 14:01:05 +02:00
2013-08-27 17:14:08 +02:00
HandlerList.unregisterAll(game);
2014-09-06 14:01:05 +02:00
for (StatTracker tracker : game.getStatTrackers())
HandlerList.unregisterAll(tracker);
2014-06-25 21:06:24 +02:00
TimingManager.start("GameCreationManager - Attempting Removal - " + game.GetName());
2014-05-01 01:31:47 +02:00
2013-08-27 17:14:08 +02:00
//Cleaned
if (game.WorldData == null || game.WorldData.World == null)
2013-08-27 17:14:08 +02:00
{
2014-06-04 09:55:13 +02:00
gameIterator.remove();
2013-08-27 17:14:08 +02:00
}
else
2014-06-04 09:55:13 +02:00
{
2014-06-05 02:15:22 +02:00
boolean removedPlayers = false;
if (UtilTime.elapsed(game.GetStateTime(), 20000))
{
2014-06-25 21:06:24 +02:00
TimingManager.start("GameCreationManager - Kick Players - " + game.GetName());
for (Player player : game.WorldData.World.getPlayers())
2014-06-04 09:55:13 +02:00
{
2014-06-04 09:56:21 +02:00
System.out.println("Kicking [" + player.getName() + "] with Validity [" + player.isValid() + "] with Online [" + player.isOnline() + "]");
2014-06-04 09:55:13 +02:00
2014-06-05 02:15:22 +02:00
player.remove();
player.kickPlayer("Dead World");
2014-06-04 09:55:13 +02:00
}
2014-06-05 02:15:22 +02:00
removedPlayers = true;
2014-06-25 21:06:24 +02:00
TimingManager.stop("GameCreationManager - Kick Players - " + game.GetName());
}
//Clean
2014-06-05 02:15:22 +02:00
if (removedPlayers || game.WorldData.World.getPlayers().isEmpty())
{
2014-06-05 02:15:22 +02:00
if (game.WorldData.World.getPlayers().isEmpty())
System.out.println("World Player Count [" + game.WorldData.World.getPlayers().size() + "]");
2014-06-25 21:06:24 +02:00
TimingManager.start("GameCreationManager - Uninit World - " + game.GetName());
game.WorldData.Uninitialize();
game.WorldData = null;
gameIterator.remove();
2014-06-25 21:06:24 +02:00
TimingManager.stop("GameCreationManager - Uninit World - " + game.GetName());
};
2013-08-27 17:14:08 +02:00
}
2014-06-25 21:06:24 +02:00
TimingManager.stop("GameCreationManager - Attempting Removal - " + game.GetName());
2013-08-27 17:14:08 +02:00
}
}
private void CreateGame(GameType gameType)
{
//Reset Changes
Manager.GetDamage().DisableDamageChanges = false;
Manager.GetCreature().SetDisableCustomDrops(false);
Manager.GetDamage().SetEnabled(true);
2013-11-07 09:49:21 +01:00
Manager.GetExplosion().SetRegenerate(false);
2013-11-15 08:52:46 +01:00
Manager.GetExplosion().SetTNTSpread(true);
2014-11-24 02:51:23 +01:00
// Manager.GetAntiStack().SetEnabled(true);
2014-08-09 17:04:53 +02:00
Manager.getCosmeticManager().setHideParticles(false);
Manager.GetDamage().GetCombatManager().setUseWeaponName(AttackReason.CustomWeaponName);
Manager.GetChat().setThreeSecondDelay(true);
2015-06-27 03:40:40 +02:00
ItemStackFactory.Instance.SetUseCustomNames(false);
2015-04-01 11:22:13 +02:00
2013-08-27 17:14:08 +02:00
HashMap<String, ChatColor> pastTeams = null;
2014-05-02 06:35:41 +02:00
//Chosen Game
if (_nextGame != null)
2013-08-27 17:14:08 +02:00
{
gameType = _nextGame;
_nextGame = null;
2014-05-02 06:35:41 +02:00
System.out.println("Staff Selected GameType: " + gameType);
2013-08-27 17:14:08 +02:00
}
//Pick Game
if (gameType == null)
{
for (int i=0 ; i<50 ; i++)
{
gameType = Manager.GetGameList().get(UtilMath.r(Manager.GetGameList().size()));
if (!_lastGames.contains(gameType))
2013-08-27 17:14:08 +02:00
break;
}
}
2014-12-23 01:39:03 +01:00
//Champions
Manager.toggleChampionsModules(gameType);
_lastGames.add(0, gameType);
2013-08-27 17:14:08 +02:00
try
{
Game game = gameType.getGameClass().getConstructor(ArcadeManager.class).newInstance(Manager);
Manager.SetGame(game);
}
catch (NoSuchMethodException ex)
{
System.err.println("Is the constructor for " + gameType.GetName() + " using only one argument?");
ex.printStackTrace();
return;
}
catch (InvocationTargetException ex)
{
ex.getCause().printStackTrace();
return;
}
catch (Exception ex)
{
ex.printStackTrace();
return;
}
2013-08-27 17:14:08 +02:00
if (Manager.GetGame() == null)
{
return;
}
2014-06-25 21:06:24 +02:00
TimingManager.start("DisplayNext");
2013-08-27 17:14:08 +02:00
Manager.GetLobby().DisplayNext(Manager.GetGame(), pastTeams);
2014-06-25 21:06:24 +02:00
TimingManager.stop("DisplayNext");
2013-08-27 17:14:08 +02:00
2014-06-25 21:06:24 +02:00
TimingManager.start("registerEvents");
UtilServer.getServer().getPluginManager().registerEvents(Manager.GetGame(), Manager.getPlugin());
2014-06-25 21:06:24 +02:00
TimingManager.stop("registerEvents");
2013-08-27 17:14:08 +02:00
}
2014-05-02 06:35:41 +02:00
public void SetNextGameType(GameType type)
{
_nextGame = type;
}
2013-08-27 17:14:08 +02:00
}