Skip to content

Commit ef76864

Browse files
authored
Add <proc.context_map> tag (#109)
* add proc.context_map tag * format fixes * cleanup * fix * rename to param
1 parent 6bb13f0 commit ef76864

File tree

1 file changed

+65
-65
lines changed

1 file changed

+65
-65
lines changed
Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,90 @@
11
package com.denizenscript.denizencore.tags.core;
22

3-
import com.denizenscript.denizencore.objects.core.DurationTag;
3+
import com.denizenscript.denizencore.objects.ObjectTag;
4+
import com.denizenscript.denizencore.objects.core.*;
45
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
5-
import com.denizenscript.denizencore.tags.TagRunnable;
6-
import com.denizenscript.denizencore.objects.core.ListTag;
7-
import com.denizenscript.denizencore.objects.core.ScriptTag;
86
import com.denizenscript.denizencore.scripts.containers.core.ProcedureScriptContainer;
9-
import com.denizenscript.denizencore.tags.Attribute;
10-
import com.denizenscript.denizencore.tags.ReplaceableTagEvent;
11-
import com.denizenscript.denizencore.utilities.CoreUtilities;
127
import com.denizenscript.denizencore.utilities.ScriptUtilities;
13-
import com.denizenscript.denizencore.utilities.debugging.Debug;
148
import com.denizenscript.denizencore.tags.TagManager;
9+
import com.denizenscript.denizencore.utilities.text.StringHolder;
10+
11+
import java.util.Map;
1512

1613
public class ProcedureScriptTagBase {
1714

1815
public ProcedureScriptTagBase() {
19-
TagManager.registerTagHandler(new TagRunnable.RootForm() {
20-
@Override
21-
public void run(ReplaceableTagEvent event) {
22-
procedureTag(event);
23-
}
24-
}, "proc");
25-
}
26-
27-
public void procedureTag(ReplaceableTagEvent event) {
2816

2917
// <--[tag]
3018
// @attribute <proc[<procedure_script_name>]>
3119
// @returns ObjectTag
3220
// @description
3321
// Returns the 'determine' result of a procedure script.
3422
// -->
35-
if (!event.matches("proc")) {
36-
return;
37-
}
38-
39-
Attribute attribute = event.getAttributes();
40-
ScriptTag script;
41-
String path = null;
42-
if (attribute.hasParam()) {
43-
if (attribute.getParam().indexOf('.') > 0) {
44-
String[] split = attribute.getParam().split("\\.", 2);
23+
TagManager.registerTagHandler(ObjectTag.class, ElementTag.class, "proc", (attribute, param) -> {
24+
ScriptTag script;
25+
String path = null;
26+
if (param.asString().indexOf('.') > 0) {
27+
String[] split = param.asString().split("\\.", 2);
4528
path = split[1];
4629
script = ScriptTag.valueOf(split[0], attribute.context);
4730
}
4831
else {
49-
script = attribute.paramAsType(ScriptTag.class);
32+
script = param.asType(ScriptTag.class, attribute.context);
5033
}
51-
}
52-
else {
53-
Debug.echoError("Invalid procedure script tag!");
54-
return;
55-
}
56-
if (script == null) {
57-
attribute.echoError("Missing script for procedure script tag '" + attribute.getParam() + "'!");
58-
return;
59-
}
60-
if (!(script.getContainer() instanceof ProcedureScriptContainer)) {
61-
attribute.echoError("Chosen script is not a procedure script!");
62-
return;
63-
}
64-
ListTag definitions = null;
34+
if (script == null) {
35+
attribute.echoError("Missing script for procedure script tag '" + param.asString() + "'!");
36+
return null;
37+
}
38+
if (!(script.getContainer() instanceof ProcedureScriptContainer)) {
39+
attribute.echoError("Chosen script is not a procedure script!");
40+
return null;
41+
}
42+
ListTag definitions = null;
43+
MapTag mappedDefinitions;
44+
45+
// <--[tag]
46+
// @attribute <proc[<procedure_script_name>].context[<object>|...]>
47+
// @returns ObjectTag
48+
// @description
49+
// Returns the 'determine' result of a procedure script with the given context.
50+
// -->
51+
if (attribute.startsWith("context", 2)) {
52+
mappedDefinitions = null;
53+
definitions = attribute.contextAsType(2, ListTag.class);
54+
attribute.fulfill(1);
55+
}
56+
57+
// <--[tag]
58+
// @attribute <proc[<procedure_script_name>].context_map[<map>]>
59+
// @returns ObjectTag
60+
// @description
61+
// Returns the 'determine' result of a procedure script with the given context.
62+
// -->
63+
else if (attribute.startsWith("context_map", 2)) {
64+
mappedDefinitions = attribute.contextAsType(2, MapTag.class);
65+
attribute.fulfill(1);
66+
}
67+
else {
68+
mappedDefinitions = null;
69+
}
70+
ScriptQueue queue = ScriptUtilities.createAndStartQueue(script.getContainer(), path, attribute.context.getScriptEntryData(), null, (q) -> {
71+
if (mappedDefinitions != null) {
72+
for (Map.Entry<StringHolder, ObjectTag> val : mappedDefinitions.entrySet()) {
73+
q.addDefinition(val.getKey().str, val.getValue());
74+
}
75+
}
76+
q.procedural = true;
77+
}, new DurationTag(0), null, definitions, script.getContainer());
78+
if (queue == null) {
79+
attribute.echoError("Procedure queue start failed.");
80+
return null;
81+
}
82+
if (queue.determinations == null || queue.determinations.isEmpty()) {
83+
attribute.echoError("Procedure call did not determine any value.");
84+
return null;
85+
}
86+
return queue.determinations.getObject(0);
87+
});
6588

66-
// <--[tag]
67-
// @attribute <proc[<procedure_script_name>].context[<object>|...]>
68-
// @returns ObjectTag
69-
// @description
70-
// Returns the 'determine' result of a procedure script with the given context.
71-
// -->
72-
if (attribute.startsWith("context", 2)) {
73-
definitions = attribute.contextAsType(2, ListTag.class);
74-
attribute.fulfill(1);
75-
}
76-
ScriptQueue queue = ScriptUtilities.createAndStartQueue(script.getContainer(), path, attribute.context.getScriptEntryData(), null, (q) -> {
77-
q.procedural = true;
78-
}, new DurationTag(0), null, definitions, script.getContainer());
79-
if (queue == null) {
80-
attribute.echoError("Procedure queue start failed.");
81-
return;
82-
}
83-
attribute.fulfill(1);
84-
if (queue.determinations == null || queue.determinations.size() == 0) {
85-
attribute.echoError("Procedure call did not determine any value.");
86-
return;
87-
}
88-
event.setReplacedObject(CoreUtilities.autoAttribTyped(queue.determinations.getObject(0), attribute));
8989
}
9090
}

0 commit comments

Comments
 (0)