Mrhand3

ContainerInventory

Jan 30th, 2014
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. package Mrhand3.SOARYN.Gui;
  2.  
  3. import java.util.HashSet;
  4. import java.util.List;
  5. import java.util.Set;
  6.  
  7. import net.minecraft.entity.player.EntityPlayer;
  8. import net.minecraft.entity.player.EntityPlayerMP;
  9. import net.minecraft.inventory.Container;
  10. import net.minecraft.inventory.ICrafting;
  11. import net.minecraft.inventory.IInventory;
  12. import net.minecraft.inventory.Slot;
  13. import net.minecraft.item.ItemStack;
  14. public abstract class ContainerInventory<T extends IInventory> extends
  15. Container
  16. {
  17. protected final int inventorySize;
  18. protected final IInventory playerInventory;
  19. protected final T owner;
  20.  
  21. protected static class RestrictedSlot extends Slot
  22. {
  23. public RestrictedSlot(IInventory inventory, int slot, int x, int y)
  24. {
  25. super(inventory, slot, x, y);
  26. }
  27.  
  28. @Override
  29. public boolean isItemValid(ItemStack itemstack)
  30. {
  31. return inventory.isItemValidForSlot(slotNumber, itemstack);
  32. }
  33. }
  34.  
  35. public ContainerInventory(IInventory playerInventory, T owner)
  36. {
  37. this.inventorySize = owner.getSizeInventory();
  38. this.playerInventory = playerInventory;
  39. this.owner = owner;
  40. }
  41.  
  42. protected void addInventoryGrid(int xOffset, int yOffset, int width)
  43. {
  44. int height = (int)Math.ceil((double)inventorySize / width);
  45.  
  46. for (int y = 0, slotId = 0; y < height; y++)
  47. {
  48. for (int x = 0; x < width; x++, slotId++)
  49. {
  50. addSlotToContainer(new RestrictedSlot(owner, slotId,
  51. xOffset + x * 18,
  52. yOffset + y * 18));
  53. }
  54. }
  55. }
  56.  
  57. protected void addPlayerInventorySlots(int offsetY)
  58. {
  59. addPlayerInventorySlots(8, offsetY);
  60. }
  61.  
  62. protected void addPlayerInventorySlots(int offsetX, int offsetY)
  63. {
  64. for (int row = 0; row < 3; row++)
  65. for (int column = 0; column < 9; column++)
  66. addSlotToContainer(new Slot(playerInventory,
  67. column + row * 9 + 9,
  68. offsetX + column * 18,
  69. offsetY + row * 18));
  70.  
  71. for (int slot = 0; slot < 9; slot++)
  72. addSlotToContainer(new Slot(playerInventory, slot, offsetX + slot
  73. * 18, offsetY + 58));
  74. }
  75.  
  76. @Override
  77. public boolean canInteractWith(EntityPlayer entityplayer)
  78. {
  79. return owner.isUseableByPlayer(entityplayer);
  80. }
  81.  
  82. public T getOwner()
  83. {
  84. return owner;
  85. }
  86.  
  87. @Override
  88. public ItemStack transferStackInSlot(EntityPlayer pl, int i)
  89. {
  90. ItemStack itemstack = null;
  91. Slot slot = (Slot)inventorySlots.get(i);
  92.  
  93. if (slot != null && slot.getHasStack())
  94. {
  95. ItemStack itemstack1 = slot.getStack();
  96. itemstack = itemstack1.copy();
  97.  
  98. if (i < inventorySize)
  99. {
  100. if (!mergeItemStack(itemstack1, inventorySize, inventorySlots.size(), true))
  101. {
  102. return null;
  103. }
  104. }
  105. else if (!mergeItemStack(itemstack1, 0, inventorySize, false))
  106. {
  107. return null;
  108. }
  109.  
  110. if (itemstack1.stackSize == 0)
  111. {
  112. slot.putStack(null);
  113. }
  114. else
  115. {
  116. slot.onSlotChanged();
  117. }
  118. }
  119.  
  120. return itemstack;
  121. }
  122.  
  123. public int getInventorySize()
  124. {
  125. return inventorySize;
  126. }
  127.  
  128. @SuppressWarnings("unchecked")
  129. public Set<EntityPlayer> getPlayers()
  130. {
  131. Set<EntityPlayer> players = new HashSet<EntityPlayer>();
  132.  
  133. for (ICrafting crafter : (List<ICrafting>)crafters)
  134. {
  135. if (crafter instanceof EntityPlayerMP)
  136. {
  137. players.add((EntityPlayerMP)crafter);
  138. }
  139. }
  140.  
  141. return players;
  142. }
  143.  
  144. public void onButtonClicked(EntityPlayer player, int buttonId) {}
  145.  
  146. @Override
  147. public boolean enchantItem(EntityPlayer player, int buttonId)
  148. {
  149. onButtonClicked(player, buttonId);
  150. return false;
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment