Posted September 24, 201510 yr Hey everyone! I have a problem with setting the nbt data of an item inside a gui. I am able to set the nbt data of the item in the Constructor and in the initGui() method, but when i try to set it in the onGuiClosed() method the nbt data isn't changed. Can someone explain why and how i can fix it? Here the code of the onGuiClosed method: @Override public void onGuiClosed() { super.onGuiClosed(); String s = textField.getText(); itemTest.stackTagCompound.setString("text", s); System.out.println(itemTest.stackTagCompound.getString("text")); //here it shows the right value PacketHandler.INSTANCE.sendToServer(new PacketTestItem(itemTest, s)); }
September 24, 201510 yr Author public class PacketTestItem implements IMessageHandler<PacketTestItem, IMessage>, IMessage { private ItemStack itemTest; private String text; public PacketTestItem () { } public PacketTestItem (ItemStack itemTest, String text) { this.itemTest= itemTest; this.text= text; } @Override public void fromBytes(ByteBuf buf) { itemTest= ByteBufUtils.readItemStack(buf); text= ByteBufUtils.readUTF8String(buf); } @Override public void toBytes(ByteBuf buf) { ByteBufUtils.writeItemStack(buf, itemTest); ByteBufUtils.writeUTF8String(buf, text); } @Override public IMessage onMessage(PacketTestItem message, MessageContext ctx) { message.itemTest.stackTagCompound.setString("text",message.text); return null; } }
September 24, 201510 yr Author If it helps here is the class of the ItemTest: public class ItemTest extends Item { public static ItemTest create() { ItemTest result = new ItemTest (); result.init(); return result; } protected ItemTest () { setCreativeTab(EnderIOTab.tabEnderIO); setUnlocalizedName("itemTest"); setMaxDamage(0); setMaxStackSize(1); } @Override public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) { if (itemStack.stackTagCompound==null){ itemStack.stackTagCompound = new NBTTagCompound(); itemStack.stackTagCompound.setString("text", ""); } if (!world.isRemote){ Minecraft.getMinecraft().displayGuiScreen(new TestGui(itemStack)); } return itemStack; } @Override public void onCreated(ItemStack itemStack, World world, EntityPlayer entityPlayer) { if (itemStack.stackTagCompound==null){ itemStack.stackTagCompound = new NBTTagCompound(); itemStack.stackTagCompound.setString("text", ""); } super.onCreated(itemStack, world, entityPlayer); } @Override public void addInformation(ItemStack itemStack, EntityPlayer entityPlayer, List list, boolean b) { if (itemStack.stackTagCompound==null){ itemStack.stackTagCompound = new NBTTagCompound(); itemStack.stackTagCompound.setString("text", ""); } list.add(itemStack.stackTagCompound.getString("text")); } @Override @SideOnly(Side.CLIENT) public void registerIcons(IIconRegister IIconRegister) { itemIcon = IIconRegister.registerIcon("enderiofilters:itemTest"); } private void init() { GameRegistry.registerItem(this, "itemTest"); } }
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.