Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

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

 

  • Author

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?

  • Author

Is there any recent documentation for WorldSaveData? I have a rather poor understanding of how it should be attached to the world.

  • Author

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

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.