/
CustomObject.java
191 lines (163 loc) · 6.22 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
186
187
188
189
190
191
package com.denizenscript.denizencore.objects;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.scripts.containers.core.CustomScriptContainer;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.TagContext;
import java.util.Map;
import java.util.regex.Matcher;
public class CustomObject implements dObject, dObject.ObjectAttributable, Adjustable {
// <--[language]
// @name Custom Objects
// @group Object System
// @description
// Custom objects are custom object types. They use a script basis to create an object
// similar to the base object types (dLists, dPlayers, etc).
//
// Usage of these should generally be avoided, as they can be considered 'over-engineering'...
// That is, using a very complicated solution to solve a problem that can be solved much more simply.
//
// -->
// <--[language]
// @name custom@
// @group Object Fetcher System
// @description
// custom@ refers to the 'object identifier' of a Custom Object. The 'custom@' is notation for Denizen's Object
// Fetcher. The constructor for an Custom Object is the name of the custom script, with any relevant properties specified.
// -->
@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) {
Debug.echoError("Null script container for " + typeData);
}
return null;
}
if (!(sc instanceof CustomScriptContainer)) {
if (context == null || context.debug) {
Debug.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 "custom@" + container.getName();
}
@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 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 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)) {
dList outcomes = container.runTagScript(taggo, attribute.getContextObject(1), this,
attribute.getScriptEntry() != null ? attribute.getScriptEntry().entryData :
DenizenCore.getImplementation().getEmptyScriptEntryData());
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)) {
dList outcomes = container.runMechScript(name, this, value);
if (outcomes == null) {
return;
}
CustomObject co = CustomObject.getFor(outcomes.getObject(0), null);
container = co.container;
vars = co.vars;
}
else {
vars.put(name, value);
}
mechanism.fulfill();
}
}