Skip to content

Commit

Permalink
BaseWithParamInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 21, 2022
1 parent ba63cc3 commit 06ddfff
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/main/java/com/denizenscript/denizencore/tags/TagManager.java
Expand Up @@ -76,12 +76,39 @@ public <R extends ObjectTag> TagBaseData(String name, Class<R> returnType, TagRu

public static HashMap<String, TagBaseData> baseTags = new HashMap<>();

public static <R extends ObjectTag, P extends ObjectTag> void registerStaticTagBaseHandler(Class<R> returnType, Class<P> paramType, String name, TagRunnable.BaseWithParamInterface<R, P> run) {
internalRegisterTagHandler(returnType, paramType, name, run, true);
}

public static <R extends ObjectTag, P extends ObjectTag> void registerTagHandler(Class<R> returnType, Class<P> paramType, String name, TagRunnable.BaseWithParamInterface<R, P> run) {
internalRegisterTagHandler(returnType, paramType, name, run, false);
}

public static <R extends ObjectTag, P extends ObjectTag> void internalRegisterTagHandler(Class<R> returnType, Class<P> paramType, String name, TagRunnable.BaseWithParamInterface<R, P> run, boolean isStatic) {
internalRegisterTagHandler(returnType, name, (attribute) -> {
if (!attribute.hasParam()) {
return null;
}
ObjectTag param = attribute.getParamObject();
P result = param.asType(paramType, attribute.context);
if (result == null) {
attribute.echoError("Tag '<Y>" + name + "<W>' requires input of type '<Y>" + paramType.getSimpleName() + "<W>' but received input '<R>" + param + "<W>'.");
return null;
}
return run.run(attribute, result);
}, true);
}

public static <R extends ObjectTag> void registerStaticTagBaseHandler(Class<R> returnType, String name, TagRunnable.BaseInterface<R> run) {
baseTags.put(name, new TagBaseData(name, returnType, TagNamer.nameBaseInterface(name, run), true));
internalRegisterTagHandler(returnType, name, run, true);
}

public static <R extends ObjectTag> void registerTagHandler(Class<R> returnType, String name, TagRunnable.BaseInterface<R> run) {
baseTags.put(name, new TagBaseData(name, returnType, TagNamer.nameBaseInterface(name, run), false));
internalRegisterTagHandler(returnType, name, run, false);
}

public static <R extends ObjectTag> void internalRegisterTagHandler(Class<R> returnType, String name, TagRunnable.BaseInterface<R> run, boolean isStatic) {
baseTags.put(name, new TagBaseData(name, returnType, TagNamer.nameBaseInterface(name, run), isStatic));
}

@Deprecated
Expand Down
Expand Up @@ -23,6 +23,12 @@ public interface BaseInterface<R extends ObjectTag> {
R run(Attribute attribute);
}

@FunctionalInterface
public interface BaseWithParamInterface<R extends ObjectTag, P extends ObjectTag> {

R run(Attribute attribute, P param);
}

@Deprecated
public static abstract class RootForm implements Cloneable {

Expand Down

0 comments on commit 06ddfff

Please sign in to comment.