Jump to content

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


wefewdsad

Recommended Posts

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Announcements



×
×
  • Create New...

Important Information

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