summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/TimedTeleport.java
blob: e30eff96697f8bc820acd7f66d274f89376977be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.earth2me.essentials;

import static com.earth2me.essentials.I18n._;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.ess3.api.IEssentials;
import net.ess3.api.IUser;
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;
	private final String timer_teleportee;
	private int timer_task = -1;
	private final long timer_started;	// time this task was initiated
	private final long timer_delay;		// how long to delay the teleportPlayer
	private double timer_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)
	// so, the X/Y/Z values are stored instead and rounded off
	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;

	public TimedTeleport(IUser user, IEssentials ess, Teleport teleport, long delay, IUser teleportUser, ITarget target, Trade chargeFor, TeleportCause cause, boolean respawn)
	{

		this.teleportOwner = user;
		this.ess = ess;
		this.teleport = teleport;
		this.timer_started = System.currentTimeMillis();
		this.timer_delay = delay;
		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);
		this.timer_teleportee = teleportUser.getName();
		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");

		timer_task = ess.scheduleSyncRepeatingTask(this, 20, 20);
	}

	@Override
	public void run()
	{

		if (teleportOwner == null || !teleportOwner.getBase().isOnline() || teleportOwner.getBase().getLocation() == null)
		{
			cancelTimer(false);
			return;
		}

		IUser teleportUser = ess.getUser(this.timer_teleportee);

		if (teleportUser == null || !teleportUser.getBase().isOnline())
		{
			cancelTimer(false);
			return;
		}

		final Location currLocation = teleportUser.getBase().getLocation();
		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
				|| teleportUser.getBase().getHealth() < timer_health))
		{
			// user moved, cancelTimer teleportPlayer
			cancelTimer(true);
			return;
		}

		timer_health = teleportUser.getBase().getHealth();  // in case user healed, then later gets injured
		final long now = System.currentTimeMillis();
		if (now > timer_started + timer_delay)
		{
			try
			{
				teleport.cooldown(false);
			}
			catch (Exception ex)
			{
				teleportOwner.sendMessage(_("cooldownWithMessage", ex.getMessage()));
				if (teleportOwner != teleportUser)
				{
					teleportUser.sendMessage(_("cooldownWithMessage", ex.getMessage()));
				}
			}
			try
			{
				cancelTimer(false);
				teleportUser.sendMessage(_("teleportationCommencing"));
				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)
			{
				ess.showError(teleportOwner.getSource(), ex, "\\ teleport");
			}

		}
	}

	//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)
			{
				teleportOwner.sendMessage(_("pendingTeleportCancelled"));
				if (timer_teleportee != null && !timer_teleportee.equals(teleportOwner.getName()))
				{
					ess.getUser(timer_teleportee).sendMessage(_("pendingTeleportCancelled"));
				}
			}
		}
		finally
		{
			timer_task = -1;
		}
	}
}