Skip to content

Commit

Permalink
tooling for more advanced automatic object conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Sep 27, 2021
1 parent 71eb4e4 commit 4eb5b91
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
Expand Up @@ -553,8 +553,7 @@ public String toString() {
// 'on player breaks block with:iron_pickaxe|gold_pickaxe|diamond_axe|wood_shovel:'
//
// You can also combine wildcards and lists... note that lists are the 'wider' option.
// That is, if you have wildcards and lists together, you will have a list of possible matches, where each entry
// may contain wildcards. You do not have a a wildcard match with a list.
// That is, if you have wildcards and lists together, you will have a list of possible matches, where each entry may contain wildcards, you will not have a wildcard match with a list.
// As a specific example,
// '*_pickaxe|*_axe' will match any pickaxe or any axe.
// '*_pickaxe|stone' will match any pickaxe or specifically stone. It will NOT match other types of stone, as it interprets
Expand Down
Expand Up @@ -226,7 +226,7 @@ public void execute(ScriptEntry scriptEntry) {
String idString = id != null ? "FORCE:" + id.asString() : null;
ScriptQueue result = ScriptUtilities.createAndStartQueue(script.getContainer(), path, scriptEntry.entryData, null, configure, speed, idString, definitions, scriptEntry);
if (result == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Script run failed!");
Debug.echoError(scriptEntry.getResidingQueue(), "Script run failed (are you sure it's a task script, and the path exists?)!");
return;
}
}
Expand Down
Expand Up @@ -117,14 +117,6 @@ public ScriptQueue holdScriptEntry(String id, ScriptEntry entry) {
return this;
}

public ObjectTag getContext(String id) {
id = CoreUtilities.toLowerCase(id);
if (contextSource == null) {
return null;
}
return contextSource.getContext(id);
}

public ContextSource contextSource = null;

public DeterminationTarget determinationTarget = null;
Expand Down
Expand Up @@ -23,6 +23,7 @@
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.*;
import java.util.function.Function;

public class CoreUtilities {

Expand All @@ -48,19 +49,21 @@ public static ObjectTag objectToTagForm(Object obj, TagContext context, boolean
return objectToTagForm(obj, context, scriptStrip, false);
}

public static List<Function<Object, ObjectTag>> objectConversions = new ArrayList<>();

public static ObjectTag objectToTagForm(Object obj, TagContext context, boolean scriptStrip, boolean doParse) {
return objectToTagForm(obj, context, scriptStrip, doParse, true);
}

public static ObjectTag objectToTagForm(Object obj, TagContext context, boolean scriptStrip, boolean doParse, boolean canPick) {
if (obj == null) {
return new ElementTag("null");
}
if (obj instanceof YamlConfiguration) {
obj = ((YamlConfiguration) obj).contents;
}
if (obj instanceof List) {
ListTag listResult = new ListTag();
for (Object subObj : (List) obj) {
listResult.addObject(objectToTagForm(subObj, context, scriptStrip, doParse));
}
return listResult;
if (obj instanceof ObjectTag) {
return (ObjectTag) obj;
}
else if (obj instanceof Map) {
MapTag result = new MapTag();
Expand All @@ -69,19 +72,37 @@ else if (obj instanceof Map) {
if (scriptStrip) {
key = ScriptBuilder.stripLinePrefix(key);
}
result.putObject(key, CoreUtilities.objectToTagForm(entry.getValue(), context, scriptStrip, doParse));
result.putObject(key, objectToTagForm(entry.getValue(), context, scriptStrip, doParse, canPick));
}
return result;
}
if (obj instanceof Iterable) {
ListTag listResult = new ListTag();
for (Object subObj : (Iterable) obj) {
listResult.addObject(objectToTagForm(subObj, context, scriptStrip, doParse, canPick));
}
return listResult;
}
else {
for (Function<Object, ObjectTag> func : objectConversions) {
ObjectTag result = func.apply(obj);
if (result != null) {
return result.duplicate();
}
}
String result = obj.toString();
if (scriptStrip) {
result = ScriptBuilder.stripLinePrefix(result);
}
if (doParse) {
return TagManager.tagObject(result, context);
}
return ObjectFetcher.pickObjectFor(result, context);
if (canPick) {
return ObjectFetcher.pickObjectFor(result, context);
}
else {
return new ElementTag(result, true);
}
}
}

Expand Down
Expand Up @@ -33,7 +33,6 @@ public static <T> T getFieldValue(Class clazz, String fieldName, Object object)
if (field == null) {
return null;
}
cache.put(fieldName, field);
return (T) field.get(object);
}
catch (Exception ex) {
Expand Down

0 comments on commit 4eb5b91

Please sign in to comment.