/
CommandTrait.java
157 lines (138 loc) · 4.96 KB
/
CommandTrait.java
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
package net.citizensnpcs.trait;
import java.util.List;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.DelegatePersistence;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.persistence.Persister;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.api.util.Placeholders;
import net.citizensnpcs.util.Messages;
@TraitName("commandtrait")
public class CommandTrait extends Trait {
@Persist
@DelegatePersistence(NPCCommandPersister.class)
private final Map<String, NPCCommand> commands = Maps.newHashMap();
public CommandTrait() {
super("commandtrait");
}
public int addCommand(String command, Hand hand, boolean player, boolean op) {
int id = getNewId();
commands.put(String.valueOf(id), new NPCCommand(String.valueOf(id), command, hand, player, op));
return id;
}
/**
* Send a brief description of the current state of the trait to the supplied {@link CommandSender}.
*/
public void describe(CommandSender sender) {
List<NPCCommand> left = Lists.newArrayList();
List<NPCCommand> right = Lists.newArrayList();
for (NPCCommand command : commands.values()) {
if (command.hand == Hand.LEFT || command.hand == Hand.BOTH) {
left.add(command);
}
if (command.hand == Hand.RIGHT || command.hand == Hand.BOTH) {
right.add(command);
}
}
String output = "";
if (left.size() > 0) {
output += Messaging.tr(Messages.COMMAND_LEFT_HAND_HEADER);
for (NPCCommand command : left) {
output += "<br> - [" + command.id + "]: " + command.command;
}
}
if (right.size() > 0) {
output += Messaging.tr(Messages.COMMAND_RIGHT_HAND_HEADER);
for (NPCCommand command : right) {
output += "<br> - [" + command.id + "]: " + command.command;
}
}
if (output.isEmpty()) {
output = Messaging.tr(Messages.COMMAND_NO_COMMANDS_ADDED);
}
Messaging.send(sender, output);
}
public void dispatch(Player player, Hand hand) {
for (NPCCommand command : commands.values()) {
if (command.hand != hand && command.hand != Hand.BOTH)
continue;
command.run(npc, player);
}
}
private int getNewId() {
int i = 0;
while (commands.containsKey(String.valueOf(i))) {
i++;
}
return i;
}
public boolean hasCommandId(int id) {
return commands.containsKey(String.valueOf(id));
}
public void removeCommandById(int id) {
commands.remove(String.valueOf(id));
}
public static enum Hand {
BOTH,
LEFT,
RIGHT;
}
private static class NPCCommand {
String command;
Hand hand;
String id;
boolean player;
boolean op;
public NPCCommand(String id, String command, Hand hand, boolean player, boolean op) {
this.id = id;
this.command = command;
this.hand = hand;
this.player = player;
this.op = op;
}
public void run(NPC npc, Player clicker) {
String interpolatedCommand = Placeholders.replace(command, clicker, npc);
if (player) {
boolean wasOp = clicker.isOp();
if (op) {
clicker.setOp(true);
}
try {
clicker.performCommand(interpolatedCommand);
} catch (Throwable t) {
t.printStackTrace();
}
if (op) {
clicker.setOp(wasOp);
}
} else {
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), interpolatedCommand);
}
}
}
private static class NPCCommandPersister implements Persister<NPCCommand> {
public NPCCommandPersister() {
}
@Override
public NPCCommand create(DataKey root) {
return new NPCCommand(root.name(), root.getString("command"), Hand.valueOf(root.getString("hand")),
Boolean.valueOf(root.getString("player")), Boolean.valueOf(root.getString("op")));
}
@Override
public void save(NPCCommand instance, DataKey root) {
root.setString("command", instance.command);
root.setString("hand", instance.hand.name());
root.setBoolean("player", instance.player);
root.setBoolean("op", instance.op);
}
}
}