From 21e7a5bf5d48f27aaf8b9fdf55ddff19d00c2886 Mon Sep 17 00:00:00 2001 From: Alexdoru <57050655+Alexdoru@users.noreply.github.com> Date: Tue, 22 Oct 2024 09:32:29 +0200 Subject: [PATCH] merge iih + mt hand command in one (#84) * merge iih + mt hand command in one * remove the nbt tag, it's included in the mt hand line below * address review --- .../com/gtnewhorizon/gtnhlib/ClientProxy.java | 3 + .../gtnhlib/commands/GTNHClientCommand.java | 34 ++++++++ .../gtnhlib/commands/ItemInHandCommand.java | 86 +++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 src/main/java/com/gtnewhorizon/gtnhlib/commands/GTNHClientCommand.java create mode 100644 src/main/java/com/gtnewhorizon/gtnhlib/commands/ItemInHandCommand.java diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/ClientProxy.java b/src/main/java/com/gtnewhorizon/gtnhlib/ClientProxy.java index 000f0f0..edc3e3e 100644 --- a/src/main/java/com/gtnewhorizon/gtnhlib/ClientProxy.java +++ b/src/main/java/com/gtnewhorizon/gtnhlib/ClientProxy.java @@ -6,8 +6,10 @@ import net.minecraft.util.ChatComponentText; import net.minecraft.util.EnumChatFormatting; import net.minecraft.util.IChatComponent; +import net.minecraftforge.client.ClientCommandHandler; import com.gtnewhorizon.gtnhlib.client.model.ModelLoader; +import com.gtnewhorizon.gtnhlib.commands.ItemInHandCommand; import com.gtnewhorizon.gtnhlib.eventbus.EventBusSubscriber; import com.gtnewhorizon.gtnhlib.util.AboveHotbarHUD; @@ -32,6 +34,7 @@ public void preInit(FMLPreInitializationEvent event) { @Override public void init(FMLInitializationEvent event) { super.init(event); + ClientCommandHandler.instance.registerCommand(new ItemInHandCommand()); } @Override diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/commands/GTNHClientCommand.java b/src/main/java/com/gtnewhorizon/gtnhlib/commands/GTNHClientCommand.java new file mode 100644 index 0000000..c670ad4 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/commands/GTNHClientCommand.java @@ -0,0 +1,34 @@ +package com.gtnewhorizon.gtnhlib.commands; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.command.CommandBase; +import net.minecraft.command.ICommandSender; +import net.minecraft.util.ChatComponentText; +import net.minecraft.util.IChatComponent; + +public abstract class GTNHClientCommand extends CommandBase { + + @Override + public boolean canCommandSenderUseCommand(ICommandSender sender) { + return true; + } + + @Override + public String getCommandUsage(ICommandSender sender) { + return "/" + this.getCommandName(); + } + + protected void addChatMessage(String msg) { + addChatMessage(new ChatComponentText(msg)); + } + + protected void addChatMessage(IChatComponent msg) { + Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(msg); + } + + protected void copyToClipboard(String s) { + GuiScreen.setClipboardString(s); + } + +} diff --git a/src/main/java/com/gtnewhorizon/gtnhlib/commands/ItemInHandCommand.java b/src/main/java/com/gtnewhorizon/gtnhlib/commands/ItemInHandCommand.java new file mode 100644 index 0000000..d17e723 --- /dev/null +++ b/src/main/java/com/gtnewhorizon/gtnhlib/commands/ItemInHandCommand.java @@ -0,0 +1,86 @@ +package com.gtnewhorizon.gtnhlib.commands; + +import static net.minecraft.util.EnumChatFormatting.*; + +import java.util.Collections; +import java.util.List; + +import net.minecraft.client.entity.EntityClientPlayerMP; +import net.minecraft.command.ICommandSender; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fluids.IFluidContainerItem; +import net.minecraftforge.oredict.OreDictionary; + +import cpw.mods.fml.common.registry.GameRegistry; + +public class ItemInHandCommand extends GTNHClientCommand { + + @Override + public String getCommandName() { + return "iteminhand"; + } + + @Override + public List getCommandAliases() { + return Collections.singletonList("iih"); + } + + @Override + public void processCommand(ICommandSender iCommandSender, String[] args) { + // spotless:off + if (iCommandSender instanceof EntityClientPlayerMP player) { + ItemStack itemStack = player.getCurrentEquippedItem(); + if (itemStack == null) { + addChatMessage("Hand is empty!"); + return; + } + GameRegistry.UniqueIdentifier UID = GameRegistry.findUniqueIdentifierFor(itemStack.getItem()); + addChatMessage(GREEN.toString() + STRIKETHROUGH + "--------------------" + RESET + " Item info " + GREEN + STRIKETHROUGH + "--------------------"); + addChatMessage(String.format(GREEN + "UnlocalizedName:" + RESET + " [%s]", itemStack.getUnlocalizedName())); + if (UID != null) addChatMessage(String.format(GREEN + "RegistryName:" + RESET + " [%s]", UID)); + addChatMessage(String.format(GREEN + "ItemMeta:" + RESET + " [%s]", itemStack.getItemDamage())); + printFluidContent(itemStack); + addChatMessage(String.format(GREEN + "ClassName:" + RESET + " [%s]", itemStack.getItem().getClass())); + printMTHand(itemStack); + addChatMessage(GREEN.toString() + STRIKETHROUGH + "--------------------------------------------------"); + } + // spotless:on + } + + private void printFluidContent(ItemStack itemStack) { + if (itemStack.getItem() instanceof IFluidContainerItem tFluidContainer) { + FluidStack fluidStack = tFluidContainer.getFluid(itemStack); + if (fluidStack != null) { + String s = String.format( + "FluidID: [%d], UnlocName: [%s], Name: [%s]", + fluidStack.getFluid().getID(), + fluidStack.getFluid().getUnlocalizedName(), + fluidStack.getFluid().getName()); + addChatMessage(String.format(GREEN + "FluidContainer:" + RESET + " [%s]", s)); + } + } + } + + private void printMTHand(ItemStack itemStack) { + StringBuilder result = new StringBuilder(); + result.append('<'); + result.append(Item.itemRegistry.getNameForObject(itemStack.getItem())); + if (itemStack.getItemDamage() == OreDictionary.WILDCARD_VALUE) { + result.append(":*"); + } else if (itemStack.getItemDamage() > 0) { + result.append(':').append(itemStack.getItemDamage()); + } + result.append('>'); + if (itemStack.getTagCompound() != null) { + result.append(".withTag("); + result.append(itemStack.getTagCompound().toString()); + result.append(")"); + } + String msg = result.toString(); + addChatMessage(GREEN + "mt hand: " + RESET + msg); + copyToClipboard(msg); + } + +}