Fix drive viewer blockstate improperly updating
This commit is contained in:
parent
61b87e02f0
commit
3358b5247e
@ -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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setChanged() {
|
||||
super.setChanged();
|
||||
private void updateState(Level level) {
|
||||
if (isRemoved())
|
||||
return;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setChanged() {
|
||||
super.setChanged();
|
||||
|
||||
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() {
|
||||
|
@ -284,7 +284,7 @@ public class MatteryContainer implements Container, Iterable<ItemStack> {
|
||||
|
||||
@Override
|
||||
public ItemStack getItem(int slot) {
|
||||
return slots[slot];
|
||||
return slots[slot].isEmpty() ? ItemStack.EMPTY : slots[slot];
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user