-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
ItemPotion.java
419 lines (383 loc) · 18.1 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
package com.denizenscript.denizen.objects.properties.item;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
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.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Material;
import org.bukkit.inventory.meta.PotionMeta;
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(ObjectTag item) {
return item instanceof ItemTag
&& (((ItemTag) item).getBukkitMaterial() == Material.POTION
|| ((ItemTag) item).getBukkitMaterial() == Material.SPLASH_POTION
|| ((ItemTag) item).getBukkitMaterial() == Material.LINGERING_POTION
|| ((ItemTag) item).getBukkitMaterial() == Material.TIPPED_ARROW);
}
public static ItemPotion getFrom(ObjectTag _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemPotion((ItemTag) _item);
}
}
public static final String[] handledTags = new String[] {
"potion_base_type", "potion_base", "has_potion_effect", "potion_effect", "potion_effects"
};
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 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 NMSHandler.getItemHelper().getPotionEffect(type, duration, amplifier, ambient, particles, icon);
}
@Override
public String getPropertyString() {
if (!(item.getItemMeta() instanceof PotionMeta)) {
return null;
}
PotionMeta meta = (PotionMeta) item.getItemMeta();
ListTag effects = new ListTag();
effects.add(meta.getBasePotionData().getType()
+ "," + meta.getBasePotionData().isUpgraded()
+ "," + meta.getBasePotionData().isExtended()
+ (meta.hasColor() ? "," + new ColorTag(meta.getColor()).identify().replace(",", "&comma") : "")
);
for (PotionEffect pot : meta.getCustomEffects()) {
effects.add(stringifyEffect(pot));
}
return effects.identify();
}
@Override
public String getPropertyId() {
return "potion_effects";
}
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
boolean has = item.getItemMeta() instanceof PotionMeta
&& ((PotionMeta) item.getItemMeta()).hasCustomEffects();
// <--[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>.
// -->
if (attribute.startsWith("potion_base_type") && item.getItemMeta() instanceof PotionMeta) {
PotionMeta meta = ((PotionMeta) item.getItemMeta());
return new ElementTag(meta.getBasePotionData().getType().name()).getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.potion_base>
// @returns ElementTag
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns the potion type details for this potion item.
// In the format Type,Level,Extended,Splash,Color
// The type will be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionType.html>.
// -->
if (attribute.startsWith("potion_base") && item.getItemMeta() instanceof PotionMeta) {
PotionMeta meta = ((PotionMeta) item.getItemMeta());
return new ElementTag(meta.getBasePotionData().getType().name() + "," + (meta.getBasePotionData().isUpgraded() ? 2 : 1)
+ "," + meta.getBasePotionData().isExtended() + "," + (item.getBukkitMaterial() == Material.SPLASH_POTION)
+ (meta.hasColor() ? "," + new ColorTag(meta.getColor()).identify() : "")
).getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.potion_effects>
// @returns ListTag
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns the list of potion effects on this item.
// The effect type will be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>.
// -->
if (attribute.startsWith("potion_effects") && item.getItemMeta() instanceof PotionMeta) {
ListTag result = new ListTag();
PotionMeta meta = ((PotionMeta) item.getItemMeta());
for (PotionEffect pot : meta.getCustomEffects()) {
result.add(stringifyEffect(pot));
}
return result.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.has_potion_effect>
// @returns ElementTag(Boolean)
// @mechanism ItemTag.potion_effects
// @description
// Returns whether the potion has a potion effect.
// -->
if (attribute.startsWith("has_potion_effect")) {
return new ElementTag(has)
.getObjectAttribute(attribute.fulfill(1));
}
if (has) {
if (attribute.startsWith("potion_effect")) {
PotionMeta meta = ((PotionMeta) item.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 <ItemTag.potion_effect[<#>].is_splash>
// @returns ElementTag(Boolean)
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns whether the potion is a splash potion.
// -->
if (attribute.startsWith("is_splash")) {
return new ElementTag(item.getBukkitMaterial() == Material.SPLASH_POTION)
.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.potion_effect[<#>].is_extended>
// @returns ElementTag(Boolean)
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns whether the potion effect is extended.
// -->
if (attribute.startsWith("is_extended")) {
return new ElementTag(meta.getBasePotionData().isExtended())
.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.potion_effect[<#>].level>
// @returns ElementTag(Number)
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns the potion effect's level.
// -->
if (attribute.startsWith("level")) {
return new ElementTag(meta.getBasePotionData().isUpgraded() ? 2 : 1)
.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.potion_effect[<#>].is_ambient>
// @returns ElementTag(Boolean)
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns whether the potion effect is ambient.
// "Ambient" effects in vanilla came from a beacon, while non-ambient came from a potion.
// -->
if (attribute.startsWith("is_ambient")) {
return new ElementTag(meta.getCustomEffects().get(potN).isAmbient())
.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.potion_effect[<#>].icon>
// @returns ElementTag(Boolean)
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns whether the potion effect shows an icon.
// -->
if (attribute.startsWith("icon")) {
return new ElementTag(meta.getCustomEffects().get(potN).hasIcon()).getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.potion_effect[<#>].has_particles>
// @returns ElementTag(Boolean)
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns whether the potion effect has particles.
// -->
if (attribute.startsWith("has_particles")) {
return new ElementTag(meta.getCustomEffects().get(potN).hasParticles())
.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.potion_effect[<#>].duration>
// @returns ElementTag(Number)
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns the duration in ticks of the potion.
// -->
if (attribute.startsWith("duration")) {
return new ElementTag(meta.getCustomEffects().get(potN).getDuration())
.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.potion_effect[<#>].amplifier>
// @returns ElementTag(Number)
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns the amplifier level of the potion effect.
// -->
if (attribute.startsWith("amplifier")) {
return new ElementTag(meta.getCustomEffects().get(potN).getAmplifier())
.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.potion_effect[<#>].type>
// @returns ElementTag
// @mechanism ItemTag.potion_effects
// @group properties
// @description
// Returns the type of the potion effect.
// The effect type will be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>.
// -->
if (attribute.startsWith("type")) {
return new ElementTag(meta.getCustomEffects().get(potN).getType().getName())
.getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("data")) {
return new ElementTag(0)
.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.potion_effect[<#>]>
// @returns ElementTag
// @mechanism ItemTag.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 ElementTag(meta.getBasePotionData().getType().name() + "," + (meta.getBasePotionData().isUpgraded() ? 2 : 1)
+ "," + meta.getBasePotionData().isExtended() + "," + (item.getBukkitMaterial() == Material.SPLASH_POTION))
.getObjectAttribute(attribute);
}
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ItemTag
// @name potion_effects
// @input ListTag
// @description
// Sets the potion's potion effect(s).
// Input is a formed like: Type,Upgraded,Extended(,Color)|Effect,Amplifier,Duration,Ambient,Particles,Icon|...
// For example: SPEED,true,false|SPEED,2,200,false,true,true
// Second example: REGEN,false,true,RED|REGENERATION,1,500,true,false,false
// Color can also be used like "255&comma128&comma0" (r,g,b but replace ',' with '&comma').
// The primary type must be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionType.html>.
// The effect type must be from <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/potion/PotionEffectType.html>.
// @tags
// <ItemTag.potion_effect[<#>]>
// <ItemTag.potion_effect[<#>].type>
// <ItemTag.potion_effect[<#>].duration>
// <ItemTag.potion_effect[<#>].amplifier>
// <ItemTag.potion_effect[<#>].is_ambient>
// <ItemTag.potion_effect[<#>].has_particles>
// <ItemTag.potion_effect[<#>].icon>
// <server.potion_types>
// <server.potion_effect_types>
// -->
if (mechanism.matches("potion_effects")) {
ListTag data = mechanism.valueAsType(ListTag.class);
String[] d1 = data.get(0).split(",");
PotionMeta meta = (PotionMeta) item.getItemMeta();
PotionType type;
try {
type = PotionType.valueOf(d1[0].toUpperCase());
}
catch (IllegalArgumentException ex) {
mechanism.echoError("Invalid potion type name '" + d1[0] + "'");
return;
}
boolean upgraded = CoreUtilities.equalsIgnoreCase(d1[1], "true");
boolean extended = CoreUtilities.equalsIgnoreCase(d1[2], "true");
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() + "'");
upgraded = false;
}
if (upgraded && extended) {
mechanism.echoError("Cannot both upgrade and extend a potion");
extended = false;
}
meta.setBasePotionData(new PotionData(type, extended, upgraded));
if (d1.length > 3) {
ColorTag color = ColorTag.valueOf(d1[3].replace("&comma", ","), mechanism.context);
if (color == null) {
mechanism.echoError("Invalid ColorTag input '" + d1[3] + "'");
}
meta.setColor(color.getColor());
}
meta.clearCustomEffects();
for (int i = 1; i < data.size(); i++) {
PotionEffect effect = parseEffect(data.get(i), mechanism.context);
if (effect == null) {
mechanism.echoError("Invalid potion effect '" + data.get(i) + "'");
continue;
}
meta.addCustomEffect(effect, false);
}
item.setItemMeta(meta);
}
}
}