TheOrangeInd Posted April 20, 2021 Posted April 20, 2021 I have fluid pipes in my mod and I use a custom Grid object to distribute fluid in this pipes. This Grid object does not inherit either Tilentity or Entity, but I need to write data from it to the world NBT. The only solution that I have found is to create my own class that inherits WorldSavedData. The problem is that I don't quite understand how to use it correctly in newer versions of the game and I'm not sure if this is the best way. I would be very grateful for any advice. Custom WorldSavedData implementation: public class NBTHandler extends WorldSavedData { private static final String DATA_IDENTIFIER = MainClass.MODID + "_nbt"; public final Set<ISaveable> subscribers = new HashSet(); public NBTHandler() { this(DATA_IDENTIFIER); } public NBTHandler(String name) { super(name); } public static NBTHandler get(World world) { if(!(world instanceof ServerWorld)) throw new RuntimeException("Attempted to get data from client."); ServerWorld w = (ServerWorld)world; DimensionSavedDataManager storage = w.getSavedData(); return storage.getOrCreate(NBTHandler::new, DATA_IDENTIFIER); } public void addSubscriber(ISaveable s) { subscribers.add(s); } @Override public boolean isDirty() { boolean flag = false; for (ISaveable item : subscribers) flag &= item.isDirty(); return super.isDirty() || flag; } @Override public void read(CompoundNBT nbt) { subscribers.forEach(item -> item.readFromNBT(nbt)); } @Override public CompoundNBT write(CompoundNBT nbt) { subscribers.forEach(item -> item.writeToNBT(nbt)); return nbt; } } Quote
TheOrangeInd Posted April 20, 2021 Author Posted April 20, 2021 The point is that my Grid is not tied to any position in the world. It acts as a data store for connected pipes, from which these pipes can extract information about the contained fluid when interacting with the world. So, is there a way to avoid binding the Grid to specific chunks? Quote
TheOrangeInd Posted April 20, 2021 Author Posted April 20, 2021 Is there any recent documentation for WorldSaveData? I have a rather poor understanding of how it should be attached to the world. Quote
TheOrangeInd Posted April 20, 2021 Author Posted April 20, 2021 Should I call the read and write methods of my NBTHandler in any event? Quote
TheOrangeInd Posted April 20, 2021 Author Posted April 20, 2021 For some reason, the read(), wright() and isDirty() methods are not called. I cannot figure out what I am missing. Quote
TheOrangeInd Posted April 20, 2021 Author Posted April 20, 2021 (edited) After fixing some mistakes, I managed to write my Grid to the world NBT. But the reading is still not happening. Under what conditions is the read() method called? NBTHandler class: public class NBTHandler extends WorldSavedData { private static final String DATA_IDENTIFIER = MainClass.MODID + "_nbt"; public final Set<ISaveable> subscribers = new HashSet(); public NBTHandler() { this(DATA_IDENTIFIER); } public NBTHandler(String name) { super(name); } public static NBTHandler get(World world) { if(!(world instanceof ServerWorld)) throw new RuntimeException("Attempted to get data from client."); DimensionSavedDataManager storage = ServerLifecycleHooks.getCurrentServer().func_241755_D_().getWorld().getSavedData(); //DimensionSavedDataManager storage = ((ServerWorld)world).getSavedData(); return storage.getOrCreate(NBTHandler::new, DATA_IDENTIFIER); } public void addSubscriber(ISaveable s) { subscribers.add(s); } @Override public boolean isDirty() { boolean flag = false; for (ISaveable item : subscribers) flag |= item.isDirty(); return super.isDirty() || flag; } @Override public void read(CompoundNBT nbt) { subscribers.forEach(item -> item.readFromNBT(nbt)); } @Override public CompoundNBT write(CompoundNBT nbt) { subscribers.forEach(item -> item.writeToNBT(nbt)); return nbt; } } And get() method call in my Grid class: @Override public boolean addPipe(IPipe pipe) { if(this.world == null) { world = ((TileEntity)pipe).getWorld(); if(world != null) NBTHandler.get(world).addSubscriber(this); } /*some code*/ return false; } Edited April 20, 2021 by TheOrangeInd Quote
Recommended Posts
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.