#what is the best way to attach a single object to multiple BlockEntities?

4 messages · Page 1 of 1 (latest)

sturdy ridge
#

I'm trying to implement a multi-block struture of block entities. i encoutered one issue however, how to make is so all of those block entities have a reference to the same instance of shared-state object. I thought about saving it in all of the blocks and then do something along the lines of:

/// in Block Entity

MySharedStateInfo info = null; // all the data needed to create MySharedState
MySharedState state = null; // the actual shared state

@Override
public void readNbt(NbtCompound nbt) {
    super.readNbt(nbt);
    this.info = new MySharedStateInfo(nbt); 
    this.state = null;
}

MySharedState get() {
    if (state == null) {
        // duck interface over a basic concurent hash map in the world class
        WorldSharedStateView view = WorldSharedStateView.of(this.world);
  
        // wrapper over computeIfAbsent
        this.state = view.example_getOrCreateSharedState(this.pos, this.info);
    }

    return this.state;
}

so that only one instance is created, but idk if this is a good way to do this
it would require a lot of redundancy (saving the whole thing multiple times)
i thought about just saving the map in the World instance but i don't have experiance with doing that so i first thought about saving it just in the blocks

maiden ravine
#

One way to do it is to designate one of the blocks as controller and have it store the state. Sometimes this makes sense from a gameplay perspective, but sometime it might not work well.

#

You could also use PersistentState to store the data in world.

#

Another option, that might work especially well if you allow for arbitrary shapes for your multiblocks is using graphlib. With graphlib you can use a GraphEntity to store the state of the multiblock. However, graphlib isn't really well suited to fixed shape multiblocks