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

457 lines
12 KiB
Java
Raw Normal View History

package nautilus.game.arcade.managers;
2014-12-17 03:37:14 +01:00
import java.util.ArrayList;
import java.util.HashSet;
2014-12-22 03:19:45 +01:00
import java.util.Iterator;
2014-12-17 03:37:14 +01:00
import java.util.List;
2014-12-17 03:37:14 +01:00
import mineplex.core.common.Rank;
2014-11-07 02:14:33 +01:00
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
2014-11-07 02:14:33 +01:00
import mineplex.core.common.util.UtilGear;
import mineplex.core.common.util.UtilPlayer;
2014-12-22 03:19:45 +01:00
import mineplex.core.common.util.UtilServer;
import mineplex.core.common.util.UtilTime;
2014-11-07 02:14:33 +01:00
import mineplex.core.itemstack.ItemStackFactory;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
import nautilus.game.arcade.ArcadeManager;
2014-11-07 02:14:33 +01:00
import nautilus.game.arcade.GameType;
import nautilus.game.arcade.game.Game.GameState;
2014-12-18 01:45:15 +01:00
import nautilus.game.arcade.gui.privateServer.PrivateServerShop;
2014-11-07 02:14:33 +01:00
import org.bukkit.Bukkit;
2014-12-23 06:20:20 +01:00
import org.bukkit.ChatColor;
2014-11-07 02:14:33 +01:00
import org.bukkit.Material;
2014-12-22 03:19:45 +01:00
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
2014-11-07 02:14:33 +01:00
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
2014-11-07 02:14:33 +01:00
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
public class GameHostManager implements Listener
{
2014-12-22 06:31:13 +01:00
private ArrayList<GameType> ultraGames = new ArrayList<GameType>();
private ArrayList<GameType> heroGames = new ArrayList<GameType>();
private ArrayList<GameType> legendGames = new ArrayList<GameType>();
2014-12-16 18:42:06 +01:00
ArcadeManager Manager;
private Player _host;
2014-12-22 00:51:19 +01:00
private Rank _hostRank;
private long _serverStartTime = System.currentTimeMillis();
2014-12-23 03:01:44 +01:00
private long _serverExpireTime = 21600000;
private long _lastOnline = System.currentTimeMillis();
private long _expireTime = 300000;
private boolean _hostExpired = false;
2014-12-16 18:42:06 +01:00
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>();
2014-12-16 18:42:06 +01:00
2014-12-18 01:45:15 +01:00
private PrivateServerShop _shop;
public GameHostManager(ArcadeManager manager)
{
Manager = manager;
2014-12-18 01:45:15 +01:00
_shop = new PrivateServerShop(manager, manager.GetClients(), manager.GetDonation());
Manager.GetPluginManager().registerEvents(this, Manager.GetPlugin());
2014-12-23 01:39:03 +01:00
//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);
2014-12-16 18:42:06 +01:00
2014-12-23 01:39:03 +01:00
//Hero Games
heroGames.add(GameType.ChampionsDominate);
heroGames.add(GameType.ChampionsTDM);
heroGames.add(GameType.HideSeek);
heroGames.add(GameType.Draw);
2014-12-22 03:19:45 +01:00
2014-12-23 01:39:03 +01:00
//Legend Games
legendGames.add(GameType.Bridge);
legendGames.add(GameType.SurvivalGames);
legendGames.add(GameType.CastleSiege);
//legendGames.add(GameType.MineStrike);
2014-12-22 03:19:45 +01:00
2014-12-22 06:31:13 +01:00
//Config Defaults
if (Manager.GetHost() != null && Manager.GetHost().length() > 0)
{
setDefaultConfig();
}
}
2014-12-16 18:42:06 +01:00
@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;
2014-12-16 18:42:06 +01:00
// Admins update
2014-12-22 03:19:45 +01:00
for (Player player : UtilServer.getPlayers())
2014-12-16 18:42:06 +01:00
{
2014-12-23 03:01:44 +01:00
if (player.equals(_host) || _adminList.contains(player.getName()) || Manager.GetClients().Get(player).GetRank().Has(Rank.ADMIN))
2014-12-22 03:19:45 +01:00
{
if (Manager.GetGame() == null || Manager.GetGame().GetState() == GameState.Recruit)
giveAdminItem(player);
}
2014-12-23 03:01:44 +01:00
if (player.equals(_host))
_lastOnline = System.currentTimeMillis();
2014-12-16 18:42:06 +01:00
}
}
@EventHandler
2014-12-16 18:42:06 +01:00
public void adminJoin(PlayerJoinEvent event)
{
2014-12-16 18:42:06 +01:00
if (!isPrivateServer())
return;
2014-12-16 18:42:06 +01:00
if (Manager.GetHost().equals(event.getPlayer().getName()))
{
_host = event.getPlayer();
2014-12-22 00:51:19 +01:00
_hostRank = Manager.GetClients().Get(_host).GetRank();
2014-12-16 18:42:06 +01:00
System.out.println("Game Host Joined.");
}
2014-12-22 06:31:13 +01:00
else if (isAdmin(event.getPlayer(), false))
2014-12-16 18:42:06 +01:00
{
System.out.println("Admin Joined.");
_onlineAdmins.add(event.getPlayer());
}
}
@EventHandler
2014-12-16 18:42:06 +01:00
public void adminQuit(PlayerQuitEvent event)
{
2014-12-16 18:42:06 +01:00
if (!isPrivateServer())
return;
2014-12-16 18:42:06 +01:00
if (isHost(event.getPlayer()))
{
System.out.println("Game Host Quit.");
_host = null;
}
2014-12-22 06:31:13 +01:00
else if (isAdmin(event.getPlayer(), false))
2014-12-16 18:42:06 +01:00
{
_onlineAdmins.remove(event.getPlayer());
}
}
public boolean isHostExpired()
{
if (Manager.GetHost() == null || Manager.GetHost().length() == 0)
return false;
return _hostExpired || UtilTime.elapsed(_lastOnline, _expireTime) || UtilTime.elapsed(_serverStartTime, _serverExpireTime);
}
public void setHostExpired(boolean expired)
{
_hostExpired = expired;
}
2014-12-16 18:42:06 +01:00
private void giveAdminItem(Player player)
{
2014-11-07 02:14:33 +01:00
if (Manager.GetGame() == null)
return;
2014-12-16 18:42:06 +01:00
2014-12-23 07:06:04 +01:00
if (UtilGear.isMat(player.getInventory().getItem(5), Material.SPECKLED_MELON))
2014-11-07 02:14:33 +01:00
return;
2014-12-23 07:06:04 +01:00
player.getInventory().setItem(7, ItemStackFactory.Instance.CreateStack(Material.SPECKLED_MELON, (byte)0, 1, C.cGreen + C.Bold + "/menu"));
2014-11-07 02:14:33 +01:00
}
private void removeAdminItem(Player player)
{
2014-12-23 07:06:04 +01:00
if (player.getInventory().getItem(7).getType() == Material.SPECKLED_MELON)
{
player.getInventory().setItem(7, null);
}
}
2014-11-07 02:14:33 +01:00
public HashSet<String> getWhitelist()
{
return _whitelist;
}
public HashSet<String> getBlacklist()
{
return _blacklist;
}
2014-12-16 18:42:06 +01:00
public HashSet<String> getAdminList()
{
return _adminList;
}
2014-11-07 02:14:33 +01:00
@EventHandler
public void menuCommand(PlayerCommandPreprocessEvent event)
{
2014-12-23 09:04:10 +01:00
if (!isPrivateServer())
return;
2014-12-22 06:31:13 +01:00
if (!isAdmin(event.getPlayer(), true))
2014-11-07 02:14:33 +01:00
return;
if (!event.getMessage().toLowerCase().startsWith("/menu"))
return;
2014-12-16 18:42:06 +01:00
openMenu(event.getPlayer());
}
2014-11-07 02:14:33 +01:00
@EventHandler
public void menuInteract(PlayerInteractEvent event)
{
2014-12-23 09:04:10 +01:00
if (!isPrivateServer())
return;
2014-12-22 06:31:13 +01:00
if (!isAdmin(event.getPlayer(), true))
2014-11-07 02:14:33 +01:00
return;
2014-12-23 07:06:04 +01:00
if (!UtilGear.isMat(event.getPlayer().getItemInHand(), Material.SPECKLED_MELON))
2014-11-07 02:14:33 +01:00
return;
2014-12-16 18:42:06 +01:00
openMenu(event.getPlayer());
event.setCancelled(true);
2014-11-07 02:14:33 +01:00
}
2014-12-16 18:42:06 +01:00
private void openMenu(Player player)
2014-11-07 02:14:33 +01:00
{
2014-12-17 03:37:14 +01:00
_shop.attemptShopOpen(player);
2014-12-16 18:42:06 +01:00
}
2014-12-22 06:31:13 +01:00
public boolean isAdmin(Player player, boolean includeStaff)
2014-12-16 18:42:06 +01:00
{
2014-12-23 03:01:44 +01:00
return player.equals(_host) || _adminList.contains(player.getName()) || (includeStaff && Manager.GetClients().Get(player).GetRank().Has(Rank.ADMIN));
2014-12-16 18:42:06 +01:00
}
public boolean isHost(Player player)
{
2014-12-22 03:19:45 +01:00
return player.getName().equals(Manager.GetHost());
2014-12-16 18:42:06 +01:00
}
public boolean isPrivateServer()
{
return Manager.GetHost() != null && Manager.GetHost().length() > 0;
2014-11-07 02:14:33 +01:00
}
@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."));
}
}
}
2014-11-07 02:14:33 +01:00
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.");
}
2014-12-22 00:51:19 +01:00
public boolean hasRank(Rank rank)
2014-12-17 03:37:14 +01:00
{
2014-12-22 00:51:19 +01:00
if (_hostRank == null)
return false;
return _hostRank.Has(rank);
2014-12-17 03:37:14 +01:00
}
2014-12-22 06:31:13 +01:00
public ArrayList<GameType> getAvailableGames(Player player)
2014-12-17 03:37:14 +01:00
{
2014-12-22 06:31:13 +01:00
ArrayList<GameType> games = new ArrayList<GameType>();
2014-12-17 03:37:14 +01:00
2014-12-22 00:51:19 +01:00
if (hasRank(Rank.ULTRA))
2014-12-17 03:37:14 +01:00
games.addAll(ultraGames);
2014-12-22 00:51:19 +01:00
if (hasRank(Rank.HERO))
2014-12-17 03:37:14 +01:00
games.addAll(heroGames);
2014-12-22 00:51:19 +01:00
if (hasRank(Rank.LEGEND))
2014-12-17 03:37:14 +01:00
games.addAll(legendGames);
return games;
}
public void ban(Player player)
{
_blacklist.add(player.getName());
2014-12-22 03:19:45 +01:00
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());
2014-12-17 03:37:14 +01:00
_onlineAdmins.add(player);
2014-12-22 00:51:19 +01:00
UtilPlayer.message(player, F.main("Server", "You were given admin privileges."));
}
public void removeAdmin(String playerName)
{
_adminList.remove(playerName);
Player player = UtilPlayer.searchExact(playerName);
if (player != null)
{
_onlineAdmins.remove(player);
removeAdminItem(player);
if (_shop.isPlayerInShop(player))
{
player.closeInventory();
}
UtilPlayer.message(player, F.main("Server", "Your admin privileges were removed."));
}
}
2014-12-18 03:06:57 +01:00
public boolean isAdminOnline()
{
return _onlineAdmins.isEmpty();
}
2014-12-22 06:31:13 +01:00
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;
}
2014-12-23 06:20:20 +01:00
public int getMaxPlayerCap()
{
if (hasRank(Rank.LEGEND))
return 40;
else if (hasRank(Rank.HERO))
return 12;
else
return 4;
}
2014-12-22 06:31:13 +01:00
@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);
}
2014-12-23 06:20:20 +01:00
@EventHandler
public void playerJoin(PlayerJoinEvent event)
{
2014-12-23 09:04:10 +01:00
if (!isPrivateServer())
return;
2014-12-23 06:20:20 +01:00
String serverName = Manager.GetPlugin().getConfig().getString("serverstatus.name");
UtilPlayer.message(event.getPlayer(), ChatColor.BOLD + "Welcome to Mineplex Private Servers!");
UtilPlayer.message(event.getPlayer(), C.Bold + "Friends can connect with " + C.cGreen + C.Bold + "/server " + serverName);
}
}