From d5e6afcaf7401bf6e244479e6e91101c4d7ad2ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Tue, 29 Dec 2020 18:06:30 +0100 Subject: [PATCH] Properly mark chunk dirty if computer inventory changes. Also update neighbors if this happens (batched, next frame), in case added/removed devices would affect neighbors. --- .../block/entity/ComputerTileEntity.java | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/li/cil/oc2/common/block/entity/ComputerTileEntity.java b/src/main/java/li/cil/oc2/common/block/entity/ComputerTileEntity.java index e80ebe37..7f631a1d 100644 --- a/src/main/java/li/cil/oc2/common/block/entity/ComputerTileEntity.java +++ b/src/main/java/li/cil/oc2/common/block/entity/ComputerTileEntity.java @@ -61,6 +61,7 @@ import org.jetbrains.annotations.NotNull; import javax.annotation.Nullable; import java.nio.ByteBuffer; import java.util.*; +import java.util.function.Function; public final class ComputerTileEntity extends AbstractTileEntity implements ITickableTileEntity { private static final Logger LOGGER = LogManager.getLogger(); @@ -106,13 +107,14 @@ public final class ComputerTileEntity extends AbstractTileEntity implements ITic private RunState runState; private ITextComponent bootError; private int loadDevicesDelay; + private boolean isNeighborUpdateScheduled; /////////////////////////////////////////////////////////////////// - private final DeviceItemStackHandler memoryItemHandler = new TypedDeviceItemStackHandler(MEMORY_SLOTS, this::getDevices, DeviceTypes.MEMORY); - private final DeviceItemStackHandler hardDriveItemHandler = new TypedDeviceItemStackHandler(HARD_DRIVE_SLOTS, this::getDevices, DeviceTypes.HARD_DRIVE); - private final DeviceItemStackHandler flashMemoryItemHandler = new TypedDeviceItemStackHandler(FLASH_MEMORY_SLOTS, this::getDevices, DeviceTypes.FLASH_MEMORY); - private final DeviceItemStackHandler cardItemHandler = new TypedDeviceItemStackHandler(CARD_SLOTS, this::getDevices, DeviceTypes.CARD); + private final DeviceItemStackHandler memoryItemHandler = new ComputerItemHandler(MEMORY_SLOTS, this::getDevices, DeviceTypes.MEMORY); + private final DeviceItemStackHandler hardDriveItemHandler = new ComputerItemHandler(HARD_DRIVE_SLOTS, this::getDevices, DeviceTypes.HARD_DRIVE); + private final DeviceItemStackHandler flashMemoryItemHandler = new ComputerItemHandler(FLASH_MEMORY_SLOTS, this::getDevices, DeviceTypes.FLASH_MEMORY); + private final DeviceItemStackHandler cardItemHandler = new ComputerItemHandler(CARD_SLOTS, this::getDevices, DeviceTypes.CARD); private final IItemHandler itemHandlers = new CombinedInvWrapper(memoryItemHandler, hardDriveItemHandler, flashMemoryItemHandler, cardItemHandler); @@ -289,6 +291,11 @@ public final class ComputerTileEntity extends AbstractTileEntity implements ITic chunk = world.getChunkAt(getPos()); } + if (isNeighborUpdateScheduled) { + isNeighborUpdateScheduled = false; + world.notifyNeighborsOfStateChange(getPos(), getBlockState().getBlock()); + } + busController.scan(); setBusState(busController.getState()); if (busState != AbstractDeviceBusController.BusState.READY) { @@ -572,6 +579,19 @@ public final class ComputerTileEntity extends AbstractTileEntity implements ITic /////////////////////////////////////////////////////////////////// + private final class ComputerItemHandler extends TypedDeviceItemStackHandler { + public ComputerItemHandler(final int size, final Function> deviceLookup, final DeviceType deviceType) { + super(size, deviceLookup, deviceType); + } + + @Override + protected void onContentsChanged(final int slot) { + super.onContentsChanged(slot); + markDirty(); + isNeighborUpdateScheduled = true; + } + } + private final class BusController extends TileEntityDeviceBusController { private BusController(final DeviceBusElement root) { super(root, ComputerTileEntity.this);