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();
}
}
}