Skip to content

Commit

Permalink
feat(utils): command handler dynamic loader supports custom method name
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Jun 23, 2022
1 parent 8acb51e commit a18eb3f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
Expand Up @@ -13,15 +13,9 @@ public final class AHServerCommands {

public static final CommandNode<CommandSourceStack> NODE = literal(ArnicaLib.MOD_ID).build();

private static final String TEST_COMMAND_CLASS_NAME = "org.auioc.mcmod.arnicalib.server.command.TestCommandHandler";

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
VersionCommand.addVersionNode(NODE, ArnicaLib.class);
NODE.addChild(
literal("test")
.executes((ctx) -> CommandHandlerDynamicLoader.run(TEST_COMMAND_CLASS_NAME, ctx))
.build()
);
NODE.addChild(TEST_NODE);

getAHNode(dispatcher).addChild(NODE);
}
Expand All @@ -43,4 +37,14 @@ public static CommandNode<CommandSourceStack> getRootNode(CommandDispatcher<Comm
return getAHNode(dispatcher);
}

private static final CommandNode<CommandSourceStack> TEST_NODE = literal("test")
.executes(
(ctx) -> CommandHandlerDynamicLoader.run(
"org.auioc.mcmod.arnicalib.server.command.TestCommandHandler",
"run",
ctx
)
)
.build();

}
Expand Up @@ -2,7 +2,6 @@

import static org.auioc.mcmod.arnicalib.ArnicaLib.LOGGER;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.logging.log4j.Marker;
import org.auioc.mcmod.arnicalib.utils.LogUtil;
import com.mojang.brigadier.Command;
Expand All @@ -14,9 +13,9 @@ public class CommandHandlerDynamicLoader {

private static final Marker MARKER = LogUtil.getMarker(CommandHandlerDynamicLoader.class);

public static int run(String className, CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
public static int run(String className, String methodName, CommandContext<CommandSourceStack> ctx) throws CommandSyntaxException {
try {
_run(className, ctx);
_run(className, methodName, ctx);
} catch (Throwable e) {
if (e instanceof CommandSyntaxException cse) {
LOGGER.warn(MARKER, "Command handler " + className + " throws a CommandSyntaxException");
Expand All @@ -27,17 +26,17 @@ public static int run(String className, CommandContext<CommandSourceStack> ctx)
return Command.SINGLE_SUCCESS;
}

private static void _run(String className, CommandContext<CommandSourceStack> ctx) throws Throwable {
private static void _run(String className, String methodName, CommandContext<CommandSourceStack> ctx) throws Throwable {
try {
Class<?> clazz = Class.forName(className);
Method runMethod = clazz.getMethod("run", CommandContext.class);
runMethod.invoke(null, ctx);
Class.forName(className)
.getMethod(methodName, CommandContext.class)
.invoke(null, ctx);
} catch (ClassNotFoundException e) {
rethrow(e, "Cannot load class");
} catch (NoSuchMethodException | SecurityException e) {
rethrow(e, "Cannot get \"run\" method");
rethrow(e, "Cannot get method");
} catch (IllegalAccessException | IllegalArgumentException e) {
rethrow(e, "Cannot invoke \"run\" method");
rethrow(e, "Cannot invoke method");
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof CommandSyntaxException cse) {
throw cse;
Expand Down

0 comments on commit a18eb3f

Please sign in to comment.