Jump to content

Recommended Posts

Posted (edited)

I have attached a capability to my custom item, and it successfully stores the data server side. But I cannot figure out how to sync it to the client item stack properly. 

EDIT: I'm not storing the item stack itself within this capability, just some extra string data that I can edit/change.

 

I sync the server side cap to client every tick in the item class:

/**
     * Called each tick as long the item is on a player inventory. Uses by maps to check if is on a player hand and
     * update it's contents.
     */
    public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) {

        if (!worldIn.isRemote && entityIn instanceof ServerPlayerEntity) {
            IConsoleItem cap = stack.getCapability(CONSOLE_ITEM, null).orElseThrow(() -> new IllegalArgumentException(("LazyOptional must not be empty!")));

            PacketConsoleItemSync newPacket = new PacketConsoleItemSync(cap.getLinkedUUID(), stack);
            PacketHandler.sendTo(newPacket, (ServerPlayerEntity) entityIn);
        }
    }

 

Here is my packet:

public class PacketConsoleItemSync {

    private String linkedUUID = "none";
    private ItemStack stack = null;

    public PacketConsoleItemSync(PacketBuffer buf) {
        linkedUUID = buf.readString();
        stack = buf.readItemStack();
    }

    public PacketConsoleItemSync(String linkedUUID, ItemStack stack) {
        this.linkedUUID = linkedUUID;
        this.stack = stack;

    }

    public static void encode(PacketConsoleItemSync packet, PacketBuffer buf) {
        buf.writeString(packet.linkedUUID);
        buf.writeItemStack(packet.stack);

    }

    public static void handle(final PacketConsoleItemSync packet, Supplier<NetworkEvent.Context> context) {
        NetworkEvent.Context ctx = context.get();
        ctx.enqueueWork(() -> {

               if (packet.stack != null) {

                   IConsoleItem cap = packet.stack.getCapability(CONSOLE_ITEM, null).orElseThrow(() -> new IllegalArgumentException(("LazyOptional must not be empty!")));
//                   System.out.println(packet.stack);
                   cap.setLinkedUUID(packet.linkedUUID);
//                   System.out.println(packet.linkedUUID);

               }

        });

        ctx.setPacketHandled(true);
    }
}

 

I'm pretty certain that the problem lies with my packet class, as I'm not sure how to properly get the itemstack and replace it client side. Simply sending it through the packet buff and accessing its capability isn't working. It isn't null, but it just won't change.

Edited by Electric
Posted (edited)

Thanks guys! I added the getShareTag and readShareTag to my item class. Now everything works well! 

@Override
    public CompoundNBT getShareTag(ItemStack stack) {
        CompoundNBT nbt = stack.getOrCreateTag();
        IConsoleItem cap = stack.getCapability(CONSOLE_ITEM, null).orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!"));

        nbt.putString("linkedUUID", cap.getLinkedUUID());

        return nbt;
    }

    @Override
    public void readShareTag(ItemStack stack, @Nullable CompoundNBT nbt) {
        super.readShareTag(stack, nbt);

        if (nbt != null) {
            IConsoleItem cap = stack.getCapability(CONSOLE_ITEM, null).orElseThrow(() -> new IllegalArgumentException("LazyOptional must not be empty!"));
            cap.setLinkedUUID(nbt.getString("linkedUUID"));
        }
    }

 

I'm wondering what exactly what these methods do. I turned off my packet syncing from earlier and the client still seems to have an updated item stack whenever I change my capability server side. So do these methods sync the capability between server and client by themselves? And it updates every tick or just knows when I change something server side?

Edited by Electric
Posted

I believe whenever there are changes being made to the tag, it will be sync to the client. What 

getShareTag()

does is allows you to modify the tag before the message of syncing being sent.

  • Thanks 1
Posted
33 minutes ago, poopoodice said:

I believe whenever there are changes being made to the tag, it will be sync to the client. What 


getShareTag()

does is allows you to modify the tag before the message of syncing being sent.

 

Ahh that makes sense, thanks!

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.