Views
Actions
Difference between revisions of "Networking with Entities"
(Inital Import dokuwiki) |
Ferri Arnus (talk | contribs) |
||
(One intermediate revision by one other user not shown) | |||
Line 9: | Line 9: | ||
You can add extra data to the spawn packet Forge sends by implementing the following interface. | You can add extra data to the spawn packet Forge sends by implementing the following interface. | ||
− | === IEntityAdditionalSpawnData === | + | ===IEntityAdditionalSpawnData=== |
− | + | If your entity has data that is needed on the client, but doesn't change over time, then it can be added to the entity spawn packet using this interface. <code><nowiki>writeSpawnData()</nowiki></code> and <code><nowiki>readSpawnData()</nowiki></code> control how the data should be en/decoded to/from the network buffer. Also override <code><nowiki>getAddEntityPacket()</nowiki></code> to return <code><nowiki>NetworkHooks.getEntitySpawningPacket(...)</nowiki></code> for the data to be send. | |
− | If your entity has data that is needed on the client, but doesn't change over time, then it can be added to the entity spawn packet using this interface. <code><nowiki>writeSpawnData()</nowiki></code> and <code><nowiki>readSpawnData()</nowiki></code> control how the data should be en/decoded to/from the network buffer. | ||
== Dynamic Data == | == Dynamic Data == | ||
Line 19: | Line 18: | ||
This is the main vanilla system for synchronizing entity data from the server to the client. As such, a number of vanilla examples are available to refer to. | This is the main vanilla system for synchronizing entity data from the server to the client. As such, a number of vanilla examples are available to refer to. | ||
− | Firstly you need a <code><nowiki> | + | Firstly you need a <code><nowiki>EntityDataAccessor<T></nowiki></code> for the data you wish to keep synchronized. This should be stored as a static final field in your entity class, obtained by calling <code><nowiki>SynchedEntityData.defineId()</nowiki></code> and passing the entity class and a serializer for that type of data. The available serializer implementations can be found as static constants within the <code><nowiki>EntityDataSerializers</nowiki></code> class. |
{{Colored box|title=Alert|content=You should '''only''' create data parameters for your own entities, ''within that entity's class''. Adding parameters to entities you do not control can cause the IDs used to send that data over the network to become desynchronized, causing difficult to debug crashes.}} | {{Colored box|title=Alert|content=You should '''only''' create data parameters for your own entities, ''within that entity's class''. Adding parameters to entities you do not control can cause the IDs used to send that data over the network to become desynchronized, causing difficult to debug crashes.}} | ||
− | Then, override <code><nowiki> | + | Then, override <code><nowiki>Entity#defineSynchedData()</nowiki></code> and call <code><nowiki>this.entityData.define(...)</nowiki></code> for each of your data parameters, passing the parameter and an initial value to use. Remember to always call <code><nowiki>super.defineSynchedData()</nowiki></code> first! |
− | You can then get and set these values via your entity's <code><nowiki> | + | You can then get and set these values via your entity's <code><nowiki>entityData</nowiki></code> instance. Changes made will be synchronized to the client automatically. |
Latest revision as of 21:49, 21 February 2022
In addition to regular network messages, there are various other systems provided to handle synchronizing entity data.
Spawn Data
In general, the spawning of modded entities is handled seperately, by Forge.
Info
You can add extra data to the spawn packet Forge sends by implementing the following interface.
IEntityAdditionalSpawnData
If your entity has data that is needed on the client, but doesn't change over time, then it can be added to the entity spawn packet using this interface. writeSpawnData()
and readSpawnData()
control how the data should be en/decoded to/from the network buffer. Also override getAddEntityPacket()
to return NetworkHooks.getEntitySpawningPacket(...)
for the data to be send.
Dynamic Data
Data Parameters
This is the main vanilla system for synchronizing entity data from the server to the client. As such, a number of vanilla examples are available to refer to.
Firstly you need a EntityDataAccessor<T>
for the data you wish to keep synchronized. This should be stored as a static final field in your entity class, obtained by calling SynchedEntityData.defineId()
and passing the entity class and a serializer for that type of data. The available serializer implementations can be found as static constants within the EntityDataSerializers
class.
Alert
Then, override Entity#defineSynchedData()
and call this.entityData.define(...)
for each of your data parameters, passing the parameter and an initial value to use. Remember to always call super.defineSynchedData()
first!
You can then get and set these values via your entity's entityData
instance. Changes made will be synchronized to the client automatically.