/
PlayEffectCommand.java
151 lines (122 loc) · 6.33 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
package net.aufdemrand.denizen.scripts.commands.world;
import org.bukkit.Effect;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.ParticleEffect;
/**
* Lets you play a Bukkit effect or a ParticleEffect from the
* ParticleEffect Library at a certain location.
*
* Arguments: [] - Required, () - Optional
* [location:<x,y,z,world>] specifies location of the effect
* [effect:<name>] sets the name of effect to be played
* (data:<#>) sets the special data value of the effect
* (visibility:<#>) adjusts the radius within which players can observe the effect
* (qty:<#>) sets the number of times the effect will be played
* (offset:<#>) sets the offset of ParticleEffects.
*
* Example Usage:
* playeffect location:123,65,765,world effect:record_play data:2259 radius:7
* playeffect location:<npc.location> e:smoke r:3
* playeffect location:<npc.location> effect:heart radius:7 qty:1000 offset:20
*
* @author David Cernat
*/
public class PlayEffectCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Iterate through arguments
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(dLocation.class)) {
scriptEntry.addObject("location", arg.asType(dLocation.class));
}
else if (!scriptEntry.hasObject("effect") &&
!scriptEntry.hasObject("particleeffect")) {
if (arg.matchesEnum(Effect.values())) {
scriptEntry.addObject("effect", Effect.valueOf(arg.getValue().toUpperCase()));
}
else if (arg.matchesEnum(ParticleEffect.values())) {
scriptEntry.addObject("particleeffect",
ParticleEffect.valueOf(arg.getValue().toUpperCase()));
}
}
else if (!scriptEntry.hasObject("visibility")
&& arg.matchesPrimitive(aH.PrimitiveType.Double)
&& arg.matchesPrefix("visibility, v, radius, r")) {
scriptEntry.addObject("visibility", arg.asElement());
}
else if (!scriptEntry.hasObject("data")
&& arg.matchesPrimitive(aH.PrimitiveType.Double)
&& arg.matchesPrefix("data, d")) {
scriptEntry.addObject("data", arg.asElement());
}
else if (!scriptEntry.hasObject("qty")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)
&& arg.matchesPrefix("qty, q")) {
scriptEntry.addObject("qty", arg.asElement());
}
else if (!scriptEntry.hasObject("offset")
&& arg.matchesPrimitive(aH.PrimitiveType.Double)
&& arg.matchesPrefix("offset, o")) {
scriptEntry.addObject("offset", arg.asElement());
}
else
arg.reportUnhandled();
}
// Use default values if necessary
scriptEntry.defaultObject("location",
scriptEntry.hasNPC() ? scriptEntry.getNPC().getLocation() : null,
scriptEntry.hasPlayer() ? scriptEntry.getPlayer().getLocation() : null);
scriptEntry.defaultObject("data", new Element(0));
scriptEntry.defaultObject("visibility", new Element(5));
scriptEntry.defaultObject("qty", new Element(1));
scriptEntry.defaultObject("offset", new Element(0.5));
// Check to make sure required arguments have been filled
if (!scriptEntry.hasObject("effect") &&
!scriptEntry.hasObject("particleeffect"))
throw new InvalidArgumentsException("Missing effect argument!");
if (!scriptEntry.hasObject("location"))
throw new InvalidArgumentsException("Missing location argument!");
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Extract objects from ScriptEntry
dLocation location = (dLocation) scriptEntry.getObject("location");
Effect effect = (Effect) scriptEntry.getObject("effect");
ParticleEffect particleEffect = (ParticleEffect) scriptEntry.getObject("particleeffect");
Element visibility = (Element) scriptEntry.getObject("visibility");
Element data = (Element) scriptEntry.getObject("data");
Element qty = (Element) scriptEntry.getObject("qty");
Element offset = (Element) scriptEntry.getObject("offset");
// Slightly increase the location's Y so effects don't seem
// to come out of the ground
location.add(0, 1, 0);
// Report to dB
dB.report(scriptEntry, getName(), (effect != null ? aH.debugObj("effect", effect.name()) :
aH.debugObj("special effect", particleEffect.name())) +
aH.debugObj("location", location.toString()) +
aH.debugObj("radius", visibility) +
aH.debugObj("data", data) +
aH.debugObj("qty", qty) +
(effect != null ? "" : aH.debugObj("offset", offset)));
// Play the Bukkit effect the number of times specified
if (effect != null) {
for (int n = 0; n < qty.asInt(); n++) {
location.getWorld().playEffect(location, effect, data.asInt(), visibility.asInt());
}
}
// Play a ParticleEffect
else {
ParticleEffect.valueOf(particleEffect.name())
.play(location, visibility.asDouble(),
offset.asFloat(), offset.asFloat(), offset.asFloat(), data.asFloat(), qty.asInt());
}
}
}