From 10c8540fa0d9733a1ad403d8f3361cab0adacbb9 Mon Sep 17 00:00:00 2001 From: ementalo Date: Thu, 25 Oct 2012 15:41:29 +0100 Subject: Porting 2.9 teleport changes --- Essentials/src/net/ess3/Teleport.java | 184 ++++++++++++--------- Essentials/src/net/ess3/api/ITeleport.java | 5 + .../src/net/ess3/commands/Commandtpaccept.java | 3 +- .../src/net/ess3/commands/Commandtphere.java | 3 +- .../src/net/ess3/permissions/Permissions.java | 1 + Essentials/src/net/ess3/utils/Target.java | 32 ++++ 6 files changed, 151 insertions(+), 77 deletions(-) create mode 100644 Essentials/src/net/ess3/utils/Target.java diff --git a/Essentials/src/net/ess3/Teleport.java b/Essentials/src/net/ess3/Teleport.java index 9ca5ff875..ef1957e64 100644 --- a/Essentials/src/net/ess3/Teleport.java +++ b/Essentials/src/net/ess3/Teleport.java @@ -13,6 +13,7 @@ import net.ess3.user.CooldownException; import net.ess3.user.UserData.TimestampType; import net.ess3.utils.DateUtil; import net.ess3.utils.LocationUtil; +import net.ess3.utils.Target; import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; @@ -22,37 +23,11 @@ public class Teleport implements Runnable, ITeleport { private static final double MOVE_CONSTANT = 0.3; - - private static class Target - { - private final Location location; - private final Entity entity; - - Target(Location location) - { - this.location = location; - this.entity = null; - } - - Target(Entity entity) - { - this.entity = entity; - this.location = null; - } - - public Location getLocation() - { - if (this.entity != null) - { - return this.entity.getLocation(); - } - return location; - } - } private IUser user; + private IUser teleportUser; private int teleTimer = -1; private long started; // time this task was initiated - private long delay; // how long to delay the teleport + private long tpDelay; // how long to delay the teleport private int health; // note that I initially stored a clone of the location for reference, but... // when comparing locations, I got incorrect mismatches (rounding errors, looked like) @@ -67,13 +42,19 @@ public class Teleport implements Runnable, ITeleport private TeleportCause cause; private void initTimer(long delay, Target target, Trade chargeFor, TeleportCause cause) + { + initTimer(delay, user, target, chargeFor, cause); + } + + private void initTimer(long delay, IUser teleportUser, Target target, Trade chargeFor, TeleportCause cause) { this.started = System.currentTimeMillis(); - this.delay = delay; - this.health = user.getPlayer().getHealth(); - this.initX = Math.round(user.getPlayer().getLocation().getX() * MOVE_CONSTANT); - this.initY = Math.round(user.getPlayer().getLocation().getY() * MOVE_CONSTANT); - this.initZ = Math.round(user.getPlayer().getLocation().getZ() * MOVE_CONSTANT); + this.tpDelay = delay; + this.health = teleportUser.getPlayer().getHealth(); + this.initX = Math.round(teleportUser.getPlayer().getLocation().getX() * MOVE_CONSTANT); + this.initY = Math.round(teleportUser.getPlayer().getLocation().getY() * MOVE_CONSTANT); + this.initZ = Math.round(teleportUser.getPlayer().getLocation().getZ() * MOVE_CONSTANT); + this.teleportUser = teleportUser; this.teleportTarget = target; this.chargeFor = chargeFor; this.cause = cause; @@ -88,28 +69,36 @@ public class Teleport implements Runnable, ITeleport cancel(); return; } - if (Math.round(user.getPlayer().getLocation().getX() * MOVE_CONSTANT) != initX - || Math.round(user.getPlayer().getLocation().getY() * MOVE_CONSTANT) != initY - || Math.round(user.getPlayer().getLocation().getZ() * MOVE_CONSTANT) != initZ - || user.getPlayer().getHealth() < health) + + if (teleportUser == null || !teleportUser.isOnline() || teleportUser.getPlayer().getLocation() == null) + { + cancel(false); + return; + } + + if (!Permissions.TELEPORT_TIMER_MOVE.isAuthorized(user) + &&(Math.round(teleportUser.getPlayer().getLocation().getX() * MOVE_CONSTANT) != initX + || Math.round(teleportUser.getPlayer().getLocation().getY() * MOVE_CONSTANT) != initY + || Math.round(teleportUser.getPlayer().getLocation().getZ() * MOVE_CONSTANT) != initZ + || teleportUser.getPlayer().getHealth() < health)) { // user moved, cancel teleport cancel(true); return; } - health = user.getPlayer().getHealth(); // in case user healed, then later gets injured + health = teleportUser.getPlayer().getHealth(); // in case user healed, then later gets injured long now = System.currentTimeMillis(); - if (now > started + delay) + if (now > started + tpDelay) { try { cooldown(false); - user.sendMessage(_("teleportationCommencing")); + teleportUser.sendMessage(_("teleportationCommencing")); try { - now(teleportTarget, cause); + teleportUser.getTeleport().now(teleportTarget, cause); if (chargeFor != null) { chargeFor.charge(user); @@ -123,6 +112,10 @@ public class Teleport implements Runnable, ITeleport catch (Exception ex) { user.sendMessage(_("cooldownWithMessage", ex.getMessage())); + if (user != teleportUser) + { + teleportUser.sendMessage(_("cooldownWithMessage", ex.getMessage())); + } } } } @@ -133,21 +126,7 @@ public class Teleport implements Runnable, ITeleport this.ess = ess; } - @Override - public void respawn(final Trade chargeFor, TeleportCause cause) throws Exception - { - final Location bed = user.getBedSpawnLocation(); - final Location respawnLoc = ess.getPlugin().callRespawnEvent(user.getPlayer(), bed == null ? user.getPlayer().getWorld().getSpawnLocation() : bed, bed != null); - teleport(new Target(respawnLoc), chargeFor, cause); - } - - @Override - public void warp(String warp, Trade chargeFor, TeleportCause cause) throws Exception - { - final Location loc = ess.getWarps().getWarp(warp); - teleport(new Target(loc), chargeFor, cause); - user.sendMessage(_("warpingTo", warp)); - } + public void cooldown(boolean check) throws Exception { @@ -162,6 +141,7 @@ public class Teleport implements Runnable, ITeleport } } + //If we need to cancel a pending teleport call this method public void cancel(boolean notifyUser) { if (teleTimer == -1) @@ -174,6 +154,10 @@ public class Teleport implements Runnable, ITeleport if (notifyUser) { user.sendMessage(_("pendingTeleportCancelled")); + if (teleportUser != user) + { + teleportUser.sendMessage(_("pendingTeleportCancelled")); + } } } finally @@ -186,18 +170,14 @@ public class Teleport implements Runnable, ITeleport { cancel(false); } - - public void teleport(Location loc, Trade chargeFor) throws Exception - { - teleport(new Target(loc), chargeFor, TeleportCause.PLUGIN); - } - + @Override public void teleport(Location loc, Trade chargeFor, TeleportCause cause) throws Exception { teleport(new Target(loc), chargeFor, cause); } + @Override public void teleport(Entity entity, Trade chargeFor, TeleportCause cause) throws Exception { teleport(new Target(entity), chargeFor, cause); @@ -205,14 +185,14 @@ public class Teleport implements Runnable, ITeleport private void teleport(Target target, Trade chargeFor, TeleportCause cause) throws Exception { - double tDelay = ess.getRanks().getTeleportDelay(user); + double delay = ess.getRanks().getTeleportDelay(user); if (chargeFor != null) { chargeFor.isAffordableFor(user); } cooldown(true); - if (tDelay <= 0 || Permissions.TELEPORT_TIMER_BYPASS.isAuthorized(user)) + if (delay <= 0 || Permissions.TELEPORT_TIMER_BYPASS.isAuthorized(user)) { cooldown(false); now(target, cause); @@ -224,16 +204,14 @@ public class Teleport implements Runnable, ITeleport } cancel(); - Calendar c = new GregorianCalendar(); - c.add(Calendar.SECOND, (int)tDelay); - c.add(Calendar.MILLISECOND, (int)((tDelay * 1000.0) % 1000.0)); - user.sendMessage(_("dontMoveMessage", DateUtil.formatDateDiff(c.getTimeInMillis()))); - initTimer((long)(tDelay * 1000.0), target, chargeFor, cause); + warnUser(user, delay); + initTimer((long)(delay * 1000.0), target, chargeFor, cause); teleTimer = ess.getPlugin().scheduleSyncRepeatingTask(this, 10, 10); } - private void now(final Target target, final TeleportCause cause) throws Exception + @Override + public void now(final Target target, final TeleportCause cause) throws Exception { cancel(); user.setLastLocation(); @@ -258,20 +236,76 @@ public class Teleport implements Runnable, ITeleport now(new Target(loc), cause); } + + @Override + //The now function is used when you want to skip tp delay when teleporting someone to a location or player. + public void now(Entity entity, boolean cooldown, TeleportCause cause) throws Exception + { + if (cooldown) + { + cooldown(false); + } + now(new Target(entity), cause); + } + public void now(Location loc, Trade chargeFor, TeleportCause cause) throws Exception { cooldown(false); chargeFor.charge(user); now(new Target(loc), cause); } - - public void now(Entity entity, boolean cooldown, TeleportCause cause) throws Exception + + //The teleportToMe function is a wrapper used to handle teleporting players to them, like /tphere + public void teleportToMe(IUser otherUser, Trade chargeFor, TeleportCause cause) throws Exception { - if (cooldown) + Target target = new Target(user.getPlayer()); + + double delay = ess.getRanks().getTeleportDelay(user); + + if (chargeFor != null) { - cooldown(false); + chargeFor.isAffordableFor(user); } - now(new Target(entity), cause); + cooldown(true); + if (delay <= 0 || Permissions.TELEPORT_TIMER_BYPASS.isAuthorized(user)) + { + cooldown(false); + otherUser.getTeleport().now(target, cause); + if (chargeFor != null) + { + chargeFor.charge(user); + } + return; } + + cancel(false); + warnUser(otherUser, delay); + initTimer((long)(delay * 1000.0), otherUser, target, chargeFor, cause); + + teleTimer = ess.getPlugin().scheduleSyncRepeatingTask(this, 10, 10); + } + + private void warnUser(final IUser user, final double delay) + { + Calendar c = new GregorianCalendar(); + c.add(Calendar.SECOND, (int)delay); + c.add(Calendar.MILLISECOND, (int)((delay * 1000.0) % 1000.0)); + user.sendMessage(_("dontMoveMessage", DateUtil.formatDateDiff(c.getTimeInMillis()))); + } + + @Override + public void respawn(final Trade chargeFor, TeleportCause cause) throws Exception + { + final Location bed = user.getBedSpawnLocation(); + final Location respawnLoc = ess.getPlugin().callRespawnEvent(user.getPlayer(), bed == null ? user.getPlayer().getWorld().getSpawnLocation() : bed, bed != null); + teleport(new Target(respawnLoc), chargeFor, cause); + } + + @Override + public void warp(String warp, Trade chargeFor, TeleportCause cause) throws Exception + { + final Location loc = ess.getWarps().getWarp(warp); + user.sendMessage(_("warpingTo", warp)); + teleport(new Target(loc), chargeFor, cause); } @Override diff --git a/Essentials/src/net/ess3/api/ITeleport.java b/Essentials/src/net/ess3/api/ITeleport.java index afec3e213..35f973ce5 100644 --- a/Essentials/src/net/ess3/api/ITeleport.java +++ b/Essentials/src/net/ess3/api/ITeleport.java @@ -1,6 +1,7 @@ package net.ess3.api; import net.ess3.economy.Trade; +import net.ess3.utils.Target; import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; @@ -11,12 +12,16 @@ public interface ITeleport void now(Location loc, boolean cooldown, TeleportCause cause) throws Exception; void now(Entity entity, boolean cooldown, TeleportCause cause) throws Exception; + + void now(final Target target, final TeleportCause cause) throws Exception; void back(Trade chargeFor) throws Exception; void teleport(Location bed, Trade charge, TeleportCause teleportCause) throws Exception; void teleport(Entity entity, Trade chargeFor, TeleportCause cause) throws Exception; + + void teleportToMe(IUser otherUser, Trade chargeFor, TeleportCause cause) throws Exception; void home(Location loc, Trade chargeFor) throws Exception; diff --git a/Essentials/src/net/ess3/commands/Commandtpaccept.java b/Essentials/src/net/ess3/commands/Commandtpaccept.java index a33d216ad..8aed3028e 100644 --- a/Essentials/src/net/ess3/commands/Commandtpaccept.java +++ b/Essentials/src/net/ess3/commands/Commandtpaccept.java @@ -56,12 +56,13 @@ public class Commandtpaccept extends EssentialsCommand if (user.isTpRequestHere()) { - user.getTeleport().teleport(target.getPlayer(), charge, TeleportCause.COMMAND); + target.getTeleport().teleportToMe(user, charge, TeleportCause.COMMAND); } else { target.getTeleport().teleport(user.getPlayer(), charge, TeleportCause.COMMAND); } user.requestTeleport(null, false); + throw new NoChargeException(); } } diff --git a/Essentials/src/net/ess3/commands/Commandtphere.java b/Essentials/src/net/ess3/commands/Commandtphere.java index 525591e9b..7cacbd9e9 100644 --- a/Essentials/src/net/ess3/commands/Commandtphere.java +++ b/Essentials/src/net/ess3/commands/Commandtphere.java @@ -16,7 +16,8 @@ public class Commandtphere extends EssentialsCommand { throw new Exception(_("teleportDisabled", player.getPlayer().getDisplayName())); } - player.getTeleport().teleport(user.getPlayer(), new Trade(commandName, ess), TeleportCause.COMMAND); + + user.getTeleport().teleportToMe(player, new Trade(commandName, ess), TeleportCause.COMMAND); user.sendMessage(_("teleporting")); player.sendMessage(_("teleporting")); throw new NoChargeException(); diff --git a/Essentials/src/net/ess3/permissions/Permissions.java b/Essentials/src/net/ess3/permissions/Permissions.java index 2dbf91ec2..84afcef23 100644 --- a/Essentials/src/net/ess3/permissions/Permissions.java +++ b/Essentials/src/net/ess3/permissions/Permissions.java @@ -96,6 +96,7 @@ public enum Permissions implements IPermission TELEPORT_HIDDEN, TELEPORT_OTHERS, TELEPORT_TIMER_BYPASS, + TELEPORT_TIMER_MOVE, TEMPBAN_EXEMPT, TEMPBAN_OFFLINE, TIME_SET, diff --git a/Essentials/src/net/ess3/utils/Target.java b/Essentials/src/net/ess3/utils/Target.java new file mode 100644 index 000000000..43e124eea --- /dev/null +++ b/Essentials/src/net/ess3/utils/Target.java @@ -0,0 +1,32 @@ +package net.ess3.utils; + +import org.bukkit.Location; +import org.bukkit.entity.Entity; + +public class Target +{ + private final Location location; + private final Entity entity; + + public Target(Location location) + { + this.location = location; + this.entity = null; + } + + public Target(Entity entity) + { + this.entity = entity; + this.location = null; + } + + public Location getLocation() + { + if (this.entity != null) + { + return this.entity.getLocation(); + } + return location; + } + } + -- cgit v1.2.3