Mineplex2018-withcommit/Plugins/Nautilus.Game.Arcade/src/nautilus/game/arcade/managers/GameHostManager.java
Chiss ec8e1f0546 more MPS stuff
fixed bridges exploit
2014-12-22 16:31:13 +11:00

418 lines
11 KiB
Java

package nautilus.game.arcade.managers;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import mineplex.core.common.Rank;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilGear;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilTime;
import mineplex.core.itemstack.ItemStackFactory;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import nautilus.game.arcade.ArcadeManager;
import nautilus.game.arcade.GameType;
import nautilus.game.arcade.game.Game.GameState;
import nautilus.game.arcade.gui.privateServer.PrivateServerShop;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
public class GameHostManager implements Listener
{
private ArrayList<GameType> ultraGames = new ArrayList<GameType>();
private ArrayList<GameType> heroGames = new ArrayList<GameType>();
private ArrayList<GameType> legendGames = new ArrayList<GameType>();
ArcadeManager Manager;
private Player _host;
private Rank _hostRank;
private long _serverStartTime = System.currentTimeMillis();
private long _serverExpireTime = 43200000;
private long _lastOnline = System.currentTimeMillis();
private long _expireTime = 300000;
private HashSet<Player> _onlineAdmins = new HashSet<Player>();
private HashSet<String> _adminList = new HashSet<String>();
private HashSet<String> _whitelist = new HashSet<String>();
private HashSet<String> _blacklist = new HashSet<String>();
private PrivateServerShop _shop;
public GameHostManager(ArcadeManager manager)
{
Manager = manager;
_shop = new PrivateServerShop(manager, manager.GetClients(), manager.GetDonation());
Manager.GetPluginManager().registerEvents(this, Manager.GetPlugin());
//Game Rotation
if (Manager.GetServerConfig().GameList.contains(GameType.ChampionsDominate) ||
Manager.GetServerConfig().GameList.contains(GameType.ChampionsDominate))
{
Iterator<GameType> gameIterator = Manager.GetServerConfig().GameList.iterator();
//Remove Non-Champion Games
while (gameIterator.hasNext())
{
GameType type = gameIterator.next();
if (type != GameType.ChampionsDominate && type != GameType.ChampionsDominate)
gameIterator.remove();
}
//Hero Games
heroGames.add(GameType.ChampionsDominate);
heroGames.add(GameType.ChampionsTDM);
}
else
{
//Ultra Games
ultraGames.add(GameType.Smash);
ultraGames.add(GameType.BaconBrawl);
ultraGames.add(GameType.DeathTag);
ultraGames.add(GameType.DragonEscape);
ultraGames.add(GameType.Dragons);
ultraGames.add(GameType.Micro);
ultraGames.add(GameType.Paintball);
ultraGames.add(GameType.Quiver);
ultraGames.add(GameType.Runner);
ultraGames.add(GameType.Sheep);
ultraGames.add(GameType.Snake);
ultraGames.add(GameType.SneakyAssassins);
ultraGames.add(GameType.TurfWars);
ultraGames.add(GameType.Spleef);
//Hero Games
heroGames.add(GameType.HideSeek);
heroGames.add(GameType.Draw);
//Legend Games
legendGames.add(GameType.Bridge);
legendGames.add(GameType.SurvivalGames);
legendGames.add(GameType.CastleSiege);
//legendGames.add(GameType.MineStrike);
}
//Config Defaults
if (Manager.GetHost() != null && Manager.GetHost().length() > 0)
{
setDefaultConfig();
}
}
@EventHandler
public void updateHost(UpdateEvent event)
{
if (event.getType() != UpdateType.FAST)
return;
//No Host - Not MPS
if (Manager.GetHost() == null || Manager.GetHost().length() == 0)
return;
// Admins update
for (Player player : UtilServer.getPlayers())
{
if (player.equals(_host) || _adminList.contains(player.getName()) || Manager.GetClients().Get(player).GetRank().Has(Rank.MODERATOR))
{
if (Manager.GetGame() == null || Manager.GetGame().GetState() == GameState.Recruit)
giveAdminItem(player);
_lastOnline = System.currentTimeMillis();
}
}
}
@EventHandler
public void adminJoin(PlayerJoinEvent event)
{
if (!isPrivateServer())
return;
if (Manager.GetHost().equals(event.getPlayer().getName()))
{
_host = event.getPlayer();
_hostRank = Manager.GetClients().Get(_host).GetRank();
System.out.println("Game Host Joined.");
}
else if (isAdmin(event.getPlayer(), false))
{
System.out.println("Admin Joined.");
_onlineAdmins.add(event.getPlayer());
}
}
@EventHandler
public void adminQuit(PlayerQuitEvent event)
{
if (!isPrivateServer())
return;
if (isHost(event.getPlayer()))
{
System.out.println("Game Host Quit.");
_host = null;
}
else if (isAdmin(event.getPlayer(), false))
{
_onlineAdmins.remove(event.getPlayer());
}
}
public boolean isHostExpired()
{
if (Manager.GetHost() == null || Manager.GetHost().length() == 0)
return false;
return UtilTime.elapsed(_lastOnline, _expireTime) || UtilTime.elapsed(_serverStartTime, _serverExpireTime);
}
private void giveAdminItem(Player player)
{
if (Manager.GetGame() == null)
return;
if (UtilGear.isMat(player.getInventory().getItem(5), Material.BOOK_AND_QUILL))
return;
player.getInventory().setItem(7, ItemStackFactory.Instance.CreateStack(Material.BLAZE_POWDER, (byte)0, 1, C.cGreen + C.Bold + "/menu"));
}
public HashSet<String> getWhitelist()
{
return _whitelist;
}
public HashSet<String> getBlacklist()
{
return _blacklist;
}
public HashSet<String> getAdminList()
{
return _adminList;
}
@EventHandler
public void menuCommand(PlayerCommandPreprocessEvent event)
{
if (!isAdmin(event.getPlayer(), true))
return;
if (!event.getMessage().toLowerCase().startsWith("/menu"))
return;
openMenu(event.getPlayer());
}
@EventHandler
public void menuInteract(PlayerInteractEvent event)
{
if (!isAdmin(event.getPlayer(), true))
return;
if (!UtilGear.isMat(event.getPlayer().getItemInHand(), Material.BLAZE_POWDER))
return;
openMenu(event.getPlayer());
event.setCancelled(true);
}
private void openMenu(Player player)
{
_shop.attemptShopOpen(player);
}
public boolean isAdmin(Player player, boolean includeStaff)
{
return player.equals(_host) || _adminList.contains(player.getName()) || (includeStaff && Manager.GetClients().Get(player).GetRank().Has(Rank.MODERATOR));
}
public boolean isHost(Player player)
{
return player.getName().equals(Manager.GetHost());
}
public boolean isPrivateServer()
{
return Manager.GetHost() != null || Manager.GetHost().length() > 0;
}
@EventHandler
public void whitelistCommand(PlayerCommandPreprocessEvent event)
{
if (_host == null || !event.getPlayer().equals(_host))
return;
if (!event.getMessage().toLowerCase().startsWith("/whitelist "))
return;
String[] args = event.getMessage().split(" ");
for (int i=1 ; i<args.length ; i++)
{
String name = args[i].toLowerCase();
if (_whitelist.add(name))
{
UtilPlayer.message(event.getPlayer(), F.main("Host", "Added " + F.elem(name) + " to the whitelist."));
}
}
}
public void setGame(GameType type)
{
if (_host == null)
return;
Manager.GetGameCreationManager().SetNextGameType(type);
//End Current
if (Manager.GetGame().GetState() == GameState.Recruit)
{
Manager.GetGame().SetState(GameState.Dead);
Bukkit.broadcastMessage(C.cGreen + C.Bold + _host.getName() + " has changed game to " + type.GetName() + ".");
}
else
{
Bukkit.broadcastMessage(C.cGreen + C.Bold + _host.getName() + " set next game to " + type.GetName() + ".");
}
}
public void startGame()
{
if (_host == null)
return;
Manager.GetGameManager().StateCountdown(Manager.GetGame(), 10, true);
Manager.GetGame().Announce(C.cGreen + C.Bold + _host.getName() + " has started the game.");
}
public void stopGame()
{
if (_host == null)
return;
if (Manager.GetGame() == null)
return;
HandlerList.unregisterAll(Manager.GetGame());
if (Manager.GetGame().GetState() == GameState.End || Manager.GetGame().GetState() == GameState.End)
{
_host.sendMessage("Game is already ending...");
return;
}
else if (Manager.GetGame().GetState() == GameState.Recruit)
{
Manager.GetGame().SetState(GameState.Dead);
}
else
{
Manager.GetGame().SetState(GameState.End);
}
Manager.GetGame().Announce(C.cGreen + C.Bold + _host.getName() + " has stopped the game.");
}
public boolean hasRank(Rank rank)
{
if (_hostRank == null)
return false;
return _hostRank.Has(rank);
}
public ArrayList<GameType> getAvailableGames(Player player)
{
ArrayList<GameType> games = new ArrayList<GameType>();
if (hasRank(Rank.ULTRA))
games.addAll(ultraGames);
if (hasRank(Rank.HERO))
games.addAll(heroGames);
if (hasRank(Rank.LEGEND))
games.addAll(legendGames);
return games;
}
public void ban(Player player)
{
_blacklist.add(player.getName());
UtilPlayer.message(player, C.cGold + C.Bold + "You were kicked from this Mineplex Private Server.");
player.playSound(player.getLocation(), Sound.ENDERDRAGON_GROWL, 10f, 1f);
Manager.GetPortal().SendPlayerToServer(player, "Lobby");
}
public void giveAdmin(Player player)
{
_adminList.add(player.getName());
_onlineAdmins.add(player);
UtilPlayer.message(player, F.main("Server", "You were given admin privileges."));
}
public boolean isAdminOnline()
{
return _onlineAdmins.isEmpty();
}
public void setDefaultConfig()
{
Manager.GetServerConfig().HotbarInventory = false;
Manager.GetServerConfig().RewardAchievements = false;
Manager.GetServerConfig().RewardGems = false;
Manager.GetServerConfig().RewardItems = false;
Manager.GetServerConfig().RewardStats = false;
Manager.GetServerConfig().GameAutoStart = true;
Manager.GetServerConfig().GameTimeout = true;
Manager.GetServerConfig().PlayerKickIdle = true;
Manager.GetServerConfig().TeamForceBalance = true;
}
@EventHandler
public void setHostDebug(PlayerCommandPreprocessEvent event)
{
if (!event.getPlayer().isOp())
return;
if (!event.getMessage().toLowerCase().startsWith("/sethost "))
return;
Manager.GetServerConfig().HostName = event.getMessage().split(" ")[1];
event.getPlayer().sendMessage("Set host to: " + event.getMessage().split(" ")[1]);
_host = Bukkit.getPlayerExact(Manager.GetServerConfig().HostName);
if (_host != null)
_hostRank = Manager.GetClients().Get(_host).GetRank();
setDefaultConfig();
event.setCancelled(true);
}
}