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