Minecraft Modding: DataWatcher

Background


There is a common need to transfer data between the server and client in order to keep them synchronized. This is particularly important for entities which have several aspects of their state (such as their health) that need to synchronize regularly. Vanilla entities use a system for passing this information. Prior to 1.9 this system was called the DataWatcher which is described here. For versions 1.9+ you should see my information on my main networking tips page.

The DataWatcher


It is limited in terms of types and number of fields, but is quite suitable for passing a couple float, int, string, etc. You have to avoid using slots that are already used for your class -- e.g. if you extend EntityLiving some DataWatcher values are already used.

If you look through the entity type hierarchy you can find various already used datawatcher values

  • 0 = byte value, index indicating action state: 0=burning, 1=sneaking, 2=riding, 3=sprinting; 4=eating (Entity)
  • 1 = short value, amount of air (Entity)
  • 6 = float value, health (EntityLivingBase)
  • 7 = int value, potion effect color index (EntityLivingBase)
  • 8 = byte value, value 1 means potion in effect, 0 means no potion in effect (EntityLivingBase)
  • 9 = byte value, number of arrows in entity (EntityLivingBase)
  • 10 = string value, custom name tag (EntityLiving)
  • 11 = boolean value, has a name tag (EntityLiving)
  • 12 = int value, growing age (EntityAgeable)
  • 16 = byte value, value 1 means tamed (EntityTameable)
  • 17 = string value, UUID of the owner (EntityTameable)
Note that values can be reused as long as you're not a subclass of the class that uses it. For example, 16 is used by EntityBlaze to indicate if it is burning and it is not a conflict because EntityBlaze is not a subclass of EntityTameable.

No comments:

Post a Comment