Posted August 17, 20223 yr I have an issue with adding data to an entity (mob). I want it to have a boolean value to determine whether it has a tracking device or not. However, the data doesn't get saved when I exit the game. Does anyone have an explaination for this? The variant of this mob does work correctly (and gets saved properly) and I have no idea what I did differently. See the relevant code below: private static final EntityDataAccessor<Integer> DATA_ID_TYPE_VARIANT = SynchedEntityData.defineId(AllosaurusEntityExample.class, EntityDataSerializers.INT); private static final EntityDataAccessor<Boolean> HAS_TRACKER = SynchedEntityData.defineId(AllosaurusEntityExample.class, EntityDataSerializers.BOOLEAN); public boolean hasTracker() { return this.entityData.get(HAS_TRACKER); } public void setTracker(boolean tracker) { this.entityData.set(HAS_TRACKER, tracker); } public InteractionResult addTracker(Player player) { this.entityData.set(HAS_TRACKER, true); Component confirmationMsg = new TextComponent("This " + this.getDisplayName().getString() + " now has a tracker"); player.sendMessage(confirmationMsg, Util.NIL_UUID); return InteractionResult.SUCCESS; } /* ----- DATA SYNC ----- */ @Override public void readAdditionalSaveData(CompoundTag tag) { super.readAdditionalSaveData(tag); this.entityData.set(DATA_ID_TYPE_VARIANT, tag.getInt("variant")); this.entityData.set(HAS_TRACKER, tag.getBoolean("hasTracker")); this.setTracker(tag.getBoolean("hasTracker")); } @Override public void addAdditionalSaveData(CompoundTag tag) { super.addAdditionalSaveData(tag); tag.putInt("variant", this.getTypeVariant()); tag.putBoolean("hasTracker", this.hasTracker()); } @Override protected void defineSynchedData() { super.defineSynchedData(); this.entityData.define(DATA_ID_TYPE_VARIANT, 0); this.entityData.define(HAS_TRACKER, false); } /* ----- INTERACTION WITH ITEM ----- */ @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { Item item = player.getItemInHand(hand).getItem(); Item addTrackerItem = ModItems.TRACKER.get(); if (item == addTrackerItem && !this.hasTracker()) { if (this.level.isClientSide) { return this.addTracker(player); } } return super.mobInteract(player, hand); } Edited August 17, 20223 yr by Myxtro Changed title to indicate that the problem has been solved
August 17, 20223 yr Author 10 minutes ago, diesieben07 said: You are calling addTracker on the client. SynchedEntityData syncs from server to client only. You should set the data on the server, not the client. What can I use to sync the data to the server instead?
August 17, 20223 yr Author Could you please elaborate? Because I thought the readAdditionalSaveData and addAdditionalSaveData took care of that.
August 17, 20223 yr 18 minutes ago, Myxtro said: Could you please elaborate? You call #addTracker in #mobInteract client side, you need to check if your not on client: if (!this.level.isClientSide) { // your code } 18 minutes ago, Myxtro said: Because I thought the readAdditionalSaveData and addAdditionalSaveData took care of that. The methods are used to store the data on disk, not sending them through the network Edited August 17, 20223 yr by Luis_ST
August 17, 20223 yr @Override public InteractionResult mobInteract(Player player, InteractionHand hand) { Item item = player.getItemInHand(hand).getItem(); Item addTrackerItem = ModItems.TRACKER.get(); if (item == addTrackerItem && !this.hasTracker()) { // HERE: You are trying to add the tracker on the client when it should be on the server if (this.level.isClientSide) { return this.addTracker(player); } } // HERE: This will return PASS which means the server never sees this interaction return super.mobInteract(player, hand); } Compare your code with for example how the creeper interacts with flint and steel. Edited August 17, 20223 yr by warjort Boilerplate: If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one. If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install Large files should be posted to a file sharing site like https://gist.github.com You should also read the support forum sticky post.
August 17, 20223 yr Author Thanks guys, the problem was indeed that I had to change this.level.isClientSide To !this.level.isClientSide
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.