-
-
Notifications
You must be signed in to change notification settings - Fork 35
Add <proc.context_map> tag #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,93 @@ | ||
package com.denizenscript.denizencore.tags.core; | ||
|
||
import com.denizenscript.denizencore.objects.core.DurationTag; | ||
import com.denizenscript.denizencore.objects.ObjectTag; | ||
import com.denizenscript.denizencore.objects.core.*; | ||
import com.denizenscript.denizencore.scripts.queues.ScriptQueue; | ||
import com.denizenscript.denizencore.tags.TagRunnable; | ||
import com.denizenscript.denizencore.objects.core.ListTag; | ||
import com.denizenscript.denizencore.objects.core.ScriptTag; | ||
import com.denizenscript.denizencore.scripts.containers.core.ProcedureScriptContainer; | ||
import com.denizenscript.denizencore.tags.Attribute; | ||
import com.denizenscript.denizencore.tags.ReplaceableTagEvent; | ||
import com.denizenscript.denizencore.utilities.CoreUtilities; | ||
import com.denizenscript.denizencore.utilities.ScriptUtilities; | ||
import com.denizenscript.denizencore.utilities.debugging.Debug; | ||
import com.denizenscript.denizencore.tags.TagManager; | ||
import com.denizenscript.denizencore.utilities.text.StringHolder; | ||
|
||
import java.util.Map; | ||
|
||
public class ProcedureScriptTagBase { | ||
|
||
public ProcedureScriptTagBase() { | ||
TagManager.registerTagHandler(new TagRunnable.RootForm() { | ||
@Override | ||
public void run(ReplaceableTagEvent event) { | ||
procedureTag(event); | ||
} | ||
}, "proc"); | ||
} | ||
|
||
public void procedureTag(ReplaceableTagEvent event) { | ||
|
||
// <--[tag] | ||
// @attribute <proc[<procedure_script_name>]> | ||
// @returns ObjectTag | ||
// @description | ||
// Returns the 'determine' result of a procedure script. | ||
// --> | ||
if (!event.matches("proc")) { | ||
return; | ||
} | ||
|
||
Attribute attribute = event.getAttributes(); | ||
ScriptTag script; | ||
String path = null; | ||
if (attribute.hasParam()) { | ||
if (attribute.getParam().indexOf('.') > 0) { | ||
TagManager.registerTagHandler(ObjectTag.class, ElementTag.class, "proc", (attribute, object) -> { | ||
ScriptTag script; | ||
String path = null; | ||
if (object.asString().indexOf('.') > 0) { | ||
String[] split = attribute.getParam().split("\\.", 2); | ||
path = split[1]; | ||
script = ScriptTag.valueOf(split[0], attribute.context); | ||
} | ||
else { | ||
script = attribute.paramAsType(ScriptTag.class); | ||
} | ||
} | ||
else { | ||
Debug.echoError("Invalid procedure script tag!"); | ||
return; | ||
} | ||
if (script == null) { | ||
attribute.echoError("Missing script for procedure script tag '" + attribute.getParam() + "'!"); | ||
return; | ||
} | ||
if (!(script.getContainer() instanceof ProcedureScriptContainer)) { | ||
attribute.echoError("Chosen script is not a procedure script!"); | ||
return; | ||
} | ||
ListTag definitions = null; | ||
if (script == null) { | ||
attribute.echoError("Missing script for procedure script tag '" + attribute.getParam() + "'!"); | ||
return null; | ||
} | ||
if (!(script.getContainer() instanceof ProcedureScriptContainer)) { | ||
attribute.echoError("Chosen script is not a procedure script!"); | ||
return null; | ||
} | ||
ListTag definitions = null; | ||
MapTag mappedDefinitions; | ||
|
||
// <--[tag] | ||
// @attribute <proc[<procedure_script_name>].context[<object>|...]> | ||
// @returns ObjectTag | ||
// @description | ||
// Returns the 'determine' result of a procedure script with the given context. | ||
// --> | ||
if (attribute.startsWith("context", 2)) { | ||
definitions = attribute.contextAsType(2, ListTag.class); | ||
// <--[tag] | ||
// @attribute <proc[<procedure_script_name>].context[<object>|...]> | ||
// @returns ObjectTag | ||
// @description | ||
// Returns the 'determine' result of a procedure script with the given context. | ||
// --> | ||
if (attribute.startsWith("context", 2)) { | ||
mappedDefinitions = null; | ||
definitions = attribute.contextAsType(2, ListTag.class); | ||
attribute.fulfill(1); | ||
} | ||
|
||
// <--[tag] | ||
// @attribute <proc[<procedure_script_name>].context_map[<map>]> | ||
// @returns ObjectTag | ||
// @description | ||
// Returns the 'determine' result of a procedure script with the given context. | ||
// --> | ||
else if (attribute.startsWith("context_map", 2)) { | ||
mappedDefinitions = attribute.contextAsType(2, MapTag.class); | ||
attribute.fulfill(1); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. format |
||
mappedDefinitions = null; | ||
mcmonkey4eva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
ScriptQueue queue = ScriptUtilities.createAndStartQueue(script.getContainer(), path, attribute.context.getScriptEntryData(), null, (q) -> { | ||
if (mappedDefinitions != null) { | ||
for (Map.Entry<StringHolder, ObjectTag> val : mappedDefinitions.entrySet()) { | ||
q.addDefinition(val.getKey().str, val.getValue()); | ||
} | ||
} | ||
q.procedural = true; | ||
}, new DurationTag(0), null, definitions, script.getContainer()); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. stray newlines |
||
if (queue == null) { | ||
attribute.echoError("Procedure queue start failed."); | ||
return null; | ||
} | ||
attribute.fulfill(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's valid to leave this here. Test |
||
} | ||
ScriptQueue queue = ScriptUtilities.createAndStartQueue(script.getContainer(), path, attribute.context.getScriptEntryData(), null, (q) -> { | ||
q.procedural = true; | ||
}, new DurationTag(0), null, definitions, script.getContainer()); | ||
if (queue == null) { | ||
attribute.echoError("Procedure queue start failed."); | ||
return; | ||
} | ||
attribute.fulfill(1); | ||
if (queue.determinations == null || queue.determinations.size() == 0) { | ||
attribute.echoError("Procedure call did not determine any value."); | ||
return; | ||
} | ||
event.setReplacedObject(CoreUtilities.autoAttribTyped(queue.determinations.getObject(0), attribute)); | ||
if (queue.determinations == null || queue.determinations.isEmpty()) { | ||
attribute.echoError("Procedure call did not determine any value."); | ||
return null; | ||
} | ||
return CoreUtilities.autoAttribTyped(queue.determinations.getObject(0), attribute); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is definitely not right, see above re testing subtags |
||
}); | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be registered with the param input format, rather than using legacy
getParam()
callsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, wait, it is, you just labeled it
object
and didn't use it in all callsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please fix the name of the param var