Fix networks and network devices being removed when a chunk unloads. Fixes #3178

This commit is contained in:
raoulvdberge
2021-12-17 12:06:04 +01:00
parent a4ebfe190d
commit a522edea08
4 changed files with 33 additions and 6 deletions

View File

@@ -7,6 +7,9 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] ## [Unreleased]
### Fixed
- Fixed networks and network devices being removed when a chunk unloads.
## [v1.10.0-beta.2] - 2021-12-16 ## [v1.10.0-beta.2] - 2021-12-16
### Fixed ### Fixed

View File

@@ -12,6 +12,8 @@ import net.minecraft.world.level.block.state.BlockState;
public abstract class BaseBlockEntity extends BlockEntity { public abstract class BaseBlockEntity extends BlockEntity {
protected final BlockEntitySynchronizationManager dataManager = new BlockEntitySynchronizationManager(this); protected final BlockEntitySynchronizationManager dataManager = new BlockEntitySynchronizationManager(this);
private boolean unloaded;
public BaseBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) { public BaseBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state) {
super(type, pos, state); super(type, pos, state);
} }
@@ -48,6 +50,28 @@ public abstract class BaseBlockEntity extends BlockEntity {
readUpdate(tag); readUpdate(tag);
} }
@Override
public void setRemoved() {
super.setRemoved();
// @Volatile: MC calls setRemoved when a chunk unloads now as well (see ServerLevel#unload -> LevelChunk#clearAllBlockEntities).
// Since we don't want to remove network node data in that case, we need to know if it was removed due to unloading.
// We can use "unloaded" for that, it's set in #onChunkUnloaded.
// Since MC first calls #onChunkUnloaded and then #setRemoved, this check keeps working.
if (!unloaded) {
onRemovedNotDueToChunkUnload();
}
}
protected void onRemovedNotDueToChunkUnload() {
// NO OP
}
@Override
public void onChunkUnloaded() {
super.onChunkUnloaded();
unloaded = true;
}
// @Volatile: Copied with some changes from the super method (avoid sending neighbor updates, it's not needed) // @Volatile: Copied with some changes from the super method (avoid sending neighbor updates, it's not needed)
@Override @Override
public void setChanged() { public void setChanged() {

View File

@@ -9,11 +9,11 @@ import com.refinedmods.refinedstorage.api.network.node.INetworkNodeProxy;
import com.refinedmods.refinedstorage.apiimpl.API; import com.refinedmods.refinedstorage.apiimpl.API;
import com.refinedmods.refinedstorage.apiimpl.network.Network; import com.refinedmods.refinedstorage.apiimpl.network.Network;
import com.refinedmods.refinedstorage.apiimpl.network.node.RootNetworkNode; import com.refinedmods.refinedstorage.apiimpl.network.node.RootNetworkNode;
import com.refinedmods.refinedstorage.capability.NetworkNodeProxyCapability;
import com.refinedmods.refinedstorage.blockentity.config.IRedstoneConfigurable; import com.refinedmods.refinedstorage.blockentity.config.IRedstoneConfigurable;
import com.refinedmods.refinedstorage.blockentity.config.RedstoneMode; import com.refinedmods.refinedstorage.blockentity.config.RedstoneMode;
import com.refinedmods.refinedstorage.blockentity.data.RSSerializers;
import com.refinedmods.refinedstorage.blockentity.data.BlockEntitySynchronizationParameter; import com.refinedmods.refinedstorage.blockentity.data.BlockEntitySynchronizationParameter;
import com.refinedmods.refinedstorage.blockentity.data.RSSerializers;
import com.refinedmods.refinedstorage.capability.NetworkNodeProxyCapability;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.network.syncher.EntityDataSerializers; import net.minecraft.network.syncher.EntityDataSerializers;
@@ -116,8 +116,8 @@ public class ControllerBlockEntity extends BaseBlockEntity implements INetworkNo
} }
@Override @Override
public void setRemoved() { public void onRemovedNotDueToChunkUnload() {
super.setRemoved(); super.onRemovedNotDueToChunkUnload();
if (!level.isClientSide) { if (!level.isClientSide) {
INetworkManager manager = API.instance().getNetworkManager((ServerLevel) level); INetworkManager manager = API.instance().getNetworkManager((ServerLevel) level);

View File

@@ -82,8 +82,8 @@ public abstract class NetworkNodeBlockEntity<N extends NetworkNode> extends Base
} }
@Override @Override
public void setRemoved() { public void onRemovedNotDueToChunkUnload() {
super.setRemoved(); super.onRemovedNotDueToChunkUnload();
if (!level.isClientSide) { if (!level.isClientSide) {
INetworkNodeManager manager = API.instance().getNetworkNodeManager((ServerLevel) level); INetworkNodeManager manager = API.instance().getNetworkNodeManager((ServerLevel) level);