/
CustomObjectTag.java
186 lines (159 loc) · 6.21 KB
/
CustomObjectTag.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
package com.denizenscript.denizencore.objects.core;
import 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.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.core.EscapeTagBase;
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 CustomObjectTag implements ObjectTag, 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 (ListTag, PlayerTags, 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 CustomObjectTag 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(CustomObjectTag.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 CustomObjectTag((CustomScriptContainer) sc, ((CustomScriptContainer) sc).getVars());
}
public static CustomObjectTag getFor(ObjectTag obj, TagContext context) {
return obj instanceof CustomObjectTag ? (CustomObjectTag) obj : valueOf(obj.toString(), context);
}
public static boolean matches(String string) {
return string.startsWith("custom@");
}
public CustomScriptContainer container;
public Map<String, ObjectTag> vars;
public CustomObjectTag(CustomScriptContainer type, Map<String, ObjectTag> 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, ObjectTag> var : vars.entrySet()) {
outp.append(var.getKey()).append("=").append(EscapeTagBase.escape(var.getValue().toString())).append(";");
}
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 ObjectTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public boolean isUnique() {
return false;
}
public static ObjectTagProcessor<CustomObjectTag> tagProcessor = new ObjectTagProcessor<>();
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
if (attribute.isComplete()) {
return this;
}
ObjectTag res = vars.get(attribute.getAttribute(1));
if (res != null) {
return CoreUtilities.autoAttribTyped(res, attribute.fulfill(1));
}
String taggo = attribute.getAttributeWithoutContext(1);
if (container.hasPath("tags." + taggo)) {
ListTag 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 tagProcessor.getObjectAttribute(this, attribute);
}
@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;
}
ObjectTag value = mechanism.getValue();
if (container.hasPath("mechanisms." + name)) {
ListTag outcomes = container.runMechScript(name, this, value);
if (outcomes == null) {
return;
}
CustomObjectTag co = CustomObjectTag.getFor(outcomes.getObject(0), null);
container = co.container;
vars = co.vars;
}
else {
vars.put(name, value);
}
mechanism.fulfill();
}
}