Jump to content

Recommended Posts

Posted

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

 

Posted

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?

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

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.