Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16.4] Write custom object to World NBT


Recommended Posts

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

 

Link to post
Share on other sites

It looks like you are missing any kind of loading mechanism that keeps track of which data needs to be loaded and which doesn't. It would be better to only keep things loaded for loaded chunks, so a chunk capability would be a better way.

Link to post
Share on other sites

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?

Link to post
Share on other sites
51 minutes ago, TheOrangeInd said:

Should I call the read and write methods of my NBTHandler in any event?

No. Minecraft will save the WorldSavedData to disk when it saves the world, provided isDirty returns true.

  • Like 1
Link to post
Share on other sites
Posted (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 by TheOrangeInd
Link to post
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.

Guest
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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.