Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(command): support getting version from mod main class using refl…
…ection
- Loading branch information
1 parent
58f30d5
commit 7878059
Showing
6 changed files
with
44 additions
and
8 deletions.
There are no files selected for viewing
36 changes: 34 additions & 2 deletions
36
src/main/java/org/auioc/mods/arnicalib/common/command/impl/VersionCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters