Skip to content

Commit

Permalink
feat(command): support getting version from mod main class using refl…
Browse files Browse the repository at this point in the history
…ection
  • Loading branch information
WakelessSloth56 committed Mar 28, 2022
1 parent 58f30d5 commit 7878059
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
@@ -1,29 +1,61 @@
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;
import net.minecraft.network.chat.MutableComponent;

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<CommandSourceStack> 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);

return Command.SINGLE_SUCCESS;
}

public static int getModVersion(CommandContext<CommandSourceStack> 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);
}

}
Expand Up @@ -17,7 +17,7 @@ public final class AHServerCommands {
public static final CommandNode<CommandSourceStack> NODE = literal(ArnicaLib.MOD_ID).build();

public static void register(CommandDispatcher<CommandSourceStack> 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));

Expand Down
Expand Up @@ -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
Expand Down
Expand Up @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/resources/assets/arnicalib/lang/en_us.json
Expand Up @@ -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"
}
5 changes: 3 additions & 2 deletions src/main/resources/assets/arnicalib/lang/zh_cn.json
Expand Up @@ -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": "使用反射获取版本失败"
}

0 comments on commit 7878059

Please sign in to comment.