/
ItemPotion.java
428 lines (389 loc) · 18.5 KB
/
ItemPotion.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package net.aufdemrand.denizen.objects.properties.item;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.NMSVersion;
import net.aufdemrand.denizen.nms.interfaces.ItemHelper;
import net.aufdemrand.denizen.objects.dColor;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Color;
import org.bukkit.Material;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.Potion;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
public class ItemPotion implements Property {
public static boolean describes(dObject item) {
return item instanceof dItem
&& (((dItem) item).getItemStack().getType() == Material.POTION
|| ((dItem) item).getItemStack().getType() == Material.SPLASH_POTION
|| ((dItem) item).getItemStack().getType() == Material.LINGERING_POTION
|| ((dItem) item).getItemStack().getType() == Material.TIPPED_ARROW);
}
public static ItemPotion getFrom(dObject _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemPotion((dItem) _item);
}
}
public static final String[] handledTags = new String[] {
"potion_base", "has_potion_effect", "potion_effect"
};
public static final String[] handledMechs = new String[] {
"potion_effects"
};
private ItemPotion(dItem item) {
this.item = item;
}
dItem item;
@Override
public String getPropertyString() {
if (!item.getItemStack().hasItemMeta()) {
return null;
}
if (!(item.getItemStack().getItemMeta() instanceof PotionMeta)) {
return null;
}
PotionMeta meta = (PotionMeta) item.getItemStack().getItemMeta();
dList effects = new dList();
effects.add(meta.getBasePotionData().getType()
+ "," + meta.getBasePotionData().isUpgraded()
+ "," + meta.getBasePotionData().isExtended()
+ (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_12_R1) && meta.hasColor() ? "," + new dColor(meta.getColor()).identify().replace(",", "&comma") : "")
);
for (PotionEffect pot : meta.getCustomEffects()) {
StringBuilder sb = new StringBuilder();
sb.append(pot.getType().getName()).append(",")
.append(pot.getAmplifier()).append(",")
.append(pot.getDuration()).append(",")
.append(pot.isAmbient()).append(",")
.append(pot.hasParticles());
if (pot.getColor() != null) {
sb.append(",").append(new dColor(pot.getColor()).identify().replace(",", "&comma"));
}
effects.add(sb.toString());
}
return effects.identify();
}
@Override
public String getPropertyId() {
return "potion_effects";
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
boolean has = item.getItemStack().hasItemMeta() && item.getItemStack().getItemMeta() instanceof PotionMeta
&& ((PotionMeta) item.getItemStack().getItemMeta()).hasCustomEffects();
// <--[tag]
// @attribute <i@item.potion_base>
// @returns Element
// @mechanism dItem.potion_effects
// @group properties
// @description
// Returns the potion effect on this item.
// In the format Effect,Level,Extended,Splash,Color
// -->
if (attribute.startsWith("potion_base") && item.getItemStack().hasItemMeta() && item.getItemStack().getItemMeta() instanceof PotionMeta) {
PotionMeta meta = ((PotionMeta) item.getItemStack().getItemMeta());
return new Element(meta.getBasePotionData().getType().name() + "," + (meta.getBasePotionData().isUpgraded() ? 2 : 1)
+ "," + meta.getBasePotionData().isExtended() + "," + (item.getItemStack().getType() == Material.SPLASH_POTION)
+ (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_11_R1) && meta.hasColor() ? "," + new dColor(meta.getColor()).identify() : "")
).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.has_potion_effect>
// @returns Element(Boolean)
// @mechanism dItem.potion
// @description
// Returns whether the potion has a potion effect.
// -->
if (attribute.startsWith("has_potion_effect")) {
return new Element(has)
.getAttribute(attribute.fulfill(1));
}
if (has) {
if (attribute.startsWith("potion_effect")) {
PotionMeta meta = ((PotionMeta) item.getItemStack().getItemMeta());
int potN = attribute.hasContext(1) ? attribute.getIntContext(1) - 1 : 0;
if (potN < 0 || potN > meta.getCustomEffects().size()) {
return null;
}
attribute = attribute.fulfill(1);
// <--[tag]
// @attribute <i@item.potion_effect[<#>].is_splash>
// @returns Element(Boolean)
// @mechanism dItem.potion_effects
// @group properties
// @description
// Returns whether the potion is a splash potion.
// -->
if (attribute.startsWith("is_splash")) {
return new Element(item.getItemStack().getType() == Material.SPLASH_POTION)
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect[<#>].is_extended>
// @returns Element(Boolean)
// @mechanism dItem.potion_effects
// @group properties
// @description
// Returns whether the potion effect is extended.
// -->
if (attribute.startsWith("is_extended")) {
return new Element(meta.getBasePotionData().isExtended())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect[<#>].level>
// @returns Element(Number)
// @mechanism dItem.potion_effects
// @group properties
// @description
// Returns the potion effect's level.
// -->
if (attribute.startsWith("level")) {
return new Element(meta.getBasePotionData().isUpgraded() ? 2 : 1)
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect[<#>].is_ambient>
// @returns Element(Boolean)
// @mechanism dItem.potion_effects
// @group properties
// @description
// Returns whether the potion effect is ambient.
// -->
if (attribute.startsWith("is_ambient")) {
return new Element(meta.getCustomEffects().get(potN).isAmbient())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect[<#>].color>
// @returns dColor
// @mechanism dItem.potion_effects
// @group properties
// @description
// Returns the potion effect's color.
// NOTE: Not supported as of Minecraft version 1.13.
// -->
if (attribute.startsWith("color")) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
dB.echoError("Custom effects with the color option are not supported as of Minecraft version 1.13.");
return null;
}
return new dColor(meta.getCustomEffects().get(potN).getColor())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect[<#>].icon>
// @returns Element(Boolean)
// @mechanism dItem.potion_effects
// @group properties
// @description
// Returns whether the potion effect shows an icon.
// -->
if (attribute.startsWith("icon") && NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
return new Element(meta.getCustomEffects().get(potN).hasIcon()).getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect[<#>].has_particles>
// @returns Element(Boolean)
// @mechanism dItem.potion_effects
// @group properties
// @description
// Returns whether the potion effect has particles.
// -->
if (attribute.startsWith("has_particles")) {
return new Element(meta.getCustomEffects().get(potN).hasParticles())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect[<#>].duration>
// @returns Element(Number)
// @mechanism dItem.potion_effects
// @group properties
// @description
// Returns the duration in ticks of the potion.
// -->
if (attribute.startsWith("duration")) {
return new Element(meta.getCustomEffects().get(potN).getDuration())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect[<#>].amplifier>
// @returns Element(Number)
// @mechanism dItem.potion_effects
// @group properties
// @description
// Returns the amplifier level of the potion effect.
// -->
if (attribute.startsWith("amplifier")) {
return new Element(meta.getCustomEffects().get(potN).getAmplifier())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect[<#>].type>
// @returns Element
// @mechanism dItem.potion_effects
// @group properties
// @description
// Returns the type of the potion effect.
// -->
if (attribute.startsWith("type")) {
return new Element(meta.getCustomEffects().get(potN).getType().getName())
.getAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("data")) {
return new Element(0)
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect[<#>]>
// @returns Element
// @mechanism dItem.potion_effects
// @group properties
// @warning Don't use this directly, use its sub-tags!
// @description
// Returns the potion effect on this item.
// In the format Effect,Level,Extended,Splash
// -->
return new Element(meta.getBasePotionData().getType().name() + "," + (meta.getBasePotionData().isUpgraded() ? 2 : 1)
+ "," + meta.getBasePotionData().isExtended() + "," + (item.getItemStack().getType() == Material.SPLASH_POTION))
.getAttribute(attribute);
}
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object dItem
// @name potion_effects
// @input dList
// @description
// Sets the potion's potion effect(s).
// Input is a formed like: Effect,Upgraded,Extended(,Color)|Type,Amplifier,Duration,Ambient,Particles(,Icon)|...
// For example: SPEED,true,false|SPEED,2,200,false,true,true
// NOTE: In pre-1.13 Minecraft versions, you could set a color in the custom effects list instead of "icon".
// @tags
// <i@item.potion_effect[<#>]>
// <i@item.potion_effect[<#>].type>
// <i@item.potion_effect[<#>].duration>
// <i@item.potion_effect[<#>].amplifier>
// <i@item.potion_effect[<#>].is_ambient>
// <i@item.potion_effect[<#>].has_particles>
// <i@item.potion_effect[<#>].color>
// <i@item.potion_effect[<#>].icon>
// <server.list_potion_types>
// <server.list_potion_effects>
// -->
if (mechanism.matches("potion_effects")) {
dList data = mechanism.valueAsType(dList.class);
String[] d1 = data.get(0).split(",");
PotionMeta meta = (PotionMeta) item.getItemStack().getItemMeta();
meta.setBasePotionData(new PotionData(PotionType.valueOf(d1[0].toUpperCase()),
CoreUtilities.toLowerCase(d1[2]).equals("true"),
CoreUtilities.toLowerCase(d1[1]).equals("true")));
if (d1.length > 3) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_11_R1)) {
meta.setColor(dColor.valueOf(d1[3].replace("&comma", ",")).getColor());
}
else {
dB.echoError("Potion effect color is not supported before Minecraft version 1.11.");
}
}
meta.clearCustomEffects();
ItemHelper itemHelper = NMSHandler.getInstance().getItemHelper();
for (int i = 1; i < data.size(); i++) {
String[] d2 = data.get(i).split(",");
PotionEffectType type = PotionEffectType.getByName(d2[0].toUpperCase());
// NOTE: amplifier and duration are swapped around in the input format
// as compared to the PotionEffect constructor!
int duration = new Element(d2[2]).asInt();
int amplifier = new Element(d2[1]).asInt();
boolean ambient = new Element(d2[3]).asBoolean();
boolean particles = new Element(d2[4]).asBoolean();
Color color = null;
boolean icon = false;
if (d2.length > 5) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
Element check = new Element(d2[5]);
if (check.isBoolean()) {
icon = check.asBoolean();
}
else {
dB.echoError("Custom effects with the color option are not supported as of Minecraft version 1.13.");
}
}
else {
String check = d2[5].replace("&comma", ",");
if (dColor.matches(check)) {
color = dColor.valueOf(check).getColor();
}
}
}
meta.addCustomEffect(itemHelper.getPotionEffect(type, duration, amplifier, ambient, particles, color, icon), false);
}
item.getItemStack().setItemMeta(meta);
}
if (mechanism.matches("potion")) {
String[] data = mechanism.getValue().asString().split(",", 4);
if (data.length < 4) {
if (mechanism.getValue().isInt()) {
item.getItemStack().setDurability((short) mechanism.getValue().asInt());
}
else {
dB.echoError("Invalid effect format, use name,amplifier,extended,splash.");
}
}
else {
Element data1 = new Element(data[1]);
Element data2 = new Element(data[2]);
Element data3 = new Element(data[3]);
PotionType type;
try {
type = PotionType.valueOf(data[0].toUpperCase());
}
catch (Exception ex) {
dB.echoError("Invalid potion effect type '" + data[0] + "'");
return;
}
if (!data1.isInt()) {
dB.echoError("Cannot apply effect '" + data[0] + "': '" + data[1] + "' is not a valid integer!");
return;
}
if (!data2.isBoolean()) {
dB.echoError("Cannot apply effect '" + data[0] + "': '" + data[2] + "' is not a valid boolean!");
return;
}
if (!data3.isBoolean()) {
dB.echoError("Cannot apply effect '" + data[0] + "': '" + data[3] + "' is not a valid boolean!");
return;
}
Potion pot = new Potion(type);
int d1 = data1.asInt();
if (d1 >= 1 && d1 <= pot.getType().getMaxLevel()) {
pot.setLevel(d1);
}
if (!pot.getType().isInstant()) {
pot.setHasExtendedDuration(data2.asBoolean());
}
pot.setSplash(data3.asBoolean());
item.setDurability((short) 0);
pot.apply(item.getItemStack());
}
}
}
}