Posted October 6, 20231 yr 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 November 2, 20231 yr by LeeCrafts
October 28, 20231 yr Did you saved the value of DATA_IS_SOMETHING to disk when the Entity is unloaded in addAdditionalSaveData and load it in readAdditionalSaveData?
November 2, 20231 yr Author 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")); }
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.