summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/commands/Commandtpaccept.java
blob: ed56a62757c1bf3110f920ac9b643d78842b79c1 (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
package com.earth2me.essentials.commands;

import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.Trade;
import com.earth2me.essentials.User;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;


public class Commandtpaccept extends EssentialsCommand
{
	public Commandtpaccept()
	{
		super("tpaccept");
	}

	@Override
	public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
	{
		final User requester;
		try
		{
			requester = ess.getUser(user.getTeleportRequest());
		}
		catch (Exception ex)
		{
			throw new Exception(tl("noPendingRequest"));
		}

		if (!requester.getBase().isOnline())
		{
			throw new Exception(tl("noPendingRequest"));
		}

		if (user.isTpRequestHere() && ((!requester.isAuthorized("essentials.tpahere") && !requester.isAuthorized("essentials.tpaall"))
									   || (user.getWorld() != requester.getWorld() && ess.getSettings().isWorldTeleportPermissions()
										   && !user.isAuthorized("essentials.worlds." + user.getWorld().getName()))))
		{
			throw new Exception(tl("noPendingRequest"));
		}

		if (!user.isTpRequestHere() && (!requester.isAuthorized("essentials.tpa")
										|| (user.getWorld() != requester.getWorld() && ess.getSettings().isWorldTeleportPermissions()
											&& !user.isAuthorized("essentials.worlds." + requester.getWorld().getName()))))
		{
			throw new Exception(tl("noPendingRequest"));
		}

		if (args.length > 0 && !requester.getName().contains(args[0]))
		{
			throw new Exception(tl("noPendingRequest"));
		}

		long timeout = ess.getSettings().getTpaAcceptCancellation();
		if (timeout != 0 && (System.currentTimeMillis() - user.getTeleportRequestTime()) / 1000 > timeout)
		{
			user.requestTeleport(null, false);
			throw new Exception(tl("requestTimedOut"));
		}

		final Trade charge = new Trade(this.getName(), ess);
		user.sendMessage(tl("requestAccepted"));
		requester.sendMessage(tl("requestAcceptedFrom", user.getDisplayName()));

		try
		{
			if (user.isTpRequestHere())
			{
				final Location loc = user.getTpRequestLocation();
				requester.getTeleport().teleportPlayer(user, user.getTpRequestLocation(), charge, TeleportCause.COMMAND);
				requester.sendMessage(tl("teleporting", loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()));
			}
			else
			{
				requester.getTeleport().teleport(user.getBase(), charge, TeleportCause.COMMAND);
			}
		}
		catch (Exception ex)
		{
			user.sendMessage(tl("pendingTeleportCancelled"));
			ess.showError(requester.getSource(), ex, commandLabel);
		}
		user.requestTeleport(null, false);
		throw new NoChargeException();
	}

}