2014-11-18 06:43:39 +01:00
|
|
|
package nautilus.game.arcade.managers;
|
|
|
|
|
2016-01-25 04:30:42 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2016-01-24 20:00:05 +01:00
|
|
|
import org.bukkit.GameMode;
|
2016-01-25 04:30:42 +01:00
|
|
|
import org.bukkit.entity.Entity;
|
2014-11-18 06:43:39 +01:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.Listener;
|
2016-01-25 04:30:42 +01:00
|
|
|
import org.bukkit.event.block.Action;
|
2016-01-24 20:00:05 +01:00
|
|
|
import org.bukkit.event.entity.PlayerDeathEvent;
|
2014-11-18 06:43:39 +01:00
|
|
|
import org.bukkit.event.player.PlayerInteractEntityEvent;
|
|
|
|
import org.bukkit.event.player.PlayerInteractEvent;
|
2016-01-24 20:00:05 +01:00
|
|
|
import org.bukkit.event.player.PlayerToggleSneakEvent;
|
2014-11-18 06:43:39 +01:00
|
|
|
import org.bukkit.event.vehicle.VehicleDamageEvent;
|
2016-01-31 16:22:59 +01:00
|
|
|
|
|
|
|
import mineplex.core.common.util.C;
|
|
|
|
import mineplex.core.common.util.F;
|
|
|
|
import mineplex.core.common.util.UtilMath;
|
|
|
|
import mineplex.core.common.util.UtilPlayer;
|
|
|
|
import mineplex.core.common.util.UtilServer;
|
|
|
|
import mineplex.core.common.util.UtilTextBottom;
|
|
|
|
import mineplex.core.updater.UpdateType;
|
|
|
|
import mineplex.core.updater.event.UpdateEvent;
|
|
|
|
import nautilus.game.arcade.ArcadeManager;
|
|
|
|
import nautilus.game.arcade.game.Game.GameState;
|
2014-11-18 06:43:39 +01:00
|
|
|
|
|
|
|
public class GameSpectatorManager implements Listener
|
|
|
|
{
|
|
|
|
ArcadeManager Manager;
|
|
|
|
|
|
|
|
public GameSpectatorManager(ArcadeManager manager)
|
|
|
|
{
|
|
|
|
Manager = manager;
|
|
|
|
|
2015-02-26 06:07:07 +01:00
|
|
|
Manager.getPluginManager().registerEvents(this, Manager.getPlugin());
|
2014-11-18 06:43:39 +01:00
|
|
|
}
|
|
|
|
|
2016-01-25 04:30:42 +01:00
|
|
|
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
|
2014-11-18 06:43:39 +01:00
|
|
|
public void interactCancel(PlayerInteractEvent event)
|
2016-01-25 05:44:06 +01:00
|
|
|
{
|
2014-11-18 06:43:39 +01:00
|
|
|
if (Manager.GetGame() == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Player player = event.getPlayer();
|
|
|
|
|
|
|
|
if (!Manager.GetGame().IsAlive(player))
|
|
|
|
event.setCancelled(true);
|
2016-01-25 04:30:42 +01:00
|
|
|
|
2016-01-25 07:45:38 +01:00
|
|
|
processClick(player, event.getAction());
|
2016-01-25 05:44:06 +01:00
|
|
|
}
|
|
|
|
|
2016-01-25 07:45:38 +01:00
|
|
|
public void processClick(Player player, Action action)
|
2016-01-25 05:44:06 +01:00
|
|
|
{
|
|
|
|
if (Manager.GetGame() == null)
|
|
|
|
return;
|
|
|
|
|
2016-01-25 04:30:42 +01:00
|
|
|
if(!Manager.GetGame().AllowEntitySpectate)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!Manager.GetGame().IsLive())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(player.getGameMode() != GameMode.SPECTATOR)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(player.getSpectatorTarget() == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!(player.getSpectatorTarget() instanceof Player))
|
|
|
|
return;
|
|
|
|
|
|
|
|
List<Player> players = Manager.GetGame().GetPlayers(true);
|
|
|
|
int currentPlayer = 0;
|
|
|
|
for(Player otherPlayer : players)
|
|
|
|
{
|
|
|
|
currentPlayer++;
|
|
|
|
if(((Player) player.getSpectatorTarget()) == otherPlayer)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-25 05:44:06 +01:00
|
|
|
if(action == Action.LEFT_CLICK_AIR || action == Action.LEFT_CLICK_BLOCK)
|
2016-01-25 04:30:42 +01:00
|
|
|
currentPlayer = currentPlayer - 2;
|
2016-01-25 07:45:38 +01:00
|
|
|
else
|
|
|
|
return;
|
2016-01-25 04:30:42 +01:00
|
|
|
|
|
|
|
if(currentPlayer < 0)
|
|
|
|
currentPlayer = players.size() - 1;
|
|
|
|
|
|
|
|
if(currentPlayer >= players.size())
|
|
|
|
currentPlayer = 0;
|
|
|
|
|
|
|
|
if(players.get(currentPlayer) == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Player specPlayer = players.get(currentPlayer);
|
|
|
|
|
|
|
|
setSpectating(player, specPlayer);
|
2016-01-25 07:45:38 +01:00
|
|
|
}
|
2014-11-18 06:43:39 +01:00
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.LOW)
|
|
|
|
public void interactEntityCancel(PlayerInteractEntityEvent event)
|
|
|
|
{
|
|
|
|
if (Manager.GetGame() == null)
|
|
|
|
return;
|
2015-10-09 04:28:07 +02:00
|
|
|
|
2014-11-18 06:43:39 +01:00
|
|
|
Player player = event.getPlayer();
|
|
|
|
|
2015-10-09 04:28:07 +02:00
|
|
|
if (Manager.GetGame().GetState() == GameState.Recruit)
|
|
|
|
{
|
|
|
|
if (Manager.getCosmeticManager().getMountManager().isMount(event.getRightClicked()))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-18 06:43:39 +01:00
|
|
|
if (!Manager.GetGame().IsAlive(player))
|
2016-01-24 20:00:05 +01:00
|
|
|
{
|
2014-11-18 06:43:39 +01:00
|
|
|
event.setCancelled(true);
|
2016-01-24 20:00:05 +01:00
|
|
|
if(Manager.GetGame().IsLive())
|
|
|
|
{
|
|
|
|
if(Manager.GetGame().AllowEntitySpectate)
|
|
|
|
{
|
2016-01-25 04:30:42 +01:00
|
|
|
setSpectating(player, event.getRightClicked());
|
2016-01-24 20:00:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void updateSpecEntitys(UpdateEvent event)
|
|
|
|
{
|
|
|
|
if(event.getType() != UpdateType.FASTER)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(Manager.GetGame() == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(Manager.GetGame().IsLive() || Manager.GetGame().GetState() == GameState.End)
|
|
|
|
{
|
|
|
|
if(Manager.GetGame().AllowEntitySpectate)
|
|
|
|
{
|
|
|
|
for(Player player : UtilServer.getPlayers())
|
|
|
|
{
|
|
|
|
if (!Manager.GetGame().IsAlive(player))
|
|
|
|
{
|
|
|
|
if(player.getGameMode() == GameMode.SPECTATOR)
|
|
|
|
{
|
|
|
|
if(player.getSpectatorTarget() == null)
|
|
|
|
{
|
|
|
|
player.setGameMode(GameMode.SURVIVAL);
|
|
|
|
player.setAllowFlight(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.LOW)
|
|
|
|
public void spectatedEntityDeath(PlayerDeathEvent event)
|
|
|
|
{
|
|
|
|
if(Manager.GetGame() == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(Manager.GetGame().IsLive() || Manager.GetGame().GetState() == GameState.End)
|
|
|
|
{
|
|
|
|
if(Manager.GetGame().AllowEntitySpectate)
|
|
|
|
{
|
|
|
|
for(Player player : UtilServer.getPlayers())
|
|
|
|
{
|
|
|
|
if (!Manager.GetGame().IsAlive(player))
|
|
|
|
{
|
|
|
|
if(player.getGameMode() == GameMode.SPECTATOR)
|
|
|
|
{
|
|
|
|
if(player.getSpectatorTarget() == event.getEntity())
|
|
|
|
{
|
2016-01-25 04:30:42 +01:00
|
|
|
if(Manager.GetGame().GetPlayers(true).size() >= 1)
|
|
|
|
{
|
|
|
|
setSpectating(player, Manager.GetGame().GetPlayers(true).get(UtilMath.r(Manager.GetGame().GetPlayers(true).size())));
|
|
|
|
return;
|
|
|
|
}
|
2016-01-24 20:00:05 +01:00
|
|
|
player.setGameMode(GameMode.SURVIVAL);
|
|
|
|
player.setAllowFlight(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.LOW)
|
|
|
|
public void dismountEntity(PlayerToggleSneakEvent event)
|
|
|
|
{
|
|
|
|
if(Manager.GetGame() == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(Manager.GetGame().IsLive() || Manager.GetGame().GetState() == GameState.End)
|
|
|
|
{
|
|
|
|
if(Manager.GetGame().AllowEntitySpectate)
|
|
|
|
{
|
|
|
|
if(!Manager.GetGame().IsAlive(event.getPlayer()))
|
|
|
|
{
|
|
|
|
if(event.getPlayer().getGameMode() == GameMode.SPECTATOR)
|
|
|
|
{
|
|
|
|
event.getPlayer().setGameMode(GameMode.SURVIVAL);
|
|
|
|
event.getPlayer().setAllowFlight(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 06:43:39 +01:00
|
|
|
}
|
|
|
|
|
2016-01-25 04:30:42 +01:00
|
|
|
private void setSpectating(Player player, Entity target)
|
|
|
|
{
|
|
|
|
player.setGameMode(GameMode.SURVIVAL);
|
|
|
|
player.teleport(target.getLocation().add(0, 1, 0));
|
|
|
|
player.getInventory().setHeldItemSlot(5);
|
|
|
|
Manager.runSyncLater(new Runnable()
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
2016-04-14 01:20:39 +02:00
|
|
|
if (Manager.GetGame().IsAlive(player))
|
|
|
|
return;
|
|
|
|
|
2016-01-31 16:22:59 +01:00
|
|
|
Player ptarget = null;
|
|
|
|
if (target instanceof Player)
|
|
|
|
ptarget = (Player) target;
|
|
|
|
|
|
|
|
if (ptarget != null)
|
2016-01-25 04:30:42 +01:00
|
|
|
{
|
2016-01-31 16:22:59 +01:00
|
|
|
if (!Manager.GetGame().IsAlive(ptarget))
|
2016-01-25 04:30:42 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-01-31 16:22:59 +01:00
|
|
|
|
2016-01-25 04:30:42 +01:00
|
|
|
player.setGameMode(GameMode.SPECTATOR);
|
|
|
|
player.setSpectatorTarget(target);
|
|
|
|
|
2016-01-31 16:22:59 +01:00
|
|
|
if (ptarget != null)
|
|
|
|
UtilTextBottom.display(C.cGray + "You are spectating " + F.elem(Manager.GetGame().GetTeam(ptarget).GetColor() + ptarget.getName()) + ".", player);
|
|
|
|
|
|
|
|
UtilPlayer.message(player, F.main("Game", "Sneak to stop spectating."));
|
2016-01-25 04:30:42 +01:00
|
|
|
}
|
2016-01-31 16:22:59 +01:00
|
|
|
}, 1);
|
2016-01-25 04:30:42 +01:00
|
|
|
}
|
|
|
|
|
2014-11-18 06:43:39 +01:00
|
|
|
@EventHandler(priority = EventPriority.LOW)
|
|
|
|
public void vehicleDamage(VehicleDamageEvent event)
|
|
|
|
{
|
|
|
|
if (Manager.GetGame() == null)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(event.getAttacker() instanceof Player))
|
|
|
|
return;
|
|
|
|
|
|
|
|
Player player = (Player)event.getAttacker();
|
|
|
|
|
|
|
|
if (!Manager.GetGame().IsAlive(player))
|
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
|
|
|
}
|