Skip to content

Commit

Permalink
feat(command): test command dynamic loader
Browse files Browse the repository at this point in the history
  • Loading branch information
lainio24 committed Jun 23, 2022
1 parent 6dd88d2 commit b5cb190
Showing 1 changed file with 38 additions and 3 deletions.
@@ -1,11 +1,18 @@
package org.auioc.mcmod.arnicalib.server.command;

import static net.minecraft.commands.Commands.literal;
import static org.auioc.mcmod.arnicalib.ArnicaLib.LOGGER;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import org.apache.logging.log4j.Marker;
import org.auioc.mcmod.arnicalib.ArnicaLib;
import org.auioc.mcmod.arnicalib.common.command.impl.VersionCommand;
import org.auioc.mcmod.arnicalib.utils.LogUtil;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.tree.CommandNode;
import net.minecraft.commands.CommandSourceStack;

Expand All @@ -15,9 +22,7 @@ public final class AHServerCommands {

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
VersionCommand.addVersionNode(NODE, ArnicaLib.class);
NODE.addChild(literal("test").executes((ctx) -> {
return Command.SINGLE_SUCCESS;
}).build());
NODE.addChild(TestCommand.TEST_NODE);

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

private class TestCommand {

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

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

public static final CommandNode<CommandSourceStack> TEST_NODE = literal("test")
.executes((ctx) -> {
try {
Class<?> c = Class.forName(CLASS_NAME);
Method m = c.getMethod("run", CommandContext.class);
m.invoke(null, ctx);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException e) {
rethrow(e, "Could not load test command");
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof CommandSyntaxException) {
throw (CommandSyntaxException) e.getTargetException();
}
rethrow(e, "Test command executor throws an exception that is not a CommandSyntaxException");
}
return Command.SINGLE_SUCCESS;
}).build();

private static void rethrow(Exception e, String message) {
LOGGER.error(MARKER, message, e);
throw new RuntimeException(e);
}

}

}

0 comments on commit b5cb190

Please sign in to comment.