-
Notifications
You must be signed in to change notification settings - Fork 1
DataWatcher
Iltotore edited this page Jan 21, 2020
·
2 revisions
DataWatchers items are non-persistent entities's values sent to the Player.
Firstly, you need to get the entity's DataWatcher.
MetadataUtil dataManager = EntityMetadataAPI.getAPI().getDataWatcherManager();
//An EnderCrystal will be used for this example
DataWatcher dataWatcher = dataManager.getDataWatcher(enderCrystal);
Secondly, you need to get the DataWatcherObject instance that point to the targetted value.
DataWatcherRegistry dataRegistry =
EntityMetadataAPI.getAPI().getDataWatcherRegistry();
/* The DataWatcherSerializer contains functions to convert the input
to the NMS equivalent*/
DataWatcherSerializer<Vector, ?> dataSerializer = dataRegistry.getSerializer(
/* The type serializer'key. It define the serialier id,
the input and output generic types. */
DataWatcherKey.OPTIONAL_POSITION);
//The value index can be found in https://wiki.vg/Entity_metadata
DataWatcherObject<Vector, ?> dataObject = dataSerializer.index(6);
Now you can use DataWatcher#set(DataWatcherObject<F, T>, F).
//Set the enderCrystal's beam to 0, 0, 0.
dataWatcher.set(dataObject, new Vector(0, 0, 0));
DataWatcherEntry is an object to support multiples versions for DataWatchers. It allow to put an index and a DataWatcherKey with a version. This is an example with the Ghast attack animation:
DataWatcherEntry entry = new DataWatcherEntry().addVersion(new VersionEntry<>(ServerVersion.v1_8_4, DataWatcherKey.BYTE, 16))
.addVersion(new VersionEntry<>(ServerVersion.v1_9, DataWatcherKey.BOOLEAN, 11))
.addVersion(new VersionEntry<>(ServerVersion.v1_10, DataWatcherKey.BOOLEAN, 12));
boolean value1_14 = entry.getVersion(ServerVersion.v1_14).getIndex(); //12