Skip to content

Data Watcher

Anarchick edited this page May 8, 2024 · 4 revisions

A Data Watcher contains a list of data in the form of an index (Integer) associated with a value (Object).

Data Watchers are utilized for manipulating the play_server_entity_metadata packet. For example, you can create a glowing effect on the client side.

You should refer to the Entity Metadata Format to understand how to manipulate the Data Watcher.

Be cautious with the type of the value! It can be a Byte, Float, etc., so you must use the ExprNumberAs to convert the value to the correct type.

You can use this code to create the metadata packet :

function medatadaPacket(entity: entity, index: integer, value: object) :: packet:
    set {_packet} to new play_server_entity_metadata packet
    set field 0 of {_packet} to {_entity}
    set {_dw} to new datawatcher from {_packet}
    set datawatcher index {_index} of {_dw} to {_value}
    return {_packet}

If you want to display a client side glowing effect use this :

function glow(entity: entity):
    set {_id} to id of {_entity}
    set {glow::%{_id}%} to {_entity}
    # The first method is to call play_server_entity_metadata mannually
    # but we have to handle all actions performing by the player (sneaking, flying, burning, ...)
    # https://wiki.vg/Entity_metadata#Entity_Metadata
    # send packet medatadaPacket({_entity}, 0, 64 as byte) to all players without calling event

    # The second method is to apply invisibility to automaticly call the metadata packet
    # and handle all actions for us without a lot of code
    apply invisibility without any particles whilst hiding the potion icon to {_entity} for 2 ticks

function unglow(entity: entity):
    set {_id} to id of {_entity}
    delete {glow::%{_id}%}
    apply invisibility without any particles whilst hiding the potion icon to {_entity} for 2 ticks

# Fix the metadata update cancelling glow when the entity
# perform some action like sneaking, flying, ...
on packet event play_server_entity_metadata:
    set {_id} to field 0
    if {glow::%{_id}%} is set:
        set {_dw} to new datawatcher from event-packet
        if datawatcher index 0 of {_dw} is set:
            set {_value} to datawatcher index 0 of {_dw}
            # Need the addon Skript-reflect
            # this will use binary operation to add glow state
            # and keeping all other states
            set bit 6 of {_value} to 1 
            set datawatcher index 0 of {_dw} to {_value}
Clone this wiki locally