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.

[1.15.2] [Solved] TileEntity data is reset on world load

Featured Replies

Posted

Hello, I am currently making my first mod and am currently struggling with tile entities. When the block is placed it is working fine, however, if i save and quit to title and start the world again, the completionBlocks list does not have anything in it. 

I tried placing breakpoints on CompletionBlockTileEntity#write and CompletionBlockTileEntity#read, and noticed read is not triggered when the world is loaded so I guess that is why completionBlocks is empty?

 

How do I solve this problem? Is this even the proper way of storing data for a tile entity? I read stuff about capabilities but don't really see why that should be used here.

 

My TileEntity

public class CompletionBlockTileEntity extends TileEntity {
    public List<MyVector> completionBlocks = null;

    private final String delimiter = "_";
    private final String nbtName = "locations";

    public CompletionBlockTileEntity() {
        super(BorisRegistry.completion_block_entity.get());
        if(completionBlocks == null) {
            completionBlocks = new ArrayList<>();
        }
    }
  
    ...
  
    // region Storage
    private String listToString() {
        return completionBlocks.stream().map(MyVector::toStringRounded).collect(Collectors.joining(delimiter));
    }

    private List<MyVector> stringToList(String string) {
        return Arrays.stream(string.split(delimiter)).map(MyVector::new).collect(Collectors.toList());
    }

    @Override
    public CompoundNBT write(CompoundNBT nbt) {
        nbt.putString(nbtName, listToString());
        return nbt;
    }

    @Override
    public void read(CompoundNBT nbt) {
        completionBlocks = stringToList(nbt.getString(nbtName));
    }
    // endregion
}

 

The code that sets completionBlocks.

    public void Spawn() {
        if(!world.isRemote) {
            for (Map.Entry<MyVector, BlockState> entry : structure.entrySet()) {
                BlockPos position = entry.getKey().Add(this.coordinates).toBlockPos();
                BlockState block = entry.getValue();
              
                ...
              
                if(block == BorisRegistry.completion_block.get().getDefaultState()){
                    completionBlocks.add(new MyVector(position));
                }
            }

            for(MyVector blockloc : completionBlocks) {
                CompletionBlockTileEntity cbte = (CompletionBlockTileEntity) this.world.getTileEntity(blockloc.toBlockPos());
                cbte.completionBlocks = completionBlocks;
                cbte.markDirty();
            }
        }
    }

 

Edited by wefewdsad

  • Author
6 minutes ago, diesieben07 said:

You must call super in both read and write.

Wow that solved it, cant believe it was that simple. Thank you very much!

 

40 minutes ago, wefewdsad said:

Wow that solved it, cant believe it was that simple. Thank you very much!

 

You should have a look at its implementation in the super class when you override a method. In most case, you need to call the super method.

My mod: SimElectricity - High Voltages, Electrical Power Transmission & Distribution

https://github.com/RoyalAliceAcademyOfSciences/SimElectricity

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.