/
PlayEffectCommand.java
377 lines (348 loc) · 19.8 KB
/
PlayEffectCommand.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.abstracts.ParticleHelper;
import com.denizenscript.denizen.nms.interfaces.Particle;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Effect;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;
import java.util.*;
public class PlayEffectCommand extends AbstractCommand {
// <--[language]
// @name Particle Effects
// @group Useful Lists
// @description
// All of the effects listed here can be used by <@link command PlayEffect> to display visual effects or play sounds
//
// Effects:
// - iconcrack_[item] (item break effect - examples: iconcrack_stone, iconcrack_grass)
// - blockcrack_[material] (block break effect - examples: blockcrack_stone, blockcrack_grass)
// - blockdust_[material] (block break effect - examples: blockdust_stone, blockdust_grass)
// - Everything on <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Particle.html>
// - Everything on <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Effect.html>
// - RANDOM (chooses a random visual effect from the list starting with 'huge_explosion')
// -->
// <--[command]
// @Name PlayEffect
// @Syntax playeffect [effect:<name>] [at:<location>|...] (data:<#.#>) (special_data:<data>) (visibility:<#.#>) (quantity:<#>) (offset:<#.#>,<#.#>,<#.#>) (targets:<player>|...)
// @Required 2
// @Short Plays a visible or audible effect at the location.
// @Group world
//
// @Description
// Allows the playing of particle effects anywhere without the need of the source it comes from originally.
// The particles you may use, can come from sources such as a potion effect or a portal/Enderman with their particles respectively.
// Some particles have different data which may include different behavior depending on the data. Default data is 0
// Specifying a visibility value changes the sight radius of the effect. For example if visibility is 15; Targeted players won't see it unless they are 15 blocks or closer.
// You can add a quantity value that allow multiple of the same effect played at the same time. If an offset is set, each particle will be played at a different location in the offset area.
// Everyone will see the particle effects unless a target has been specified.
// See <@link language Particle Effects> for a list of valid effect names.
//
// Version change note: The original PlayEffect command raised all location inputs 1 block-height upward to avoid effects playing underground when played at eg a player's location.
// This was found to cause too much confusion, so it is no longer on by default. However, it will still happen for older commands.
// The distinction is in whether you include the (now expected to use) "at:" prefix on your location argument.
// If you do not have this prefix, the system will assume your command is older, and will apply the 1-block height offset.
//
// Some particles will require input to the "special_data" argument. The data input is unique per particle.
// - For REDSTONE particles, the input is of format: <size>|<color>, for example: "1.2|red". Color input is any valid ColorTag object.
//
// @Tags
// None
//
// @Usage
// Use to create a fake explosion.
// - playeffect effect:EXPLOSION_HUGE at:<player.location> visibility:500 quantity:10 offset:2.0
//
// @Usage
// Use to play a cloud effect.
// - playeffect effect:CLOUD at:<player.location.add[0,5,0]> quantity:20 data:1 offset:0.0
//
// @Usage
// Use to play some effects at spawn.
// - playeffect effect:FIREWORKS_SPARK at:<WorldTag.spawn_location> visibility:100 quantity:375 data:0 offset:50.0
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
ParticleHelper particleHelper = NMSHandler.getInstance().getParticleHelper();
// Iterate through arguments
for (Argument arg : ArgumentHelper.interpretArguments(scriptEntry.aHArgs)) {
if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentList(LocationTag.class)) {
if (arg.matchesOnePrefix("at")) {
scriptEntry.addObject("no_offset", new ElementTag(true));
}
scriptEntry.addObject("location", arg.asType(ListTag.class).filter(LocationTag.class, scriptEntry));
continue;
}
else if (!scriptEntry.hasObject("effect") &&
!scriptEntry.hasObject("particleeffect") &&
!scriptEntry.hasObject("iconcrack") &&
!scriptEntry.hasObject("blockcrack") &&
!scriptEntry.hasObject("blockdust")) {
if (particleHelper.hasParticle(arg.getValue())) {
scriptEntry.addObject("particleeffect", particleHelper.getParticle(arg.getValue()));
continue;
}
else if (arg.matches("random")) {
// Get another effect if "RANDOM" is used
List<Particle> visible = particleHelper.getVisibleParticles();
scriptEntry.addObject("particleeffect", visible.get(CoreUtilities.getRandom().nextInt(visible.size())));
continue;
}
else if (arg.startsWith("iconcrack_")) {
// Allow iconcrack_[item] for item break effects (ex: iconcrack_stone)
String shrunk = arg.getValue().substring("iconcrack_".length());
ItemTag item = ItemTag.valueOf(shrunk, scriptEntry.entryData.getTagContext());
if (item != null) {
scriptEntry.addObject("iconcrack", item);
}
else {
Debug.echoError("Invalid iconcrack_[item]. Must be a valid ItemTag!");
}
continue;
}
else if (arg.startsWith("blockcrack_")) {
String shrunk = arg.getValue().substring("blockcrack_".length());
MaterialTag material = MaterialTag.valueOf(shrunk);
if (material != null) {
scriptEntry.addObject("blockcrack", material);
}
else {
Debug.echoError("Invalid blockcrack_[item]. Must be a valid MaterialTag!");
}
continue;
}
else if (arg.startsWith("blockdust_")) {
String shrunk = arg.getValue().substring("blockdust_".length());
MaterialTag material = MaterialTag.valueOf(shrunk);
if (material != null) {
scriptEntry.addObject("blockdust", material);
}
else {
Debug.echoError("Invalid blockdust_[item]. Must be a valid MaterialTag!");
}
continue;
}
else if (arg.matchesEnum(Effect.values())) {
scriptEntry.addObject("effect", Effect.valueOf(arg.getValue().toUpperCase()));
continue;
}
else if (NMSHandler.getInstance().getParticleHelper().effectRemap.containsKey(arg.getValue().toUpperCase())) {
scriptEntry.addObject("effect", NMSHandler.getInstance().getParticleHelper().effectRemap.get(arg.getValue().toUpperCase()));
}
}
if (!scriptEntry.hasObject("radius")
&& arg.matchesPrimitive(ArgumentHelper.PrimitiveType.Double)
&& arg.matchesPrefix("visibility", "v", "radius", "r")) {
scriptEntry.addObject("radius", arg.asElement());
}
else if (!scriptEntry.hasObject("data")
&& arg.matchesPrimitive(ArgumentHelper.PrimitiveType.Double)
&& arg.matchesPrefix("data", "d")) {
scriptEntry.addObject("data", arg.asElement());
}
else if (!scriptEntry.hasObject("special_data")
&& arg.matchesOnePrefix("special_data")) {
scriptEntry.addObject("special_data", arg.asElement());
}
else if (!scriptEntry.hasObject("qty")
&& arg.matchesPrimitive(ArgumentHelper.PrimitiveType.Integer)
&& arg.matchesPrefix("qty", "q", "quantity")) {
scriptEntry.addObject("qty", arg.asElement());
}
else if (!scriptEntry.hasObject("offset")
&& arg.matchesPrimitive(ArgumentHelper.PrimitiveType.Double)
&& arg.matchesPrefix("offset", "o")) {
double offset = arg.asElement().asDouble();
scriptEntry.addObject("offset", new LocationTag(null, offset, offset, offset));
}
else if (!scriptEntry.hasObject("offset")
&& arg.matchesArgumentType(LocationTag.class)
&& arg.matchesPrefix("offset", "o")) {
scriptEntry.addObject("offset", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("targets")
&& arg.matchesArgumentList(PlayerTag.class)
&& arg.matchesPrefix("targets", "target", "t")) {
scriptEntry.addObject("targets", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
// Use default values if necessary
scriptEntry.defaultObject("location",
Utilities.entryHasNPC(scriptEntry) && Utilities.getEntryNPC(scriptEntry).isSpawned() ? Arrays.asList(Utilities.getEntryNPC(scriptEntry).getLocation()) : null,
Utilities.entryHasPlayer(scriptEntry) && Utilities.getEntryPlayer(scriptEntry).isOnline() ? Arrays.asList(Utilities.getEntryPlayer(scriptEntry).getLocation()) : null);
scriptEntry.defaultObject("data", new ElementTag(0));
scriptEntry.defaultObject("radius", new ElementTag(15));
scriptEntry.defaultObject("qty", new ElementTag(1));
scriptEntry.defaultObject("offset", new LocationTag(null, 0.5, 0.5, 0.5));
// Check to make sure required arguments have been filled
if (!scriptEntry.hasObject("effect") &&
!scriptEntry.hasObject("particleeffect") &&
!scriptEntry.hasObject("iconcrack") &&
!scriptEntry.hasObject("blockcrack") &&
!scriptEntry.hasObject("blockdust")) {
throw new InvalidArgumentsException("Missing effect argument!");
}
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Missing location argument!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
// Extract objects from ScriptEntry
List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("location");
List<PlayerTag> targets = (List<PlayerTag>) scriptEntry.getObject("targets");
Effect effect = (Effect) scriptEntry.getObject("effect");
Particle particleEffect = (Particle) scriptEntry.getObject("particleeffect");
ItemTag iconcrack = scriptEntry.getObjectTag("iconcrack");
MaterialTag blockcrack = scriptEntry.getObjectTag("blockcrack");
MaterialTag blockdust = scriptEntry.getObjectTag("blockdust");
ElementTag radius = scriptEntry.getElement("radius");
ElementTag data = scriptEntry.getElement("data");
ElementTag qty = scriptEntry.getElement("qty");
ElementTag no_offset = scriptEntry.getElement("no_offset");
boolean should_offset = no_offset == null || !no_offset.asBoolean();
LocationTag offset = scriptEntry.getObjectTag("offset");
ElementTag special_data = scriptEntry.getElement("special_data");
// Report to dB
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (effect != null ? ArgumentHelper.debugObj("effect", effect.name()) :
particleEffect != null ? ArgumentHelper.debugObj("special effect", particleEffect.getName()) :
(iconcrack != null ? iconcrack.debug()
: blockcrack != null ? blockcrack.debug()
: blockdust.debug())) +
ArgumentHelper.debugObj("locations", locations.toString()) +
(targets != null ? ArgumentHelper.debugObj("targets", targets.toString()) : "") +
radius.debug() +
data.debug() +
qty.debug() +
offset.debug() +
(special_data != null ? special_data.debug() : "") +
(should_offset ? ArgumentHelper.debugObj("note", "Location will be offset 1 block-height upward (see documentation)") : ""));
}
for (LocationTag location : locations) {
if (should_offset) {
// Slightly increase the location's Y so effects don't seem to come out of the ground
location = new LocationTag(location.clone().add(0, 1, 0));
}
// Play the Bukkit effect the number of times specified
if (effect != null) {
for (int n = 0; n < qty.asInt(); n++) {
if (targets != null) {
for (PlayerTag player : targets) {
if (player.isValid() && player.isOnline()) {
player.getPlayerEntity().playEffect(location, effect, data.asInt()); // TODO: 1.13
}
}
}
else {
location.getWorld().playEffect(location, effect, data.asInt(), radius.asInt());
}
}
}
// Play a ParticleEffect
else if (particleEffect != null) {
float osX = (float) offset.getX();
float osY = (float) offset.getY();
float osZ = (float) offset.getZ();
List<Player> players = new ArrayList<>();
if (targets == null) {
float rad = radius.asFloat();
for (Player player : location.getWorld().getPlayers()) {
if (player.getLocation().distanceSquared(location) < rad * rad) {
players.add(player);
}
}
}
else {
for (PlayerTag player : targets) {
if (player.isValid() && player.isOnline()) {
players.add(player.getPlayerEntity());
}
}
}
for (Player player : players) {
Class clazz = particleEffect.neededData();
if (clazz == null) {
particleEffect.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat());
}
else {
Object dataObject = null;
if (special_data == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Missing required special data for particle: " + particleEffect.getName());
}
else if (clazz == org.bukkit.Particle.DustOptions.class) {
ListTag dataList = ListTag.valueOf(special_data.asString());
if (dataList.size() != 2) {
Debug.echoError(scriptEntry.getResidingQueue(), "DustOptions special_data must have 2 list entries for particle: " + particleEffect.getName());
}
else {
float size = ArgumentHelper.getFloatFrom(dataList.get(0));
ColorTag color = ColorTag.valueOf(dataList.get(1));
dataObject = new org.bukkit.Particle.DustOptions(color.getColor(), size);
}
}
else {
Debug.echoError(scriptEntry.getResidingQueue(), "Unknown particle data type: " + clazz.getCanonicalName() + " for particle: " + particleEffect.getName());
}
particleEffect.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), dataObject);
}
}
}
// Play an iconcrack (item break) effect
else {
List<Player> players = new ArrayList<>();
if (targets == null) {
float rad = radius.asFloat();
for (Player player : location.getWorld().getPlayers()) {
if (player.getLocation().distanceSquared(location) < rad * rad) {
players.add(player);
}
}
}
else {
for (PlayerTag player : targets) {
if (player.isValid() && player.isOnline()) {
players.add(player.getPlayerEntity());
}
}
}
if (iconcrack != null) {
ItemStack itemStack = iconcrack.getItemStack();
Particle particle = NMSHandler.getInstance().getParticleHelper().getParticle("ITEM_CRACK");
for (Player player : players) {
particle.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), itemStack);
}
}
else if (blockcrack != null) {
MaterialData materialData = blockcrack.getMaterialData();
Particle particle = NMSHandler.getInstance().getParticleHelper().getParticle("BLOCK_CRACK");
for (Player player : players) {
particle.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), materialData);
}
}
else { // blockdust
MaterialData materialData = blockdust.getMaterialData();
Particle particle = NMSHandler.getInstance().getParticleHelper().getParticle("BLOCK_DUST");
for (Player player : players) {
particle.playFor(player, location, qty.asInt(), offset.toVector(), data.asFloat(), materialData);
}
}
}
}
}
}