diff --git a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityDriveViewer.java b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityDriveViewer.java index 5309ac16c..aae9d55ab 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityDriveViewer.java +++ b/src/main/java/ru/dbotthepony/mc/otm/block/entity/BlockEntityDriveViewer.java @@ -7,11 +7,13 @@ import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; +import ru.dbotthepony.mc.otm.OverdriveThatMatters; import ru.dbotthepony.mc.otm.Registry; import ru.dbotthepony.mc.otm.block.BlockDriveViewer; import ru.dbotthepony.mc.otm.block.entity.worker.WorkerState; @@ -30,37 +32,29 @@ public class BlockEntityDriveViewer extends BlockEntityMatteryPowered { return energy.getBatteryLevel().compareTo(MTE_PER_OPERATION) >= 0; } + private void updateState(Level level) { + if (isRemoved()) + return; + + var state = getBlockState(); + + if (drive_slot.getItem(0).getCapability(MatteryCapability.DRIVE).isPresent() && canIOItems()) { + state = state.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.WORKING); + } else { + state = state.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE); + } + + if (state != getBlockState()) { + level.setBlock(getBlockPos(), state, Block.UPDATE_CLIENTS); + } + } + @Override public void setChanged() { super.setChanged(); - if (level != null) { - var state = getBlockState(); - - if (drive_slot.getItem(0).getCapability(MatteryCapability.DRIVE).isPresent()) { - if (!getBlockState().getValue(BlockDriveViewer.DRIVE_PRESENT)) { - state = state.setValue(BlockDriveViewer.DRIVE_PRESENT, true); - } - } else { - if (getBlockState().getValue(BlockDriveViewer.DRIVE_PRESENT)) { - state = state.setValue(BlockDriveViewer.DRIVE_PRESENT, false); - } - } - - if (drive_slot.getItem(0).getCapability(MatteryCapability.DRIVE).isPresent() && canIOItems()) { - if (getBlockState().getValue(WorkerState.SEMI_WORKER_STATE) != WorkerState.WORKING) { - state = state.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.WORKING); - } - } else { - if (getBlockState().getValue(WorkerState.SEMI_WORKER_STATE) != WorkerState.IDLE) { - state = state.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE); - } - } - - if (state != getBlockState()) { - level.setBlock(getBlockPos(), state, Block.UPDATE_CLIENTS); - } - } + if (level != null) + OverdriveThatMatters.tickOnce(level, this::updateState); } public int getIOItemCount(int desired, boolean simulate) { @@ -85,7 +79,29 @@ public class BlockEntityDriveViewer extends BlockEntityMatteryPowered { private static final TranslatableComponent NAME = new TranslatableComponent("block.overdrive_that_matters.drive_viewer"); - public final MatteryContainer drive_slot = new MatteryContainer(this::setChanged, 1); + public final MatteryContainer drive_slot = new MatteryContainer(this::setChanged, 1) { + @Override + public void setChanged(int slot, ItemStack new_state, ItemStack old_state) { + super.setChanged(slot, new_state, old_state); + + if (level != null) + OverdriveThatMatters.tickOnce(level, (level) -> { + if (!isRemoved()) { + var state = getBlockState(); + + if (new_state.getCapability(MatteryCapability.DRIVE).isPresent()) { + state = state.setValue(BlockDriveViewer.DRIVE_PRESENT, true); + } else { + state = state.setValue(BlockDriveViewer.DRIVE_PRESENT, false); + } + + if (state != getBlockState()) { + level.setBlock(getBlockPos(), state, Block.UPDATE_CLIENTS); + } + } + }); + } + }; @Override protected Component getDefaultDisplayName() { diff --git a/src/main/java/ru/dbotthepony/mc/otm/container/MatteryContainer.java b/src/main/java/ru/dbotthepony/mc/otm/container/MatteryContainer.java index 184122ee0..469bc9336 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/container/MatteryContainer.java +++ b/src/main/java/ru/dbotthepony/mc/otm/container/MatteryContainer.java @@ -284,7 +284,7 @@ public class MatteryContainer implements Container, Iterable { @Override public ItemStack getItem(int slot) { - return slots[slot]; + return slots[slot].isEmpty() ? ItemStack.EMPTY : slots[slot]; } @Override