-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
NBTCommand.java
77 lines (61 loc) · 2.83 KB
/
NBTCommand.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
package com.denizenscript.denizen.scripts.commands.item;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.nbt.CustomNBT;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.utilities.Deprecations;
import org.bukkit.inventory.ItemStack;
public class NBTCommand extends AbstractCommand {
public NBTCommand() {
setName("nbt");
setSyntax("(Deprecated)");
setRequiredArguments(0, -1);
isProcedural = false;
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("key")
&& arg.getRawValue().split(":", 2).length == 2) {
String[] flagArgs = arg.getRawValue().split(":", 2);
scriptEntry.addObject("key", new ElementTag(flagArgs[0]));
scriptEntry.addObject("value", new ElementTag(flagArgs[1]));
}
else if (!scriptEntry.hasObject("item")
&& arg.matchesArgumentType(ItemTag.class)) {
scriptEntry.addObject("item", arg.asType(ItemTag.class));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("item")) {
throw new InvalidArgumentsException("Must specify item!");
}
if (!scriptEntry.hasObject("key") || !scriptEntry.hasObject("value")) {
throw new InvalidArgumentsException("Must specify key and value!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
Deprecations.nbtCommand.warn(scriptEntry);
ItemTag item = scriptEntry.getObjectTag("item");
ElementTag key = scriptEntry.getElement("key");
ElementTag value = scriptEntry.getElement("value");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), item.debug() + key.debug() + value.debug());
}
ItemStack itemStack = item.getItemStack();
if (value.asString().equals("!")) {
itemStack = CustomNBT.removeCustomNBT(itemStack, key.asString(), CustomNBT.KEY_DENIZEN);
}
else {
itemStack = CustomNBT.addCustomNBT(itemStack, key.asString(), value.asString(), CustomNBT.KEY_DENIZEN);
}
scriptEntry.addObject("new_item", new ItemTag(itemStack));
}
}