Skip to content

Commit

Permalink
fix: crashing issues due to node desync
Browse files Browse the repository at this point in the history
  • Loading branch information
raoulvdberge committed Nov 5, 2023
1 parent 327c97b commit 9b30bec
Show file tree
Hide file tree
Showing 26 changed files with 67 additions and 42 deletions.
9 changes: 5 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
apply from: "https://raw.githubusercontent.com/refinedmods/refinedarchitect/v0.7.1/helper.gradle"

ext {
forgeVersion = "47.1.0"
}

repositories {
maven {
name = 'JEI'
Expand Down Expand Up @@ -38,10 +42,7 @@ dependencies {

compileOnly fg.deobf('curse.maven:crafting-tweaks-233071:4596466')

// TODO mixin issue
// runtimeOnly fg.deobf("top.theillusivec4.curios:curios-forge:5.2.0-beta.3+1.20.1")
// can't depend on API due to Javadoc build error (CuriosApi imports stuff from common)
compileOnly fg.deobf("top.theillusivec4.curios:curios-forge:5.2.0-beta.3+1.20.1")
implementation fg.deobf("top.theillusivec4.curios:curios-forge:5.2.0-beta.3+1.20.1")
}

minecraft {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public BlockState updateShape(BlockState state, Direction dir, BlockState facing
}

@Override
@SuppressWarnings("deprecation")
public boolean isPathfindable(BlockState state, BlockGetter worldIn, BlockPos pos, PathComputationType type) {
return false;
}
Expand Down Expand Up @@ -197,8 +198,8 @@ private boolean hasNodeConnection(LevelAccessor world, BlockPos pos, BlockState
}

return blockEntity.getCapability(NetworkNodeProxyCapability.NETWORK_NODE_PROXY_CAPABILITY, direction).isPresent()
&& !isSideCovered(blockEntity, direction)
&& !isSideCovered(world.getBlockEntity(pos.relative(direction)), direction.getOpposite());
&& !isSideCovered(blockEntity, direction)
&& !isSideCovered(world.getBlockEntity(pos.relative(direction)), direction.getOpposite());
}

private boolean isSideCovered(BlockEntity blockEntity, Direction direction) {
Expand Down Expand Up @@ -229,12 +230,12 @@ private BlockState getState(BlockState currentState, LevelAccessor world, BlockP
boolean down = hasNodeConnection(world, pos.relative(Direction.DOWN), currentState, Direction.UP);

return currentState
.setValue(NORTH, north)
.setValue(EAST, east)
.setValue(SOUTH, south)
.setValue(WEST, west)
.setValue(UP, up)
.setValue(DOWN, down);
.setValue(NORTH, north)
.setValue(EAST, east)
.setValue(SOUTH, south)
.setValue(WEST, west)
.setValue(UP, up)
.setValue(DOWN, down);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CableBlockEntity extends NetworkNodeBlockEntity<CableNetworkNode> {
.build();

public CableBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.CABLE.get(), pos, state, SPEC);
super(RSBlockEntities.CABLE.get(), pos, state, SPEC, CableNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class ConstructorBlockEntity extends NetworkNodeBlockEntity<ConstructorNe
.build();

public ConstructorBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.CONSTRUCTOR.get(), pos, state, SPEC);
super(RSBlockEntities.CONSTRUCTOR.get(), pos, state, SPEC, ConstructorNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class CrafterBlockEntity extends NetworkNodeBlockEntity<CrafterNetworkNod
private final LazyOptional<IItemHandler> patternsCapability = LazyOptional.of(() -> getNode().getPatternInventory());

public CrafterBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.CRAFTER.get(), pos, state, SPEC);
super(RSBlockEntities.CRAFTER.get(), pos, state, SPEC, CrafterNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class CrafterManagerBlockEntity extends NetworkNodeBlockEntity<CrafterMan
.build();

public CrafterManagerBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.CRAFTER_MANAGER.get(), pos, state, SPEC);
super(RSBlockEntities.CRAFTER_MANAGER.get(), pos, state, SPEC, CrafterManagerNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class DestructorBlockEntity extends NetworkNodeBlockEntity<DestructorNetw
.build();

public DestructorBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.DESTRUCTOR.get(), pos, state, SPEC);
super(RSBlockEntities.DESTRUCTOR.get(), pos, state, SPEC, DestructorNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class DetectorBlockEntity extends NetworkNodeBlockEntity<DetectorNetworkN
private static final String NBT_POWERED = "Powered";

public DetectorBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.DETECTOR.get(), pos, state, SPEC);
super(RSBlockEntities.DETECTOR.get(), pos, state, SPEC, DetectorNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class DiskDriveBlockEntity extends NetworkNodeBlockEntity<DiskDriveNetwor
private final DiskState[] diskState = new DiskState[8];

public DiskDriveBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.DISK_DRIVE.get(), pos, state, SPEC);
super(RSBlockEntities.DISK_DRIVE.get(), pos, state, SPEC, DiskDriveNetworkNode.class);
Arrays.fill(diskState, DiskState.NONE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class DiskManipulatorBlockEntity extends NetworkNodeBlockEntity<DiskManip
private final DiskState[] diskState = new DiskState[6];

public DiskManipulatorBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.DISK_MANIPULATOR.get(), pos, state, SPEC);
super(RSBlockEntities.DISK_MANIPULATOR.get(), pos, state, SPEC, DiskManipulatorNetworkNode.class);
Arrays.fill(diskState, DiskState.NONE);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ExporterBlockEntity extends NetworkNodeBlockEntity<ExporterNetworkN
.build();

public ExporterBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.EXPORTER.get(), pos, state, SPEC);
super(RSBlockEntities.EXPORTER.get(), pos, state, SPEC, ExporterNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.refinedmods.refinedstorage.blockentity.data.BlockEntitySynchronizationParameter;
import com.refinedmods.refinedstorage.blockentity.data.BlockEntitySynchronizationSpec;
import com.refinedmods.refinedstorage.blockentity.data.RSSerializers;
import com.refinedmods.refinedstorage.config.ServerConfig;
import com.refinedmods.refinedstorage.util.LevelUtils;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -72,7 +73,7 @@ public class ExternalStorageBlockEntity extends NetworkNodeBlockEntity<ExternalS
.build();

public ExternalStorageBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.EXTERNAL_STORAGE.get(), pos, state, SPEC);
super(RSBlockEntities.EXTERNAL_STORAGE.get(), pos, state, SPEC, ExternalStorageNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ public class FluidInterfaceBlockEntity extends NetworkNodeBlockEntity<FluidInter
public static final BlockEntitySynchronizationParameter<FluidStack, FluidInterfaceBlockEntity> TANK_OUT = new BlockEntitySynchronizationParameter<>(new ResourceLocation(RS.ID, "fluid_interface_out"), RSSerializers.FLUID_STACK_SERIALIZER, FluidStack.EMPTY, t -> t.getNode().getTankOut().getFluid());

public static BlockEntitySynchronizationSpec SPEC = BlockEntitySynchronizationSpec.builder()
.addWatchedParameter(REDSTONE_MODE)
.addParameter(TANK_IN)
.addParameter(TANK_OUT)
.build();
.addWatchedParameter(REDSTONE_MODE)
.addParameter(TANK_IN)
.addParameter(TANK_OUT)
.build();

private final LazyOptional<IFluidHandler> tankCapability = LazyOptional.of(() -> getNode().getTank());
private final LazyOptional<IItemHandler> inCapability = LazyOptional.of(() -> getNode().getIn());

public FluidInterfaceBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.FLUID_INTERFACE.get(), pos, state, SPEC);
super(RSBlockEntities.FLUID_INTERFACE.get(), pos, state, SPEC, FluidInterfaceNetworkNode.class);
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class FluidStorageBlockEntity extends NetworkNodeBlockEntity<FluidStorage
private final FluidStorageType type;

public FluidStorageBlockEntity(FluidStorageType type, BlockPos pos, BlockState state) {
super(getType(type), pos, state, SPEC);
super(getType(type), pos, state, SPEC, FluidStorageNetworkNode.class);
this.type = type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ImporterBlockEntity extends NetworkNodeBlockEntity<ImporterNetworkN
.build();

public ImporterBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.IMPORTER.get(), pos, state, SPEC);
super(RSBlockEntities.IMPORTER.get(), pos, state, SPEC, ImporterNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class InterfaceBlockEntity extends NetworkNodeBlockEntity<InterfaceNetwor
private final LazyOptional<IItemHandler> itemsCapability = LazyOptional.of(() -> getNode().getItems());

public InterfaceBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.INTERFACE.get(), pos, state, SPEC);
super(RSBlockEntities.INTERFACE.get(), pos, state, SPEC, InterfaceNetworkNode.class);
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,26 @@
import javax.annotation.Nullable;

public abstract class NetworkNodeBlockEntity<N extends NetworkNode> extends BaseBlockEntity implements INetworkNodeProxy<N>, IRedstoneConfigurable {
public static final BlockEntitySynchronizationParameter<Integer, NetworkNodeBlockEntity> REDSTONE_MODE = RedstoneMode.createParameter(new ResourceLocation(RS.ID, "redstone_mode"));
public static final BlockEntitySynchronizationParameter<Integer, NetworkNodeBlockEntity<?>> REDSTONE_MODE = RedstoneMode.createParameter(new ResourceLocation(RS.ID, "redstone_mode"));

private final LazyOptional<INetworkNodeProxy<N>> networkNodeProxy = LazyOptional.of(() -> this);
private final Class<N> networkNodeClass;
private N clientNode;
private N removedNode;

private static final Logger LOGGER = LogManager.getLogger();

// TODO: remove this ctor in 1.21
@Deprecated
protected NetworkNodeBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state, BlockEntitySynchronizationSpec syncSpec) {
super(type, pos, state, syncSpec);
this.networkNodeClass = null;
}

protected NetworkNodeBlockEntity(BlockEntityType<?> type, BlockPos pos, BlockState state, BlockEntitySynchronizationSpec syncSpec,
Class<N> networkNodeClass) {
super(type, pos, state, syncSpec);
this.networkNodeClass = networkNodeClass;
}

@Override
Expand Down Expand Up @@ -72,13 +82,20 @@ public N getNode() {
node = createAndSetNode(manager);
}

return (N) node;
return doCast(node);
} catch (ClassCastException e) {
LOGGER.warn("Node @ {} got desynced with it's block entity container, recreating", worldPosition, e);
return (N) createAndSetNode(manager);
}
}

private N doCast(INetworkNode node) {
if (networkNodeClass == null) {
return (N) node;
}
return networkNodeClass.cast(node);
}

private INetworkNode createAndSetNode(INetworkNodeManager manager) {
INetworkNode node = createNode(level, worldPosition);
manager.setNode(worldPosition, node);
Expand Down Expand Up @@ -110,7 +127,11 @@ public void onRemovedNotDueToChunkUnload() {
INetworkNode node = manager.getNode(worldPosition);

if (node != null) {
removedNode = (N) node;
try {
removedNode = doCast(node);
} catch (ClassCastException e) {
removedNode = null;
}
}

manager.removeNode(worldPosition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class NetworkReceiverBlockEntity extends NetworkNodeBlockEntity<NetworkRe
.build();

public NetworkReceiverBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.NETWORK_RECEIVER.get(), pos, state, SPEC);
super(RSBlockEntities.NETWORK_RECEIVER.get(), pos, state, SPEC, NetworkReceiverNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class NetworkTransmitterBlockEntity extends NetworkNodeBlockEntity<Networ
private final LazyOptional<IItemHandler> networkCardCapability = LazyOptional.of(() -> getNode().getNetworkCard());

public NetworkTransmitterBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.NETWORK_TRANSMITTER.get(), pos, state, SPEC);
super(RSBlockEntities.NETWORK_TRANSMITTER.get(), pos, state, SPEC, NetworkTransmitterNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class RelayBlockEntity extends NetworkNodeBlockEntity<RelayNetworkNode> {
.build();

public RelayBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.RELAY.get(), pos, state, SPEC);
super(RSBlockEntities.RELAY.get(), pos, state, SPEC, RelayNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SecurityManagerBlockEntity extends NetworkNodeBlockEntity<SecurityM
.build();

public SecurityManagerBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.SECURITY_MANAGER.get(), pos, state, SPEC);
super(RSBlockEntities.SECURITY_MANAGER.get(), pos, state, SPEC, SecurityManagerNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class StorageBlockEntity extends NetworkNodeBlockEntity<StorageNetworkNod
private final ItemStorageType type;

public StorageBlockEntity(ItemStorageType type, BlockPos pos, BlockState state) {
super(getType(type), pos, state, SPEC);
super(getType(type), pos, state, SPEC, StorageNetworkNode.class);
this.type = type;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.refinedmods.refinedstorage.blockentity.config.IType;
import com.refinedmods.refinedstorage.blockentity.data.BlockEntitySynchronizationParameter;
import com.refinedmods.refinedstorage.blockentity.data.BlockEntitySynchronizationSpec;
import com.refinedmods.refinedstorage.config.ServerConfig;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -40,7 +41,7 @@ public class StorageMonitorBlockEntity extends NetworkNodeBlockEntity<StorageMon
private FluidStack fluidStack = FluidStack.EMPTY;

public StorageMonitorBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.STORAGE_MONITOR.get(), pos, state, SPEC);
super(RSBlockEntities.STORAGE_MONITOR.get(), pos, state, SPEC, StorageMonitorNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class WirelessTransmitterBlockEntity extends NetworkNodeBlockEntity<Wirel
.build();

public WirelessTransmitterBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.WIRELESS_TRANSMITTER.get(), pos, state, SPEC);
super(RSBlockEntities.WIRELESS_TRANSMITTER.get(), pos, state, SPEC, WirelessTransmitterNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class CraftingMonitorBlockEntity extends NetworkNodeBlockEntity<CraftingM
.build();

public CraftingMonitorBlockEntity(BlockPos pos, BlockState state) {
super(RSBlockEntities.CRAFTING_MONITOR.get(), pos, state, SPEC);
super(RSBlockEntities.CRAFTING_MONITOR.get(), pos, state, SPEC, CraftingMonitorNetworkNode.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static void trySortGrid(boolean initial) {
private final LazyOptional<IItemHandler> diskCapability = LazyOptional.of(() -> getNode().getPatterns());

public GridBlockEntity(GridType type, BlockPos pos, BlockState state) {
super(getType(type), pos, state, SPEC);
super(getType(type), pos, state, SPEC, GridNetworkNode.class);
this.type = type;
}

Expand Down

0 comments on commit 9b30bec

Please sign in to comment.