Skip to content

Commit

Permalink
merge iih + mt hand command in one (#84)
Browse files Browse the repository at this point in the history
* merge iih + mt hand command in one

* remove the nbt tag, it's included in the mt hand line below

* address review
  • Loading branch information
Alexdoru authored Oct 22, 2024
1 parent 3d6099b commit 21e7a5b
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/gtnewhorizon/gtnhlib/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,6 +34,7 @@ public void preInit(FMLPreInitializationEvent event) {
@Override
public void init(FMLInitializationEvent event) {
super.init(event);
ClientCommandHandler.instance.registerCommand(new ItemInHandCommand());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}

}

0 comments on commit 21e7a5b

Please sign in to comment.