Skip to content

Commit

Permalink
new ObjectTag helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Feb 11, 2022
1 parent af5f22b commit 4115ee2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 49 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/denizenscript/denizencore/objects/ObjectTag.java
Expand Up @@ -176,10 +176,25 @@ default Class<? extends ObjectTag> getObjectTagClass() {
return getClass();
}

/**
* Converts the object to the given type. May error and/or return null if conversion is not possible.
*/
default <T extends ObjectTag> T asType(Class<T> type, TagContext context) {
return CoreUtilities.asType(this, type, context);
}

/**
* Returns whether the object is likely intended to be the type.
* This is if it already is that type, or if it has the proper prefixing to be that type.
*/
default boolean shouldBeType(Class<? extends ObjectTag> type) {
return CoreUtilities.shouldBeType(this, type);
}

/**
* Returns whether the object can probably be converted to the type.
* This is if it already is that type, or if it's reasonably convertible.
*/
default boolean canBeType(Class<? extends ObjectTag> type) {
return CoreUtilities.canPossiblyBeType(this, type);
}
Expand Down
Expand Up @@ -308,8 +308,9 @@ public static <T extends ObjectTag> T asType(ObjectTag inp, Class<T> type, TagCo
return ObjectFetcher.getObjectFrom(type, inp.toString(), context);
}

public static abstract class TypeComparisonRunnable {
public abstract boolean canBecome(ObjectTag inp);
@FunctionalInterface
public interface TypeComparisonRunnable {
boolean doesCompare(ObjectTag inp);
}

@FunctionalInterface
Expand All @@ -319,6 +320,8 @@ public interface TagTypeConverter {

public static Map<Class<? extends ObjectTag>, TypeComparisonRunnable> typeCheckers = new HashMap<>();

public static Map<Class<? extends ObjectTag>, TypeComparisonRunnable> typeShouldBeCheckers = new HashMap<>();

public static Map<Class<? extends ObjectTag>, TagTypeConverter> typeConverters = new HashMap<>();

static {
Expand All @@ -333,74 +336,92 @@ public interface TagTypeConverter {
typeConverters.put(ElementTag.class, (obj, c) -> obj.asElement());
typeConverters.put(ListTag.class, ListTag::getListFor);
typeConverters.put(MapTag.class, MapTag::getMapFor);
typeCheckers.put(MapTag.class, new TypeComparisonRunnable() {
@Override
public boolean canBecome(ObjectTag inp) {
if (inp == null) {
return false;
}
if (inp instanceof MapTag) {
return true;
}
if (!(inp instanceof ElementTag)) {
return false;
}
String simple = inp.toString();
if (simple.startsWith("map@")) {
return true;
}
if (simple.startsWith("[") && simple.endsWith("]") && (simple.contains(";") || simple.contains("="))) {
return true;
}
typeCheckers.put(MapTag.class, (inp) -> {
if (inp == null) {
return false;
}
if (inp instanceof MapTag) {
return true;
}
if (!(inp instanceof ElementTag)) {
return false;
}
String simple = inp.toString();
if (simple.startsWith("map@")) {
return true;
}
if (simple.startsWith("[") && simple.endsWith("]") && simple.contains("=")) {
return true;
}
return false;
});
}

public static void registerTypeAsNoOtherTypeCode(Class<? extends ObjectTag> type, final String knownCode) {
typeCheckers.put(type, new TypeComparisonRunnable() {
@Override
public boolean canBecome(ObjectTag inp) {
if (inp == null) {
return false;
}
Class<? extends ObjectTag> inpType = inp.getObjectTagClass();
if (inpType == type) {
return true;
}
if (inpType == ElementTag.class) {
String simple = inp.toString();
int atIndex = simple.indexOf('@');
if (atIndex != -1) {
String code = simple.substring(0, atIndex);
if (!code.equals(knownCode) && !code.equals("el")) {
if (ObjectFetcher.objectsByPrefix.containsKey(code)) {
return false;
}
typeCheckers.put(type, (inp) -> {
if (inp == null) {
return false;
}
Class<? extends ObjectTag> inpType = inp.getObjectTagClass();
if (inpType == type) {
return true;
}
if (inpType == ElementTag.class) {
String simple = inp.toString();
int atIndex = simple.indexOf('@');
if (atIndex != -1) {
String code = simple.substring(0, atIndex);
if (!code.equals(knownCode) && !code.equals("el")) {
if (ObjectFetcher.objectsByPrefix.containsKey(code)) {
return false;
}
}
return true;
}
return false;
return true;
}
return false;
});
}

public static void registerTypeAsTrueAlways(Class<? extends ObjectTag> type) {
typeCheckers.put(type, new TypeComparisonRunnable() {
@Override
public boolean canBecome(ObjectTag inp) {
return true;
}
});
typeCheckers.put(type, (inp) -> true);
}

public static boolean shouldBeType(ObjectTag inp, Class<? extends ObjectTag> type) {
if (type == ElementTag.class || type == ObjectTag.class) {
return true;
}
if (inp.getObjectTagClass() == type) {
return true;
}
TypeComparisonRunnable comp = typeShouldBeCheckers.get(type);
if (comp != null) {
return comp.doesCompare(inp);
}
if (!(inp instanceof ElementTag)) {
return false;
}
if (((ElementTag) inp).isPlainText || ((ElementTag) inp).isRawInput) {
return false;
}
String raw = inp.toString();
int atSign = raw.indexOf('@');
if (atSign == -1) {
return false;
}
ObjectFetcher.ObjectType<?> typeData = ObjectFetcher.objectsByClass.get(type);
return typeData.prefix.equals(raw.substring(0, atSign));
}

public static boolean canPossiblyBeType(ObjectTag inp, Class<? extends ObjectTag> type) {
if (type == ObjectTag.class) {
return true;
}
if (inp.getObjectTagClass() == type) {
return true;
}
TypeComparisonRunnable comp = typeCheckers.get(type);
if (comp != null && !comp.canBecome(inp)) {
if (comp != null && !comp.doesCompare(inp)) {
return false;
}
return ObjectFetcher.checkMatch(type, inp.toString());
Expand Down

0 comments on commit 4115ee2

Please sign in to comment.