/
AdvancementCommand.java
273 lines (265 loc) · 14.7 KB
/
AdvancementCommand.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
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.util.Advancement;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.objects.PlayerTag;
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.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AdvancementCommand extends AbstractCommand {
public AdvancementCommand() {
setName("advancement");
setSyntax("advancement [id:<name>] (delete/grant:<players>/revoke:<players>/{create}) (parent:<name>) (icon:<item>) (title:<text>) (description:<text>) (background:<key>) (frame:<type>) (toast:<boolean>) (announce:<boolean>) (hidden:<boolean>) (x:<offset>) (y:<offset>) (progress_length:<#>)");
setRequiredArguments(1, 14);
isProcedural = false;
}
// <--[command]
// @Name Advancement
// @Syntax advancement [id:<name>] (delete/grant:<players>/revoke:<players>/{create}) (parent:<name>) (icon:<item>) (title:<text>) (description:<text>) (background:<key>) (frame:<type>) (toast:<boolean>) (announce:<boolean>) (hidden:<boolean>) (x:<offset>) (y:<offset>) (progress_length:<#>)
// @Required 1
// @Maximum 14
// @Short Controls a custom advancement.
// @Group player
//
// @Description
// Controls custom Minecraft player advancements. You should generally create advancements manually on server start.
// Currently, the ID argument may only refer to advancements added through this command.
// The default action is to create and register a new advancement.
// You may also delete an existing advancement, in which case do not provide any further arguments.
// You may grant or revoke an advancement for a list of players, in which case do not provide any further arguments.
// The parent argument sets the root advancement in the advancements menu, in the format "namespace:key".
// If no namespace is specified, the parent is assumed to have been created through this command.
// The icon argument sets the icon displayed in toasts and the advancements menu.
// The title argument sets the title that will show on toasts and in the advancements menu.
// The description argument sets the information that will show when scrolling over a chat announcement or in the advancements menu.
// The background argument sets the image to use if the advancement goes to a new tab.
// If the background is unspecified, defaults to "minecraft:textures/gui/advancements/backgrounds/stone.png".
// The frame argument sets the type of advancement - valid arguments are CHALLENGE, GOAL, and TASK.
// The toast argument sets whether the advancement should display a toast message when a player completes it. Default is true.
// The announce argument sets whether the advancement should display a chat message to the server when a player completes it. Default is true.
// The hidden argument sets whether the advancement should be hidden until it is completed.
// The x and y arguments are offsets based on the size of an advancement icon in the menu. They are required for custom tabs to look reasonable.
//
// When creating an advancement, optionally specify 'progress_length' to make it require multiple parts.
// When granting an advancement, optionally specify 'progress_length' to only grant partial progress.
//
// To award a pre-existing vanilla advancement, instead use <@link mechanism PlayerTag.award_advancement>
//
// WARNING: Failure to re-create advancements on every server start may result in loss of data - use <@link event server prestart>.
//
// If you mess with datapacks, you will also need to re-create advancements during <@link event server resources reloaded>
//
// @Tags
// <PlayerTag.has_advancement[<advancement>]>
// <PlayerTag.advancements>
// <server.advancement_types>
//
// @Usage
// Creates a new advancement that has a potato icon.
// - advancement id:hello_world icon:baked_potato "title:Hello World" "description:You said hello to the world."
//
// @Usage
// Creates a new advancement with the parent "hello_world" and a CHALLENGE frame. Hidden until it is completed.
// - advancement id:hello_universe parent:hello_world icon:ender_pearl "title:Hello Universe" "description:You said hello to the UNIVERSE." frame:challenge hidden:true x:1
//
// @Usage
// Grants the "hello_world" advancement to the current player.
// - advancement id:hello_world grant:<player>
//
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("id")
&& arg.matchesPrefix("id")) {
scriptEntry.addObject("id", arg.asElement());
}
else if (!scriptEntry.hasObject("parent")
&& arg.matchesPrefix("parent")) {
scriptEntry.addObject("parent", arg.asElement());
}
else if (!scriptEntry.hasObject("create")
&& arg.matches("create")) {
scriptEntry.addObject("create", new ElementTag(true)); // unused, just to be explicit
}
else if (!scriptEntry.hasObject("delete")
&& arg.matches("delete", "remove")) {
scriptEntry.addObject("delete", new ElementTag(true));
}
else if (!scriptEntry.hasObject("grant")
&& arg.matchesPrefix("grant", "give", "g")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("grant", arg.asType(ListTag.class));
}
else if (!scriptEntry.hasObject("revoke")
&& arg.matchesPrefix("revoke", "take", "r")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("revoke", arg.asType(ListTag.class));
}
else if (!scriptEntry.hasObject("icon")
&& arg.matchesPrefix("icon", "i")
&& arg.matchesArgumentType(ItemTag.class)) {
scriptEntry.addObject("icon", arg.asType(ItemTag.class));
}
else if (!scriptEntry.hasObject("title")
&& arg.matchesPrefix("title", "text", "t")) {
scriptEntry.addObject("title", arg.asElement());
}
else if (!scriptEntry.hasObject("description")
&& arg.matchesPrefix("description", "desc", "d")) {
scriptEntry.addObject("description", arg.asElement());
}
else if (!scriptEntry.hasObject("background")
&& arg.matchesPrefix("background", "bg")) {
scriptEntry.addObject("background", arg.asElement());
}
else if (!scriptEntry.hasObject("frame")
&& arg.matchesPrefix("frame", "f")
&& arg.matchesEnum(Advancement.Frame.class)) {
scriptEntry.addObject("frame", arg.asElement());
}
else if (!scriptEntry.hasObject("toast")
&& arg.matchesPrefix("toast", "show")
&& arg.matchesBoolean()) {
scriptEntry.addObject("toast", arg.asElement());
}
else if (!scriptEntry.hasObject("announce")
&& arg.matchesPrefix("announce", "chat")
&& arg.matchesBoolean()) {
scriptEntry.addObject("announce", arg.asElement());
}
else if (!scriptEntry.hasObject("hidden")
&& arg.matchesPrefix("hidden", "hide", "h")
&& arg.matchesBoolean()) {
scriptEntry.addObject("hidden", arg.asElement());
}
else if (!scriptEntry.hasObject("x")
&& arg.matchesPrefix("x")
&& arg.matchesFloat()) {
scriptEntry.addObject("x", arg.asElement());
}
else if (!scriptEntry.hasObject("y")
&& arg.matchesPrefix("y")
&& arg.matchesFloat()) {
scriptEntry.addObject("y", arg.asElement());
}
else if (!scriptEntry.hasObject("progress_length")
&& arg.matchesPrefix("progress_length")
&& arg.matchesInteger()) {
scriptEntry.addObject("progress_length", arg.asElement());
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("id")) {
throw new InvalidArgumentsException("Must specify an ID!");
}
scriptEntry.defaultObject("icon", new ItemTag(Material.AIR));
scriptEntry.defaultObject("title", new ElementTag(""));
scriptEntry.defaultObject("description", new ElementTag(""));
scriptEntry.defaultObject("background", new ElementTag("minecraft:textures/gui/advancements/backgrounds/stone.png"));
scriptEntry.defaultObject("frame", new ElementTag("TASK"));
scriptEntry.defaultObject("toast", new ElementTag(true));
scriptEntry.defaultObject("announce", new ElementTag(true));
scriptEntry.defaultObject("hidden", new ElementTag(false));
scriptEntry.defaultObject("x", new ElementTag(0f));
scriptEntry.defaultObject("y", new ElementTag(0f));
}
public static final Map<NamespacedKey, Advancement> customRegistered = new HashMap<>();
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag id = scriptEntry.getElement("id");
ElementTag parent = scriptEntry.getElement("parent");
ElementTag delete = scriptEntry.getElement("delete");
ListTag grant = scriptEntry.getObjectTag("grant");
ListTag revoke = scriptEntry.getObjectTag("revoke");
ItemTag icon = scriptEntry.getObjectTag("icon");
ElementTag title = scriptEntry.getElement("title");
ElementTag description = scriptEntry.getElement("description");
ElementTag background = scriptEntry.getElement("background");
ElementTag frame = scriptEntry.getElement("frame");
ElementTag toast = scriptEntry.getElement("toast");
ElementTag announce = scriptEntry.getElement("announce");
ElementTag hidden = scriptEntry.getElement("hidden");
ElementTag x = scriptEntry.getElement("x");
ElementTag y = scriptEntry.getElement("y");
ElementTag progressLength = scriptEntry.getElement("progress_length");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, name, id, parent, delete, grant, revoke, icon, title, description, background, progressLength, frame, toast, announce, hidden, x, y);
}
NamespacedKey key = new NamespacedKey(Denizen.getInstance(), id.asString());
if (delete == null && grant == null && revoke == null) {
NamespacedKey parentKey = null;
NamespacedKey backgroundKey = null;
if (parent != null) {
List<String> split = CoreUtilities.split(parent.asString(), ':', 2);
if (split.size() == 1) {
parentKey = new NamespacedKey(Denizen.getInstance(), split.get(0));
}
else {
parentKey = new NamespacedKey(CoreUtilities.toLowerCase(split.get(0)), CoreUtilities.toLowerCase(split.get(1)));
}
}
else if (background != null) {
List<String> backgroundSplit = CoreUtilities.split(background.asString(), ':', 2);
if (backgroundSplit.size() == 1) {
backgroundKey = NamespacedKey.minecraft(backgroundSplit.get(0));
}
else {
backgroundKey = new NamespacedKey(CoreUtilities.toLowerCase(backgroundSplit.get(0)), CoreUtilities.toLowerCase(backgroundSplit.get(1)));
}
}
final Advancement advancement = new Advancement(false, key, parentKey,
icon.getItemStack(), title.asString(), description.asString(),
backgroundKey, Advancement.Frame.valueOf(frame.asString().toUpperCase()),
toast.asBoolean(), announce.asBoolean(), hidden.asBoolean(), x.asFloat(), y.asFloat(), progressLength == null ? 1 : progressLength.asInt());
NMSHandler.advancementHelper.register(advancement);
customRegistered.put(key, advancement);
return;
}
Advancement advancement = customRegistered.get(key);
if (advancement == null) {
Debug.echoError("Invalid advancement key '" + key + "': not registered. Are you sure you registered it using the 'advancement' command previously?"
+ " Note that the 'advancement' command is not for vanilla advancements.");
return;
}
if (delete != null) {
NMSHandler.advancementHelper.unregister(advancement);
customRegistered.remove(key);
}
else if (grant != null) {
for (PlayerTag target : grant.filter(PlayerTag.class, scriptEntry)) {
Player player = target.getPlayerEntity();
if (player != null) {
if (progressLength == null) {
NMSHandler.advancementHelper.grant(advancement, player);
}
else {
NMSHandler.advancementHelper.grantPartial(advancement, player, progressLength.asInt());
}
}
}
}
else /*if (revoke != null)*/ {
for (PlayerTag target : revoke.filter(PlayerTag.class, scriptEntry)) {
Player player = target.getPlayerEntity();
if (player != null) {
NMSHandler.advancementHelper.revoke(advancement, player);
}
}
}
}
}