Jump to content

[1.19.2] Capability sync question.


Viesis

Recommended Posts

I have an entity that uses the ForgeCapabilities.ITEM_HANDLER for an inventory. It works very well. The server keeps track of the items and I get no issues. What I am looking for are any leads on how to sync the client with the server. Currently, I have an entity that will render a model part based on an item in a particular itemslot. The entity doesn't update until you manually open up the gui.

According to the minecraft forge docs about capabilities under the synchronizing data with clients, by default, capability data is not sent to clients. I see it says send a synchronization packet and the docs suggest 3 generic options. I can't really find an example of the kind of packet you need to send. The forge docs, while giving a descriptive "how to", isn't filling in the gaps for me.

Does anyone have a site or github example for syncing the client to server for capabilities? I have tried various how to videos starting at forge 1.14 up to 1.19, various web sites, and tried looking at the forums here. Any guidance would be greatly appreciated.

Thanks in advanced!
 

Link to comment
Share on other sites

As usual with this stuff, what you need to do depends upon the use case.

People get very confused about this mainly because they don't fully understand what they need to do.

That people try to blindly copy tutorials for this doesn't help them.

 

If this really is your own entity then a "simple" way is to use an EntityDataAccessor with EntityDataSerializers.COMPOUND_TAG (the CompoundTag will be used to store the serialized form of your inventory over the network).

See: https://forge.gemwire.uk/wiki/Networking_with_Entities

An example of the pattern is how ItemEntity handles its ItemStack: ItemEntity.DATA_ITEM

On the client you override onSynchedDataUpdated() to process the data.

 

But the important part is the call to getEntityData().set() which needs to be called when your entity is created or reloaded from disk and when the data changes.

Since you are probably using an ItemStackHandler you can make your own custom subclass to override load() and onContentsChanged() to forward these events to that set().

 

However, if this is a capability you are adding to other people's entities then you do need to do your own network handling and you need to be somewhat careful about who you send the packets to. https://forge.gemwire.uk/wiki/SimpleChannel

Your network packet will need to contain the inventory and the entityId so you can find the entity and update the capability when it gets to the client.

You need to trap the change of the inventory as before and broadcasting to all players that are tracking the entity using PacketDistributor.TRACKING_ENTITY.

But for the create/load of entities you will instead need to subscribe to PlayerEvent.StartTracking and just send the packet to that player.

The StartTracking event tells you when an entity comes into range of player and the PacketDistributor.TRACKING_ENTITY knows which players are still in range of the entity.

 

NOTE: For your own entity it is still an option to do custom network handling instead of the EntityDataAccessor.

This would be case for example if you have a large inventory and only want to send changed items instead of the whole inventory every time.

 

 

  • Like 1

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

Link to comment
Share on other sites

Thank you so much @warjort! I was so confused. I actually started to put in a data accessor to watch the 5 inventory spaces I cared about (I use data accessors for quite a few things in my machines already to sync info), but just in reading some of the documentation on capabilities, it really sounds like there was something specific I could do in capabilities itself. Too many times I have not asked a question and did someone 10 times harder, just to find out I could of call a few simple lines, hook into a system already there, and gotten the same result.

Greatly appreciate it! You rock!

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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.