From de5c422e6613f9fc292db2fb3e8570456c7cbf7a Mon Sep 17 00:00:00 2001 From: anastarawneh Date: Sat, 13 Jan 2024 03:10:48 +0300 Subject: [PATCH] Add /logexception for debugging --- .../mccinametagmod/MCCINametagMod.java | 8 ++++++ .../command/LogExceptionCommand.java | 27 +++++++++++++++++++ .../mixin/LivingEntityRendererMixin.java | 1 + 3 files changed, 36 insertions(+) create mode 100644 src/main/java/me/anastarawneh/mccinametagmod/command/LogExceptionCommand.java diff --git a/src/main/java/me/anastarawneh/mccinametagmod/MCCINametagMod.java b/src/main/java/me/anastarawneh/mccinametagmod/MCCINametagMod.java index bb7c0ee..ce0700f 100644 --- a/src/main/java/me/anastarawneh/mccinametagmod/MCCINametagMod.java +++ b/src/main/java/me/anastarawneh/mccinametagmod/MCCINametagMod.java @@ -1,11 +1,14 @@ package me.anastarawneh.mccinametagmod; +import me.anastarawneh.mccinametagmod.command.LogExceptionCommand; import me.anastarawneh.mccinametagmod.config.Config; import me.anastarawneh.mccinametagmod.util.Game; import me.shedaniel.autoconfig.AutoConfig; import me.shedaniel.autoconfig.serializer.Toml4jConfigSerializer; import net.fabricmc.api.ModInitializer; import net.minecraft.client.MinecraftClient; +import net.minecraft.text.MutableText; +import net.minecraft.text.Text; import net.minecraft.text.TextColor; import net.minecraft.util.Formatting; import org.apache.logging.log4j.LogManager; @@ -14,6 +17,7 @@ public class MCCINametagMod implements ModInitializer { public static final String MODID = "mccinametagmod"; public static final Logger LOGGER = LogManager.getLogger(); + public static final MutableText MESSAGE_PREFIX = Text.literal(Formatting.GRAY + "[" + Formatting.GREEN + "MCCINametagMod" + Formatting.GRAY + "] " + Formatting.RESET); public static String TEAM = ""; public static String RANK = ""; public static TextColor COLOR = TextColor.fromFormatting(Formatting.DARK_GRAY); @@ -22,10 +26,14 @@ public class MCCINametagMod implements ModInitializer { public static String STAGE = ""; public static String PHASE_TYPE = ""; + public static Exception LATEST_EXCEPTION = null; + @Override public void onInitialize() { AutoConfig.register(Config.class, Toml4jConfigSerializer::new); LOGGER.info("Hello there"); + + LogExceptionCommand.register(); } public static Config getConfig() { diff --git a/src/main/java/me/anastarawneh/mccinametagmod/command/LogExceptionCommand.java b/src/main/java/me/anastarawneh/mccinametagmod/command/LogExceptionCommand.java new file mode 100644 index 0000000..fead1f7 --- /dev/null +++ b/src/main/java/me/anastarawneh/mccinametagmod/command/LogExceptionCommand.java @@ -0,0 +1,27 @@ +package me.anastarawneh.mccinametagmod.command; + +import com.mojang.brigadier.context.CommandContext; +import me.anastarawneh.mccinametagmod.MCCINametagMod; +import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback; +import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; + +import static net.fabricmc.fabric.api.client.command.v2.ClientCommandManager.literal; + +public class LogExceptionCommand { + public static void register() { + ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(literal("logexception") + .executes(LogExceptionCommand::execute))); + } + + static int execute(CommandContext context) { + if (MCCINametagMod.LATEST_EXCEPTION == null) { + context.getSource().sendFeedback(MCCINametagMod.MESSAGE_PREFIX.copy().append("Nothing to debug.")); + return 1; + } + MCCINametagMod.LOGGER.error("Logging latest exception:"); + MCCINametagMod.LOGGER.throwing(MCCINametagMod.LATEST_EXCEPTION); + context.getSource().sendFeedback(MCCINametagMod.MESSAGE_PREFIX.copy().append("Logged the latest exception.")); + MCCINametagMod.LATEST_EXCEPTION = null; + return 1; + } +} diff --git a/src/main/java/me/anastarawneh/mccinametagmod/mixin/LivingEntityRendererMixin.java b/src/main/java/me/anastarawneh/mccinametagmod/mixin/LivingEntityRendererMixin.java index d921efa..da211aa 100644 --- a/src/main/java/me/anastarawneh/mccinametagmod/mixin/LivingEntityRendererMixin.java +++ b/src/main/java/me/anastarawneh/mccinametagmod/mixin/LivingEntityRendererMixin.java @@ -484,6 +484,7 @@ else if (game == Game.DYNABALL) { } catch (Exception ex) { topLabel = Text.literal(playerName).setStyle(Style.EMPTY.withColor(Formatting.GRAY)); bottomLabel = Text.literal("Error.").setStyle(Style.EMPTY.withColor(Formatting.DARK_RED)); + MCCINametagMod.LATEST_EXCEPTION = ex; } double d = this.dispatcher.getSquaredDistanceToCamera(entity);