Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I was wondering what the best way to store persistent data in an entity is.

Right now, I'm attaching a custom capability to my entity and attempting to store the capability's data in EntityDataAccessors  every time the Entity is constructed.

This isn't working, and I was wondering if there was a better way of doing this?

Thank you!

 

Edit: By non-volatile I mean saves the data to the specific instance of the entity even when the game is restarted

Edited by squidlex
Clarification

  • Author

Thank you for your help! I'm only using the accessors for my own Entity and I'm implementing INBTSerializable in my provider so this shouldn't cause any issues.

I'm trying to access my capability in my Entity's constructor but it isn't working, however it works if I try to access it in another method such as interactAt(). Do you know if there is a way I can access my attached capability when my entity is being constructed? Thanks again.

 

  • Author

Thanks for the tip! I've now got:

	@Override
	public void readAdditionalSaveData(CompoundTag pCompound) {
		super.readAdditionalSaveData(pCompound);
		this.nameId = pCompound.getInt("nameId");
	}
	
	@Override
	public void addAdditionalSaveData(CompoundTag pCompound) {
		super.addAdditionalSaveData(pCompound);
		pCompound.putInt("nameId", this.nameId);
	}

but this is not saving correctly. Do I need to call these methods myself?

  • Author

Sorry, I should have been more clear.

I am checking my field on the client side in an interaction result, where if they are their default value of -1 I randomly generate a new value:

	@Override
	public InteractionResult interactAt(Player player, Vec3 position, InteractionHand hand) {
		if (level.isClientSide()) {
			if(this.nameId < 0) {
			this.nameId = level.random.nextInt(10);
		}
			GuiWrapper.openGUI(level, player, this);
			System.out.println("Client: " + this.nameId); // I know this is inefficient but just for testing purposes
		} 
		
		return super.interactAt(player, position, hand);
	}

And here is the field:

	private int nameId = -1;

The issue is that every time I close and open the world, they reset to -1 and a new value is generated.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.