summaryrefslogtreecommitdiffstats
path: root/Essentials/src/net/ess3/commands/Commandwarp.java
blob: 1d9f64f86c7353470348f5b3c71ef835ad93d5ea (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
package net.ess3.commands;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import static net.ess3.I18n._;
import net.ess3.api.IUser;
import net.ess3.api.IWarps;
import net.ess3.economy.Trade;
import net.ess3.permissions.Permissions;
import net.ess3.utils.Util;
import org.bukkit.command.CommandSender;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;


public class Commandwarp extends EssentialsCommand
{
	private final static int WARPS_PER_PAGE = 20;

	@Override
	public void run(final IUser user, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length == 0 || args[0].matches("[0-9]+"))
		{
			if (!Permissions.WARP_LIST.isAuthorized(user))
			{
				throw new Exception(_("You do not have Permission to list warps."));
			}
			warpList(user, args);
			throw new NoChargeException();
		}
		if (args.length > 0)
		{
			IUser otherUser = null;
			if (args.length == 2 && Permissions.WARP_OTHERS.isAuthorized(user))
			{
				if (Permissions.WARP_HIDDEN.isAuthorized(user))
				{
					otherUser = ess.getUserMap().matchUser(args[1], false);
				}
				else
				{
					otherUser = ess.getUserMap().matchUserExcludingHidden(args[1], user.getPlayer());
				}
				warpUser(otherUser, args[0]);
				throw new NoChargeException();
			}
			warpUser(user, args[0]);
			throw new NoChargeException();
		}
	}

	@Override
	protected void run(final CommandSender sender, final String commandLabel, final String[] args) throws Exception
	{
		if (args.length < 2 || args[0].matches("[0-9]+"))
		{
			warpList(sender, args);
			throw new NoChargeException();
		}
		IUser otherUser = ess.getUserMap().matchUser(args[1], false);
		if (otherUser == null)
		{
			throw new Exception(_("Player not found."));
		}
		otherUser.getTeleport().warp(args[0], null, TeleportCause.COMMAND);
		throw new NoChargeException();

	}

	//TODO: Use one of the new text classes, like /help ?
	private void warpList(final CommandSender sender, final String[] args) throws Exception
	{
		final IWarps warps = ess.getWarps();
		if (warps.isEmpty())
		{
			throw new Exception(_("No warps defined."));
		}
		final List<String> warpNameList = new ArrayList<String>(warps.getList());

		if (sender instanceof IUser)
		{
			final Iterator<String> iterator = warpNameList.iterator();
			while (iterator.hasNext())
			{
				final String warpName = iterator.next();
				if (!Permissions.WARPS.isAuthorized(sender, warpName))
				{
					iterator.remove();
				}
			}
		}
		int page = 1;
		if (args.length > 0)
		{
			page = Integer.parseInt(args[0]);
		}

		final int warpPage = (page - 1) * WARPS_PER_PAGE;
		final String warpList = Util.joinList(warpNameList.subList(warpPage, warpPage + Math.min(warpNameList.size() - warpPage, WARPS_PER_PAGE)));

		if (warpNameList.size() > WARPS_PER_PAGE)
		{
			sender.sendMessage(_("There are {0} warps. Showing page {1} of {2}.", warpNameList.size(), page, (int)Math.ceil(warpNameList.size() / (double)WARPS_PER_PAGE)));
			sender.sendMessage(warpList);
		}
		else
		{
			sender.sendMessage(_("Warps: {0}", warpList));
		}
	}

	private void warpUser(final IUser user, final String name) throws Exception
	{
		final Trade chargeWarp = new Trade("warp-" + name.toLowerCase(Locale.ENGLISH).replace('_', '-'), ess);
		final Trade chargeCmd = new Trade(this.commandName, ess);
		final double fullCharge = chargeWarp.getCommandCost(user) + chargeCmd.getCommandCost(user);
		final Trade charge = new Trade(fullCharge, ess);
		charge.isAffordableFor(user);
		if (Permissions.WARPS.isAuthorized(user, name))
		{
			user.getTeleport().warp(name, charge, TeleportCause.COMMAND);
			return;
		}
		throw new Exception(_("You do not have Permission to use that warp."));
	}
}