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

import com.earth2me.essentials.User;
import com.earth2me.essentials.Util;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.inventory.ItemStack;


public class Commandpowertool extends EssentialsCommand
{
	public Commandpowertool()
	{
		super("powertool");
	}

	@Override
	protected void run(Server server, User user, String commandLabel, String[] args) throws Exception
	{
		ItemStack is = user.getItemInHand();
		if (is == null || is.getType() == Material.AIR)
		{
			user.sendMessage(Util.i18n("powerToolAir"));
			return;
		}

		String itemName = is.getType().toString().toLowerCase().replaceAll("_", " ");
		String command = getFinalArg(args, 0);
		if (command != null && !command.isEmpty())
		{
			if (command.equalsIgnoreCase("list"))
			{
				String powertools = user.getPowertool(is);
				if (powertools == null)
				{
					user.sendMessage(Util.format("powerToolListEmpty", itemName));
				}
				else
				{
					user.sendMessage(Util.format("powerToolList", powertools.replace("|", ", "), itemName));
				}
				return;
			}
			if (command.startsWith("r:"))
			{
				try
				{
					String removedCommand = command.substring(2);
					command = removePowerTool(user, removedCommand, is, itemName);
					user.sendMessage(Util.format("powerToolRemove", removedCommand, itemName));
				}
				catch (Exception e)
				{
					user.sendMessage(e.getMessage());
					return;
				}
			}
			else
			{
				if (command.startsWith("a:"))
				{
					try
					{
						command = appendPowerTool(user, command, is, itemName);
					}
					catch (Exception e)
					{
						user.sendMessage(e.getMessage());
						return;
					}
				}

				user.sendMessage(Util.format("powerToolAttach", command.replace("|", ", "), itemName));
			}
		}
		else
		{
			user.sendMessage(Util.format("powerToolRemoveAll", itemName));
		}

		charge(user);
		user.setPowertool(is, command);
	}

	private String appendPowerTool(User user, String command, ItemStack is, String itemName) throws Exception
	{
		command = command.substring(2); // Ignore the first 2 chars
		String powertools = user.getPowertool(is);
		if (powertools != null)
		{
			if (powertools.contains(command))
			{
				throw new Exception((Util.format("powerToolAlreadySet", command, itemName)));
			}

			StringBuilder newCommand = new StringBuilder();
			command = newCommand.append(powertools).append("|").append(command).toString();
		}

		return command;
	}

	private String removePowerTool(User user, String command, ItemStack is, String itemName) throws Exception
	{
		String powertools = user.getPowertool(is);
		if (!powertools.contains(command))
		{
			throw new Exception((Util.format("powerToolNoSuchCommandAssigned", command, itemName)));
		}

		command = powertools.replace(command, "").replace("||", "|");

		// Trim off any leading/trailing '|' chars
		if (command.startsWith("|"))
		{
			command = command.substring(1);
		}
		if (command.endsWith("|"))
		{
			command = command.substring(0, command.length() - 1);
		}

		return command;
	}
}