|
1 | 1 | package com.denizenscript.denizencore.tags.core;
|
2 | 2 |
|
3 |
| -import com.denizenscript.denizencore.objects.core.DurationTag; |
| 3 | +import com.denizenscript.denizencore.objects.ObjectTag; |
| 4 | +import com.denizenscript.denizencore.objects.core.*; |
4 | 5 | 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; |
8 | 6 | 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; |
12 | 7 | import com.denizenscript.denizencore.utilities.ScriptUtilities;
|
13 |
| -import com.denizenscript.denizencore.utilities.debugging.Debug; |
14 | 8 | import com.denizenscript.denizencore.tags.TagManager;
|
| 9 | +import com.denizenscript.denizencore.utilities.text.StringHolder; |
| 10 | + |
| 11 | +import java.util.Map; |
15 | 12 |
|
16 | 13 | public class ProcedureScriptTagBase {
|
17 | 14 |
|
18 | 15 | 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) { |
28 | 16 |
|
29 | 17 | // <--[tag]
|
30 | 18 | // @attribute <proc[<procedure_script_name>]>
|
31 | 19 | // @returns ObjectTag
|
32 | 20 | // @description
|
33 | 21 | // Returns the 'determine' result of a procedure script.
|
34 | 22 | // -->
|
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); |
45 | 28 | path = split[1];
|
46 | 29 | script = ScriptTag.valueOf(split[0], attribute.context);
|
47 | 30 | }
|
48 | 31 | else {
|
49 |
| - script = attribute.paramAsType(ScriptTag.class); |
| 32 | + script = param.asType(ScriptTag.class, attribute.context); |
50 | 33 | }
|
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 | + }); |
65 | 88 |
|
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)); |
89 | 89 | }
|
90 | 90 | }
|
0 commit comments