Jump to content

[1.12] Server not update nbt


WildHeart

Recommended Posts

Hello, i send packet to server(update nbt for ItemBook) and server not update nbt. What's the problem?

public final class SPacketUpdateNBT extends AbstractPacket<SPacketUpdateNBT>
{
    private static NBTTagCompound tagCompound;
    private static ItemStack stack;

    public SPacketUpdateNBT(){}
    public SPacketUpdateNBT(NBTTagCompound tagCompound, ItemStack stack)
    {
        this.tagCompound = tagCompound;
        this.stack = stack;
    }

    @Override
    public void handleClientSide(EntityPlayer player)
    {

    }

    @Override
    public void handleServerSide(EntityPlayer player)
    {
        this.stack.setTagCompound(this.tagCompound);
    }

    @Override
    public void fromBytes(ByteBuf byteBuf)
    {
        this.tagCompound = ByteBufUtils.readTag(byteBuf);
        this.stack = ByteBufUtils.readItemStack(byteBuf);
    }

    @Override
    public void toBytes(ByteBuf byteBuf)
    {
        ByteBufUtils.writeTag(byteBuf, this.tagCompound);
        ByteBufUtils.writeItemStack(byteBuf, this.stack);
    }
}

In GuiBook:

    @Override
    protected void actionPerformed(GuiButton button) throws IOException
    {
        boolean updateLines = false;
        if (button.enabled)
        {
            switch (button.id)
            {
                case BUTTON_DONE_ID:
                    this.sendBookToServer();
                    this.mc.displayGuiScreen(null);
                    break;

and

private void sendBookToServer() throws IOException
    {
        if (this.bookIsModified && this.bookPages != null)
        {
            while (this.bookPages.tagCount() > 1)
            {
                String s = this.bookPages.getStringTagAt(this.bookPages.tagCount() - 1);

                if (!s.isEmpty())
                {
                    break;
                }

                this.bookPages.removeTag(this.bookPages.tagCount() - 1);
            }

            if (this.bookObj.hasTagCompound())
            {
                NBTTagCompound nbttagcompound = this.bookObj.getTagCompound();
                nbttagcompound.setTag("pages", this.bookPages);
            }
            else
            {
                this.bookObj.setTagInfo("pages", this.bookPages);
            }

            String title = this.bookTitle;

            if (title.equals(TITLE_PLACEHOLDER)) title = "";

            this.bookObj.setTagInfo("title", new NBTTagString(title));

            //PacketBuffer packetbuffer = new PacketBuffer(Unpooled.buffer());
            //packetbuffer.writeItemStack(this.bookObj);

            //this.mc.getConnection().sendPacket(new CPacketCustomPayload("MC|BEdit", packetbuffer));

            NetworkHandler.NETWORK.sendToServer(new SPacketUpdateNBT(this.bookObj.getTagCompound(), this.bookObj));
        }
    }

 

Link to comment
Share on other sites

In your packet, you're reading an ItemStack and a compound tag from the byte buffer and then setting the compound tag as the ItemStack's compound tag; but then you do absolutely nothing with the ItemStack and it's garbage collected some time after the packet is received.

 

You need to make the changes to the ItemStack in the player's inventory, but make sure you check that the NBT data and the ItemStack's Item are valid to prevent malicious clients arbitrarily modifying item NBT. Look at the "MC|BEdit" and "MC|BSign" sections of NetHandlerPlayServer#processCustomPayload for examples of how to validate the data and modify the ItemStack in the player's inventory.

 

There's no reason to send the compound tag separately from the ItemStackByteBufUtils.writeItemStack includes the ItemStack's compound tag.

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

1 hour ago, Choonster said:

In your packet, you're reading an ItemStack and a compound tag from the byte buffer and then setting the compound tag as the ItemStack's compound tag; but then you do absolutely nothing with the ItemStack and it's garbage collected some time after the packet is received.

 

You need to make the changes to the ItemStack in the player's inventory, but make sure you check that the NBT data and the ItemStack's Item are valid to prevent malicious clients arbitrarily modifying item NBT. Look at the "MC|BEdit" and "MC|BSign" sections of NetHandlerPlayServer#processCustomPayload for examples of how to validate the data and modify the ItemStack in the player's inventory.

 

There's no reason to send the compound tag separately from the ItemStackByteBufUtils.writeItemStack includes the ItemStack's compound tag.

I use in my GuiBook this code:

            PacketBuffer packetbuffer = new PacketBuffer(Unpooled.buffer());
            packetbuffer.writeItemStack(this.bookObj);

            this.mc.getConnection().sendPacket(new CPacketCustomPayload("MC|BEdit", packetbuffer));

But it also didn't work although this code is in vanilla gui book. What could be the problem?

Link to comment
Share on other sites

28 minutes ago, WildHeart said:

But it also didn't work although this code is in vanilla gui book. What could be the problem?

 

The handler for the vanilla book edit/sign packets specifically checks if the ItemStack's Item is Items.WRITABLE_BOOK, so you can't use them for your own item. You should do the same thing in your packet handlers but check for your own Item instead of the vanilla one.

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

6 minutes ago, Choonster said:

 

The handler for the vanilla book edit/sign packets specifically checks if the ItemStack's Item is Items.WRITABLE_BOOK, so you can't use them for your own item. You should do the same thing in your packet handlers but check for your own Item instead of the vanilla one.

In principle it is possible, but there is one thing. In Minecraft pack CPacketCustomPayload spike, and at the moment I'm looking for a solution that is better. Speaking of bags, did you know that in forge batch system is bad?

Link to comment
Share on other sites

4 minutes ago, WildHeart said:

In principle it is possible, but there is one thing. In Minecraft pack CPacketCustomPayload spike, and at the moment I'm looking for a solution that is better.

 

What do you mean by "In Minecraft pack CPacketCustomPayload spike"?

 

 

4 minutes ago, WildHeart said:

Speaking of bags, did you know that in forge batch system is bad?

 

What do you mean by "forge batch system"? Why is it bad?

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

4 minutes ago, Choonster said:

 

What do you mean by "In Minecraft pack CPacketCustomPayload spike"?

 

 

 

What do you mean by "forge batch system"? Why is it bad?

Forge uses packets netty, these packages are very laggs. You can wrap an ordinary batch system in Minecraft will be much better. And forge, why not wrapped model(obj) to display list? About the vanilla package, it's bad, you can do much better.

Link to comment
Share on other sites

Solved.

Code:

    private static ItemStack stack;

    public SPacketUpdateNBT(){}
    public SPacketUpdateNBT(ItemStack stack)
    {
        this.stack = stack;
    }

    @Override
    public void handleClientSide(EntityPlayer player)
    {

    }

    @Override
    public void handleServerSide(EntityPlayerMP player)
    {
        try {
            ItemStack itemstack = this.stack;

            if (itemstack.isEmpty()) return;

            if (!ItemWritableBook.isNBTValid(itemstack.getTagCompound()))
            {
                FMLLog.log.error("Invalid book tag!");
            }

            ItemStack itemstack1 = player.getHeldItemMainhand();

            if (itemstack1.isEmpty()) return;

            if (itemstack.getItem() == ModItems.python_book && itemstack.getItem() == itemstack1.getItem())
            {
                itemstack1.setTagInfo("pages", itemstack.getTagCompound().getTagList("pages", 8));
            }
        }
        catch (Exception exception6)
        {
            exception6.printStackTrace();
        }
    }

    @Override
    public void fromBytes(ByteBuf byteBuf)
    {
        this.stack = ByteBufUtils.readItemStack(byteBuf);
    }

    @Override
    public void toBytes(ByteBuf byteBuf)
    {
        ByteBufUtils.writeItemStack(byteBuf, this.stack);
    }

 

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.