From 787805996ce941e8b0905086ae853dacef105ae8 Mon Sep 17 00:00:00 2001 From: WakelessSloth56 Date: Tue, 29 Mar 2022 02:26:37 +0800 Subject: [PATCH] feat(command): support getting version from mod main class using reflection --- .../common/command/impl/VersionCommand.java | 36 +++++++++++++++++-- .../server/command/AHServerCommands.java | 2 +- .../arnicalib/utils/game/CommandUtils.java | 2 +- .../mods/arnicalib/utils/game/TextUtils.java | 2 ++ .../assets/arnicalib/lang/en_us.json | 5 +-- .../assets/arnicalib/lang/zh_cn.json | 5 +-- 6 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/auioc/mods/arnicalib/common/command/impl/VersionCommand.java b/src/main/java/org/auioc/mods/arnicalib/common/command/impl/VersionCommand.java index 956ca665..b756213a 100644 --- a/src/main/java/org/auioc/mods/arnicalib/common/command/impl/VersionCommand.java +++ b/src/main/java/org/auioc/mods/arnicalib/common/command/impl/VersionCommand.java @@ -1,8 +1,13 @@ package org.auioc.mods.arnicalib.common.command.impl; +import static org.auioc.mods.arnicalib.ArnicaLib.LOGGER; import com.mojang.brigadier.Command; import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; +import org.apache.logging.log4j.Marker; import org.auioc.mods.arnicalib.ArnicaLib; +import org.auioc.mods.arnicalib.utils.LogUtil; import org.auioc.mods.arnicalib.utils.game.TextUtils; import net.minecraft.ChatFormatting; import net.minecraft.commands.CommandSourceStack; @@ -10,15 +15,19 @@ public class VersionCommand { + private static final Marker MARKER = LogUtil.getMarker(VersionCommand.class); + + private static final SimpleCommandExceptionType GET_VERSION_REFLECTION_ERROR = new SimpleCommandExceptionType(i18n("failure.reflection")); + public static int getModVersion(CommandContext ctx, String mainVersion, String fullVersion, String modName) { MutableComponent message = TextUtils.EmptyText(); message.append(TextUtils.StringText("[" + modName + "] ").withStyle(ChatFormatting.AQUA)); if (mainVersion.equals("0") && fullVersion.equals("0")) { - message.append(TextUtils.I18nText(ArnicaLib.i18n("command.version.failure"))); + message.append(i18n("failure.zero")); } else { - message.append(TextUtils.I18nText(ArnicaLib.i18n("command.version.success"), mainVersion, fullVersion)); + message.append(i18n("success", mainVersion, fullVersion)); } ctx.getSource().sendSuccess(message, false); @@ -26,4 +35,27 @@ public static int getModVersion(CommandContext ctx, String m return Command.SINGLE_SUCCESS; } + public static int getModVersion(CommandContext ctx, Class modClazz) throws CommandSyntaxException { + try { + return getModVersion( + ctx, + (String) modClazz.getField("MAIN_VERSION").get(modClazz), + (String) modClazz.getField("FULL_VERSION").get(modClazz), + (String) modClazz.getField("MOD_NAME").get(modClazz) + ); + } catch (Exception e) { + var commandException = GET_VERSION_REFLECTION_ERROR.create(); + LOGGER.error(MARKER, commandException.getMessage(), e); + throw commandException; + } + } + + private static MutableComponent i18n(String key, Object... args) { + return TextUtils.I18nText(ArnicaLib.i18n("command.version." + key), args); + } + + private static MutableComponent i18n(String key) { + return i18n(key, TextUtils.NO_ARGS); + } + } diff --git a/src/main/java/org/auioc/mods/arnicalib/server/command/AHServerCommands.java b/src/main/java/org/auioc/mods/arnicalib/server/command/AHServerCommands.java index bf1f3425..aabac494 100644 --- a/src/main/java/org/auioc/mods/arnicalib/server/command/AHServerCommands.java +++ b/src/main/java/org/auioc/mods/arnicalib/server/command/AHServerCommands.java @@ -17,7 +17,7 @@ public final class AHServerCommands { public static final CommandNode NODE = literal(ArnicaLib.MOD_ID).build(); public static void register(CommandDispatcher dispatcher) { - NODE.addChild(literal("version").executes((ctx) -> VersionCommand.getModVersion(ctx, ArnicaLib.MAIN_VERSION, ArnicaLib.FULL_VERSION, ArnicaLib.MOD_NAME)).build()); + NODE.addChild(literal("version").executes((ctx) -> VersionCommand.getModVersion(ctx, ArnicaLib.class)).build()); NODE.addChild(literal("test").then(argument("tab", CreativeModeTabArgument.creativeModeTab()).executes((ctx) -> { System.err.println(ctx.getArgument("tab", CreativeModeTab.class)); diff --git a/src/main/java/org/auioc/mods/arnicalib/utils/game/CommandUtils.java b/src/main/java/org/auioc/mods/arnicalib/utils/game/CommandUtils.java index e6e41b84..887b074d 100644 --- a/src/main/java/org/auioc/mods/arnicalib/utils/game/CommandUtils.java +++ b/src/main/java/org/auioc/mods/arnicalib/utils/game/CommandUtils.java @@ -25,7 +25,7 @@ public interface CommandUtils { SimpleCommandExceptionType INTERNAL_ERROR = new SimpleCommandExceptionType(I18nText(i18n("command.failure.internal"))); SimpleCommandExceptionType NOT_SERVER_ERROR = new SimpleCommandExceptionType(I18nText(i18n("command.failure.not_server"))); SimpleCommandExceptionType NOT_DEDICATED_SERVER_ERROR = new SimpleCommandExceptionType(I18nText(i18n("command.failure.not_dedicated_server"))); - SimpleCommandExceptionType REFLECTION_ERROR = new SimpleCommandExceptionType(I18nText(i18n("command.failure.reflection"))); + SimpleCommandExceptionType GET_REAL_SOURCE_REFLECTION_ERROR = new SimpleCommandExceptionType(I18nText(i18n("command.failure.get_real_source.reflection"))); /** * @param sourceStack diff --git a/src/main/java/org/auioc/mods/arnicalib/utils/game/TextUtils.java b/src/main/java/org/auioc/mods/arnicalib/utils/game/TextUtils.java index a7716b3e..3c981d32 100644 --- a/src/main/java/org/auioc/mods/arnicalib/utils/game/TextUtils.java +++ b/src/main/java/org/auioc/mods/arnicalib/utils/game/TextUtils.java @@ -8,6 +8,8 @@ public interface TextUtils { + Object[] NO_ARGS = new Object[0]; + static void chat(Player player, String message) { player.sendMessage(new TextComponent(message), Util.NIL_UUID); } diff --git a/src/main/resources/assets/arnicalib/lang/en_us.json b/src/main/resources/assets/arnicalib/lang/en_us.json index 9ad00897..2b3ce906 100644 --- a/src/main/resources/assets/arnicalib/lang/en_us.json +++ b/src/main/resources/assets/arnicalib/lang/en_us.json @@ -7,11 +7,12 @@ "arnicalib.command.failure.internal": "An internal error occurred trying to execute that command", "arnicalib.command.failure.not_server": "This command must be executed by the server", "arnicalib.command.failure.not_dedicated_server": "This command must be executed by the dedicated server", - "arnicalib.command.failure.reflection": "Failed while using reflection to get private source", + "arnicalib.command.failure.get_real_source.reflection": "Failed while using reflection to get real command source", "arnicalib.argument.damage_source.invalid": "Invalid damage source \"%s\"", "arnicalib.argument.creative_mod_tab.unknown": "Unknown creative mod tab \"%s\"", "arnicalib.command.version.success": "Version: %s (%s)", - "arnicalib.command.version.failure": "§eCould not read the mod version. §7If this is a development environment you can ignore this message." + "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" } diff --git a/src/main/resources/assets/arnicalib/lang/zh_cn.json b/src/main/resources/assets/arnicalib/lang/zh_cn.json index 99d09a4f..632f45f1 100644 --- a/src/main/resources/assets/arnicalib/lang/zh_cn.json +++ b/src/main/resources/assets/arnicalib/lang/zh_cn.json @@ -7,11 +7,12 @@ "arnicalib.command.failure.internal": "试图执行该命令时出现内部错误", "arnicalib.command.failure.not_server": "此命令必须由服务器执行", "arnicalib.command.failure.not_dedicated_server": "此命令必须由专用服务器执行", - "arnicalib.command.failure.reflection": "使用反射获取命令源失败", + "arnicalib.command.failure.get_real_source.reflection": "使用反射获取真实命令源失败", "arnicalib.argument.damage_source.invalid": "无效的伤害类型\"%s\"", "arnicalib.argument.creative_mod_tab.unknown": "未知的创造模式物品栏标签\"%s\"", "arnicalib.command.version.success": "版本: %s (%s)", - "arnicalib.command.version.failure": "§e无法读取模组版本。§7若处在开发环境中则您可以忽略此消息" + "arnicalib.command.version.failure.zero": "§e无法读取模组版本。§7若处在开发环境中则您可以忽略此消息", + "arnicalib.command.version.failure.reflection": "使用反射获取版本失败" }