/
CommandSmartEvent.java
225 lines (185 loc) · 8.39 KB
/
CommandSmartEvent.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package net.aufdemrand.denizen.events.core;
import net.aufdemrand.denizen.objects.dCuboid;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.dWorld;
import net.aufdemrand.denizen.scripts.containers.core.BukkitWorldScriptHelper;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.events.OldSmartEvent;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.command.BlockCommandSender;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.minecart.CommandMinecart;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.server.ServerCommandEvent;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CommandSmartEvent implements OldSmartEvent, Listener {
class CommandHandlerData {
public final String name;
public final String event;
public CommandHandlerData(String name, String event) {
this.name = name;
this.event = event;
}
}
List<CommandHandlerData> cmds = new ArrayList<>();
///////////////////
// SMARTEVENT METHODS
///////////////
@Override
public boolean shouldInitialize(Set<String> events) {
cmds = new ArrayList<>();
// Loop through event names from loaded world script events
boolean pass = false;
for (String event : events) {
// Use a regex pattern to narrow down matches
Matcher m = Pattern.compile("on( ([^\\s]+))? command(in \\w+)?", Pattern.CASE_INSENSITIVE)
.matcher(event);
if (m.matches()) {
String cmd = m.group(2);
if (cmd != null) {
dList split = dList.valueOf(cmd);
for (String str : split) {
cmds.add(new CommandHandlerData(CoreUtilities.toLowerCase(str), cmd));
}
}
pass = true;
}
}
// No matches at all, so return false.
return pass;
}
@Override
public void _initialize() {
// Yay! Your event is in use! Register it here.
DenizenAPI.getCurrentInstance().getServer().getPluginManager()
.registerEvents(this, DenizenAPI.getCurrentInstance());
// Record that you loaded in the debug.
dB.log("Loaded Command SmartEvent.");
}
@Override
public void breakDown() {
// Unregister events or any other temporary links your event created in _intialize()
PlayerCommandPreprocessEvent.getHandlerList().unregister(this);
ServerCommandEvent.getHandlerList().unregister(this);
}
//////////////
// MECHANICS
///////////
private List<String> getAll(String cmd) {
List<String> newEvents = new ArrayList<>();
cmd = CoreUtilities.toLowerCase(cmd);
for (CommandHandlerData chd : cmds) {
if (chd.name.equalsIgnoreCase(cmd)) {
newEvents.add(chd.event + " command");
}
}
return newEvents;
}
// <--[event]
// @Events
// command
// <command_name>|... command
//
// @Regex ^on( [^\s]+)? command$
// @Switch in <area>
//
// @Triggers when a player, console, or command block/minecart runs a Bukkit command. This happens before
// any code of established commands allowing scripters to 'override' existing commands.
// @Context
// <context.command> returns the command name as an Element.
// <context.raw_args> returns any args used as an Element.
// <context.args> returns a dList of the arguments.
// <context.server> returns true if the command was run from the console.
// <context.command_block_location> returns the command block's location (if the command was run from one).
// <context.command_minecart> returns the dEntity of the command minecart (if the command was run from one).
//
// @Determine
// "FULFILLED" to tell Bukkit the command was handled.
//
// -->
@EventHandler
public void playerCommandPreprocess(PlayerCommandPreprocessEvent event) {
Map<String, dObject> context = new HashMap<>();
String message = event.getMessage();
String command = message.split(" ")[0].replace("/", "").toUpperCase();
List<String> events = new ArrayList<>();
events.add("command");
events.add(command + " command");
events.addAll(getAll(command));
// Look for cuboids that contain the block's location
List<dCuboid> cuboids = dCuboid.getNotableCuboidsContaining(event.getPlayer().getLocation());
dList cuboid_context = new dList();
List<String> cuboidEvents = new ArrayList<>();
for (dCuboid cuboid : cuboids) {
for (String str : events) {
cuboidEvents.add(str + " in " + cuboid.identifySimple());
}
cuboid_context.add(cuboid.identifySimple());
}
for (String str : events) {
cuboidEvents.add(str + " in " + new dWorld(event.getPlayer().getLocation().getWorld()).identifySimple());
}
events.addAll(cuboidEvents);
// Add in cuboids context, with either the cuboids or an empty list
context.put("cuboids", cuboid_context);
List<String> args = Arrays.asList(aH.buildArgs(message.split(" ").length > 1 ? message.split(" ", 2)[1] : ""));
// Fill context
context.put("args", new dList(args));
context.put("parsed_args", new dList(args));
context.put("command", new Element(command));
context.put("raw_args", new Element((message.split(" ").length > 1
? message.split(" ", 2)[1] : "")));
context.put("server", new Element(false));
String determination;
// Run any event scripts and get the determination.
determination = BukkitWorldScriptHelper.doEvents(events,
null, dEntity.getPlayerFrom(event.getPlayer()), context, true).toUpperCase();
// If a script has determined fulfilled, cancel this event so the player doesn't
// receive the default 'Invalid command' gibberish from bukkit.
if (determination.equals("FULFILLED") || determination.equals("CANCELLED")) {
event.setCancelled(true);
}
}
@EventHandler
public void serverCommand(ServerCommandEvent event) {
if (event.getCommand().trim().length() == 0) {
return;
}
Map<String, dObject> context = new HashMap<>();
String message = event.getCommand();
String command = event.getCommand().split(" ")[0].replace("/", "").toUpperCase();
List<String> events = new ArrayList<>();
events.add("command");
events.add(command + " command");
events.addAll(getAll(command));
List<String> args = Arrays.asList(aH.buildArgs(message.split(" ").length > 1 ? message.split(" ", 2)[1] : ""));
List<String> parsed_args = Arrays.asList(aH.buildArgs(event.getCommand().split(" ").length > 1 ? event.getCommand().split(" ", 2)[1] : ""));
// Fill context
context.put("args", new dList(args));
context.put("parsed_args", new dList(parsed_args));
context.put("command", new Element(command));
context.put("raw_args", new Element((message.split(" ").length > 1 ? event.getCommand().split(" ", 2)[1] : "")));
context.put("server", new Element(true));
CommandSender sender = event.getSender();
if (sender instanceof BlockCommandSender) {
context.put("command_block_location", new dLocation(((BlockCommandSender) sender).getBlock().getLocation()));
}
else if (sender instanceof CommandMinecart) {
context.put("command_minecart", new dEntity((CommandMinecart) sender));
}
String determination = BukkitWorldScriptHelper.doEvents(events, null, null, context);
if (determination.equalsIgnoreCase("FULFILLED") || determination.equalsIgnoreCase("CANCELLED")) {
event.setCommand("denizen do_nothing");
}
}
}