Advertisement
HalestormXV

Container

Mar 21st, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.55 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.  
  36.         // ITEM INVENTORY - you'll need to adjust the slot locations to match your texture file
  37.         // I have them set vertically in columns of 4 to the right of the player model
  38.         for (i = 0; i < InventoryKeyPouch.INV_SIZE; ++i)
  39.         {
  40.             // You can make a custom Slot if you need different behavior,
  41.             // such as only certain item types can be put into this slot
  42.             // We made a custom slot to prevent our inventory-storing item
  43.             // from being stored within itself, but if you want to allow that and
  44.             // you followed my advice at the end of the above step, then you
  45.             // could get away with using the vanilla Slot class
  46.             this.addSlotToContainer(new CelestialKeypouchSlot(this.inventory, i, 80 + (18 * (int)(i/4)), 8 + (18*(i%4))));
  47.         }
  48.  
  49.         // PLAYER INVENTORY - uses default locations for standard inventory texture file
  50.         for (i = 0; i < 3; ++i)
  51.         {
  52.             for (int j = 0; j < 9; ++j)
  53.             {
  54.                 this.addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
  55.             }
  56.         }
  57.  
  58.         // PLAYER ACTION BAR - uses default locations for standard action bar texture file
  59.         for (i = 0; i < 9; ++i)
  60.         {
  61.             this.addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
  62.         }
  63.     }
  64.  
  65.     @Override
  66.     public boolean canInteractWith(EntityPlayer entityplayer)
  67.     {
  68.         // be sure to return the inventory's isUseableByPlayer method
  69.         // if you defined special behavior there:
  70.         return inventory.isUseableByPlayer(entityplayer);
  71.     }
  72.  
  73.     /**
  74.      * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
  75.      */
  76.     @Override
  77.     public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int index)
  78.     {
  79.         ItemStack itemstack = null;
  80.         Slot slot = (Slot) this.inventorySlots.get(index);
  81.  
  82.         if (slot != null && slot.getHasStack())
  83.         {
  84.             ItemStack itemstack1 = slot.getStack();
  85.             itemstack = itemstack1.copy();
  86.  
  87.             // If item is in our custom Inventory or armor slot
  88.             if (index < INV_START)
  89.             {
  90.                 // try to place in player inventory / action bar
  91.                 if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END+1, true))
  92.                 {
  93.                     return null;
  94.                 }
  95.  
  96.                 slot.onSlotChange(itemstack1, itemstack);
  97.             }
  98.             // Item is in inventory / hotbar, try to place in custom inventory or armor slots
  99.             else
  100.             {
  101.                 if (itemstack1.getItem() == CelestialCraft_items.celKeys)
  102.                 {
  103.                     // Try to merge into your custom inventory slots
  104.                     // We use 'InventoryItem.INV_SIZE' instead of INV_START just in case
  105.                     // you also add armor or other custom slots
  106.                     if (!this.mergeItemStack(itemstack1, 0, InventoryKeyPouch.INV_SIZE, false))
  107.                     {
  108.                         return null;
  109.                     }
  110.                 }
  111.  
  112.                 /**
  113.                  * Implementation number 2: Shift-click items between action bar and inventory
  114.                  */
  115.                 // item is in player's inventory, but not in action bar
  116.                 if (index >= INV_START && index < HOTBAR_START)
  117.                 {
  118.                     // place in action bar
  119.                     if (!this.mergeItemStack(itemstack1, HOTBAR_START, HOTBAR_END+1, false))
  120.                     {
  121.                         return null;
  122.                     }
  123.                 }
  124.                 // item in action bar - place in player inventory
  125.                 else if (index >= HOTBAR_START && index < HOTBAR_END+1)
  126.                 {
  127.                     if (!this.mergeItemStack(itemstack1, INV_START, INV_END+1, false))
  128.                     {
  129.                         return null;
  130.                     }
  131.                 }
  132.             }
  133.  
  134.             if (itemstack1.stackSize == 0)
  135.             {
  136.                 slot.putStack((ItemStack) null);
  137.             }
  138.             else
  139.             {
  140.                 slot.onSlotChanged();
  141.             }
  142.  
  143.             if (itemstack1.stackSize == itemstack.stackSize)
  144.             {
  145.                 return null;
  146.             }
  147.  
  148.             slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
  149.         }
  150.  
  151.         return itemstack;
  152.     }
  153.  
  154.     @Override
  155.     public ItemStack slotClick(int slot, int button, int flag, EntityPlayer player) {
  156.         // this will prevent the player from interacting with the item that opened the inventory:
  157.         if (slot >= 0 && getSlot(slot) != null && getSlot(slot).getStack() == player.getHeldItem()) {
  158.             return null;
  159.         }
  160.         return super.slotClick(slot, button, flag, player);
  161.     }
  162.  
  163.     @Override
  164.     protected boolean mergeItemStack(ItemStack stack, int start, int end, boolean backwards)
  165.     {
  166.         boolean flag1 = false;
  167.         int k = (backwards ? end - 1 : start);
  168.         Slot slot;
  169.         ItemStack itemstack1;
  170.  
  171.         if (stack.isStackable())
  172.         {
  173.             while (stack.stackSize > 0 && (!backwards && k < end || backwards && k >= start))
  174.             {
  175.                 slot = (Slot) inventorySlots.get(k);
  176.                 itemstack1 = slot.getStack();
  177.  
  178.                 if (!slot.isItemValid(stack)) {
  179.                     k += (backwards ? -1 : 1);
  180.                     continue;
  181.                 }
  182.  
  183.                 if (itemstack1 != null && itemstack1.getItem() == stack.getItem() &&
  184.                         (!stack.getHasSubtypes() || stack.getItemDamage() == itemstack1.getItemDamage()) && ItemStack.areItemStackTagsEqual(stack, itemstack1))
  185.                 {
  186.                     int l = itemstack1.stackSize + stack.stackSize;
  187.  
  188.                     if (l <= stack.getMaxStackSize() && l <= slot.getSlotStackLimit()) {
  189.                         stack.stackSize = 0;
  190.                         itemstack1.stackSize = l;
  191.                         inventory.markDirty();
  192.                         flag1 = true;
  193.                     } else if (itemstack1.stackSize < stack.getMaxStackSize() && l < slot.getSlotStackLimit()) {
  194.                         stack.stackSize -= stack.getMaxStackSize() - itemstack1.stackSize;
  195.                         itemstack1.stackSize = stack.getMaxStackSize();
  196.                         inventory.markDirty();
  197.                         flag1 = true;
  198.                     }
  199.                 }
  200.  
  201.                 k += (backwards ? -1 : 1);
  202.             }
  203.         }
  204.         if (stack.stackSize > 0)
  205.         {
  206.             k = (backwards ? end - 1 : start);
  207.             while (!backwards && k < end || backwards && k >= start) {
  208.                 slot = (Slot) inventorySlots.get(k);
  209.                 itemstack1 = slot.getStack();
  210.  
  211.                 if (!slot.isItemValid(stack)) {
  212.                     k += (backwards ? -1 : 1);
  213.                     continue;
  214.                 }
  215.  
  216.                 if (itemstack1 == null) {
  217.                     int l = stack.stackSize;
  218.                     if (l <= slot.getSlotStackLimit()) {
  219.                         slot.putStack(stack.copy());
  220.                         stack.stackSize = 0;
  221.                         inventory.markDirty();
  222.                         flag1 = true;
  223.                         break;
  224.                     } else {
  225.                         putStackInSlot(k, new ItemStack(stack.getItem(), slot.getSlotStackLimit(), stack.getItemDamage()));
  226.                         stack.stackSize -= slot.getSlotStackLimit();
  227.                         inventory.markDirty();
  228.                         flag1 = true;
  229.                     }
  230.                 }
  231.  
  232.                 k += (backwards ? -1 : 1);
  233.             }
  234.         }
  235.  
  236.         return flag1;
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement