-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
MaterialSwitchFace.java
143 lines (126 loc) · 4.89 KB
/
MaterialSwitchFace.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
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.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.BlockFace;
import org.bukkit.block.data.Directional;
import org.bukkit.block.data.FaceAttachable;
import org.bukkit.block.data.type.Switch;
public class MaterialSwitchFace implements Property {
public static boolean describes(ObjectTag material) {
// TODO: After 1.14 is dropped, remove Switch entirely
return material instanceof MaterialTag
&& ((MaterialTag) material).hasModernData()
&& (((MaterialTag) material).getModernData() instanceof Switch
|| (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_15) && ((MaterialTag) material).getModernData() instanceof FaceAttachable));
}
public static MaterialSwitchFace getFrom(ObjectTag _material) {
if (!describes(_material)) {
return null;
}
else {
return new MaterialSwitchFace((MaterialTag) _material);
}
}
public static final String[] handledMechs = new String[] {
"switch_face"
};
private MaterialSwitchFace(MaterialTag _material) {
material = _material;
}
MaterialTag material;
public static void registerTags() {
// <--[tag]
// @attribute <MaterialTag.switch_face>
// @returns ElementTag
// @mechanism MaterialTag.switch_face
// @group properties
// @description
// Returns the current attach direction for a switch or other attachable material.
// Output is "CEILING", "FLOOR", or "WALL".
// -->
PropertyParser.<MaterialSwitchFace, ElementTag>registerTag(ElementTag.class, "switch_face", (attribute, material) -> {
if (material.isSwitch()) {
return new ElementTag(material.getSwitch().getFace().name());
}
else {
return new ElementTag(material.getFaceAttachable().getAttachedFace().name());
}
});
}
public boolean isSwitch() {
return material.getModernData() instanceof Switch;
}
public Switch getSwitch() {
return (Switch) material.getModernData();
}
public FaceAttachable getFaceAttachable() {
return (FaceAttachable) material.getModernData();
}
@Override
public String getPropertyString() {
if (isSwitch()) {
return getSwitch().getFace().name();
}
else {
return getFaceAttachable().getAttachedFace().name();
}
}
public BlockFace getAttachedTo() {
if (isSwitch()) {
switch (getSwitch().getFace()) {
case WALL:
return getSwitch().getFacing().getOppositeFace();
case FLOOR:
return BlockFace.DOWN;
case CEILING:
return BlockFace.UP;
default:
return BlockFace.SELF;
}
}
else {
switch (getFaceAttachable().getAttachedFace()) {
case WALL:
if (material.getModernData() instanceof Directional) {
return ((Directional) material.getModernData()).getFacing().getOppositeFace();
}
case FLOOR:
return BlockFace.DOWN;
case CEILING:
return BlockFace.UP;
default:
return BlockFace.SELF;
}
}
}
@Override
public String getPropertyId() {
return "switch_face";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object MaterialTag
// @name switch_face
// @input ElementTag
// @description
// Sets the current attach direction for a switch or other attachable material.
// @tags
// <MaterialTag.switch_face>
// -->
if (mechanism.matches("switch_face") && mechanism.requireEnum(false, Switch.Face.values())) {
if (isSwitch()) {
getSwitch().setFace(Switch.Face.valueOf(mechanism.getValue().asString().toUpperCase()));
}
else {
getFaceAttachable().setAttachedFace(FaceAttachable.AttachedFace.valueOf(mechanism.getValue().asString().toUpperCase()));
}
}
}
}