-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
SentinelCommand.java
203 lines (187 loc) · 8.96 KB
/
SentinelCommand.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package org.mcmonkey.sentinel.commands;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.command.CommandManager;
import net.citizensnpcs.api.command.Injector;
import net.citizensnpcs.api.command.Requirements;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.trait.Owner;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.*;
import org.mcmonkey.sentinel.SentinelPlugin;
import org.mcmonkey.sentinel.SentinelTrait;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;
/**
* Helper class for the Sentinel command.
*/
public class SentinelCommand implements CommandExecutor, TabCompleter {
/**
* Output string representing a message color.
*/
public static final String colorBasic = ChatColor.YELLOW.toString(), colorBad = ChatColor.RED.toString(), colorEmphasis = ChatColor.AQUA.toString();
/**
* Output string representing a success prefix.
*/
public static final String prefixGood = ChatColor.DARK_GREEN + "[Sentinel] " + colorBasic;
/**
* Output string representing a failure prefix.
*/
public static final String prefixBad = ChatColor.DARK_GREEN + "[Sentinel] " + colorBad;
/**
* The "/sentinel" command manager.
*/
public CommandManager manager;
/**
* Prepares the command handling system.
*/
public void buildCommandHandler(PluginCommand command) {
manager = new CommandManager();
manager.setInjector(new Injector());
grabCommandMethodMap(manager);
manager.register(SentinelAttackCommands.class);
manager.register(SentinelChaseCommands.class);
manager.register(SentinelGreetingCommands.class);
manager.register(SentinelHealthCommands.class);
manager.register(SentinelInfoCommands.class);
manager.register(SentinelIntelligenceCommands.class);
manager.register(SentinelTargetCommands.class);
command.setExecutor(this);
command.setTabCompleter(this);
}
private static Map<String, Method> sentinelCommandMethodMap;
private static void grabCommandMethodMap(CommandManager instance) {
try {
Field field = CommandManager.class.getDeclaredField("commands");
field.setAccessible(true);
sentinelCommandMethodMap = (Map<String, Method>) field.get(instance);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private static boolean isSentinelRequired(String command, String modifier) {
Method method = sentinelCommandMethodMap.get((command + " " + modifier).toLowerCase());
if (method == null) {
return false;
}
Requirements[] req = method.getDeclaredAnnotationsByType(Requirements.class);
if (req == null || req.length == 0) {
return false;
}
for (Class<? extends Trait> traitClass : req[0].traits()) {
if (traitClass.equals(SentinelTrait.class)) {
return true;
}
}
return false;
}
/**
* Handles a player or server command.
*/
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
String modifier = args.length > 0 ? args[0] : "";
if (!manager.hasCommand(command, modifier) && !modifier.isEmpty()) {
String closest = manager.getClosestCommandModifier(command.getName(), modifier);
if (!closest.isEmpty()) {
sender.sendMessage(prefixBad + "Unknown command. Did you mean:");
sender.sendMessage(prefixGood + " /" + command.getName() + " " + closest);
}
else {
sender.sendMessage(prefixBad + "Unknown command.");
}
return true;
}
NPC selected = CitizensAPI.getDefaultNPCSelector().getSelected(sender);
SentinelTrait sentinel = null;
CommandContext context = new CommandContext(sender, args);
if (context.hasValueFlag("id") && sender.hasPermission("npc.select")) {
try {
selected = CitizensAPI.getNPCRegistry().getById(context.getFlagInteger("id"));
}
catch (NumberFormatException ex) {
sender.sendMessage(prefixBad + "ID input is invalid (not an integer)!");
return true;
}
}
if (selected != null) {
sentinel = selected.getTraitNullable(SentinelTrait.class);
}
Object[] methodArgs;
if (isSentinelRequired(command.getName(), modifier)) {
if (sentinel == null) {
if (selected == null) {
sender.sendMessage(prefixBad + "Must have a Sentinel NPC selected!");
}
else {
sender.sendMessage(prefixBad + "Selected NPC is not a Sentinel! Use /trait sentinel to ensure an NPC becomes a Sentinel.");
}
return true;
}
if (!selected.getOrAddTrait(Owner.class).isOwnedBy(sender) && !sender.hasPermission("citizens.admin")) {
sender.sendMessage(prefixBad + "You do not own this NPC (and you are not an admin).");
return true;
}
methodArgs = new Object[]{sender, sentinel};
}
else {
methodArgs = new Object[]{ sender };
}
return manager.executeSafe(command, args, sender, methodArgs);
}
private static List<String> filterForArg(Collection<String> output, String arg1) {
String low = arg1.toLowerCase();
return output.stream().filter(s -> s.startsWith(low)).collect(Collectors.toList());
}
public static HashSet<String> addTargetTabCompletions = new HashSet<>();
public static HashSet<String> itemPrefixes = new HashSet<>(Arrays.asList("helditem", "offhand", "equipped", "in_inventory"));
public static List<String> materialNames = Arrays.stream(Material.values()).map(m -> m.name().toLowerCase()).collect(Collectors.toList());
static {
addTargetTabCompletions.addAll(Arrays.asList("player:", "npc:", "entityname:", "group:"));
addTargetTabCompletions.addAll(Arrays.asList("helditem:", "offhand:", "equipped:", "in_inventory:"));
addTargetTabCompletions.addAll(Arrays.asList("status:angry", "status:passive"));
addTargetTabCompletions.addAll(Arrays.asList("event:", "event:pvp", "event:pve", "event:pvnpc", "event:pvsentinel", "event:eve", "event:pv:", "event:ev:", "event:guarded_fight", "event:message:"));
addTargetTabCompletions.addAll(SentinelPlugin.targetOptions.keySet().stream().map(String::toLowerCase).collect(Collectors.toList()));
}
/**
* Handles tab completion for a player or server command.
*/
@Override
public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) {
if (s.equals("sentinel") && strings.length == 2) {
NPC npc = CitizensAPI.getDefaultNPCSelector().getSelected(commandSender);
if (npc != null && npc.hasTrait(SentinelTrait.class)) {
SentinelTrait sentinel = npc.getOrAddTrait(SentinelTrait.class);
switch (strings[0].toLowerCase()) {
case "addtarget":
case "addignore":
case "addavoid":
String low = strings[1].toLowerCase();
int colon = low.indexOf(':');
if (colon != -1 && colon + 1 < low.length()) {
String prefix = low.substring(0, colon);
if (itemPrefixes.contains(prefix)) {
List<String> materials = filterForArg(materialNames, low.substring(colon + 1));
materials.add("lore:");
materials.add("name:");
return filterForArg(materials.stream().map(m -> prefix + ":" + m).collect(Collectors.toList()), low);
}
}
return filterForArg(addTargetTabCompletions, low);
case "removetarget":
return filterForArg(sentinel.allTargets.getTargetRemovableStrings(), strings[1]);
case "removeignore":
return filterForArg(sentinel.allIgnores.getTargetRemovableStrings(), strings[1]);
case "removeavoid":
return filterForArg(sentinel.allAvoids.getTargetRemovableStrings(), strings[1]);
}
}
}
return manager.onTabComplete(commandSender, command, s, strings);
}
}