|
| 1 | +package com.redcrafter07.ultrautilities.container; |
| 2 | + |
| 3 | +import com.redcrafter07.ultrautilities.blocks.ModBlocks; |
| 4 | +import net.minecraft.entity.player.PlayerEntity; |
| 5 | +import net.minecraft.entity.player.PlayerInventory; |
| 6 | +import net.minecraft.inventory.container.Container; |
| 7 | +import net.minecraft.inventory.container.Slot; |
| 8 | +import net.minecraft.item.ItemStack; |
| 9 | +import net.minecraft.tileentity.TileEntity; |
| 10 | +import net.minecraft.util.IWorldPosCallable; |
| 11 | +import net.minecraft.util.math.BlockPos; |
| 12 | +import net.minecraft.world.World; |
| 13 | +import net.minecraftforge.items.CapabilityItemHandler; |
| 14 | +import net.minecraftforge.items.IItemHandler; |
| 15 | +import net.minecraftforge.items.SlotItemHandler; |
| 16 | +import net.minecraftforge.items.wrapper.InvWrapper; |
| 17 | + |
| 18 | +public class OverloadStationContainer extends Container { |
| 19 | + private final TileEntity tileEntity; |
| 20 | + private final PlayerEntity playerEntity; |
| 21 | + private final IItemHandler playerInventory; |
| 22 | + |
| 23 | + public OverloadStationContainer(int containerId, World world, BlockPos blockPos, PlayerInventory inv, PlayerEntity playerEntity) { |
| 24 | + super(ModContainers.OVERLOAD_STATION_CONTAINER.get(), containerId); |
| 25 | + this.tileEntity = world.getTileEntity(blockPos); |
| 26 | + this.playerEntity = playerEntity; |
| 27 | + this.playerInventory = new InvWrapper(inv); |
| 28 | + |
| 29 | + layoutPlayerInventorySlots(8, 86); |
| 30 | + |
| 31 | + if(tileEntity != null) { |
| 32 | + tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(handler -> { |
| 33 | + addSlot(new SlotItemHandler(handler, 0, 10, 13)); |
| 34 | + addSlot(new SlotItemHandler(handler, 1, 54, 13)); |
| 35 | + addSlot(new SlotItemHandler(handler, 2, 152, 55)); |
| 36 | + }); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean canInteractWith(PlayerEntity playerIn) { |
| 42 | + return isWithinUsableDistance(IWorldPosCallable.of(tileEntity.getWorld(), tileEntity.getPos()), playerIn, ModBlocks.OVERLOAD_STATION.get()); |
| 43 | + } |
| 44 | + |
| 45 | + // Credit: Kaupenjoe (https://gist.github.com/Kaupenjoe/cbb03a4db3f94a2bfd5b2ad2fa78f539) |
| 46 | + private int addSlotRange(IItemHandler handler, int index, int x, int y, int amount, int dx) { |
| 47 | + for (int i = 0; i < amount; i++) { |
| 48 | + addSlot(new SlotItemHandler(handler, index, x, y)); |
| 49 | + x += dx; |
| 50 | + index++; |
| 51 | + } |
| 52 | + |
| 53 | + return index; |
| 54 | + } |
| 55 | + |
| 56 | + private int addSlotBox(IItemHandler handler, int index, int x, int y, int horAmount, int dx, int verAmount, int dy) { |
| 57 | + for (int j = 0; j < verAmount; j++) { |
| 58 | + index = addSlotRange(handler, index, x, y, horAmount, dx); |
| 59 | + y += dy; |
| 60 | + } |
| 61 | + |
| 62 | + return index; |
| 63 | + } |
| 64 | + |
| 65 | + private void layoutPlayerInventorySlots(int leftCol, int topRow) { |
| 66 | + addSlotBox(playerInventory, 9, leftCol, topRow, 9, 18, 3, 18); |
| 67 | + |
| 68 | + topRow += 58; |
| 69 | + addSlotRange(playerInventory, 0, leftCol, topRow, 9, 18); |
| 70 | + } |
| 71 | + |
| 72 | + // Credit: diesieben07 (https://github.com/diesieben07/SevenCommons) |
| 73 | + private static final int HOTBAR_SLOT_COUNT = 9; |
| 74 | + private static final int PLAYER_INVENTORY_ROW_COUNT = 3; |
| 75 | + private static final int PLAYER_INVENTORY_COLUMN_COUNT = 9; |
| 76 | + private static final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT; |
| 77 | + private static final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT; |
| 78 | + private static final int VANILLA_FIRST_SLOT_INDEX = 0; |
| 79 | + private static final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT; |
| 80 | + |
| 81 | + // THIS YOU HAVE TO DEFINE! |
| 82 | + private static final int TE_INVENTORY_SLOT_COUNT = 3; // must match TileEntityInventoryBasic.NUMBER_OF_SLOTS |
| 83 | + |
| 84 | + @Override |
| 85 | + public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) { |
| 86 | + Slot sourceSlot = inventorySlots.get(index); |
| 87 | + if (sourceSlot == null || !sourceSlot.getHasStack()) return ItemStack.EMPTY; //EMPTY_ITEM |
| 88 | + ItemStack sourceStack = sourceSlot.getStack(); |
| 89 | + ItemStack copyOfSourceStack = sourceStack.copy(); |
| 90 | + |
| 91 | + // Check if the slot clicked is one of the vanilla container slots |
| 92 | + if (index < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) { |
| 93 | + // This is a vanilla container slot so merge the stack into the tile inventory |
| 94 | + if (!mergeItemStack(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX |
| 95 | + + TE_INVENTORY_SLOT_COUNT, false)) { |
| 96 | + return ItemStack.EMPTY; // EMPTY_ITEM |
| 97 | + } |
| 98 | + } else if (index < TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT) { |
| 99 | + // This is a TE slot so merge the stack into the players inventory |
| 100 | + if (!mergeItemStack(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) { |
| 101 | + return ItemStack.EMPTY; |
| 102 | + } |
| 103 | + } else { |
| 104 | + System.out.println("Invalid slotIndex:" + index); |
| 105 | + return ItemStack.EMPTY; |
| 106 | + } |
| 107 | + // If stack size == 0 (the entire stack was moved) set slot contents to null |
| 108 | + if (sourceStack.getCount() == 0) { |
| 109 | + sourceSlot.putStack(ItemStack.EMPTY); |
| 110 | + } else { |
| 111 | + sourceSlot.onSlotChanged(); |
| 112 | + } |
| 113 | + sourceSlot.onTake(playerEntity, sourceStack); |
| 114 | + return copyOfSourceStack; |
| 115 | + } |
| 116 | +} |
0 commit comments