-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
MaterialLevel.java
227 lines (199 loc) · 7.37 KB
/
MaterialLevel.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
package com.denizenscript.denizen.objects.properties.material;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Levelled;
import org.bukkit.block.data.type.Cake;
import org.bukkit.block.data.type.Beehive;
import org.bukkit.block.data.type.Farmland;
import org.bukkit.block.data.type.Snow;
public class MaterialLevel implements Property {
public static boolean describes(ObjectTag material) {
if (!(material instanceof MaterialTag)) {
return false;
}
MaterialTag mat = (MaterialTag) material;
if (!mat.hasModernData()) {
return false;
}
BlockData data = mat.getModernData();
return data instanceof Levelled
|| data instanceof Cake
|| data instanceof Snow
|| data instanceof Farmland
|| data instanceof Beehive;
}
public static MaterialLevel getFrom(ObjectTag _material) {
if (!describes(_material)) {
return null;
}
else {
return new MaterialLevel((MaterialTag) _material);
}
}
public static final String[] handledMechs = new String[] {
"level"
};
public MaterialLevel(MaterialTag _material) {
material = _material;
}
MaterialTag material;
public static void register() {
// <--[tag]
// @attribute <MaterialTag.maximum_level>
// @returns ElementTag(Number)
// @group properties
// @description
// Returns the maximum level for a Levelled material (like water, lava, and cauldrons), cake, beehives, snow, or farmland.
// -->
PropertyParser.registerStaticTag(MaterialLevel.class, ElementTag.class, "maximum_level", (attribute, material) -> {
return new ElementTag(material.getMax());
});
// <--[tag]
// @attribute <MaterialTag.minimum_level>
// @returns ElementTag(Number)
// @group properties
// @description
// Returns the minimum level for a Levelled material (like water, lava, and cauldrons), cake, beehives, snow, or farmland.
// This will return 0 for all valid materials aside from snow.
// -->
PropertyParser.registerStaticTag(MaterialLevel.class, ElementTag.class, "minimum_level", (attribute, material) -> {
return new ElementTag(material.getMin());
});
// <--[tag]
// @attribute <MaterialTag.level>
// @returns ElementTag(Number)
// @mechanism MaterialTag.level
// @group properties
// @description
// Returns the current level for a Levelled material, cake, beehives, snow, or farmland.
// "Levelled" materials include: water, lava, cauldrons, composters, light blocks, and any other future Levelled implementing types.
// For light blocks, this is the brightness of the light.
// For water/lava this is the height of the liquid block.
// For cauldrons, this is the amount of liquid contained.
// For cake, this is the number of bites left.
// For beehives/bee nests, this is the amount of honey contained.
// For snow, this is the number of partial layers, or the height, of a snow block.
// For farmland, this is the moisture level.
// -->
PropertyParser.registerStaticTag(MaterialLevel.class, ElementTag.class, "level", (attribute, material) -> {
return new ElementTag(material.getCurrent());
});
}
public Levelled getLevelled() {
return (Levelled) material.getModernData();
}
public boolean isCake() {
return material.getModernData() instanceof Cake;
}
public Cake getCake() {
return (Cake) material.getModernData();
}
public boolean isSnow() {
return material.getModernData() instanceof Snow;
}
public Snow getSnow() {
return (Snow) material.getModernData();
}
public boolean isHive() {
return material.getModernData() instanceof Beehive;
}
public Beehive getHive() {
return (Beehive) material.getModernData();
}
public boolean isFarmland() {
return material.getModernData() instanceof Farmland;
}
public Farmland getFarmland() {
return (Farmland) material.getModernData();
}
public int getCurrent() {
if (isCake()) {
return getCake().getBites();
}
else if (isSnow()) {
return getSnow().getLayers();
}
else if (isHive()) {
return getHive().getHoneyLevel();
}
else if (isFarmland()) {
return getFarmland().getMoisture();
}
return getLevelled().getLevel();
}
public int getMax() {
if (isCake()) {
return getCake().getMaximumBites();
}
else if (isSnow()) {
return getSnow().getMaximumLayers();
}
else if (isHive()) {
return getHive().getMaximumHoneyLevel();
}
else if (isFarmland()) {
return getFarmland().getMaximumMoisture();
}
return getLevelled().getMaximumLevel();
}
public int getMin() {
if (isSnow()) {
return getSnow().getMinimumLayers();
}
return 0;
}
public void setCurrent(int level) {
if (isCake()) {
getCake().setBites(level);
return;
}
else if (isSnow()) {
getSnow().setLayers(level);
return;
}
else if (isHive()) {
getHive().setHoneyLevel(level);
return;
}
else if (isFarmland()) {
getFarmland().setMoisture(level);
return;
}
getLevelled().setLevel(level);
}
@Override
public String getPropertyString() {
return String.valueOf(getCurrent());
}
@Override
public String getPropertyId() {
return "level";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object MaterialTag
// @name level
// @input ElementTag(Number)
// @description
// Sets the current level for a Levelled material (like water, lava, and cauldrons), cake, beehives, snow, or farmland.
// @tags
// <MaterialTag.level>
// <MaterialTag.maximum_level>
// <MaterialTag.minimum_level>
// -->
if (mechanism.matches("level") && mechanism.requireInteger()) {
int level = mechanism.getValue().asInt();
if (level < getMin() || level > getMax()) {
mechanism.echoError("Level value '" + level + "' is not valid. Must be between " + getMin() + " and " + getMax() + " for material '" + material.name() + "'.");
return;
}
setCurrent(level);
}
}
}