/
ItemPotion.java
557 lines (525 loc) · 24.2 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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
package com.denizenscript.denizen.objects.properties.item;
import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizen.utilities.BukkitImplDeprecations;
import org.bukkit.Material;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SuspiciousStewMeta;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import java.util.ArrayList;
import java.util.List;
public class ItemPotion implements Property {
public static boolean describes(ObjectTag item) {
if (!(item instanceof ItemTag)) {
return false;
}
ItemMeta meta = ((ItemTag) item).getItemMeta();
return meta instanceof PotionMeta || meta instanceof SuspiciousStewMeta;
}
public static ItemPotion getFrom(ObjectTag _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemPotion((ItemTag) _item);
}
}
public static final String[] handledMechs = new String[] {
"potion_effects"
};
private ItemPotion(ItemTag item) {
this.item = item;
}
ItemTag item;
public static String stringifyEffect(PotionEffect effect) {
return effect.getType().getName() + "," +
effect.getAmplifier() + "," +
effect.getDuration() + "," +
effect.isAmbient() + "," +
effect.hasParticles() + "," +
effect.hasIcon();
}
public static MapTag effectToMap(PotionEffect effect) {
MapTag map = new MapTag();
map.putObject("type", new ElementTag(effect.getType().getName()));
map.putObject("amplifier", new ElementTag(effect.getAmplifier()));
map.putObject("duration", new DurationTag((long) effect.getDuration()));
map.putObject("ambient", new ElementTag(effect.isAmbient()));
map.putObject("particles", new ElementTag(effect.hasParticles()));
map.putObject("icon", new ElementTag(effect.hasIcon()));
return map;
}
public ListTag getMapTagData() {
ListTag result = new ListTag();
MapTag base = new MapTag();
if (isPotion()) {
PotionMeta meta = getPotionMeta();
base.putObject("type", new ElementTag(meta.getBasePotionData().getType()));
base.putObject("upgraded", new ElementTag(meta.getBasePotionData().isUpgraded()));
base.putObject("extended", new ElementTag(meta.getBasePotionData().isExtended()));
if (meta.hasColor()) {
base.putObject("color", new ColorTag(meta.getColor()));
}
result.addObject(base);
}
for (PotionEffect effect : getCustomEffects()) {
result.addObject(effectToMap(effect));
}
return result;
}
public static PotionEffect parseEffect(String str, TagContext context) {
String[] d2 = str.split(",");
PotionEffectType type;
try {
type = PotionEffectType.getByName(d2[0].toUpperCase());
}
catch (IllegalArgumentException ex) {
if (context.showErrors()) {
Debug.echoError("Invalid potion effect type '" + d2[0] + "'");
}
return null;
}
if (d2.length < 3) {
return null;
}
// NOTE: amplifier and duration are swapped around in the input format
// as compared to the PotionEffect constructor!
int duration = new ElementTag(d2[2]).asInt();
int amplifier = new ElementTag(d2[1]).asInt();
boolean ambient = true;
boolean particles = true;
if (d2.length > 3) {
ambient = new ElementTag(d2[3]).asBoolean();
particles = new ElementTag(d2[4]).asBoolean();
}
boolean icon = false;
if (d2.length > 5) {
ElementTag check = new ElementTag(d2[5]);
if (check.isBoolean()) {
icon = check.asBoolean();
}
}
return new PotionEffect(type, duration, amplifier, ambient, particles, icon);
}
public static PotionEffect parseEffect(MapTag effectMap, TagContext context) {
PotionEffectType type;
DurationTag duration = new DurationTag(0);
int amplifier = 0;
boolean ambient = true;
boolean particles = true;
boolean icon = false;
if (effectMap.getObject("type") != null) {
String typeString = effectMap.getObject("type").toString();
type = PotionEffectType.getByName(typeString);
if (type == null) {
if (context.showErrors()) {
Debug.echoError("Invalid potion effect type '" + typeString + "': effect type is required.");
}
return null;
}
}
else {
if (context.showErrors()) {
Debug.echoError("Invalid potion effect type: effect type is required.");
}
return null;
}
if (effectMap.getObject("amplifier") != null) {
ElementTag amplifierElement = effectMap.getElement("amplifier");
if (amplifierElement.isInt()) {
amplifier = amplifierElement.asInt();
}
else if (context.showErrors()) {
Debug.echoError("Invalid amplifier '" + amplifierElement + "': must be an integer.");
}
}
if (effectMap.getObject("duration") != null) {
ObjectTag durationObj = effectMap.getObject("duration");
if (durationObj.canBeType(DurationTag.class)) {
duration = durationObj.asType(DurationTag.class, context);
}
else if (context.showErrors()) {
Debug.echoError("Invalid duration '" + durationObj + "': must be a valid DurationTag");
}
}
if (effectMap.getObject("ambient") != null) {
ElementTag ambientElement = effectMap.getElement("ambient");
if (ambientElement.isBoolean()) {
ambient = ambientElement.asBoolean();
}
else if (context.showErrors()) {
Debug.echoError("Invalid ambient state '" + ambientElement + "': must be a boolean.");
}
}
if (effectMap.getObject("particles") != null) {
ElementTag particlesElement = effectMap.getElement("particles");
if (particlesElement.isBoolean()) {
particles = particlesElement.asBoolean();
}
else if (context.showErrors()) {
Debug.echoError("Invalid particles state '" + particlesElement + "': must be a boolean.");
}
}
if (effectMap.getObject("icon") != null) {
ElementTag iconElement = effectMap.getElement("icon");
if (iconElement.isBoolean()) {
icon = iconElement.asBoolean();
}
else if (context.showErrors()) {
Debug.echoError("Invalid icon state '" + iconElement + "': must be a boolean.");
}
}
return new PotionEffect(type, duration.getTicksAsInt(), amplifier, ambient, particles, icon);
}
public boolean isStew() {
return item.getItemMeta() instanceof SuspiciousStewMeta;
}
public boolean isPotion() {
return item.getItemMeta() instanceof PotionMeta;
}
public PotionMeta getPotionMeta() {
return (PotionMeta) item.getItemMeta();
}
public SuspiciousStewMeta getStewMeta() {
return (SuspiciousStewMeta) item.getItemMeta();
}
public boolean hasBase() {
return isPotion();
}
public List<PotionEffect> getCustomEffects() {
if (isStew()) {
return getStewMeta().getCustomEffects();
}
else {
return getPotionMeta().getCustomEffects();
}
}
public boolean hasCustomEffects() {
if (isStew()) {
return getStewMeta().hasCustomEffects();
}
else {
return getPotionMeta().hasCustomEffects();
}
}
@Override
public String getPropertyString() {
return getMapTagData().identify();
}
@Override
public String getPropertyId() {
return "potion_effects";
}
public static void register() {
// <--[tag]
// @attribute <ItemTag.potion_base_type>
// @returns ElementTag
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns the base potion type name for this potion item.
// The type will be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionType.html>.
// -->
PropertyParser.registerTag(ItemPotion.class, ElementTag.class, "potion_base_type", (attribute, object) -> {
if (!object.hasBase()) {
attribute.echoError("This item does not have a base potion type.");
return null;
}
return new ElementTag(object.getPotionMeta().getBasePotionData().getType());
});
// <--[tag]
// @attribute <ItemTag.potion_base>
// @returns ElementTag
// @group attribute
// @mechanism ItemTag.potion_effects
// @deprecated use 'effects_data' instead
// @description
// Deprecated in favor of <@link tag ItemTag.effects_data>
// -->
PropertyParser.registerTag(ItemPotion.class, ElementTag.class, "potion_base", (attribute, object) -> {
if (!object.hasBase()) {
attribute.echoError("This item does not have a base potion type.");
return null;
}
BukkitImplDeprecations.oldPotionEffects.warn(attribute.context);
PotionMeta meta = object.getPotionMeta();
return new ElementTag(meta.getBasePotionData().getType().name() + "," + (meta.getBasePotionData().isUpgraded() ? 2 : 1)
+ "," + meta.getBasePotionData().isExtended() + "," + (object.item.getBukkitMaterial() == Material.SPLASH_POTION)
+ (meta.hasColor() ? "," + new ColorTag(meta.getColor()).identify() : ""));
});
// <--[tag]
// @attribute <ItemTag.potion_effects>
// @returns ListTag
// @group attribute
// @mechanism ItemTag.potion_effects
// @deprecated use 'effects_data' instead
// @description
// Deprecated in favor of <@link tag ItemTag.effects_data>
// -->
PropertyParser.registerTag(ItemPotion.class, ListTag.class, "potion_effects", (attribute, object) -> {
BukkitImplDeprecations.oldPotionEffects.warn(attribute.context);
ListTag result = new ListTag();
for (PotionEffect pot : object.getCustomEffects()) {
result.add(stringifyEffect(pot));
}
return result;
});
// <--[tag]
// @attribute <ItemTag.has_potion_effect>
// @returns ElementTag(Boolean)
// @mechanism ItemTag.potion_effects
// @description
// Returns whether the item (potion, tipped arrow, or suspicious stew) has a potion effect.
// -->
PropertyParser.registerTag(ItemPotion.class, ElementTag.class, "has_potion_effect", (attribute, object) -> {
return new ElementTag(object.hasCustomEffects());
});
// <--[tag]
// @attribute <ItemTag.potion_effect[<#>]>
// @returns ElementTag
// @group attribute
// @mechanism ItemTag.potion_effects
// @deprecated use 'effects_data' instead
// @description
// Deprecated in favor of <@link tag ItemTag.effects_data>
// -->
PropertyParser.registerTag(ItemPotion.class, ElementTag.class, "potion_effect", (attribute, object) -> {
BukkitImplDeprecations.oldPotionEffects.warn(attribute.context);
int potN = attribute.hasParam() ? attribute.getIntParam() - 1 : 0;
if (potN < 0 || potN > object.getCustomEffects().size()) {
return null;
}
if (attribute.startsWith("is_splash", 2)) {
attribute.fulfill(1);
return new ElementTag(object.item.getBukkitMaterial() == Material.SPLASH_POTION);
}
if (attribute.startsWith("is_extended", 2)) {
attribute.fulfill(1);
if (!object.hasBase()) {
return null;
}
return new ElementTag(object.getPotionMeta().getBasePotionData().isExtended());
}
if (attribute.startsWith("level", 2)) {
attribute.fulfill(1);
if (!object.hasBase()) {
return null;
}
return new ElementTag(object.getPotionMeta().getBasePotionData().isUpgraded() ? 2 : 1);
}
if (attribute.startsWith("is_ambient", 2)) {
attribute.fulfill(1);
return new ElementTag(object.getCustomEffects().get(potN).isAmbient());
}
if (attribute.startsWith("icon", 2)) {
attribute.fulfill(1);
return new ElementTag(object.getCustomEffects().get(potN).hasIcon());
}
if (attribute.startsWith("has_particles", 2)) {
attribute.fulfill(1);
return new ElementTag(object.getCustomEffects().get(potN).hasParticles());
}
if (attribute.startsWith("duration", 2)) {
attribute.fulfill(1);
return new ElementTag(object.getCustomEffects().get(potN).getDuration());
}
if (attribute.startsWith("amplifier", 2)) {
attribute.fulfill(1);
return new ElementTag(object.getCustomEffects().get(potN).getAmplifier());
}
if (attribute.startsWith("type", 2)) {
attribute.fulfill(1);
return new ElementTag(object.getCustomEffects().get(potN).getType().getName());
}
if (attribute.startsWith("data", 2)) {
attribute.fulfill(1);
return new ElementTag(0);
}
if (!object.hasBase()) {
return null;
}
PotionData data = object.getPotionMeta().getBasePotionData();
return new ElementTag(data.getType().name() + "," + (data.isUpgraded() ? 2 : 1)
+ "," + data.isExtended() + "," + (object.item.getBukkitMaterial() == Material.SPLASH_POTION));
});
// <--[tag]
// @attribute <ItemTag.effects_data>
// @returns ListTag(MapTag)
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns a list of all potion effects on this item, in the same format as the MapTag input to the mechanism.
// This applies to Potion items, Tipped Arrow items, and Suspicious Stews.
// Note that for potions or tipped arrows (not suspicious stew) the first value in the list is the potion's base type.
// All subsequent entries are effect data.
// -->
PropertyParser.registerTag(ItemPotion.class, ListTag.class, "effects_data", (attribute, object) -> {
return object.getMapTagData();
});
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ItemTag
// @name potion_effects
// @input ListTag
// @description
// Sets the item's potion effect(s).
// This applies to Potion items, Tipped Arrow items, and Suspicious Stews.
//
// For potions or tipped arrows (not suspicious stew), the first item in the list must be a MapTag with keys:
// "type" - from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionType.html>
// "upgraded" - boolean, true means level 2 and false means level 1 (optional, default false)
// "extended" - true means longer, false means shorter (optional, default false)
// "color" - ColorTag (optional, default none)
//
// For example: [type=SPEED;upgraded=true;extended=false;color=RED]
// This example produces an item labeled as "Potion of Swiftness - Speed II (1:30)"
//
// Each following item in the list are potion effects, which must be a MapTag with keys:
// "type" - from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>
// "amplifier" - number to increase the level by (0 for default level 1) (optional, default 0)
// "duration" - DurationTag, how long it lasts (optional, default 0s)
// "ambient", "particles", "icon" - booleans (optional, default true, true, false)
//
// For example: [type=SPEED;amplifier=2;duration=10s;ambient=false;particles=true;icon=true]
// This example would be a level 3 swiftness potion that lasts 10 seconds.
//
// A very short full default potion item would be: potion[potion_effects=[type=regen]
// A (relatively) short full potion item would be: potion[potion_effects=<list[[type=regen]|[type=speed;duration=10s]]>]
// (Note the list constructor to force data format interpretation, as potion formats can be given multiple ways and the system will get confused without a constructor)
//
// @tags
// <ItemTag.effects_data>
// <server.potion_types>
// <server.potion_effect_types>
// -->
if (mechanism.matches("potion_effects")) {
List<ObjectTag> data = new ArrayList<>(CoreUtilities.objectToList(mechanism.value, mechanism.context));
ItemMeta meta = item.getItemMeta();
if (isPotion()) {
ObjectTag firstObj = data.remove(0);
PotionType type;
boolean upgraded = false;
boolean extended = false;
ColorTag color = null;
if (firstObj.canBeType(MapTag.class)) {
MapTag baseEffect = firstObj.asType(MapTag.class, mechanism.context);
if (baseEffect.getObject("type") != null) {
ElementTag typeElement = baseEffect.getElement("type");
if (!typeElement.matchesEnum(PotionType.class)) {
mechanism.echoError("Invalid base potion type '" + typeElement + "': type is required");
return;
}
type = PotionType.valueOf(typeElement.asString().toUpperCase());
}
else {
mechanism.echoError("No base potion type specified: type is required");
return;
}
if (baseEffect.getObject("upgraded") != null) {
ElementTag upgradedElement = baseEffect.getElement("upgraded");
if (upgradedElement.isBoolean()) {
upgraded = upgradedElement.asBoolean();
}
else {
mechanism.echoError("Invalid upgraded state '" + upgradedElement + "': must be a boolean");
}
}
if (baseEffect.getObject("extended") != null) {
ElementTag extendedElement = baseEffect.getElement("extended");
if (extendedElement.isBoolean()) {
extended = extendedElement.asBoolean();
}
else {
mechanism.echoError("Invalid extended state '" + extendedElement + "': must be a boolean");
}
}
if (baseEffect.getObject("color") != null) {
ObjectTag colorObj = baseEffect.getObject("color");
if (colorObj.canBeType(ColorTag.class)) {
color = colorObj.asType(ColorTag.class, mechanism.context);
}
else {
mechanism.echoError("Invalid color '" + colorObj + "': must be a valid ColorTag");
}
}
}
else {
String[] d1 = firstObj.toString().split(",");
try {
type = PotionType.valueOf(d1[0].toUpperCase());
}
catch (IllegalArgumentException ex) {
mechanism.echoError("Invalid base potion type '" + d1[0] + "': type is required");
return;
}
upgraded = CoreUtilities.equalsIgnoreCase(d1[1], "true");
extended = CoreUtilities.equalsIgnoreCase(d1[2], "true");
if (d1.length > 3) {
ColorTag temp = ColorTag.valueOf(d1[3].replace("&comma", ","), mechanism.context);
if (temp == null) {
mechanism.echoError("Invalid color '" + d1[3] + "': must be a valid ColorTag");
}
else {
color = temp;
}
}
}
if (upgraded && !type.isUpgradeable()) {
mechanism.echoError("Cannot upgrade potion of type '" + type.name() + "'");
upgraded = false;
}
if (extended && !type.isExtendable()) {
mechanism.echoError("Cannot extend potion of type '" + type.name() + "'");
extended = false;
}
if (upgraded && extended) {
mechanism.echoError("Cannot both upgrade and extend a potion");
extended = false;
}
if (color != null) {
((PotionMeta) meta).setColor(color.getColor());
}
((PotionMeta) meta).setBasePotionData(new PotionData(type, extended, upgraded));
((PotionMeta) meta).clearCustomEffects();
}
for (ObjectTag effectObj : data) {
PotionEffect effect;
if (effectObj.canBeType(MapTag.class)) {
effect = parseEffect(effectObj.asType(MapTag.class, mechanism.context), mechanism.context);
}
else {
effect = parseEffect(effectObj.toString(), mechanism.context);
}
if (effect != null) {
if (isPotion()) {
((PotionMeta) meta).addCustomEffect(effect, false);
}
else {
((SuspiciousStewMeta) meta).addCustomEffect(effect, false);
}
}
else {
mechanism.echoError("Invalid potion effect '" + effectObj + "'");
}
}
item.setItemMeta(meta);
}
}
}