Advertisement
HalestormXV

Container

Mar 21st, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.83 KB | None | 0 0
  1. package com.halestormxv.inventory;
  2.  
  3. import com.halestormxv.item.CelestialCraft_items;
  4. import com.halestormxv.item.keyPouch;
  5.  
  6. import net.minecraft.entity.player.EntityPlayer;
  7. import net.minecraft.entity.player.InventoryPlayer;
  8. import net.minecraft.inventory.Container;
  9. import net.minecraft.inventory.IInventory;
  10. import net.minecraft.inventory.Slot;
  11. import net.minecraft.item.ItemStack;
  12.  
  13. public class ContainerCelestialKeypouch extends Container
  14. {
  15.     /** The Item Inventory for this Container, only needed if you want to reference isUseableByPlayer */
  16.     public final InventoryKeyPouch inventory;
  17.  
  18.     /** Using these will make transferStackInSlot easier to understand and implement
  19.      * INV_START is the index of the first slot in the Player's Inventory, so our
  20.      * InventoryItem's number of slots (e.g. 5 slots is array indices 0-4, so start at 5)
  21.      * Notice how we don't have to remember how many slots we made? We can just use
  22.      * InventoryItem.INV_SIZE and if we ever change it, the Container updates automatically. */
  23.     private static final int INV_START = InventoryKeyPouch.INV_SIZE, INV_END = INV_START+26,
  24.             HOTBAR_START = INV_END+1, HOTBAR_END = HOTBAR_START+8;
  25.  
  26.     // If you're planning to add armor slots, put those first like this:
  27.     // ARMOR_START = InventoryItem.INV_SIZE, ARMOR_END = ARMOR_START+3,
  28.     // INV_START = ARMOR_END+1, and then carry on like above.
  29.  
  30.     public ContainerCelestialKeypouch(EntityPlayer par1Player, InventoryPlayer inventoryPlayer, InventoryKeyPouch inventoryItem)
  31.     {
  32.         this.inventory = inventoryItem;
  33.  
  34.         int i;
  35.         for (i = 0; i < InventoryKeyPouch.INV_SIZE; ++i)
  36.         {
  37.  
  38.             this.addSlotToContainer(new CelestialKeypouchSlot(this.inventory, i, 80 + (18 * (int)(i/4)), 8 + (18*(i%4))));
  39.         }
  40.  
  41.         for (i = 0; i < 3; ++i)
  42.         {
  43.             for (int j = 0; j < 9; ++j)
  44.             {
  45.                 this.addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
  46.             }
  47.         }
  48.  
  49.         for (i = 0; i < 9; ++i)
  50.         {
  51.             this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
  52.         }
  53.     }
  54.  
  55.     @Override
  56.     public boolean canInteractWith(EntityPlayer entityplayer)
  57.     {
  58.         // be sure to return the inventory's isUseableByPlayer method
  59.         // if you defined special behavior there:
  60.         return inventory.isUseableByPlayer(entityplayer);
  61.     }
  62.  
  63.     /**
  64.      * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
  65.      */
  66.     @Override
  67.     public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int index)
  68.     {
  69.         ItemStack itemstack = null;
  70.         Slot slot = (Slot) this.inventorySlots.get(index);
  71.  
  72.         if (slot != null && slot.getHasStack())
  73.         {
  74.             ItemStack itemstack1 = slot.getStack();
  75.             itemstack = itemstack1.copy();
  76.  
  77.             // If item is in our custom Inventory or armor slot
  78.             if (index < INV_START)
  79.             {
  80.                 // try to place in player inventory / action bar
  81.                 if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END+1, true))
  82.                 {
  83.                     return null;
  84.                 }
  85.  
  86.                 slot.onSlotChange(itemstack1, itemstack);
  87.             }
  88.             // Item is in inventory / hotbar, try to place in custom inventory or armor slots
  89.             else
  90.             {
  91.                 if (itemstack1.getItem() == CelestialCraft_items.celKeys)
  92.                 {
  93.                     // Try to merge into your custom inventory slots
  94.                     // We use 'InventoryItem.INV_SIZE' instead of INV_START just in case
  95.                     // you also add armor or other custom slots
  96.                     if (!this.mergeItemStack(itemstack1, 0, InventoryKeyPouch.INV_SIZE, false))
  97.                     {
  98.                         return null;
  99.                     }
  100.                 }
  101.  
  102.                 /**
  103.                  * Implementation number 2: Shift-click items between action bar and inventory
  104.                  */
  105.                 // item is in player's inventory, but not in action bar
  106.                 if (index >= INV_START && index < HOTBAR_START)
  107.                 {
  108.                     // place in action bar
  109.                     if (!this.mergeItemStack(itemstack1, HOTBAR_START, HOTBAR_END+1, false))
  110.                     {
  111.                         return null;
  112.                     }
  113.                 }
  114.                 // item in action bar - place in player inventory
  115.                 else if (index >= HOTBAR_START && index < HOTBAR_END+1)
  116.                 {
  117.                     if (!this.mergeItemStack(itemstack1, INV_START, INV_END+1, false))
  118.                     {
  119.                         return null;
  120.                     }
  121.                 }
  122.             }
  123.  
  124.             if (itemstack1.stackSize == 0)
  125.             {
  126.                 slot.putStack((ItemStack) null);
  127.             }
  128.             else
  129.             {
  130.                 slot.onSlotChanged();
  131.             }
  132.  
  133.             if (itemstack1.stackSize == itemstack.stackSize)
  134.             {
  135.                 return null;
  136.             }
  137.  
  138.             slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
  139.         }
  140.  
  141.         return itemstack;
  142.     }
  143.  
  144.     @Override
  145.     public ItemStack slotClick(int slot, int button, int flag, EntityPlayer player) {
  146.         // this will prevent the player from interacting with the item that opened the inventory:
  147.         if (slot >= 0 && getSlot(slot) != null && getSlot(slot).getStack() == player.getHeldItem()) {
  148.             return null;
  149.         }
  150.         return super.slotClick(slot, button, flag, player);
  151.     }
  152.  
  153.     @Override
  154.     protected boolean mergeItemStack(ItemStack stack, int start, int end, boolean backwards)
  155.     {
  156.         boolean flag1 = false;
  157.         int k = (backwards ? end - 1 : start);
  158.         Slot slot;
  159.         ItemStack itemstack1;
  160.  
  161.         if (stack.isStackable())
  162.         {
  163.             while (stack.stackSize > 0 && (!backwards && k < end || backwards && k >= start))
  164.             {
  165.                 slot = (Slot) inventorySlots.get(k);
  166.                 itemstack1 = slot.getStack();
  167.  
  168.                 if (!slot.isItemValid(stack)) {
  169.                     k += (backwards ? -1 : 1);
  170.                     continue;
  171.                 }
  172.  
  173.                 if (itemstack1 != null && itemstack1.getItem() == stack.getItem() &&
  174.                         (!stack.getHasSubtypes() || stack.getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(stack, itemstack1))
  175.                 {
  176.                     int l = itemstack1.stackSize + stack.stackSize;
  177.  
  178.                     if (l <= stack.getMaxStackSize() && l <= slot.getSlotStackLimit()) {
  179.                         stack.stackSize = 0;
  180.                         itemstack1.stackSize = l;
  181.                         inventory.markDirty();
  182.                         flag1 = true;
  183.                     } else if (itemstack1.stackSize < stack.getMaxStackSize() && l < slot.getSlotStackLimit()) {
  184.                         stack.stackSize -= stack.getMaxStackSize() - itemstack1.stackSize;
  185.                         itemstack1.stackSize = stack.getMaxStackSize();
  186.                         inventory.markDirty();
  187.                         flag1 = true;
  188.                     }
  189.                 }
  190.  
  191.                 k += (backwards ? -1 : 1);
  192.             }
  193.         }
  194.         if (stack.stackSize > 0)
  195.         {
  196.             k = (backwards ? end - 1 : start);
  197.             while (!backwards && k < end || backwards && k >= start) {
  198.                 slot = (Slot) inventorySlots.get(k);
  199.                 itemstack1 = slot.getStack();
  200.  
  201.                 if (!slot.isItemValid(stack)) {
  202.                     k += (backwards ? -1 : 1);
  203.                     continue;
  204.                 }
  205.  
  206.                 if (itemstack1 == null) {
  207.                     int l = stack.stackSize;
  208.                     if (l <= slot.getSlotStackLimit()) {
  209.                         slot.putStack(stack.copy());
  210.                         stack.stackSize = 0;
  211.                         inventory.markDirty();
  212.                         flag1 = true;
  213.                         break;
  214.                     } else {
  215.                         putStackInSlot(k, new ItemStack(stack.getItem(), slot.getSlotStackLimit(), stack.getItemDamage()));
  216.                         stack.stackSize -= slot.getSlotStackLimit();
  217.                         inventory.markDirty();
  218.                         flag1 = true;
  219.                     }
  220.                 }
  221.  
  222.                 k += (backwards ? -1 : 1);
  223.             }
  224.         }
  225.  
  226.         return flag1;
  227.     }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement