/
ItemPotion.java
221 lines (199 loc) · 8.63 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
package net.aufdemrand.denizen.objects.properties.item;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Material;
import org.bukkit.potion.Potion;
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;
}
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().getDurability() == 0)
return null;
// World record winning stupidest necessary workaround for a Bukkit issue
if ((item.getItemStack().getDurability() & 0x40) != 0
&& PotionType.getByDamageValue(item.getItemStack().getDurability() & 0xF).isInstant()) {
item.getItemStack().setDurability((short)(item.getItemStack().getDurability() & ~0x40));
}
Potion pot = Potion.fromItemStack(item.getItemStack());
if (pot == null || pot.getType() == null)
return String.valueOf(item.getItemStack().getDurability());
return pot.getType().name() + "," + pot.getLevel() + "," + pot.hasExtendedDuration() + "," + pot.isSplash();
}
@Override
public String getPropertyId() {
return "potion";
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return "null";
// <--[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(item.getItemStack().getDurability() > 0)
.getAttribute(attribute.fulfill(1));
}
if (item.getItemStack().getDurability() > 0) {
if (attribute.startsWith("potion_effect")) {
attribute = attribute.fulfill(1);
// <--[tag]
// @attribute <i@item.potion_effect.is_splash>
// @returns Element(Boolean)
// @mechanism dItem.potion
// @group properties
// @description
// Returns whether the potion is a splash potion.
// -->
if (attribute.startsWith("is_splash")) {
return new Element(Potion.fromItemStack(item.getItemStack()).isSplash())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect.is_extended>
// @returns Element(Boolean)
// @mechanism dItem.potion
// @group properties
// @description
// Returns whether the potion has an extended duration.
// -->
if (attribute.startsWith("is_extended")) {
return new Element(Potion.fromItemStack(item.getItemStack()).hasExtendedDuration())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect.level>
// @returns Element(Number)
// @mechanism dItem.potion
// @group properties
// @description
// Returns the level of this potion.
// -->
if (attribute.startsWith("level")) {
return new Element(Potion.fromItemStack(item.getItemStack()).getLevel())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect.type>
// @returns Element
// @mechanism dItem.potion
// @group properties
// @description
// Returns the type name of this potion.
// -->
if (attribute.startsWith("type")) {
return new Element(Potion.fromItemStack(item.getItemStack()).getType().name())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect.data>
// @returns Element(Number)
// @mechanism dItem.potion
// @group properties
// @description
// Returns the 'damage value' of the potion, if normal potion tags don't work.
// -->
if (attribute.startsWith("data")) {
return new Element(item.getItemStack().getDurability())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.potion_effect>
// @returns Element
// @mechanism dItem.potion
// @group properties
// @description
// Returns the potion effect on this item.
// In the format Effect,Level,Extended,Splash
// -->
return new Element(getPropertyString())
.getAttribute(attribute);
}
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object dItem
// @name potion
// @input Element
// @description
// Sets the potion's custom potion effects.
// Input is a formed like: Effect,Level,Extended,Splash
// EG, speed,1,true,false
// Can also input a damage-value, EG '255'
// @tags
// <i@item.potion_effect>
// <i@item.potion_effect.level>
// <i@item.potion_effect.is_extended>
// <i@item.potion_effects.is_splash>
// -->
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());
}
}
}
}