diff --git a/src/main/java/org/auioc/mcmod/arnicalib/utils/game/CommandHandlerDynamicLoader.java b/src/main/java/org/auioc/mcmod/arnicalib/utils/game/CommandHandlerDynamicLoader.java index a40109c8..d4abdb5d 100644 --- a/src/main/java/org/auioc/mcmod/arnicalib/utils/game/CommandHandlerDynamicLoader.java +++ b/src/main/java/org/auioc/mcmod/arnicalib/utils/game/CommandHandlerDynamicLoader.java @@ -4,8 +4,6 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.apache.logging.log4j.Marker; -import org.auioc.mcmod.arnicalib.ArnicaLib; -import org.auioc.mcmod.arnicalib.api.game.command.ExceptionCommandExceptionType; import org.auioc.mcmod.arnicalib.utils.LogUtil; import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; @@ -16,50 +14,41 @@ public class CommandHandlerDynamicLoader { private static final Marker MARKER = LogUtil.getMarker(CommandHandlerDynamicLoader.class); - private static final ExceptionCommandExceptionType CLASS_NOT_FOUND_ERROR = ce("class_not_found"); - private static final ExceptionCommandExceptionType GET_RUN_METHOD_ERROR = ce("get_method"); - private static final ExceptionCommandExceptionType INVOKE_RUN_METHOD_ERROR = ce("invoke_method"); - private static final ExceptionCommandExceptionType NOT_CSE_THROWN_ERROR = ce("not_cse_thrown"); - public static int run(String className, CommandContext ctx) throws CommandSyntaxException { - Class clazz; - try { - clazz = Class.forName(className); - } catch (ClassNotFoundException e) { - throw CLASS_NOT_FOUND_ERROR.create(e); - } - - Method runMethod; try { - runMethod = clazz.getMethod("run", CommandContext.class); - } catch (NoSuchMethodException | SecurityException e) { - throw GET_RUN_METHOD_ERROR.create(e, "Failed to get \"run\" method"); + _run(className, ctx); + } catch (Throwable e) { + if (e instanceof CommandSyntaxException cse) { + LOGGER.warn(MARKER, "Command handler " + className + " throws a CommandSyntaxException"); + throw cse; + } + throw CommandUtils.LOGGABLE_INTERNAL_ERROR.create(); } + return Command.SINGLE_SUCCESS; + } + private static void _run(String className, CommandContext ctx) throws Throwable { try { + Class clazz = Class.forName(className); + Method runMethod = clazz.getMethod("run", CommandContext.class); runMethod.invoke(null, ctx); + } catch (ClassNotFoundException e) { + rethrow(e, "Cannot load class"); + } catch (NoSuchMethodException | SecurityException e) { + rethrow(e, "Cannot get \"run\" method"); } catch (IllegalAccessException | IllegalArgumentException e) { - throw INVOKE_RUN_METHOD_ERROR.create(e, "Failed to invoke \"run\" method"); + rethrow(e, "Cannot invoke \"run\" method"); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof CommandSyntaxException cse) { - LOGGER.warn(MARKER, "Command handler " + runMethod.getDeclaringClass().getName() + "#" + runMethod.getName() + " throws a CommandSyntaxException"); throw cse; } - throw NOT_CSE_THROWN_ERROR.create((Exception) e.getTargetException(), "Command handler throws an exception that is not a CommandSyntaxException"); + rethrow(e.getTargetException(), "Command handler throws an exception that is not a CommandSyntaxException"); } - - return Command.SINGLE_SUCCESS; } - - private static ExceptionCommandExceptionType ce(String key) { - return new ExceptionCommandExceptionType( - (e) -> TextUtils.I18nText( - ArnicaLib.i18n("command.handler_dynamic_loader.failure." + key), - e.getClass().getSimpleName() + ": " + e.getMessage() - ), - LOGGER, MARKER - ); + private static void rethrow(Throwable throwable, String message) throws Throwable { + LOGGER.error(MARKER, message, throwable); + throw throwable; } } diff --git a/src/main/resources/assets/arnicalib/lang/en_us.json b/src/main/resources/assets/arnicalib/lang/en_us.json index 175c5a19..b50ff8bd 100644 --- a/src/main/resources/assets/arnicalib/lang/en_us.json +++ b/src/main/resources/assets/arnicalib/lang/en_us.json @@ -18,10 +18,5 @@ "arnicalib.command.version.success": "Version: %s (%s)", "arnicalib.command.version.failure.zero": "§eCould not read the mod version. §7If this is a development environment you can ignore this message", - "arnicalib.command.version.failure.reflection": "Failed while using reflection to get mod version", - - "arnicalib.command.handler_dynamic_loader.failure.class_not_found": "Failed to load class: %s", - "arnicalib.command.handler_dynamic_loader.failure.get_method": "Failed to get \"run\" method: %s", - "arnicalib.command.handler_dynamic_loader.failure.invoke_method": "Failed to invoke \"run\" method: %s", - "arnicalib.command.handler_dynamic_loader.failure.not_cse_thrown": "Command handler throws an exception that is not a CommandSyntaxException: %s" + "arnicalib.command.version.failure.reflection": "Failed while using reflection to get mod version" }