Advertisement
HalestormXV

Pouch Container

Jan 31st, 2021
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1. package com.halestormxv.numinous.common.containers;
  2.  
  3. import com.halestormxv.numinous.common.items.MaterialPouch;
  4. import com.halestormxv.numinous.core.init.ContainerTypeInit;
  5. import net.minecraft.entity.player.PlayerEntity;
  6. import net.minecraft.entity.player.PlayerInventory;
  7. import net.minecraft.inventory.container.ClickType;
  8. import net.minecraft.inventory.container.Container;
  9. import net.minecraft.inventory.container.Slot;
  10. import net.minecraft.item.ItemStack;
  11. import net.minecraft.network.PacketBuffer;
  12. import net.minecraftforge.items.IItemHandler;
  13. import net.minecraftforge.items.SlotItemHandler;
  14.  
  15. public class MaterialPouchContainer extends Container {
  16.     private final ItemStack item;
  17.     private final IItemHandler itemHandler;
  18.     private int blocked = -1;
  19.  
  20.     public MaterialPouchContainer(final int windowId, PlayerInventory playerInventory, PacketBuffer extraData) {
  21.         super(ContainerTypeInit.MATERIAL_POUCH.get(), windowId);
  22.         this.item = getHeldItem(playerInventory.player);
  23.         this.itemHandler = ((MaterialPouch) this.item.getItem()).getInventory(this.item);
  24.  
  25.         for (int i = 0; i < this.itemHandler.getSlots(); i++){
  26.             int x = 8 + 18 * (i % 9);
  27.             int y = 18 + 18 * (i / 9);
  28.             addSlot(new SlotItemHandler(this.itemHandler, i, x, y));
  29.         }
  30.  
  31.         final int rowCount = this.itemHandler.getSlots() / 9;
  32.         final int yOffset = (rowCount - 4) * 18;
  33.  
  34.         //Player Inventory
  35.         for (int y = 0; y <3; y++){
  36.             for (int x = 0; x < 9; x++){
  37.                 addSlot(new Slot(playerInventory, x + y * 9 + 9, 8 + x * 18, 103 + y * 18 + yOffset));
  38.             }
  39.         }
  40.  
  41.         //Hotbar
  42.         for (int x = 0; x < 9; x++){
  43.             Slot slot = addSlot(new Slot(playerInventory, x, 8 + x * 18, 161 + yOffset) {
  44.                 @Override
  45.                 public boolean canTakeStack(PlayerEntity playerIn) {
  46.                     return slotNumber != blocked;
  47.                 }
  48.             });
  49.  
  50.             if (x == playerInventory.currentItem && ItemStack.areItemStacksEqual(playerInventory.getCurrentItem(), this.item)){
  51.                 blocked = slot.slotNumber;
  52.             }
  53.         }
  54.     }
  55.  
  56.     private static ItemStack getHeldItem(PlayerEntity playerEntity){
  57.         if (playerEntity.getHeldItemMainhand().getItem() instanceof MaterialPouch)
  58.             return playerEntity.getHeldItemMainhand();
  59.  
  60.         if (playerEntity.getHeldItemOffhand().getItem() instanceof MaterialPouch)
  61.             return playerEntity.getHeldItemOffhand();
  62.  
  63.         return ItemStack.EMPTY;
  64.     }
  65.  
  66.     public int getInventoryRows() {
  67.         return this.itemHandler.getSlots() / 9;
  68.     }
  69.  
  70.     @Override
  71.     public boolean canInteractWith(PlayerEntity playerIn) {
  72.         return true;
  73.     }
  74.  
  75.     @Override
  76.     public void onContainerClosed(PlayerEntity playerIn) {
  77.         super.onContainerClosed(playerIn);
  78.         ((MaterialPouch) this.item.getItem()).saveInventory(this.item, this.itemHandler);
  79.     }
  80.  
  81.     //Handles Shift+Click Functionality
  82.     @Override
  83.     public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
  84.         Slot slot = this.getSlot(index);
  85.  
  86.         if (!slot.canTakeStack(playerIn))
  87.             return slot.getStack();
  88.  
  89.         if (index == blocked || !slot.getHasStack())
  90.             return ItemStack.EMPTY;
  91.  
  92.         ItemStack stack = slot.getStack();
  93.         ItemStack newStack = stack.copy();
  94.  
  95.         int containerSlots = itemHandler.getSlots();
  96.         if (index < containerSlots){
  97.             if (!this.mergeItemStack(stack, containerSlots, this.inventorySlots.size(), true))
  98.                 return ItemStack.EMPTY;
  99.             slot.onSlotChanged();
  100.         } else if (!this.mergeItemStack(stack, 0, containerSlots, false)){
  101.             return ItemStack.EMPTY;
  102.         }
  103.  
  104.         if (stack.isEmpty())
  105.             slot.putStack(ItemStack.EMPTY);
  106.         else
  107.             slot.onSlotChanged();
  108.  
  109.         return slot.onTake(playerIn, newStack);
  110.     }
  111.  
  112.     @Override
  113.     public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
  114.         if (slotId < 0 || slotId > inventorySlots.size())
  115.             return super.slotClick(slotId, dragType, clickTypeIn, player);
  116.  
  117.         Slot slot = inventorySlots.get(slotId);
  118.         if(!canTake(slotId, slot, dragType, player, clickTypeIn))
  119.             return slot.getStack();
  120.  
  121.         return super.slotClick(slotId, dragType, clickTypeIn, player);
  122.     }
  123.  
  124.     private static boolean isMaterialPouch(ItemStack stack){
  125.         return stack.getItem() instanceof MaterialPouch;
  126.     }
  127.  
  128.     public boolean canTake(int slotId, Slot slot, int button, PlayerEntity playerEntity, ClickType clickType){
  129.         if (slotId == blocked || slotId <= itemHandler.getSlots() - 1 && isMaterialPouch(playerEntity.inventory.getItemStack()))
  130.             return false;
  131.  
  132.         //Hotbar swapping via number keys
  133.         if (clickType == ClickType.SWAP){
  134.             int hotbarId = itemHandler.getSlots() + 27 + button;
  135.             //Block swapping with container
  136.             if (blocked == hotbarId)
  137.                 return false;
  138.  
  139.             Slot hotbarSlot = getSlot(hotbarId);
  140.             if (slotId <= itemHandler.getSlots() - 1)
  141.                 return !isMaterialPouch(slot.getStack()) && !isMaterialPouch(hotbarSlot.getStack());
  142.         }
  143.  
  144.         return true;
  145.     }
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement