-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
CustomObject.java
185 lines (156 loc) · 5.68 KB
/
CustomObject.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
package net.aufdemrand.denizencore.objects;
import net.aufdemrand.denizencore.scripts.ScriptRegistry;
import net.aufdemrand.denizencore.scripts.commands.core.DetermineCommand;
import net.aufdemrand.denizencore.scripts.containers.ScriptContainer;
import net.aufdemrand.denizencore.scripts.containers.core.CustomScriptContainer;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import java.util.Map;
import java.util.regex.Matcher;
public class CustomObject implements dObject, dObject.ObjectAttributable, Adjustable {
@Fetchable("custom")
public static CustomObject valueOf(String string, TagContext context) {
Matcher m;
///////
// Handle objects with properties through the object fetcher
m = ObjectFetcher.DESCRIBED_PATTERN.matcher(string);
if (m.matches()) {
return ObjectFetcher.getObjectFrom(CustomObject.class, string, context);
}
if (string.startsWith("custom@")) {
string = string.substring("custom@".length());
}
String typeData = string;
ScriptContainer sc = ScriptRegistry.getScriptContainer(typeData);
if (sc == null) {
if (context == null || context.debug) {
dB.echoError("Null script container for " + typeData);
}
return null;
}
if (!(sc instanceof CustomScriptContainer)) {
if (context == null || context.debug) {
dB.echoError("Wrong-typed script container for " + typeData);
}
return null;
}
return new CustomObject((CustomScriptContainer) sc, ((CustomScriptContainer) sc).getVars());
}
public static CustomObject getFor(dObject obj, TagContext context) {
return obj instanceof CustomObject ? (CustomObject) obj : valueOf(obj.toString(), context);
}
public static boolean matches(String string) {
return string.startsWith("custom@");
}
public CustomScriptContainer container;
public Map<String, dObject> vars;
public CustomObject(CustomScriptContainer type, Map<String, dObject> values) {
container = type;
vars = values;
}
private String prefix = "Custom";
@Override
public String getObjectType() {
return "Custom";
}
@Override
public String identify() {
StringBuilder outp = new StringBuilder();
for (Map.Entry<String, dObject> var : vars.entrySet()) {
outp.append(var.getKey() + "=" + var.getValue().toString().replace(';', (char) 0x2011) + ";");
}
return "custom@" + container.getName() + "[" + (outp.length() > 0 ? outp.substring(0, outp.length() - 1) : "") + "]";
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public dObject setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String debug() {
return String.format("<G>%s='<A>%s<G>' ", prefix, identify());
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String getAttribute(Attribute attribute) {
return CoreUtilities.stringifyNullPass(getObjectAttribute(attribute));
}
@Override
public <T extends dObject> T asObjectType(Class<T> type, TagContext context) {
return null;
}
@Override
public boolean canPossiblyBeType(Class<? extends dObject> type) {
if (type == CustomObject.class) {
return true;
}
return false;
}
@Override
public dObject getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
if (attribute.isComplete()) {
return this;
}
dObject res = vars.get(attribute.getAttribute(1));
if (res == null) {
String taggo = attribute.getAttributeWithoutContext(1);
if (container.hasPath("tags." + taggo)) {
long ID = container.runTagScript(taggo, attribute.getContextObject(1), this,
attribute.getScriptEntry() != null ? attribute.getScriptEntry().entryData : null);
dList outcomes = DetermineCommand.getOutcome(ID);
if (outcomes == null) {
return null;
}
return CoreUtilities.autoAttribTyped(outcomes.getObject(0), attribute.fulfill(1));
}
return new Element(identify()).getObjectAttribute(attribute);
}
return CoreUtilities.autoAttribTyped(res, attribute.fulfill(1));
}
@Override
public void applyProperty(Mechanism mechanism) {
adjust(mechanism);
}
@Override
public void adjust(Mechanism mechanism) {
String name = CoreUtilities.toLowerCase(mechanism.getName());
if (!mechanism.hasValue()) {
vars.remove(name);
return;
}
dObject value = mechanism.getValue();
if (container.hasPath("mechanisms." + name)) {
long ID = container.runMechScript(name, this, value);
dList outcomes = DetermineCommand.getOutcome(ID);
if (outcomes == null) {
return;
}
CustomObject co = CustomObject.getFor(outcomes.getObject(0), null);
container = co.container;
vars = co.vars;
}
else {
vars.put(name, value);
}
}
}