/
MaterialCount.java
178 lines (156 loc) · 6.19 KB
/
MaterialCount.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
package com.denizenscript.denizen.objects.properties.material;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.block.data.type.RespawnAnchor;
import org.bukkit.block.data.type.SeaPickle;
import org.bukkit.block.data.type.TurtleEgg;
public class MaterialCount implements Property {
public static boolean describes(ObjectTag material) {
return material instanceof MaterialTag
&& ((MaterialTag) material).hasModernData()
&& (((MaterialTag) material).getModernData().data instanceof SeaPickle
|| ((MaterialTag) material).getModernData().data instanceof TurtleEgg
|| (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_16)
&& ((MaterialTag) material).getModernData().data instanceof RespawnAnchor));
}
public static MaterialCount getFrom(ObjectTag _material) {
if (!describes(_material)) {
return null;
}
else {
return new MaterialCount((MaterialTag) _material);
}
}
public static final String[] handledMechs = new String[] {
"count", "pickle_count"
};
private MaterialCount(MaterialTag _material) {
material = _material;
}
MaterialTag material;
public static void registerTags() {
// <--[tag]
// @attribute <MaterialTag.count>
// @returns ElementTag(Number)
// @mechanism MaterialTag.count
// @group properties
// @description
// Returns the amount of pickles in a Sea Pickle material, eggs in a Turtle Egg material, or charges in a Respawn Anchor material.
// -->
PropertyParser.<MaterialCount>registerTag("count", (attribute, material) -> {
return new ElementTag(material.getCurrent());
}, "pickle_count");
// <--[tag]
// @attribute <MaterialTag.count_max>
// @returns ElementTag(Number)
// @mechanism MaterialTag.count
// @group properties
// @description
// Returns the maximum amount of pickles allowed in a Sea Pickle material, eggs in a Turtle Egg material, or charges in a Respawn Anchor material.
// -->
PropertyParser.<MaterialCount>registerTag("count_max", (attribute, material) -> {
return new ElementTag(material.getMax());
}, "pickle_max");
// <--[tag]
// @attribute <MaterialTag.count_min>
// @returns ElementTag(Number)
// @mechanism MaterialTag.count
// @group properties
// @description
// Returns the minimum amount of pickles allowed in a Sea Pickle material, eggs in a Turtle Egg material, or charges in a Respawn Anchor material.
// -->
PropertyParser.<MaterialCount>registerTag("count_min", (attribute, material) -> {
return new ElementTag(material.getMin());
}, "pickle_min");
}
public boolean isSeaPickle() {
return material.getModernData().data instanceof SeaPickle;
}
public boolean isTurtleEgg() {
return material.getModernData().data instanceof TurtleEgg;
}
public TurtleEgg getTurtleEgg() {
return (TurtleEgg) material.getModernData().data;
}
public SeaPickle getSeaPickle() {
return (SeaPickle) material.getModernData().data;
}
public int getCurrent() {
if (isSeaPickle()) {
return getSeaPickle().getPickles();
}
else if (isTurtleEgg()) {
return getTurtleEgg().getEggs();
}
else {
return ((RespawnAnchor) material.getModernData().data).getCharges();
}
}
public int getMax() {
if (isSeaPickle()) {
return getSeaPickle().getMaximumPickles();
}
else if (isTurtleEgg()) {
return getTurtleEgg().getMaximumEggs();
}
else {
return ((RespawnAnchor) material.getModernData().data).getMaximumCharges();
}
}
public int getMin() {
if (isSeaPickle()) {
return getSeaPickle().getMinimumPickles();
}
else if (isTurtleEgg()) {
return getTurtleEgg().getMinimumEggs();
}
else { // Respawn anchor
return 0;
}
}
@Override
public String getPropertyString() {
return String.valueOf(getCurrent());
}
@Override
public String getPropertyId() {
return "count";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object MaterialTag
// @name count
// @input ElementTag(Number)
// @description
// Sets the amount of pickles in a Sea Pickle material, eggs in a Turtle Egg material, or charges in a Respawn Anchor material.
// @tags
// <MaterialTag.count>
// <MaterialTag.count_min>
// <MaterialTag.count_max>
// -->
if ((mechanism.matches("count") || (mechanism.matches("pickle_count"))) && mechanism.requireInteger()) {
int count = mechanism.getValue().asInt();
if (count < getMin() || count > getMax()) {
Debug.echoError("Material count mechanism value '" + count + "' is not valid. Must be between " + getMin() + " and " + getMax() + ".");
return;
}
if (isSeaPickle()) {
getSeaPickle().setPickles(count);
}
else if (isTurtleEgg()) {
getTurtleEgg().setEggs(count);
}
else {
((RespawnAnchor) material.getModernData().data).setCharges(count);
}
}
}
}