From 30cba0ca18674341d7c41a363011b2b3133dd3a0 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 11 Aug 2021 16:24:15 +0700 Subject: [PATCH] Implement quick move in android station menu --- .../capability/AndroidCapabilityPlayer.java | 3 +- .../mc/otm/menu/AndroidStationMenu.java | 34 ++++++++++++++++++- .../mc/otm/menu/PoweredMachineMenu.java | 33 ++++++++++++++++-- 3 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/dbotthepony/mc/otm/capability/AndroidCapabilityPlayer.java b/src/main/java/ru/dbotthepony/mc/otm/capability/AndroidCapabilityPlayer.java index 144c75568..1f9c9d80d 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/capability/AndroidCapabilityPlayer.java +++ b/src/main/java/ru/dbotthepony/mc/otm/capability/AndroidCapabilityPlayer.java @@ -66,7 +66,8 @@ public class AndroidCapabilityPlayer extends AndroidCapability { public void tickInner() { super.tickInner(); - extractEnergyInner(BigDecimal.valueOf(new Random().nextDouble()), false); + // TODO: Maybe passive drain? + // extractEnergyInner(BigDecimal.valueOf(new Random().nextDouble()), false); FoodData stats = ply.getFoodData(); diff --git a/src/main/java/ru/dbotthepony/mc/otm/menu/AndroidStationMenu.java b/src/main/java/ru/dbotthepony/mc/otm/menu/AndroidStationMenu.java index 70a5103fb..a1a61045f 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/menu/AndroidStationMenu.java +++ b/src/main/java/ru/dbotthepony/mc/otm/menu/AndroidStationMenu.java @@ -59,6 +59,8 @@ public class AndroidStationMenu extends PoweredMachineMenu { this(containerID, inventory,null); } + private final int battery_slot_index; + // Serverside public AndroidStationMenu( int containerID, @@ -71,9 +73,39 @@ public class AndroidStationMenu extends PoweredMachineMenu { inventory, tile); - addSlot(new AndroidBatterySlot(new AndroidStationContainer(inventory.player), 0, 39, 15)); + battery_slot_index = addSlot(new AndroidBatterySlot(new AndroidStationContainer(inventory.player), 0, 39, 15)).index; addBatterySlot(); addInventorySlots(); } + + @Override + public ItemStack quickMoveStack(Player ply, int slot_index) { + ItemStack moved = ItemStack.EMPTY; + Slot get_slot = this.slots.get(slot_index); + + if (get_slot.hasItem()) { + ItemStack slot_item = get_slot.getItem(); + moved = slot_item.copy(); + + if (slot_index < inventory_slot_index_start) { + // Moving FROM machine TO inventory + + if (!moveItemStackTo(slot_item, inventory_slot_index_start, inventory_slot_index_end + 1, false)) { + return ItemStack.EMPTY; + } + } else if (!moveItemStackTo(slot_item, battery_slot_index, battery_slot_index + 1, true)) { + // Moving FROM inventory TO machine + return ItemStack.EMPTY; + } + + if (slot_item.isEmpty()) { + get_slot.set(ItemStack.EMPTY); + } else { + get_slot.setChanged(); + } + } + + return moved; + } } diff --git a/src/main/java/ru/dbotthepony/mc/otm/menu/PoweredMachineMenu.java b/src/main/java/ru/dbotthepony/mc/otm/menu/PoweredMachineMenu.java index af8f4fe18..2d59670d4 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/menu/PoweredMachineMenu.java +++ b/src/main/java/ru/dbotthepony/mc/otm/menu/PoweredMachineMenu.java @@ -8,6 +8,7 @@ import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.MenuType; import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.ItemStack; import net.minecraftforge.common.util.LazyOptional; import ru.dbotthepony.mc.otm.block.entity.BlockEntityMatteryPoweredMachine; import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage; @@ -77,16 +78,30 @@ abstract public class PoweredMachineMenu extends AbstractContainerMenu { addInventorySlots(97); } + protected int inventory_slot_index_start; + protected int inventory_slot_index_end; + protected void addInventorySlots(int offset) { + boolean first = true; + for (int i = 0; i < 3; ++i) { for (int j = 0; j < 9; ++j) { - this.addSlot(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, offset + i * 18)); + Slot slot = this.addSlot(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, offset + i * 18)); + + if (first) { + first = false; + inventory_slot_index_start = slot.index; + } } } + Slot last = null; + for (int k = 0; k < 9; ++k) { - this.addSlot(new Slot(inventory, k, 8 + k * 18, offset + 58)); + last = this.addSlot(new Slot(inventory, k, 8 + k * 18, offset + 58)); } + + inventory_slot_index_end = last.index; } @Override @@ -101,4 +116,18 @@ abstract public class PoweredMachineMenu extends AbstractContainerMenu { BlockPos pos = tile.getBlockPos(); return player.distanceToSqr((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D) <= 64.0D; } + + // This method receive Player interactor and slot_index where Shift + Right click occurred + // It shall return item stack that got moved + @Override + public ItemStack quickMoveStack(Player ply, int slot_index) { + // this.moveItemStackTo(ItemStack, int start_slot_index, int end_slot_index, boolean iteration_order) + // returns boolean, telling whenever ItemStack was modified (moved or sharnk) + // false means nothing happened + // Last boolean determine order of slot iteration, where: + // if TRUE, iteration order is end_slot_index -> start_slot_index + // if FALSE iteration order is start_slot_index -> end_slot_index + + return ItemStack.EMPTY; + } }