diff options
Diffstat (limited to 'Essentials/src/com/earth2me/essentials/Teleport.java')
-rw-r--r-- | Essentials/src/com/earth2me/essentials/Teleport.java | 85 |
1 files changed, 52 insertions, 33 deletions
diff --git a/Essentials/src/com/earth2me/essentials/Teleport.java b/Essentials/src/com/earth2me/essentials/Teleport.java index 845f9c3a3..9c21ffc6d 100644 --- a/Essentials/src/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/com/earth2me/essentials/Teleport.java @@ -1,14 +1,19 @@ package com.earth2me.essentials; +import com.earth2me.essentials.api.ITeleport; +import static com.earth2me.essentials.I18n._; import com.earth2me.essentials.commands.NotEnoughArgumentsException; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.logging.Logger; import org.bukkit.Location; import org.bukkit.entity.Entity; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerRespawnEvent; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -public class Teleport implements Runnable +public class Teleport implements Runnable, ITeleport { private static final double MOVE_CONSTANT = 0.3; @@ -54,8 +59,9 @@ public class Teleport implements Runnable private Trade chargeFor; private final IEssentials ess; private static final Logger logger = Logger.getLogger("Minecraft"); + private TeleportCause cause; - private void initTimer(long delay, Target target, Trade chargeFor) + private void initTimer(long delay, Target target, Trade chargeFor, TeleportCause cause) { this.started = System.currentTimeMillis(); this.delay = delay; @@ -65,8 +71,10 @@ public class Teleport implements Runnable this.initZ = Math.round(user.getLocation().getZ() * MOVE_CONSTANT); this.teleportTarget = target; this.chargeFor = chargeFor; + this.cause = cause; } + @Override public void run() { @@ -92,11 +100,11 @@ public class Teleport implements Runnable try { cooldown(false); - user.sendMessage(Util.i18n("teleportationCommencing")); + user.sendMessage(_("teleportationCommencing")); try { - now(teleportTarget); + now(teleportTarget, cause); if (chargeFor != null) { chargeFor.charge(user); @@ -106,11 +114,10 @@ public class Teleport implements Runnable { ess.showError(user.getBase(), ex, "teleport"); } - return; } catch (Exception ex) { - user.sendMessage(Util.format("cooldownWithMessage", ex.getMessage())); + user.sendMessage(_("cooldownWithMessage", ex.getMessage())); } } } @@ -121,16 +128,20 @@ public class Teleport implements Runnable this.ess = ess; } - public void respawn(Spawn spawn, Trade chargeFor) throws Exception + public void respawn(final Trade chargeFor, TeleportCause cause) throws Exception { - teleport(new Target(spawn.getSpawn(user.getGroup())), chargeFor); + final Player player = user.getBase(); + final Location bed = player.getBedSpawnLocation(); + final PlayerRespawnEvent pre = new PlayerRespawnEvent(player, bed == null ? player.getWorld().getSpawnLocation() : bed, bed != null); + ess.getServer().getPluginManager().callEvent(pre); + teleport(new Target(pre.getRespawnLocation()), chargeFor, cause); } - public void warp(String warp, Trade chargeFor) throws Exception + public void warp(String warp, Trade chargeFor, TeleportCause cause) throws Exception { Location loc = ess.getWarps().getWarp(warp); - teleport(new Target(loc), chargeFor); - user.sendMessage(Util.format("warpingTo", warp)); + teleport(new Target(loc), chargeFor, cause); + user.sendMessage(_("warpingTo", warp)); } public void cooldown(boolean check) throws Exception @@ -145,7 +156,7 @@ public class Teleport implements Runnable cooldownTime.add(Calendar.MILLISECOND, (int)((cooldown * 1000.0) % 1000.0)); if (cooldownTime.after(now) && !user.isAuthorized("essentials.teleport.cooldown.bypass")) { - throw new Exception(Util.format("timeBeforeTeleport", Util.formatDateDiff(cooldownTime.getTimeInMillis()))); + throw new Exception(_("timeBeforeTeleport", Util.formatDateDiff(cooldownTime.getTimeInMillis()))); } } // if justCheck is set, don't update lastTeleport; we're just checking @@ -166,7 +177,7 @@ public class Teleport implements Runnable ess.getServer().getScheduler().cancelTask(teleTimer); if (notifyUser) { - user.sendMessage(Util.i18n("pendingTeleportCancelled")); + user.sendMessage(_("pendingTeleportCancelled")); } } finally @@ -179,18 +190,23 @@ public class Teleport implements Runnable { cancel(false); } - + public void teleport(Location loc, Trade chargeFor) throws Exception { - teleport(new Target(loc), chargeFor); + teleport(new Target(loc), chargeFor, TeleportCause.PLUGIN); } - public void teleport(Entity entity, Trade chargeFor) throws Exception + public void teleport(Location loc, Trade chargeFor, TeleportCause cause) throws Exception { - teleport(new Target(entity), chargeFor); + teleport(new Target(loc), chargeFor, cause); } - private void teleport(Target target, Trade chargeFor) throws Exception + public void teleport(Entity entity, Trade chargeFor, TeleportCause cause) throws Exception + { + teleport(new Target(entity), chargeFor, cause); + } + + private void teleport(Target target, Trade chargeFor, TeleportCause cause) throws Exception { double delay = ess.getSettings().getTeleportDelay(); @@ -202,7 +218,7 @@ public class Teleport implements Runnable if (delay <= 0 || user.isAuthorized("essentials.teleport.timer.bypass")) { cooldown(false); - now(target); + now(target, cause); if (chargeFor != null) { chargeFor.charge(user); @@ -214,49 +230,52 @@ public class Teleport implements Runnable Calendar c = new GregorianCalendar(); c.add(Calendar.SECOND, (int)delay); c.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0)); - user.sendMessage(Util.format("dontMoveMessage", Util.formatDateDiff(c.getTimeInMillis()))); - initTimer((long)(delay * 1000.0), target, chargeFor); + user.sendMessage(_("dontMoveMessage", Util.formatDateDiff(c.getTimeInMillis()))); + initTimer((long)(delay * 1000.0), target, chargeFor, cause); teleTimer = ess.scheduleSyncRepeatingTask(this, 10, 10); } - private void now(Target target) throws Exception + private void now(Target target, TeleportCause cause) throws Exception { cancel(); user.setLastLocation(); - user.getBase().teleport(Util.getSafeDestination(target.getLocation())); + user.getBase().teleport(Util.getSafeDestination(target.getLocation()), cause); } - public void now(Location loc) throws Exception + public void now(Location loc, boolean cooldown, TeleportCause cause) throws Exception { - cooldown(false); - now(new Target(loc)); + if (cooldown) + { + cooldown(false); + } + now(new Target(loc), cause); } - public void now(Location loc, Trade chargeFor) throws Exception + public void now(Location loc, Trade chargeFor, TeleportCause cause) throws Exception { cooldown(false); chargeFor.charge(user); - now(new Target(loc)); + now(new Target(loc), cause); } - public void now(Entity entity, boolean cooldown) throws Exception + public void now(Entity entity, boolean cooldown, TeleportCause cause) throws Exception { if (cooldown) { cooldown(false); } - now(new Target(entity)); + now(new Target(entity), cause); } public void back(Trade chargeFor) throws Exception { - teleport(new Target(user.getLastLocation()), chargeFor); + teleport(new Target(user.getLastLocation()), chargeFor, TeleportCause.COMMAND); } public void back() throws Exception { - now(new Target(user.getLastLocation())); + now(new Target(user.getLastLocation()), TeleportCause.COMMAND); } public void home(IUser user, String home, Trade chargeFor) throws Exception @@ -266,6 +285,6 @@ public class Teleport implements Runnable { throw new NotEnoughArgumentsException(); } - teleport(new Target(loc), chargeFor); + teleport(new Target(loc), chargeFor, TeleportCause.COMMAND); } } |