-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
CastCommand.java
171 lines (163 loc) · 8.19 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.util.List;
import java.util.function.Consumer;
public class CastCommand extends AbstractCommand {
public CastCommand() {
setName("cast");
setSyntax("cast [<effect>] (remove) (duration:<value>) (amplifier:<#>) (<entity>|...) (no_ambient) (hide_particles) (no_icon)");
setRequiredArguments(1, 8);
isProcedural = false;
}
// <--[command]
// @Name Cast
// @Syntax cast [<effect>] (remove) (duration:<value>) (amplifier:<#>) (<entity>|...) (no_ambient) (hide_particles) (no_icon)
// @Required 1
// @Maximum 8
// @Short Casts a potion effect to a list of entities.
// @Synonyms Potion,Magic
// @Group entity
//
// @Description
// Casts or removes a potion effect to or from a list of entities.
//
// The effect type must be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>.
//
// If you don't specify a duration, it defaults to 60 seconds.
// To cast an effect with a duration which displays as '**:**' or 'infinite' use a duration of '1639s' (1639 seconds) or greater.
// While it may display as infinite, it will still wear off.
//
// The amplifier is how many levels to *add* over the normal level 1.
// If you don't specify an amplifier level, it defaults to 1, meaning an effect of level 2 (this is for historical compatibility reasons).
// Specify "amplifier:0" to have no amplifier applied (ie effect level 1).
//
// If no player is specified, the command will target the player.
// If no player is present, the command will target the NPC. If an NPC is not present, there will be an error!
//
// Optionally, specify "no_ambient" to hide some translucent additional particles, while still rendering the main particles.
// "Ambient" effects in vanilla came from a beacon, while non-ambient came from a potion.
//
// Optionally, specify "hide_particles" to remove the particle effects entirely.
//
// Optionally, specify "no_icon" to hide the effect icon in the corner of your screen.
//
// @Tags
// <EntityTag.has_effect[<effect>]>
// <server.potion_effect_types>
// <EntityTag.list_effects>
//
// @Usage
// Use to cast a level 1 effect onto the player.
// - cast speed amplifier:0
//
// @Usage
// Use to cast an effect onto the player for 120 seconds with an amplifier of 3 (effect level 4).
// - cast jump d:120 amplifier:3
//
// @Usage
// Use to remove an effect from the player.
// - if <player.has_effect[jump]>:
// - cast jump remove <player>
// -->
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (PotionEffectType effect : PotionEffectType.values()) {
addOne.accept(effect.getName());
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("remove")
&& arg.matches("remove", "cancel")) {
scriptEntry.addObject("remove", new ElementTag(true));
}
else if (!scriptEntry.hasObject("ambient")
&& arg.matches("no_ambient")) {
scriptEntry.addObject("ambient", new ElementTag(false));
}
else if (!scriptEntry.hasObject("show_particles")
&& arg.matches("hide_particles")) {
scriptEntry.addObject("show_particles", new ElementTag(false));
}
else if (!scriptEntry.hasObject("show_icon")
&& arg.matches("no_icon")) {
scriptEntry.addObject("show_icon", new ElementTag(false));
}
else if (!scriptEntry.hasObject("duration")
&& arg.matchesPrefix("duration", "d")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
}
else if (!scriptEntry.hasObject("amplifier")
&& arg.matchesPrefix("power", "p", "amplifier", "a")
&& arg.matchesFloat()) {
scriptEntry.addObject("amplifier", arg.asElement());
}
else if (!scriptEntry.hasObject("effect")
&& PotionEffectType.getByName(arg.asElement().asString()) != null) {
scriptEntry.addObject("effect", PotionEffectType.getByName(arg.asElement().asString()));
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("entities")) {
scriptEntry.defaultObject("entities", Utilities.entryDefaultEntityList(scriptEntry, true));
}
if (!scriptEntry.hasObject("effect")) {
throw new InvalidArgumentsException("Must specify a valid PotionType!");
}
scriptEntry.defaultObject("duration", new DurationTag(60));
scriptEntry.defaultObject("amplifier", new ElementTag(1));
scriptEntry.defaultObject("remove", new ElementTag(false));
scriptEntry.defaultObject("show_particles", new ElementTag(true));
scriptEntry.defaultObject("ambient", new ElementTag(true));
scriptEntry.defaultObject("show_icon", new ElementTag(true));
}
@Override
public void execute(ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
PotionEffectType effect = (PotionEffectType) scriptEntry.getObject("effect");
int amplifier = scriptEntry.getElement("amplifier").asInt();
DurationTag duration = scriptEntry.getObjectTag("duration");
boolean remove = scriptEntry.getElement("remove").asBoolean();
ElementTag showParticles = scriptEntry.getElement("show_particles");
ElementTag ambient = scriptEntry.getElement("ambient");
ElementTag showIcon = scriptEntry.getElement("show_icon");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("Target(s)", entities.toString()), ArgumentHelper.debugObj("Effect", effect.getName()),
ArgumentHelper.debugObj("Amplifier", amplifier), duration, ambient, showParticles, showIcon);
}
boolean amb = ambient.asBoolean();
boolean showP = showParticles.asBoolean();
boolean icon = showIcon.asBoolean();
for (EntityTag entity : entities) {
if (entity.getLivingEntity().hasPotionEffect(effect)) {
entity.getLivingEntity().removePotionEffect(effect);
}
if (remove) {
continue;
}
PotionEffect potion = new PotionEffect(effect, duration.getTicksAsInt(), amplifier, amb, showP, icon);
if (!potion.apply(entity.getLivingEntity())) {
Debug.echoError(scriptEntry.getResidingQueue(), "Bukkit was unable to apply '" + potion.getType().getName() + "' to '" + entity.toString() + "'.");
}
}
}
}