Guest User

ICSBContainer

a guest
Aug 9th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. package com.jam.icc.container;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import com.jam.icc.slots.SlotGhost;
  6. import com.jam.icc.slots.SlotUnstackable;
  7. import com.jam.icc.tileentity.TileEntityICSB;
  8.  
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.inventory.Container;
  11. import net.minecraft.inventory.IInventory;
  12. import net.minecraft.inventory.Slot;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.util.EnumFacing;
  16. import net.minecraftforge.items.CapabilityItemHandler;
  17. import net.minecraftforge.items.IItemHandler;
  18. import net.minecraftforge.items.SlotItemHandler;
  19.  
  20. public class ICSBContainer extends Container{
  21.  
  22.     private TileEntityICSB te;
  23.    
  24.      public ICSBContainer(IInventory playerInventory, TileEntityICSB te) {
  25.             this.te = te;
  26.  
  27.             // This container references items out of our own inventory (the 10 slots we hold ourselves)
  28.             // as well as the slots from the player inventory so that the user can transfer items between
  29.             // both inventories. The two calls below make sure that slots are defined for both inventories.
  30.             addOwnSlots();
  31.             addPlayerSlots(playerInventory);
  32.         }
  33.      private void addPlayerSlots(IInventory playerInventory) {
  34.             // Slots for the main inventory
  35.             for (int row = 0; row < 3; ++row) {
  36.                 for (int col = 0; col < 9; ++col) {
  37.                     int x = 8 + col * 18;
  38.                     int y = row * 18 + 54;
  39.                     this.addSlotToContainer(new Slot(playerInventory, col + row * 9 + 9, x, y));
  40.                 }
  41.             }
  42.  
  43.             // Slots for the hotbar
  44.             for (int row = 0; row < 9; ++row) {
  45.                 int x = 8 + row * 18;
  46.                 int y = 58 + 54;
  47.                 this.addSlotToContainer(new Slot(playerInventory, row, x, y));
  48.             }
  49.         }
  50.  
  51.         private void addOwnSlots() {
  52.             IItemHandler itemHandler = te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
  53.             IItemHandler filterHandler = te.filterstack();
  54.             int x = 8;
  55.             int y = 22;
  56.            
  57.             // Add our own slots
  58.             int slotIndex = 0;
  59.             for(int i = 0; i < 9; i++)
  60.             {
  61.                 int x1 = 8 + 18 * i;
  62.                 int y1 = 18;
  63.                         this.addSlotToContainer(new SlotItemHandler(itemHandler, i, x1, y1));
  64.             }
  65.             for(int i = 0; i < 9; i++)
  66.             {
  67.                 int x1 = 8 + 18 * i;
  68.                 int y1 = 18 + 18;
  69.                         this.addSlotToContainer(new SlotItemHandler(itemHandler, i + 9, x1, y1));
  70.             }
  71.             for(int i = 0; i < 9; i++)
  72.             {
  73.                 int x1 = 8 + 18 * i;
  74.                 int y1 = 18 + 36;
  75.                         this.addSlotToContainer(new SlotItemHandler(itemHandler, i + 18, x1, y1));
  76.             }
  77.            //add other slots
  78.            
  79.             //item filter slot
  80.             addSlotToContainer(new SlotGhost(filterHandler, 0, 173, 5));
  81.         }
  82.      
  83.        
  84.         @Nullable
  85.         @Override
  86.         public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
  87.             ItemStack itemstack = ItemStack.EMPTY;
  88.             Slot slot = this.inventorySlots.get(index);
  89.            
  90.             if (slot != null && slot.getHasStack()) {
  91.                  
  92.                 ItemStack itemstack1 = slot.getStack();
  93.                 itemstack = itemstack1.copy();
  94.                
  95.                 if (index < TileEntityICSB.SIZE) {
  96.                     if (!this.mergeItemStack(itemstack1, 10, this.inventorySlots.size(), true)) {
  97.                         return ItemStack.EMPTY;
  98.                     }
  99.                 } else if (!this.mergeItemStack(itemstack1, 0, 9, false)) {
  100.                     return ItemStack.EMPTY;
  101.                 }
  102.                
  103.                 if (itemstack1.isEmpty()) {
  104.                     slot.putStack(ItemStack.EMPTY);
  105.                 } else {
  106.                     slot.onSlotChanged();
  107.                 }
  108.              
  109.            }
  110.  
  111.             return itemstack;
  112.         }
  113.        
  114.        
  115.    
  116.     @Override
  117.     public boolean canInteractWith(EntityPlayer playerIn) {
  118.        
  119.         return true;
  120.     }
  121.    
  122. }
Advertisement
Add Comment
Please, Sign In to add comment