Jump to content

[1.7.10] [Solved] NBT Data isn't set


Guje

Recommended Posts

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));
    }

Link to comment
Share on other sites

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;
    }
}

Link to comment
Share on other sites

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");
    }

}

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.