2014-11-07 01:52:58 +01:00
|
|
|
package nautilus.game.arcade.managers;
|
|
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
2014-11-07 02:14:33 +01:00
|
|
|
import mineplex.core.common.util.C;
|
2014-11-07 01:52:58 +01:00
|
|
|
import mineplex.core.common.util.F;
|
2014-11-07 02:14:33 +01:00
|
|
|
import mineplex.core.common.util.UtilGear;
|
2014-11-07 01:52:58 +01:00
|
|
|
import mineplex.core.common.util.UtilPlayer;
|
|
|
|
import mineplex.core.common.util.UtilTime;
|
2014-11-07 02:14:33 +01:00
|
|
|
import mineplex.core.itemstack.ItemStackFactory;
|
2014-11-07 01:52:58 +01:00
|
|
|
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-11-07 01:52:58 +01:00
|
|
|
|
2014-11-07 02:14:33 +01:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.Material;
|
2014-11-07 01:52:58 +01:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
2014-11-07 02:14:33 +01:00
|
|
|
import org.bukkit.event.HandlerList;
|
2014-11-07 01:52:58 +01:00
|
|
|
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;
|
2014-11-07 01:52:58 +01:00
|
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
|
|
|
import org.bukkit.event.player.PlayerQuitEvent;
|
|
|
|
|
|
|
|
public class GameHostManager implements Listener
|
|
|
|
{
|
|
|
|
ArcadeManager Manager;
|
|
|
|
|
|
|
|
private Player _host;
|
|
|
|
private long _serverStartTime = System.currentTimeMillis();
|
|
|
|
private long _serverExpireTime = 43200000;
|
|
|
|
private long _lastOnline = System.currentTimeMillis();
|
|
|
|
private long _expireTime = 300000;
|
|
|
|
|
|
|
|
private HashSet<String> _whitelist = new HashSet<String>();
|
|
|
|
private HashSet<String> _blacklist = new HashSet<String>();
|
|
|
|
|
|
|
|
public GameHostManager(ArcadeManager manager)
|
|
|
|
{
|
|
|
|
Manager = manager;
|
|
|
|
|
|
|
|
Manager.GetPluginManager().registerEvents(this, Manager.GetPlugin());
|
|
|
|
}
|
2014-11-07 11:32:44 +01:00
|
|
|
/*
|
2014-11-07 01:52:58 +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;
|
|
|
|
|
|
|
|
//Host Update
|
|
|
|
if (_host != null)
|
|
|
|
{
|
|
|
|
giveHostItem();
|
|
|
|
|
|
|
|
_lastOnline = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void hostJoin(PlayerJoinEvent event)
|
|
|
|
{
|
|
|
|
if (Manager.GetHost() == null || Manager.GetHost().length() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Manager.GetHost().equals(event.getPlayer().getName()))
|
|
|
|
{
|
|
|
|
System.out.println("Game Host Joined.");
|
|
|
|
_host = event.getPlayer();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void hostQuit(PlayerQuitEvent event)
|
|
|
|
{
|
|
|
|
if (Manager.GetHost() == null || Manager.GetHost().length() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (Manager.GetHost().equals(event.getPlayer().getName()))
|
|
|
|
{
|
|
|
|
System.out.println("Game Host Quit.");
|
|
|
|
_host = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isHostExpired()
|
|
|
|
{
|
|
|
|
if (Manager.GetHost() == null || Manager.GetHost().length() == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return UtilTime.elapsed(_lastOnline, _expireTime) || UtilTime.elapsed(_serverStartTime, _serverExpireTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void giveHostItem()
|
|
|
|
{
|
2014-11-07 02:14:33 +01:00
|
|
|
if (Manager.GetGame() == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (_host == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (UtilGear.isMat(_host.getInventory().getItem(5), Material.BOOK_AND_QUILL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
_host.getInventory().setItem(5, ItemStackFactory.Instance.CreateStack(Material.BOOK_AND_QUILL, (byte)0, 1, C.cGreen + C.Bold + "/menu"));
|
|
|
|
}
|
|
|
|
|
|
|
|
public HashSet<String> getWhitelist()
|
|
|
|
{
|
|
|
|
return _whitelist;
|
|
|
|
}
|
|
|
|
|
|
|
|
public HashSet<String> getBlacklist()
|
|
|
|
{
|
|
|
|
return _blacklist;
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void menuCommand(PlayerCommandPreprocessEvent event)
|
|
|
|
{
|
|
|
|
if (_host == null || !event.getPlayer().equals(_host))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!event.getMessage().toLowerCase().startsWith("/menu"))
|
|
|
|
return;
|
2014-11-07 01:52:58 +01:00
|
|
|
|
2014-11-07 02:14:33 +01:00
|
|
|
openMenu();
|
2014-11-07 01:52:58 +01:00
|
|
|
}
|
|
|
|
|
2014-11-07 02:14:33 +01:00
|
|
|
@EventHandler
|
|
|
|
public void menuInteract(PlayerInteractEvent event)
|
|
|
|
{
|
|
|
|
if (_host == null || !event.getPlayer().equals(_host))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!UtilGear.isMat(_host.getItemInHand(), Material.BOOK_AND_QUILL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
openMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void openMenu()
|
|
|
|
{
|
|
|
|
if (_host == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
//XXX OPEN GUI HERE
|
|
|
|
}
|
|
|
|
|
2014-11-07 01:52:58 +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
|
|
|
|
|
|
|
@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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-11-07 11:32:44 +01:00
|
|
|
*/
|
2014-11-07 01:52:58 +01:00
|
|
|
}
|