Posted January 10, 20223 yr 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 January 10, 20223 yr by squidlex Clarification
January 10, 20223 yr 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.
January 11, 20223 yr 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?
January 11, 20223 yr 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.