/
ItemPotion.java
417 lines (380 loc) · 18.4 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
package com.denizenscript.denizen.objects.properties.item;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.ColorTag;
import com.denizenscript.denizen.objects.ItemTag;
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.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(ObjectTag item) {
return item instanceof ItemTag
&& (((ItemTag) item).getItemStack().getType() == Material.POTION
|| ((ItemTag) item).getItemStack().getType() == Material.SPLASH_POTION
|| ((ItemTag) item).getItemStack().getType() == Material.LINGERING_POTION
|| ((ItemTag) item).getItemStack().getType() == 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"
};
public static final String[] handledMechs = new String[] {
"potion_effects"
};
private ItemPotion(ItemTag item) {
this.item = item;
}
ItemTag item;
public static String stringifyEffect(PotionEffect effect) {
StringBuilder sb = new StringBuilder();
sb.append(effect.getType().getName()).append(",")
.append(effect.getAmplifier()).append(",")
.append(effect.getDuration()).append(",")
.append(effect.isAmbient()).append(",")
.append(effect.hasParticles());
return sb.toString();
}
public static PotionEffect parseEffect(String str) {
String[] d2 = str.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 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();
}
Color color = null;
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, color, icon);
}
@Override
public String getPropertyString() {
if (!item.getItemStack().hasItemMeta()) {
return null;
}
if (!(item.getItemStack().getItemMeta() instanceof PotionMeta)) {
return null;
}
PotionMeta meta = (PotionMeta) item.getItemStack().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.getItemStack().hasItemMeta() && item.getItemStack().getItemMeta() instanceof PotionMeta
&& ((PotionMeta) item.getItemStack().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.getItemStack().hasItemMeta() && item.getItemStack().getItemMeta() instanceof PotionMeta) {
PotionMeta meta = ((PotionMeta) item.getItemStack().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.getItemStack().hasItemMeta() && item.getItemStack().getItemMeta() instanceof PotionMeta) {
PotionMeta meta = ((PotionMeta) item.getItemStack().getItemMeta());
return new ElementTag(meta.getBasePotionData().getType().name() + "," + (meta.getBasePotionData().isUpgraded() ? 2 : 1)
+ "," + meta.getBasePotionData().isExtended() + "," + (item.getItemStack().getType() == Material.SPLASH_POTION)
+ (meta.hasColor() ? "," + new ColorTag(meta.getColor()).identify() : "")
).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.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 <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.getItemStack().getType() == 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.
// -->
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.getItemStack().getType() == 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').
// NOTE: In pre-1.13 Minecraft versions, you could set a color in the custom effects list instead of "icon".
// 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[<#>].color>
// <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.getItemStack().getItemMeta();
meta.setBasePotionData(new PotionData(PotionType.valueOf(d1[0].toUpperCase()),
CoreUtilities.equalsIgnoreCase(d1[2], "true"),
CoreUtilities.equalsIgnoreCase(d1[1], "true")));
if (d1.length > 3) {
meta.setColor(ColorTag.valueOf(d1[3].replace("&comma", ","), mechanism.context).getColor());
}
meta.clearCustomEffects();
for (int i = 1; i < data.size(); i++) {
meta.addCustomEffect(parseEffect(data.get(i)), 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 {
Debug.echoError("Invalid effect format, use name,amplifier,extended,splash.");
}
}
else {
ElementTag data1 = new ElementTag(data[1]);
ElementTag data2 = new ElementTag(data[2]);
ElementTag data3 = new ElementTag(data[3]);
PotionType type;
try {
type = PotionType.valueOf(data[0].toUpperCase());
}
catch (Exception ex) {
Debug.echoError("Invalid potion effect type '" + data[0] + "'");
return;
}
if (!data1.isInt()) {
Debug.echoError("Cannot apply effect '" + data[0] + "': '" + data[1] + "' is not a valid integer!");
return;
}
if (!data2.isBoolean()) {
Debug.echoError("Cannot apply effect '" + data[0] + "': '" + data[2] + "' is not a valid boolean!");
return;
}
if (!data3.isBoolean()) {
Debug.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());
}
}
}
}