/
CastCommand.java
157 lines (136 loc) · 6.59 KB
/
CastCommand.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.aufdemrand.denizen.scripts.commands.entity;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.Duration;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.ArrayList;
import java.util.List;
/**
* <p>'Casts' a Bukkit PotionEffectType on a LivingEntity target(s).</p>
*
* <br><b>dScript Usage:</b><br>
* <pre>CAST [PotionEffectType] (TARGET(S):NPC|{PLAYER}|LivingEntity) (DURATION:#) (POWER:#)</pre>
*
* <ol><tt>Arguments: [] - Required, () - Optional, {} - Default</ol></tt>
*
* <ol><tt>[PotionEffectType]</tt><br>
* Uses Bukkit's PotionEffectType for specifying the potion effect to use.
* See below for a list of valid PotionEffectTypes.</ol>
*
* <ol><tt>(TARGET(S):NPC|{PLAYER}|ENTITY.entity|NPC.npcid|PLAYER.player_name)</tt><br>
* Optional. Defaults to the attached Player. Can use a dScript list format
* to specify multiple targets. The recipient of the PotionEffectType. </ol>
*
* <ol><tt>(DURATION:#{60s})</tt><br>
* Optional. Number of seconds that the PotionEffectType lasts. If not specified,
* assumed 60 seconds.</ol>
*
* <ol><tt>(POWER:#{1})</tt><br>
* Optional. A higher amplifier means the potion effect happens more often over
* its duration and in some cases has more effect on its target. Usually effective
* between 1-3.</ol>
*
* <br><b>Example Usage:</b><br>
* <ol><tt>
* - CAST NIGHT_VISION DURATION:60 <br>
* - CAST WITHER TARGET:NPC NPCID:<FLAG.P:enemy_NPCID> CASTER:PLAYER <br>
* - CAST REGENERATION DURATION:10 POWER:3 <br>
* - CAST CONFUSION TARGET:NPC.25 DURATION:60
* </ol></tt>
*
* <br><b>Extended Usage:</b><br>
* <ol><tt>
* Script: <br>
* - ^ENGAGE NOW DURATION:10 <br>
* - ^LOOKCLOSE TOGGLE:TRUE DURATION:10 <br>
* - CHAT 'The night-time is blinding around here. Allow me to give you sight.' <br>
* - WAIT 2 <br>
* - ^ANIMATE ANIMATION:ARM_SWING <br>
* - CAST NIGHT_VISION DURATION:360 <br>
* - NARRATE 'You can see through the night!' <br>
* </ol></tt>
*
* @author aufdemrand, Jeebiss, Morphan1
*
*/
public class CastCommand extends AbstractCommand{
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Iterate through arguments
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("duration")
&& arg.matchesArgumentType(Duration.class)
&& arg.matchesPrefix("duration, d"))
scriptEntry.addObject("duration", arg.asType(Duration.class));
else if (!scriptEntry.hasObject("amplifier")
&& (arg.matchesPrimitive(aH.PrimitiveType.Integer)
|| arg.matchesPrimitive(aH.PrimitiveType.Double))
&& arg.matchesPrefix("power, p"))
scriptEntry.addObject("amplifier", arg.asElement());
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(dEntity.class)) {
scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}
// Try to match a PotionEffectType (this argument is prefixless, since it's required)
else if (!scriptEntry.hasObject("effect")
&& arg.matchesPrimitive(aH.PrimitiveType.String)) {
if (PotionEffectType.getByName(arg.asElement().asString()) == null)
dB.echoError("Invalid PotionEffectType!");
else
scriptEntry.addObject("effect", PotionEffectType.getByName(arg.asElement().asString()));
}
}
// Set default duration if not specified
if (!scriptEntry.hasObject("duration"))
scriptEntry.addObject("duration", new Duration(60));
// No targets specified, let's use defaults if available
if (!scriptEntry.hasObject("entities")) {
List<dEntity> entities = new ArrayList<dEntity>();
// Target Player by default
if (scriptEntry.getPlayer() != null)
entities.add(scriptEntry.getPlayer().getDenizenEntity());
// If no Player, target the NPC by default
else if (scriptEntry.getNPC() != null)
entities.add(scriptEntry.getNPC().getDenizenEntity());
// Still no targets? Problem!
else
throw new InvalidArgumentsException("No valid target entities found!");
scriptEntry.addObject("entities", entities);
}
// No potion specified? Problem!
if (!scriptEntry.hasObject("effect"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "PotionType");
if (!scriptEntry.hasObject("amplifier"))
scriptEntry.addObject("amplifier", new Element(1));
}
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Fetch objects
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
PotionEffectType effect = (PotionEffectType) scriptEntry.getObject("effect");
int amplifier = ((Element) scriptEntry.getObject("amplifier")).asInt();
Duration duration = (Duration) scriptEntry.getObject("duration");
// Report to dB
dB.report(getName(),
aH.debugObj("Target(s)", entities.toString())
+ aH.debugObj("Effect", effect.getName())
+ aH.debugObj("Amplifier", amplifier)
+ duration.debug());
// Apply the PotionEffect to the targets!
for (dEntity entity : entities) {
PotionEffect potion = new PotionEffect(effect, duration.getTicksAsInt(), amplifier);
if (!potion.apply(entity.getLivingEntity()))
dB.echoError("Bukkit was unable to apply '" + potion.getType().getName() + "' to '" + entity.toString() + "'.");
}
}
}