Jump to content

[1.19.4, SOLVED] Synched entity data does not persist after player dies and respawns


Recommended Posts

Posted (edited)

I am implementing a custom entity, and so far I had to add synched entity data to it. For example:

private static final EntityDataAccessor<Boolean> DATA_IS_SOMETHING = SynchedEntityData.defineId(CustomEntity.class, EntityDataSerializers.BOOLEAN);

@Override
protected void defineSynchedData() {
    super.defineSynchedData();
    this.entityData.define(DATA_IS_SOMETHING, false);
}

Under certain conditions, the DATA_IS_SOMETHING of my entity will change to true. To my dismay though, when the player dies and then respawns, that entity's DATA_IS_SOMETHING resets back to false. Is this normal behavior? If so, then should I use capabilities instead? FYI I need the data to be synced between the server and client. 

Edited by LeeCrafts
  • 2 weeks later...
Posted

Wow, I didn't know that I had to do that, thank you!

This is my code, if any of you are curious:

// getter and setter methods that i already had
public boolean isSomething() {
    return this.entityData.get(DATA_IS_SOMETHING);
}

public void setSomething(boolean isSomething) {
    this.entityData.set(DATA_IS_SOMETHING, isSomething);
}

...

// what solved my issue
@Override
public void addAdditionalSaveData(@NotNull CompoundTag pCompound) {
    super.addAdditionalSaveData(pCompound);
    pCompound.putBoolean("IsSomething", this.isSomething());
}

@Override
public void readAdditionalSaveData(@NotNull CompoundTag pCompound) {
    super.readAdditionalSaveData(pCompound);
    this.setSomething(pCompound.getBoolean("IsSomething"));
}

 

  • LeeCrafts changed the title to [1.19.4, SOLVED] Synched entity data does not persist after player dies and respawns

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.

Announcements



×
×
  • Create New...

Important Information

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