/
MaterialDirectional.java
272 lines (247 loc) · 9.22 KB
/
MaterialDirectional.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
package com.denizenscript.denizen.objects.properties.material;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.utilities.debugging.Debug;
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.objects.properties.PropertyParser;
import org.bukkit.Axis;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.*;
import org.bukkit.util.Vector;
public class MaterialDirectional 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().data;
if (!(data instanceof Directional || data instanceof Orientable || data instanceof Rotatable || data instanceof Rail)) {
return false;
}
return true;
}
public static MaterialDirectional getFrom(ObjectTag _material) {
if (!describes(_material)) {
return null;
}
else {
return new MaterialDirectional((MaterialTag) _material);
}
}
public static final String[] handledMechs = new String[] {
"direction"
};
private MaterialDirectional(MaterialTag _material) {
material = _material;
}
MaterialTag material;
public static void registerTags() {
// <--[tag]
// @attribute <MaterialTag.valid_directions>
// @returns ListTag
// @mechanism MaterialTag.direction
// @group properties
// @description
// Returns a list of directions that are valid for a directional material.
// See also <@link tag MaterialTag.direction>
// -->
PropertyParser.<MaterialDirectional>registerTag("valid_directions", (attribute, material) -> {
ListTag toReturn = new ListTag();
if (material.isOrientable()) {
for (Axis axis : material.getOrientable().getAxes()) {
toReturn.add(axis.name());
}
}
else if (material.isRail()) {
for (Rail.Shape shape : material.getRail().getShapes()) {
toReturn.add(shape.name());
}
}
else if (material.isDirectional()) {
for (BlockFace face : material.getDirectional().getFaces()) {
toReturn.add(face.name());
}
}
else {
return null;
}
return toReturn;
});
// <--[tag]
// @attribute <MaterialTag.direction>
// @returns ElementTag
// @mechanism MaterialTag.direction
// @group properties
// @description
// Returns the current facing direction for a directional material (like a door or a bed).
// Output is a direction name like "NORTH", or an axis like "X", or a rail direction like "ASCENDING_NORTH".
// -->
PropertyParser.<MaterialDirectional>registerTag("direction", (attribute, material) -> {
return new ElementTag(material.getDirectionName());
});
}
public Vector getDirectionVector() {
if (isOrientable()) {
switch (getOrientable().getAxis()) {
case X:
return new Vector(1, 0, 0);
case Y:
return new Vector(0, 1, 0);
default:
return new Vector(0, 0, 1);
}
}
else if (isRotatable()) {
return getRotatable().getRotation().getDirection();
}
else if (isRail()) {
switch (getRail().getShape()) {
case ASCENDING_EAST:
return new Vector(1, 1, 0);
case ASCENDING_NORTH:
return new Vector(0, 1, -1);
case ASCENDING_SOUTH:
return new Vector(0, 1, 1);
case ASCENDING_WEST:
return new Vector(-1, 1, 0);
case EAST_WEST:
return new Vector(1, 0, 0);
case NORTH_EAST:
return new Vector(1, 0, -1);
case NORTH_SOUTH:
return new Vector(0, 0, 1);
case NORTH_WEST:
return new Vector(-1, 0, -1);
case SOUTH_EAST:
return new Vector(1, 0, 1);
case SOUTH_WEST:
return new Vector(-1, 0, 1);
}
return null; // Unreachable.
}
else {
return getDirectional().getFacing().getDirection();
}
}
public String getDirectionName() {
if (isOrientable()) {
return getOrientable().getAxis().name();
}
else if (isRotatable()) {
return getRotatable().getRotation().name();
}
else if (isRail()) {
return getRail().getShape().name();
}
else {
return getDirectional().getFacing().name();
}
}
public boolean isOrientable() {
return material.getModernData().data instanceof Orientable;
}
public boolean isRotatable() {
return material.getModernData().data instanceof Rotatable;
}
public boolean isDirectional() {
return material.getModernData().data instanceof Directional;
}
public boolean isRail() {
return material.getModernData().data instanceof Rail;
}
public Orientable getOrientable() {
return (Orientable) material.getModernData().data;
}
public Rotatable getRotatable() {
return (Rotatable) material.getModernData().data;
}
public Directional getDirectional() {
return (Directional) material.getModernData().data;
}
public Rail getRail() {
return (Rail) material.getModernData().data;
}
public void setFacing(BlockFace face) {
if (isOrientable()) {
Axis axis;
Vector vec = face.getDirection();
if (vec.getX() >= 0.5) {
axis = Axis.X;
}
else if (vec.getY() >= 0.5) {
axis = Axis.Y;
}
else {
axis = Axis.Z;
}
getOrientable().setAxis(axis);
}
else if (isRotatable()) {
getRotatable().setRotation(face);
}
else if (isRail()) {
switch (face) {
case EAST:
case WEST:
getRail().setShape(Rail.Shape.EAST_WEST);
case NORTH:
case SOUTH:
getRail().setShape(Rail.Shape.NORTH_SOUTH);
case NORTH_EAST:
getRail().setShape(Rail.Shape.NORTH_EAST);
case NORTH_WEST:
getRail().setShape(Rail.Shape.NORTH_WEST);
case SOUTH_EAST:
getRail().setShape(Rail.Shape.SOUTH_EAST);
case SOUTH_WEST:
getRail().setShape(Rail.Shape.SOUTH_WEST);
default:
Debug.echoError("Unsupported rail direction '" + face + "'.");
}
}
else if (isDirectional()) {
getDirectional().setFacing(face);
}
}
@Override
public String getPropertyString() {
return getDirectionName();
}
@Override
public String getPropertyId() {
return "direction";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object MaterialTag
// @name direction
// @input ElementTag
// @description
// Sets the current facing direction for a directional material (like a door or a bed).
// @tags
// <MaterialTag.direction>
// <MaterialTag.valid_directions>
// -->
if (mechanism.matches("direction")) {
if (isOrientable() && mechanism.requireEnum(false, Axis.values())) {
getOrientable().setAxis(Axis.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (isRail() && mechanism.requireEnum(false, Rail.Shape.values())) {
getRail().setShape(Rail.Shape.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else if (mechanism.requireEnum(false, BlockFace.values())) {
setFacing(BlockFace.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else {
mechanism.echoError("MaterialTag.Direction mechanism has bad input: directional value '" + mechanism.getValue().asString() + "' is invalid.");
}
}
}
}