From 76d009e0de6b2fde4d4cb4193eb0ad6883aad59e Mon Sep 17 00:00:00 2001 From: NewGarbo Date: Thu, 26 Nov 2015 05:18:18 +0000 Subject: [PATCH] Unstuck (Spawn) command --- .../game/clans/clans/stuck/StuckClient.java | 15 +++ .../game/clans/clans/stuck/StuckManager.java | 109 ++++++++++++++++++ .../clans/stuck/commands/StuckCommand.java | 39 +++++++ 3 files changed, 163 insertions(+) create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/stuck/StuckClient.java create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/stuck/StuckManager.java create mode 100644 Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/stuck/commands/StuckCommand.java diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/stuck/StuckClient.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/stuck/StuckClient.java new file mode 100644 index 000000000..ab5a8b285 --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/stuck/StuckClient.java @@ -0,0 +1,15 @@ +package mineplex.game.clans.clans.stuck; + +import org.bukkit.Location; + +public class StuckClient +{ + public Location StartLoc = null; + public long UnstickStartTime = -1; + public long UnstickEndTime = -1; + + public long getTimeLeft() + { + return UnstickEndTime - System.currentTimeMillis(); + } +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/stuck/StuckManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/stuck/StuckManager.java new file mode 100644 index 000000000..5285f99a6 --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/stuck/StuckManager.java @@ -0,0 +1,109 @@ +package mineplex.game.clans.clans.stuck; + +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; + +import mineplex.core.MiniClientPlugin; +import mineplex.core.common.util.F; +import mineplex.core.common.util.UtilPlayer; +import mineplex.core.common.util.UtilTextMiddle; +import mineplex.core.common.util.UtilTime; +import mineplex.core.common.util.UtilTime.TimeUnit; +import mineplex.core.recharge.Recharge; +import mineplex.core.updater.UpdateType; +import mineplex.core.updater.event.UpdateEvent; +import mineplex.game.clans.clans.ClansManager; +import mineplex.game.clans.clans.stuck.commands.StuckCommand; +import mineplex.game.clans.spawn.Spawn; + +public class StuckManager extends MiniClientPlugin +{ + public static final long UNSTICK_WAIT_TIME = UtilTime.convert(1, TimeUnit.MINUTES, TimeUnit.MILLISECONDS); + + public StuckManager(ClansManager clans) + { + super("Stuck Handler", clans.getPlugin()); + } + + @Override + public void addCommands() + { + addCommand(new StuckCommand(this)); + } + + @Override + protected StuckClient AddPlayer(String player) + { + return new StuckClient(); + } + + public void unstick(Player player) + { + Get(player).UnstickStartTime = System.currentTimeMillis(); + Get(player).UnstickEndTime = Get(player).UnstickStartTime + UNSTICK_WAIT_TIME; + Get(player).StartLoc = player.getLocation(); + } + + public boolean isUnsticking(Player caller) + { + return Get(caller).UnstickStartTime != -1; + } + + @EventHandler + public void updateTeleport(UpdateEvent event) + { + if (event.getType() != UpdateType.FASTER) + { + for (Player player : Bukkit.getOnlinePlayers()) + { + if (isUnsticking(player)) + { + if (player.getLocation().distance(Get(player).StartLoc) >= 0.75) + { + cancelTeleport(player); + break; + } + + UtilTextMiddle.display("", "Teleporting to Spawn in " + F.elem(UtilTime.MakeStr(Get(player).getTimeLeft())), 0, 20, 60, player); + + if (Get(player).getTimeLeft() <= 500) + { + teleport(player); + } + } + } + } + } + + private void cancelTeleport(Player player) + { + UtilPlayer.message(player, "You have moved, and cancelled the teleport."); + + Get(player).UnstickStartTime = -1; + Get(player).UnstickEndTime = -1; + Get(player).StartLoc = null; + } + + private void teleport(Player player) + { + Recharge.Instance.use(player, "Spawn Teleport", UtilTime.convert(15, TimeUnit.MINUTES, TimeUnit.MILLISECONDS), false, false); + + UtilPlayer.message(player, "You have been teleported to Spawn."); + + Get(player).UnstickStartTime = -1; + Get(player).UnstickEndTime = -1; + Get(player).StartLoc = null; + + if (Math.random() < .5) + { + player.teleport(Spawn.getEastSpawn()); + } + else + { + player.teleport(Spawn.getWestSpawn()); + } + + } + +} diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/stuck/commands/StuckCommand.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/stuck/commands/StuckCommand.java new file mode 100644 index 000000000..f8759808e --- /dev/null +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/stuck/commands/StuckCommand.java @@ -0,0 +1,39 @@ +package mineplex.game.clans.clans.stuck.commands; + +import org.bukkit.entity.Player; + +import mineplex.core.command.CommandBase; +import mineplex.core.common.Rank; +import mineplex.core.common.util.F; +import mineplex.core.common.util.UtilPlayer; +import mineplex.core.common.util.UtilTime; +import mineplex.core.recharge.Recharge; +import mineplex.game.clans.clans.stuck.StuckManager; + +public class StuckCommand extends CommandBase +{ + public StuckCommand(StuckManager plugin) + { + super(plugin, Rank.ALL, "spawn", "stuck", "unstick", "unstuck"); + } + + @Override + public void Execute(Player caller, String[] args) + { + if (Plugin.isUnsticking(caller)) + { + UtilPlayer.message(caller, F.main("Clans", "You are already teleporting to Spawn.")); + + return; + } + + if (!Recharge.Instance.usable(caller, "Spawn Teleport", true)) + { + return; + } + + UtilPlayer.message(caller, F.main("Clans", "Teleporting to Spawn... Please do not move for " + F.elem(UtilTime.MakeStr(StuckManager.UNSTICK_WAIT_TIME))) + "."); + + Plugin.unstick(caller); + } +}