Advertisement
HalestormXV

Untitled

Mar 21st, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. package halestormxv.inventory.runeblade;
  2.  
  3. import halestormxv.init.ItemInit;
  4. import net.minecraft.entity.player.EntityPlayer;
  5. import net.minecraft.entity.player.InventoryPlayer;
  6. import net.minecraft.inventory.ClickType;
  7. import net.minecraft.inventory.Container;
  8. import net.minecraft.inventory.Slot;
  9. import net.minecraft.item.ItemStack;
  10. import net.minecraft.util.EnumHand;
  11. import net.minecraftforge.items.IItemHandler;
  12. import net.minecraftforge.items.IItemHandlerModifiable;
  13. import net.minecraftforge.items.SlotItemHandler;
  14.  
  15. import javax.annotation.Nonnull;
  16.  
  17. public class ContainerRuneBlade extends Container
  18. {
  19.     public final EnumHand hand;
  20.     private final int blocked;
  21.  
  22.     public ContainerRuneBlade(InventoryPlayer invPlayer, EnumHand hand, IItemHandlerModifiable invBag)
  23.     {
  24.         /**
  25.          * The X Position First Number is always the pixel of the Top Left Corner Box of your GUI Image
  26.          * The Y Position First Number is always the pixel of the Top Left Corner Box of your GUI Image
  27.          * Use Paint.net to check you pixel locations.
  28.          * 18 is the usual buffer between columns.
  29.          */
  30.         this.hand = hand;
  31.  
  32.         //Bag Inventory
  33.         for (int i = 0; i < 4; i++)
  34.             for (int j = 0; j < 4; j++)
  35.                 //I just made a static class within this class because all I am doing is locking other items except Runestones out.
  36.                 //There is really no need for an entire separate SlotHandler as nothing else is being overridden.
  37.                 this.addSlotToContainer(new RuneBladeSlotHandler(invBag, j + i * 4, 24 + j * 38, 32 + i * 38));
  38.  
  39.         //Player Inventory
  40.         for(int i = 0; i < 3; i++)
  41.             for(int j = 0; j < 9; j++)
  42.                 this.addSlotToContainer(new Slot(invPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
  43.  
  44.         //Player Hotbar
  45.         for (int i = 0; i < 9; i++)
  46.             this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 142));
  47.  
  48.         blocked = hand == EnumHand.MAIN_HAND ? (inventorySlots.size() - 1) - (8 - invPlayer.currentItem) : -1;
  49.     }
  50.  
  51.     @Override
  52.     public boolean canInteractWith(@Nonnull EntityPlayer player)
  53.     {
  54.         return true;
  55.     }
  56.  
  57.     @Nonnull
  58.     @Override
  59.     public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex)
  60.     {
  61.         Slot slot = this.getSlot(slotIndex);
  62.  
  63.         if (slot == null || !slot.getHasStack())
  64.         {
  65.             return ItemStack.EMPTY;
  66.         }
  67.  
  68.         ItemStack stack = slot.getStack();
  69.         ItemStack newStack = stack.copy();
  70.  
  71.         /**
  72.          * The slotIndex is how many actual slots your GUI
  73.          * contains. This will enable Shift+Clicking and such.
  74.          * Remember in code we start our count from 0.
  75.          */
  76.         if (slotIndex < 4)
  77.         {
  78.             if (!this.mergeItemStack(stack, 16, this.inventorySlots.size(), true))
  79.                 return ItemStack.EMPTY;
  80.             slot.onSlotChanged();
  81.         }
  82.         else if (!this.mergeItemStack(stack, 0, 16, false))
  83.         {
  84.             return ItemStack.EMPTY;
  85.         }
  86.         if (stack.isEmpty())
  87.         {
  88.             slot.putStack(ItemStack.EMPTY);
  89.         }
  90.         else
  91.         {
  92.             slot.onSlotChanged();
  93.         }
  94.  
  95.         return slot.onTake(player, newStack);
  96.     }
  97.  
  98.     @Nonnull
  99.     @Override
  100.     public ItemStack slotClick(int slot, int button, ClickType flag, EntityPlayer player)
  101.     {
  102.         if (slot == blocked)
  103.         {
  104.             return ItemStack.EMPTY;
  105.         }
  106.  
  107.         return super.slotClick(slot, button, flag, player);
  108.     }
  109.  
  110.  
  111.     /**
  112.      * I just made this Static Class Within this class because all I am doing
  113.      * is locking all items except Runestones out of the bag. No need for an
  114.      * entire separate class just to achieve this.
  115.      */
  116.     public static class RuneBladeSlotHandler extends SlotItemHandler
  117.     {
  118.         private RuneBladeSlotHandler(IItemHandler itemHandler, int index, int xPosition, int yPosition)
  119.         {
  120.             super(itemHandler, index, xPosition, yPosition);
  121.         }
  122.  
  123.         @Override
  124.         public boolean isItemValid(@Nonnull ItemStack stack) {
  125.             return (stack.getItem() == ItemInit.ITEM_RUNE);
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement