Implement quick move in android station menu

This commit is contained in:
DBotThePony 2021-08-11 16:24:15 +07:00
parent c980bdc272
commit 30cba0ca18
Signed by: DBot
GPG Key ID: DCC23B5715498507
3 changed files with 66 additions and 4 deletions

View File

@ -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();

View File

@ -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;
}
}

View File

@ -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;
}
}