This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
/
ItemScript.java
212 lines (197 loc) · 9.61 KB
/
ItemScript.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
package com.denizenscript.denizen2sponge.spongescripts;
import com.denizenscript.denizen2core.Denizen2Core;
import com.denizenscript.denizen2core.arguments.Argument;
import com.denizenscript.denizen2core.commands.CommandQueue;
import com.denizenscript.denizen2core.scripts.CommandScript;
import com.denizenscript.denizen2core.tags.AbstractTagObject;
import com.denizenscript.denizen2core.tags.objects.BooleanTag;
import com.denizenscript.denizen2core.tags.objects.MapTag;
import com.denizenscript.denizen2core.tags.objects.ScriptTag;
import com.denizenscript.denizen2core.utilities.Action;
import com.denizenscript.denizen2core.utilities.CoreUtilities;
import com.denizenscript.denizen2core.utilities.ErrorInducedException;
import com.denizenscript.denizen2core.utilities.Tuple;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2core.utilities.debugging.Debug;
import com.denizenscript.denizen2core.utilities.yaml.StringHolder;
import com.denizenscript.denizen2core.utilities.yaml.YAMLConfiguration;
import com.denizenscript.denizen2sponge.Denizen2Sponge;
import com.denizenscript.denizen2sponge.tags.objects.ItemTag;
import com.denizenscript.denizen2sponge.utilities.DataKeys;
import com.denizenscript.denizen2sponge.utilities.flags.FlagMap;
import com.denizenscript.denizen2sponge.utilities.flags.FlagMapDataImpl;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.key.Key;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.text.Text;
import java.util.*;
public class ItemScript extends CommandScript {
// <--[explanation]
// @Since 0.5.0
// @Name Item Scripts
// @Group Script Types
// @Description
// An item script is a type of script that fully defines a specific in-game item stack.
// Keys in an item script define the various properties of an item stack.
//
// An item script can be used in place of any normal item, by putting the name of the item script
// where a script expects an item type. A script may block this from user input by
// requiring a valid ItemTypeTag input, which will not recognize an item script.
//
// The item script name may not be the same as an existing item type name.
//
// ItemStacks generated from an item script will remember their type using flag "_d2_script".
// To stop this from occurring, set key "plain" to "true".
//
// Set key "static" to "true" on the item script to make it load once at startup and simply be duplicated on all usages.
// If static is false or unspecified, the item script will be loaded from data at each call requesting it.
// This is likely preferred if any tags are used within the script.
//
// Quantity values should be specified in the script requesting the item (For example, in a give command)
// as that is likely very localized. The item stack given directly by the item script system will have a quantity of 1.
//
// All options listed below are used to define the item's specific details. They all support tags on input.
//
// Set key "material" directly to an ItemTag of the basic item type to use. You may list an item type, or an existing item.
// Be careful to not list the item script within itself, even indirectly, as this can cause recursion errors.
//
// Set key "display name" directly to a TextTag value of the name the item should have.
//
// Set key "lore" as a list key of lines the item should have.
// If you wish to dynamically structure the list, see the "other values" option for specifying that.
//
// Set key "flags" as a section and within it put all flags keyed by name and with the value that each flag should hold.
// If you wish to dynamically structure the mapping, see the "keys" option for specifying that.
//
// To specify other values, create a section labeled "keys" and within it put any valid item keys.
// TODO: Create and reference an explanation of basic item keys.
// -->
public ItemScript(String name, YAMLConfiguration section) {
super(name, section);
}
@Override
public boolean init() {
if (super.init()) {
try {
prepValues();
Action<String> error = (es) -> {
throw new ErrorInducedException(es);
};
if (contents.contains("static") && BooleanTag.getFor(error, contents.getString("static")).getInternal()) {
staticItem = generateItem(null);
}
}
catch (ErrorInducedException ex) {
Debug.error("Item generation for " + ColorSet.emphasis + title + ColorSet.warning + ": " + ex.getMessage());
return false;
}
Denizen2Sponge.itemScripts.put(CoreUtilities.toLowerCase(title), this);
return true;
}
return false;
}
public ItemStack staticItem = null;
public ItemStack getItemCopy(CommandQueue queue) {
if (staticItem != null) {
return staticItem;
}
return generateItem(queue);
}
public Argument displayName, plain, material;
public List<Argument> lore;
public List<Tuple<String, Argument>> otherValues, flags;
public void prepValues() {
Action<String> error = (es) -> {
throw new ErrorInducedException(es);
};
if (Sponge.getRegistry().getType(ItemType.class, title).isPresent()) {
Debug.error("Item script " + title + " may be unusable: a base item type exists with that name!");
}
if (contents.contains("display name")) {
displayName = Denizen2Core.splitToArgument(contents.getString("display name"), true, true, error);
}
if (contents.contains("plain")) {
plain = Denizen2Core.splitToArgument(contents.getString("plain"), true, true, error);
}
if (contents.contains("material")) {
material = Denizen2Core.splitToArgument(contents.getString("material"), true, true, error);
}
else {
throw new ErrorInducedException("Material key is missing. Cannot generate!");
}
if (contents.contains("lore")) {
List<String> listLore = contents.getStringList("lore");
lore = new ArrayList<>();
for (String line : listLore) {
lore.add(Denizen2Core.splitToArgument(line, true, true, error));
}
}
if (contents.contains("flags")) {
flags = new ArrayList<>();
YAMLConfiguration sec = contents.getConfigurationSection("flags");
for (StringHolder key : sec.getKeys(false)) {
Argument arg = Denizen2Core.splitToArgument(sec.getString(key.str), true, true, error);
flags.add(new Tuple<>(CoreUtilities.toUpperCase(key.low), arg));
}
}
if (contents.contains("keys")) {
otherValues = new ArrayList<>();
YAMLConfiguration sec = contents.getConfigurationSection("keys");
for (StringHolder key : sec.getKeys(false)) {
Argument arg = Denizen2Core.splitToArgument(sec.getString(key.str), true, true, error);
otherValues.add(new Tuple<>(CoreUtilities.toUpperCase(key.low), arg));
}
}
}
public AbstractTagObject parseVal(CommandQueue queue, Argument arg) {
Action<String> error = (es) -> {
throw new ErrorInducedException(es);
};
return arg.parse(queue, new HashMap<>(), getDebugMode(), error);
}
public ItemStack generateItem(CommandQueue queue) {
Action<String> error = (es) -> {
throw new ErrorInducedException(es);
};
ItemStack.Builder its = ItemStack.builder().from(ItemTag.getFor(error, parseVal(queue, material)).getInternal()).quantity(1);
if (displayName != null) {
its = its.add(Keys.DISPLAY_NAME, Denizen2Sponge.parseColor(parseVal(queue, displayName).toString()));
}
if (lore != null) {
List<Text> loreVal = new ArrayList<>();
for (Argument arg : lore) {
loreVal.add(Denizen2Sponge.parseColor(parseVal(queue, arg).toString()));
}
its.add(Keys.ITEM_LORE, loreVal);
}
MapTag flagsMap = new MapTag();
if (flags != null) {
for (Tuple<String, Argument> flagVal : flags) {
flagsMap.getInternal().put(flagVal.one, parseVal(queue, flagVal.two));
}
}
if (plain == null || !BooleanTag.getFor(error, parseVal(queue, plain)).getInternal()) {
flagsMap.getInternal().put("_d2_script", new ScriptTag(this));
}
ItemStack toRet = its.build();
if (otherValues != null) {
for (Tuple<String, Argument> input : otherValues) {
Key k = DataKeys.getKeyForName(input.one);
if (k == null) {
throw new ErrorInducedException("Key '" + input.one + "' does not seem to exist.");
}
DataKeys.tryApply(toRet, k, parseVal(queue, input.two), error);
}
}
if (!flagsMap.getInternal().isEmpty()) {
toRet.offer(new FlagMapDataImpl(new FlagMap(flagsMap)));
}
return toRet;
}
@Override
public boolean isExecutable(String section) {
return false;
}
}