/
ItemPotion.java
318 lines (286 loc) · 13.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
package net.aufdemrand.denizen.objects.properties.item;
import net.aufdemrand.denizen.objects.dColor;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.core.EscapeTags;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Material;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.*;
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);
}
public static ItemPotion getFrom(dObject _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemPotion((dItem) _item);
}
}
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());
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()).append(",");
if (pot.getColor() != null) {
sb.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.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);
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
// @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));
}
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.
// -->
if (attribute.startsWith("color")) {
return new dColor(meta.getCustomEffects().get(potN).getColor())
.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[<#>].anplifier>
// @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|Type,Amplifier,Duration,Ambient,Particles(,Color)|...
// For example: SPEED,true,false|SPEED,2,200,false,true,red
// @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>
// -->
if (mechanism.matches("potion_effects")) {
dList data = mechanism.getValue().asType(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")));
meta.clearCustomEffects();
for (int i = 1; i < data.size(); i++) {
String[] d2 = data.get(i).split(",");
meta.addCustomEffect(new PotionEffect(PotionEffectType.getByName(d2[0].toUpperCase()),
new Element(d2[2]).asInt(), new Element(d2[1]).asInt(), new Element(d2[3]).asBoolean(),
new Element(d2[4]).asBoolean(), d2.length >= 5 ? dColor.valueOf(d2[5].replace("&comma", ",")).getColor(): null), 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 (type == null) {
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());
}
}
}
}