Make setChanged be more robust

This commit is contained in:
DBotThePony 2021-08-19 00:42:35 +07:00
parent 86f2e98df3
commit 33a2000705
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 36 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot; import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import ru.dbotthepony.mc.otm.menu.slot.MatterySlot;
import ru.dbotthepony.mc.otm.menu.widget.AbstractWidget; import ru.dbotthepony.mc.otm.menu.widget.AbstractWidget;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -106,6 +107,22 @@ public abstract class MatteryMenu extends AbstractContainerMenu {
abstract protected int getWorkingSlotStart(); abstract protected int getWorkingSlotStart();
abstract protected int getWorkingSlotEnd(); abstract protected int getWorkingSlotEnd();
protected void notifySlotsWatchCurrentStack() {
for (var slot : slots) {
if (slot instanceof MatterySlot slot1) {
slot1.watchCurrentStack();
}
}
}
protected void notifySlotsUnwatchCurrentStack() {
for (var slot : slots) {
if (slot instanceof MatterySlot slot1) {
slot1.unwatchCurrentStack();
}
}
}
// This method receive Player interactor and slot_index where Shift + Right click occurred // This method receive Player interactor and slot_index where Shift + Right click occurred
// It shall return item stack that got moved // It shall return item stack that got moved
@Override @Override
@ -126,6 +143,7 @@ public abstract class MatteryMenu extends AbstractContainerMenu {
ItemStack moved = ItemStack.EMPTY; ItemStack moved = ItemStack.EMPTY;
Slot get_slot = this.slots.get(slot_index); Slot get_slot = this.slots.get(slot_index);
notifySlotsWatchCurrentStack();
if (get_slot.hasItem()) { if (get_slot.hasItem()) {
ItemStack slot_item = get_slot.getItem(); ItemStack slot_item = get_slot.getItem();
@ -135,10 +153,12 @@ public abstract class MatteryMenu extends AbstractContainerMenu {
// Moving FROM machine TO inventory // Moving FROM machine TO inventory
if (!moveItemStackTo(slot_item, inventory_slot_index_start, inventory_slot_index_end + 1, false)) { if (!moveItemStackTo(slot_item, inventory_slot_index_start, inventory_slot_index_end + 1, false)) {
notifySlotsUnwatchCurrentStack();
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
} else if (!moveItemStackTo(slot_item, start, end, false)) { } else if (!moveItemStackTo(slot_item, start, end, false)) {
// Moving FROM inventory TO machine // Moving FROM inventory TO machine
notifySlotsUnwatchCurrentStack();
return ItemStack.EMPTY; return ItemStack.EMPTY;
} }
@ -147,8 +167,11 @@ public abstract class MatteryMenu extends AbstractContainerMenu {
} else { } else {
get_slot.setChanged(); get_slot.setChanged();
} }
notifySlotsWatchCurrentStack();
} }
notifySlotsUnwatchCurrentStack();
return moved; return moved;
} }
} }

View File

@ -4,6 +4,7 @@ import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.Slot; import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
import ru.dbotthepony.mc.otm.container.MatteryContainer; import ru.dbotthepony.mc.otm.container.MatteryContainer;
// Just a sign that this slot needs auto draw // Just a sign that this slot needs auto draw
@ -20,6 +21,15 @@ public class MatterySlot extends Slot {
} }
private ItemStack last_stack; private ItemStack last_stack;
private ItemStack watching_stack;
public void watchCurrentStack() {
watching_stack = super.getItem().copy();
}
public void unwatchCurrentStack() {
watching_stack = null;
}
@Override @Override
public ItemStack getItem() { public ItemStack getItem() {
@ -31,7 +41,7 @@ public class MatterySlot extends Slot {
@Override @Override
public void setChanged() { public void setChanged() {
if (container instanceof MatteryContainer container1) { if (container instanceof MatteryContainer container1) {
ItemStack old = last_stack.copy(); ItemStack old = watching_stack != null ? watching_stack.copy() : last_stack.copy();
container1.setChanged(getSlotIndex(), super.getItem(), old); container1.setChanged(getSlotIndex(), super.getItem(), old);
} else { } else {
super.setChanged(); super.setChanged();
@ -41,7 +51,7 @@ public class MatterySlot extends Slot {
@Override @Override
public void set(ItemStack p_40240_) { public void set(ItemStack p_40240_) {
if (container instanceof MatteryContainer container1) { if (container instanceof MatteryContainer container1) {
ItemStack old = container1.getItem(getSlotIndex()).copy(); ItemStack old = watching_stack != null ? watching_stack.copy() : container1.getItem(getSlotIndex()).copy();
container1.startIgnore(); container1.startIgnore();
container1.setItem(getSlotIndex(), p_40240_); container1.setItem(getSlotIndex(), p_40240_);
container1.stopIgnore(); container1.stopIgnore();
@ -54,7 +64,7 @@ public class MatterySlot extends Slot {
@Override @Override
public void onTake(Player p_150645_, ItemStack p_150646_) { public void onTake(Player p_150645_, ItemStack p_150646_) {
if (container instanceof MatteryContainer container1) { if (container instanceof MatteryContainer container1) {
ItemStack old = last_stack.copy(); ItemStack old = watching_stack != null ? watching_stack.copy() : last_stack.copy();
container1.setChanged(getSlotIndex(), p_150646_.copy(), old); container1.setChanged(getSlotIndex(), p_150646_.copy(), old);
last_stack = ItemStack.EMPTY; last_stack = ItemStack.EMPTY;
} else { } else {