mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-12-22 19:39:24 +01:00
Hopefully the right transfer stack code 🤞
This commit is contained in:
parent
8b8aaab4f4
commit
a94af5e247
3 changed files with 56 additions and 0 deletions
|
@ -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);
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
@ParametersAreNonnullByDefault
|
||||
@MethodsReturnNonnullByDefault
|
||||
package de.ellpeck.actuallyadditions.common.container;
|
||||
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
|
@ -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<Slot> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue