-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
FireworkCommand.java
168 lines (160 loc) · 8.02 KB
/
FireworkCommand.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
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.utilities.Conversion;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.LocationTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.objects.notable.NoteManager;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Color;
import org.bukkit.FireworkEffect;
import org.bukkit.FireworkEffect.Builder;
import org.bukkit.entity.Firework;
import org.bukkit.inventory.meta.FireworkMeta;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
public class FireworkCommand extends AbstractCommand {
public FireworkCommand() {
setName("firework");
setSyntax("firework (<location>) (power:<#>) (<type>/random) (primary:<color>|...) (fade:<color>|...) (flicker) (trail)");
setRequiredArguments(0, 7);
isProcedural = false;
}
// <--[command]
// @Name Firework
// @Syntax firework (<location>) (power:<#>) (<type>/random) (primary:<color>|...) (fade:<color>|...) (flicker) (trail)
// @Required 0
// @Maximum 7
// @Short Launches a firework with specific coloring
// @Group world
//
// @Description
// This command launches a firework from the specified location.
// If no location is given, the linked NPC or player's location will be used by default.
// The power option, which defaults to 1 if left empty, specifies how high the firework will go before exploding.
// The type option which specifies the shape the firework will explode with. If unspecified, 'ball' will be used.
// The primary option specifies what color the firework explosion will start with, as a ColorTag. If unspecified, 'yellow' will be used.
// The fade option specifies what color the firework explosion will fade into, as a ColorTag.
// The trail option means the firework will leave a trail behind it.
// The flicker option means the firework will explode with a flicker effect.
//
// @Tags
// <EntityTag.firework_item>
// <ItemTag.is_firework>
// <ItemTag.firework>
// <entry[saveName].launched_firework> returns a EntityTag of the firework that was launched.
//
// @Usage
// Use to launch a star firework which explodes yellow and fades to white afterwards at the player's location.
// - firework <player.location> star primary:yellow fade:white
//
// @Usage
// Use to make the firework launch double the height before exploding.
// - firework <player.location> power:2 star primary:yellow fade:white
//
// @Usage
// Use to launch a firework which leaves a trail.
// - firework <player.location> random trail
//
// @Usage
// Use to launch a firework which leaves a trail and explodes with a flicker effect at related location.
// - firework <context.location> random trail flicker
// -->
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (Notable note : NoteManager.notesByType.get(LocationTag.class)) {
addOne.accept(NoteManager.getSavedId(note));
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("type")
&& arg.matches("random")) {
scriptEntry.addObject("type", new ElementTag(FireworkEffect.Type.values()[CoreUtilities.getRandom().nextInt(FireworkEffect.Type.values().length)].name()));
}
else if (!scriptEntry.hasObject("type")
&& arg.matchesEnum(FireworkEffect.Type.values())) {
scriptEntry.addObject("type", arg.asElement());
}
else if (!scriptEntry.hasObject("power")
&& arg.matchesInteger()) {
scriptEntry.addObject("power", arg.asElement());
}
else if (!scriptEntry.hasObject("flicker")
&& arg.matches("flicker")) {
scriptEntry.addObject("flicker", "");
}
else if (!scriptEntry.hasObject("trail")
&& arg.matches("trail")) {
scriptEntry.addObject("trail", "");
}
else if (!scriptEntry.hasObject("primary")
&& arg.matchesPrefix("primary")
&& arg.matchesArgumentList(ColorTag.class)) {
scriptEntry.addObject("primary", arg.asType(ListTag.class).filter(ColorTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("fade")
&& arg.matchesPrefix("fade")
&& arg.matchesArgumentList(ColorTag.class)) {
scriptEntry.addObject("fade", arg.asType(ListTag.class).filter(ColorTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("location", Utilities.entryDefaultLocation(scriptEntry, false));
scriptEntry.defaultObject("type", new ElementTag("ball"));
scriptEntry.defaultObject("power", new ElementTag(1));
scriptEntry.defaultObject("primary", Collections.singletonList(new ColorTag(Color.YELLOW)));
}
@Override
public void execute(final ScriptEntry scriptEntry) {
final LocationTag location = scriptEntry.hasObject("location") ?
(LocationTag) scriptEntry.getObject("location") :
Utilities.getEntryNPC(scriptEntry).getLocation();
ElementTag type = scriptEntry.getElement("type");
ElementTag power = scriptEntry.getElement("power");
boolean flicker = scriptEntry.hasObject("flicker");
boolean trail = scriptEntry.hasObject("trail");
List<ColorTag> primary = (List<ColorTag>) scriptEntry.getObject("primary");
List<ColorTag> fade = (List<ColorTag>) scriptEntry.getObject("fade");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), location, type, power, (flicker ? db("flicker", true) : ""),
(trail ? db("trail", true) : ""),
db("primary colors", primary),
(fade != null ? db("fade colors", fade) : ""));
}
Firework firework = location.getWorld().spawn(location, Firework.class);
FireworkMeta fireworkMeta = firework.getFireworkMeta();
fireworkMeta.setPower(power.asInt());
Builder fireworkBuilder = FireworkEffect.builder();
fireworkBuilder.with(FireworkEffect.Type.valueOf(type.asString().toUpperCase()));
fireworkBuilder.withColor(Conversion.convertColors(primary));
if (fade != null) {
fireworkBuilder.withFade(Conversion.convertColors(fade));
}
if (flicker) {
fireworkBuilder.withFlicker();
}
if (trail) {
fireworkBuilder.withTrail();
}
fireworkMeta.addEffects(fireworkBuilder.build());
firework.setFireworkMeta(fireworkMeta);
scriptEntry.addObject("launched_firework", new EntityTag(firework));
}
}