Jump to content

[1.14.4] [Solved] Trouble With Packets


Cerandior

Recommended Posts

I wasn't able to get my item sync using shareTags, so I decided to use packets. However I have not used packets before and I am finding their implementation a little tricky. The lack of mods running forge 1.14 makes it hard to find examples related to specific things you want to do so I am asking for help here.
I want to sync my item capability to the client, because I am using a part of the data stored in that capability to render the durability bar.
I followed the docs instructions for my packet implementation, but the data in the client is still not synced with the server. What have I done wrong here?
 

Packet Handler Class

Packet Class

Item Class

 

Thank you in advance.
 

Edited by Cerandior
Link to comment
Share on other sites

13 minutes ago, diesieben07 said:

There is no way to sync ItemStack data using custom packets. ItemStacks are written do various packets and your data needs to be included in all of them.

The share tag is the only way to include data here.

 

I can't get the shareTags working, no idea why they don't work. If there is really no way I can fix that, then maybe I can use GUI to inform the player of the item cooldown.

Can I render a UI element on the client using server data? I was thinking about rendering a [ Cooldown : (cooldown) ] text above the slots, but I still need the correct cooldown values from the item capability for that to work.

Edited by Cerandior
Link to comment
Share on other sites

4 minutes ago, diesieben07 said:

You cannot possibly control all GUIs that show ItemStacks.

How about sending a chat message to the player when he activates the item to tell him when the item is in cooldown?. It is the ugliest way of doing this, but if nothing else works, to hell with it.

Link to comment
Share on other sites

20 minutes ago, diesieben07 said:

The share tag should work. If it does not work for you, you need to debug why.


Ok, I did something and it now worked. I looked carefully into it and apparently the stack.hasTag() was returning false for my item. So whenever I Use the item, if the stack's tag is null, I set its tag to readShareTag(itemstack).
Basically I am treating the readShareTag as a normal method since it wasn't being updated automatically. It is working now, the client and the server data is synced.

This is what I did:
 

@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn) {
    ItemStack stack = playerIn.getHeldItem(handIn);
    ICoolDownItem cap = stack.getCapability(CapabilityRegistry.COOLDOWN_ITEM, null).orElse(null);

    if(stack.getTag() == null){
        stack.setTag(getShareTag(stack));
    }

    if(!worldIn.isRemote && cap.isOffCooldown()){
        if(hasHeads(playerIn) || playerIn.abilities.isCreativeMode){
            Random rn = new Random(20);
            StaffZombie zm = new StaffZombie(worldIn, playerIn);
            zm.setPosition(playerIn.posX + rn.nextDouble() * 2, playerIn.posY, playerIn.posZ + rn.nextDouble() * 2);
            zm.setItemStackToSlot(EquipmentSlotType.HEAD, new ItemStack(Items.GOLDEN_HELMET));
            worldIn.addEntity(zm);
            for(int i = 0; i<36; i++){
                if(playerIn.inventory.getStackInSlot(i).getItem() == Items.ZOMBIE_HEAD && !playerIn.abilities.isCreativeMode){
                    playerIn.inventory.getStackInSlot(i).split(1);
                    break;
                }
            }
            cap.setCooldown(cap.getMaxCooldown());
            return new ActionResult<>(ActionResultType.SUCCESS, playerIn.getHeldItem(handIn));
        }
    }
    return new ActionResult<>(ActionResultType.FAIL, playerIn.getHeldItem(handIn));
}

 

One slight worry that I have right now is a warning showing up on the console:

[Client thread/WARN] [minecraft/ClientPlayNetHandler]: Received passengers for unknown entity

Why is that happening?

Link to comment
Share on other sites

36 minutes ago, diesieben07 said:

This makes no sense.

And getShareTag / readShareTag are still called all the time. No matter what. Look at the code. PacketBuffer#writeItemStack literally always calls getShareTag.

There is no reason why hasTag being false will cause getShareTag or readShareTag to no longer be called.

 

Show your entity code and where you register it.

 

 

I have no idea man. I tried setting the breakpoints at different parts of the class because intelliJ displays a tree of all the methods called at that point and I never found any of the shareTags methods where I expected them to be. And I know, what I did doesn't really make too much sense because the stack should call the readShareTag and getShareTag automatically (I did look into a lot of methods related to itemstacks), but for some reason nothing was working as expected for me. I just tried that and everything works fine now, if I get rid of that line of code nothing works again.

 

As for the entity, that is probably caused by the "unique" type of zombie that my staff spawns. I forgot to register that in my registry events. I am surprised the entity was spawning considering I haven't registered them actually. I will register them right now, and check if the error will persist.

Thank you for your help.

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.