Jump to content

[1.11 - Solved] Changing an ItemStack's display name using NBT


Franckyi

Recommended Posts

Hi !

I'm creating a small GUI mod to edit some item stats, like it's name, lore, enchantments etc...

The GUI opens if the player has an item in his main hand.

But now I'm in trouble with using the NBT system to change an ItemStack's name.

After pressing a "Done" button, in the

actionPerformed(GuiButton button)

, this is what I did :

 

if (button == this.doneButton) { // If it's the correct button
if(itemStack.getTagCompound() == null) // If the itemstack doesn't have a TagCompound...
	itemStack.setTagCompound(new NBTTagCompound()); // ...I'll create an empty one
[color=red]itemStack.getTagCompound().getCompoundTag("tag").getCompoundTag("display").setString("Name", name.getText()); // This is where I'm wrong ('name' is the GuiTextField I'm using)[/color]
Minecraft.getMinecraft().thePlayer.openGui(ItemEditorMod.instance, ItemEditorGuiHandler.ITEM_EDITOR_MENU, Minecraft.getMinecraft().theWorld, // This is the method to go to the previous GuiScreen
(int) Minecraft.getMinecraft().thePlayer.posX, (int) Minecraft.getMinecraft().thePlayer.posY,
(int) Minecraft.getMinecraft().thePlayer.posZ);
} 

 

When I check the NBT tag of the selected item using NBTExplorer, no new compound appears. Can someone help me ?

 

 

Link to comment
Share on other sites

When I check the NBT tag of the selected item using NBTExplorer, no new compound appears. Can someone help me ?

 

This is because you've edited the client-side object, and as soon as you open the player's inventory again, the server's version overwrites your changes.  You need to send a packet to the server and make the change there.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Ok, so my Network setup is working. Thanks to Draco18s for his link and diesieben07 for his tutorial (http://www.minecraftforge.net/forum/index.php?topic=20135.0). This is in my Message Handler class :

@Override
public IMessage onMessage(final EditItemMessage message, final MessageContext ctx) {
IThreadListener mainThread = (WorldServer) ctx.getServerHandler().playerEntity.worldObj;
        mainThread.addScheduledTask(new Runnable() {
                @Override
                public void run() {
                	System.out.println("New Name : " + message.text);
                	// Help
                }
        });
        return null;
}

I know I can get the ItemStack I want by using

ctx.getServerHandler().playerEntity.getHeldItemMainhand()

, but then I don't know what to do. I just want to change the ItemStack's name using NBT.

Link to comment
Share on other sites

Well, didn't you say that it wasn't good ? Because if I do the same same thing...

if(ctx.getServerHandler().playerEntity.getHeldItemMainhand().getTagCompound() == null) ctx.getServerHandler().playerEntity.getHeldItemMainhand().setTagCompound(new NBTTagCompound());
ctx.getServerHandler().playerEntity.getHeldItemMainhand().getTagCompound().getCompoundTag("tag").getCompoundTag("display").setString("Name", message.text);

...I'm getting the same object with an empty "tag" Tag Compound.

Link to comment
Share on other sites

ItemStack#getOrCreateSubCompound

has the SRG name

func_190925_c

, so that's probably the method name you'll need to use.

 

I suggest you update your MCP mappings so this is deobfuscated for you.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.