2013-06-08 18:34:14 +00:00
|
|
|
package com.earth2me.essentials;
|
|
|
|
|
2014-03-20 15:54:07 +00:00
|
|
|
import static com.earth2me.essentials.I18n.tl;
|
2014-05-05 12:01:20 +00:00
|
|
|
import java.util.UUID;
|
2013-07-13 15:14:39 +00:00
|
|
|
import net.ess3.api.IEssentials;
|
|
|
|
import net.ess3.api.IUser;
|
2013-06-08 18:34:14 +00:00
|
|
|
import org.bukkit.Location;
|
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
|
|
|
|
|
|
|
|
|
|
|
|
public class TimedTeleport implements Runnable
|
|
|
|
{
|
|
|
|
private static final double MOVE_CONSTANT = 0.3;
|
|
|
|
private final IUser teleportOwner;
|
|
|
|
private final IEssentials ess;
|
|
|
|
private final Teleport teleport;
|
2014-05-05 12:01:20 +00:00
|
|
|
private final UUID timer_teleportee;
|
2013-06-08 18:34:14 +00:00
|
|
|
private int timer_task = -1;
|
2013-11-07 02:22:32 +00:00
|
|
|
private final long timer_started; // time this task was initiated
|
|
|
|
private final long timer_delay; // how long to delay the teleportPlayer
|
2013-07-02 02:38:27 +00:00
|
|
|
private double timer_health;
|
2013-06-08 18:34:14 +00:00
|
|
|
// note that I initially stored a clone of the location for reference, but...
|
|
|
|
// when comparing locations, I got incorrect mismatches (rounding errors, looked like)
|
|
|
|
// so, the X/Y/Z values are stored instead and rounded off
|
2013-11-07 02:22:32 +00:00
|
|
|
private final long timer_initX;
|
|
|
|
private final long timer_initY;
|
|
|
|
private final long timer_initZ;
|
|
|
|
private final ITarget timer_teleportTarget;
|
|
|
|
private final boolean timer_respawn;
|
|
|
|
private final boolean timer_canMove;
|
|
|
|
private final Trade timer_chargeFor;
|
|
|
|
private final TeleportCause timer_cause;
|
2013-06-08 18:34:14 +00:00
|
|
|
|
2013-06-08 20:40:02 +00:00
|
|
|
public TimedTeleport(IUser user, IEssentials ess, Teleport teleport, long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn)
|
2013-06-08 18:34:14 +00:00
|
|
|
{
|
2013-06-08 20:40:02 +00:00
|
|
|
|
2013-06-08 18:34:14 +00:00
|
|
|
this.teleportOwner = user;
|
|
|
|
this.ess = ess;
|
|
|
|
this.teleport = teleport;
|
|
|
|
this.timer_started = System.currentTimeMillis();
|
|
|
|
this.timer_delay = delay;
|
2013-07-07 11:11:57 +00:00
|
|
|
this.timer_health = teleportUser.getBase().getHealth();
|
|
|
|
this.timer_initX = Math.round(teleportUser.getBase().getLocation().getX() * MOVE_CONSTANT);
|
|
|
|
this.timer_initY = Math.round(teleportUser.getBase().getLocation().getY() * MOVE_CONSTANT);
|
|
|
|
this.timer_initZ = Math.round(teleportUser.getBase().getLocation().getZ() * MOVE_CONSTANT);
|
2014-05-05 12:01:20 +00:00
|
|
|
this.timer_teleportee = teleportUser.getBase().getUniqueId();
|
2013-06-08 18:34:14 +00:00
|
|
|
this.timer_teleportTarget = target;
|
|
|
|
this.timer_chargeFor = chargeFor;
|
|
|
|
this.timer_cause = cause;
|
|
|
|
this.timer_respawn = respawn;
|
|
|
|
this.timer_canMove = user.isAuthorized("essentials.teleport.timer.move");
|
|
|
|
|
2014-05-17 00:12:13 +00:00
|
|
|
timer_task = ess.runTaskTimerAsynchronously(this, 20, 20).getTaskId();
|
2013-06-08 18:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
|
2013-07-07 11:11:57 +00:00
|
|
|
if (teleportOwner == null || !teleportOwner.getBase().isOnline() || teleportOwner.getBase().getLocation() == null)
|
2013-06-08 18:34:14 +00:00
|
|
|
{
|
|
|
|
cancelTimer(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-17 00:12:13 +00:00
|
|
|
final IUser teleportUser = ess.getUser(this.timer_teleportee);
|
2013-06-08 18:34:14 +00:00
|
|
|
|
2013-07-07 11:11:57 +00:00
|
|
|
if (teleportUser == null || !teleportUser.getBase().isOnline())
|
2013-06-08 18:34:14 +00:00
|
|
|
{
|
|
|
|
cancelTimer(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-07 11:11:57 +00:00
|
|
|
final Location currLocation = teleportUser.getBase().getLocation();
|
2013-06-08 18:34:14 +00:00
|
|
|
if (currLocation == null)
|
|
|
|
{
|
|
|
|
cancelTimer(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!timer_canMove
|
|
|
|
&& (Math.round(currLocation.getX() * MOVE_CONSTANT) != timer_initX
|
|
|
|
|| Math.round(currLocation.getY() * MOVE_CONSTANT) != timer_initY
|
|
|
|
|| Math.round(currLocation.getZ() * MOVE_CONSTANT) != timer_initZ
|
2013-07-07 11:11:57 +00:00
|
|
|
|| teleportUser.getBase().getHealth() < timer_health))
|
2013-06-08 18:34:14 +00:00
|
|
|
{
|
|
|
|
// user moved, cancelTimer teleportPlayer
|
|
|
|
cancelTimer(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-18 20:32:07 +00:00
|
|
|
class DelayedTeleportTask implements Runnable
|
2013-06-08 18:34:14 +00:00
|
|
|
{
|
2014-05-17 00:12:13 +00:00
|
|
|
@Override
|
|
|
|
public void run()
|
2013-06-08 18:34:14 +00:00
|
|
|
{
|
2014-05-17 00:12:13 +00:00
|
|
|
|
|
|
|
timer_health = teleportUser.getBase().getHealth(); // in case user healed, then later gets injured
|
|
|
|
final long now = System.currentTimeMillis();
|
|
|
|
if (now > timer_started + timer_delay)
|
2014-03-19 00:01:14 +00:00
|
|
|
{
|
2014-05-17 00:12:13 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
teleport.cooldown(false);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
teleportOwner.sendMessage(tl("cooldownWithMessage", ex.getMessage()));
|
|
|
|
if (teleportOwner != teleportUser)
|
|
|
|
{
|
|
|
|
teleportUser.sendMessage(tl("cooldownWithMessage", ex.getMessage()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
cancelTimer(false);
|
|
|
|
teleportUser.sendMessage(tl("teleportationCommencing"));
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (timer_chargeFor != null)
|
|
|
|
{
|
|
|
|
timer_chargeFor.isAffordableFor(teleportOwner);
|
|
|
|
}
|
|
|
|
if (timer_respawn)
|
|
|
|
{
|
|
|
|
teleport.respawnNow(teleportUser, timer_cause);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
teleport.now(teleportUser, timer_teleportTarget, timer_cause);
|
|
|
|
}
|
|
|
|
if (timer_chargeFor != null)
|
|
|
|
{
|
|
|
|
timer_chargeFor.charge(teleportOwner);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
ess.showError(teleportOwner.getSource(), ex, "\\ teleport");
|
|
|
|
}
|
2014-03-19 00:01:14 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-18 20:32:07 +00:00
|
|
|
}
|
|
|
|
ess.scheduleSyncDelayedTask(new DelayedTeleportTask());
|
2013-06-08 18:34:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//If we need to cancelTimer a pending teleportPlayer call this method
|
|
|
|
public void cancelTimer(boolean notifyUser)
|
|
|
|
{
|
|
|
|
if (timer_task == -1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
ess.getServer().getScheduler().cancelTask(timer_task);
|
|
|
|
if (notifyUser)
|
|
|
|
{
|
2014-03-20 15:54:07 +00:00
|
|
|
teleportOwner.sendMessage(tl("pendingTeleportCancelled"));
|
2014-05-05 12:01:20 +00:00
|
|
|
if (timer_teleportee != null && !timer_teleportee.equals(teleportOwner.getBase().getUniqueId()))
|
2013-06-08 18:34:14 +00:00
|
|
|
{
|
2014-03-20 15:54:07 +00:00
|
|
|
ess.getUser(timer_teleportee).sendMessage(tl("pendingTeleportCancelled"));
|
2013-06-08 18:34:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
timer_task = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|