/
Mechanism.java
180 lines (150 loc) · 4.97 KB
/
Mechanism.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
package com.denizenscript.denizencore.objects;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.tags.TagContext;
public class Mechanism {
private boolean fulfilled;
private String raw_mechanism;
public ObjectTag value;
public ObjectTag adjusting;
public TagContext context;
public boolean isProperty = false;
public Mechanism(String mechanism, ObjectTag value, TagContext context) {
fulfilled = false;
raw_mechanism = CoreUtilities.toLowerCase(mechanism);
this.value = value;
this.context = context;
}
public void fulfill() {
fulfilled = true;
}
public boolean fulfilled() {
return fulfilled;
}
public String getName() {
return raw_mechanism;
}
public ElementTag getValue() {
if (value == null) {
return new ElementTag("");
}
return value.asElement();
}
public <T extends ObjectTag> T valueAsType(Class<T> dClass) {
return getValue().asType(dClass, context);
}
public boolean hasValue() {
if (value == null) {
return false;
}
if (value instanceof ElementTag && ((ElementTag) value).asString().isEmpty()) {
return false;
}
return true;
}
public boolean matches(String string) {
if (string.equals(raw_mechanism)) {
fulfill();
return true;
}
return false;
}
public String forMechanismText() {
String text = " <LR>For mechanism '<A>" + raw_mechanism + "<LR>'" + (value == null ? "" : " with value '<A>" + value + "<LR>'");
if (adjusting == null) {
return text;
}
return text + ", while " + (isProperty ? "applying properties to" : "adjusting") + " object '<A>" + adjusting.debuggable() + "<LR>'";
}
public boolean requireBoolean() {
return requireBoolean("Invalid boolean. Must specify TRUE or FALSE.");
}
public boolean requireDouble() {
return requireDouble("Invalid decimal number specified.");
}
public boolean requireEnum(Class<? extends Enum> clazz) {
return requireEnum(null, clazz);
}
public boolean requireFloat() {
return requireFloat("Invalid decimal number specified.");
}
public boolean requireInteger() {
return requireInteger("Invalid integer number specified.");
}
public <T extends ObjectTag> boolean requireObject(Class<T> type) {
return requireObject(null, type);
}
public boolean requireBoolean(String error) {
if (hasValue() && getValue().isBoolean()) {
return true;
}
echoError(error);
return false;
}
public boolean requireDouble(String error) {
if (hasValue() && getValue().isDouble()) {
return true;
}
echoError(error);
return false;
}
public boolean requireEnum(String error, Class<? extends Enum> clazz) {
if (!hasValue()) {
return false;
}
ElementTag value = getValue();
if (value.matchesEnum(clazz)) {
return true;
}
if (error == null) {
echoError("Invalid " + clazz.getSimpleName() + ". Must specify a valid name.");
}
else {
echoError(error);
}
return false;
}
public boolean requireFloat(String error) {
if (hasValue() && getValue().isFloat()) {
return true;
}
echoError(error);
return false;
}
public boolean requireInteger(String error) {
if (hasValue() && getValue().isInt()) {
return true;
}
echoError(error);
return false;
}
public <T extends ObjectTag> boolean requireObject(String error, Class<T> type) {
if (hasValue() && value.canBeType(type)) {
return true;
}
if (error == null) {
// TODO: Remove getSimpleName(), or simplify somehow.
echoError("Invalid " + type.getSimpleName() + " specified.");
}
else {
echoError(error);
}
return false;
}
public boolean shouldDebug() {
return context == null || context.debug;
}
public void echoError(String error) {
// TODO: Consider special cases of whether object properties with debug off should even show errors
Debug.echoError(context, forMechanismText(), error);
}
public void reportInvalid() {
echoError("Invalid mechanism specified: " + raw_mechanism);
}
public void autoReport() {
if (!fulfilled()) {
reportInvalid();
}
}
}