From a94af5e247c08157fb6564ea3b0fb7fd70882ad9 Mon Sep 17 00:00:00 2001 From: Michael Hillcox Date: Sat, 5 Dec 2020 16:44:20 +0000 Subject: [PATCH] =?UTF-8?q?Hopefully=20the=20right=20transfer=20stack=20co?= =?UTF-8?q?de=20=F0=9F=A4=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/container/DrillContainer.java | 27 +++++++++++++++++++ .../common/container/package-info.java | 7 +++++ .../common/utilities/ContainerHelper.java | 22 +++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/main/java/de/ellpeck/actuallyadditions/common/container/package-info.java diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/container/DrillContainer.java b/src/main/java/de/ellpeck/actuallyadditions/common/container/DrillContainer.java index 798c494e1..ab4c5bb69 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/container/DrillContainer.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/container/DrillContainer.java @@ -7,6 +7,7 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.inventory.container.ClickType; import net.minecraft.inventory.container.Container; +import net.minecraft.inventory.container.Slot; import net.minecraft.item.ItemStack; import net.minecraft.network.PacketBuffer; import net.minecraftforge.items.ItemStackHandler; @@ -60,6 +61,7 @@ public class DrillContainer extends Container { @Nonnull @Override public ItemStack slotClick(int slotId, int dragType, @Nonnull ClickType clickTypeIn, @Nonnull PlayerEntity player) { + // Don't let the user pickup, throw or swap the drill, any drill if ((clickTypeIn == ClickType.SWAP || clickTypeIn == ClickType.THROW || clickTypeIn == ClickType.PICKUP) && slotId >= 0 && slotId <= this.inventorySlots.size() @@ -70,6 +72,31 @@ public class DrillContainer extends Container { return super.slotClick(slotId, dragType, clickTypeIn, player); } + @Override + public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) { + Slot fromSlot = this.inventorySlots.get(index); + + if (fromSlot == null || !fromSlot.getHasStack()) { + return ItemStack.EMPTY; + } + + ItemStack fromStack = fromSlot.getStack(); + + // Moves items from the Drill to the players inventory + if (index > ContainerHelper.PLAYER_INVENTORY_END_SLOT) { + if (!this.mergeItemStack(fromStack, 0, ContainerHelper.PLAYER_INVENTORY_END_SLOT, false)) { + return ItemStack.EMPTY; + } + } else { + // Moves the item from the players inventory to the drill + if (!this.mergeItemStack(fromStack, ContainerHelper.PLAYER_INVENTORY_END_SLOT + 1, ContainerHelper.PLAYER_INVENTORY_END_SLOT + 6, false)) { + return ItemStack.EMPTY; + } + } + + return fromStack; + } + @Override public void onContainerClosed(@Nonnull PlayerEntity playerIn) { super.onContainerClosed(playerIn); diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/container/package-info.java b/src/main/java/de/ellpeck/actuallyadditions/common/container/package-info.java new file mode 100644 index 000000000..0bac7d693 --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/common/container/package-info.java @@ -0,0 +1,7 @@ +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +package de.ellpeck.actuallyadditions.common.container; + +import mcp.MethodsReturnNonnullByDefault; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/utilities/ContainerHelper.java b/src/main/java/de/ellpeck/actuallyadditions/common/utilities/ContainerHelper.java index 65735397a..cdbfba4e7 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/utilities/ContainerHelper.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/utilities/ContainerHelper.java @@ -1,7 +1,9 @@ package de.ellpeck.actuallyadditions.common.utilities; import net.minecraft.inventory.container.Slot; +import net.minecraft.item.ItemStack; import net.minecraftforge.items.IItemHandler; +import net.minecraftforge.items.ItemStackHandler; import net.minecraftforge.items.SlotItemHandler; import java.util.function.Consumer; @@ -10,6 +12,20 @@ public class ContainerHelper { public static final int DEFAULT_SLOTS_X = 8; public static final int SLOT_SPACING = 18; + /** + * This value assumes you have used the {@link ContainerHelper#setupPlayerInventory(IItemHandler, int, int, int, Consumer)} + * method to generate your inventories slots. Do not use if otherwise + */ + public static final int PLAYER_INVENTORY_END_SLOT = 35; + + /** + * + * @param handler the inventory handler (should always be the players) + * @param index the starting index, should always be 0 logically. If otherwise, do not use {@link ContainerHelper#PLAYER_INVENTORY_END_SLOT} + * @param x x offset for the inventory, for a full width inventory, this is typically 8 + * @param y y offset for the inventory, no default sorry + * @param consumer Typically a containers the Containers#addSlot(Slot) method + */ public static void setupPlayerInventory(IItemHandler handler, int index, int x, int y, Consumer consumer) { // Build the players inventory first, building from bottom to top, right to left. The (i>0) magic handles the // space between the hotbar inventory and the players remaining inventory. @@ -22,4 +38,10 @@ public class ContainerHelper { } } + public static ItemStackHandler getHandlerFromItem(ItemStack itemStack, String key, int size) { + ItemStackHandler handler = new ItemStackHandler(size); + handler.deserializeNBT(itemStack.getOrCreateChildTag(key)); + return handler; + } + }