Jump to content

[SOLVED][1.18.2] Saving data to entity not working (with EntityDataAccessor)


Myxtro

Recommended Posts

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 by Myxtro
Changed title to indicate that the problem has been solved
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 by Luis_ST
  • Like 1
Link to comment
Share on other sites

  

    @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 by warjort
  • Like 1

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.

Link to comment
Share on other sites

  • Myxtro changed the title to [SOLVED][1.18.2] Saving data to entity not working (with EntityDataAccessor)

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.



×
×
  • Create New...

Important Information

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