Posted May 17, 20205 yr 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 May 17, 20205 yr by wefewdsad
May 17, 20205 yr 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!
May 17, 20205 yr 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.