Guest User

container

a guest
Oct 17th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1.  
  2. import net.minecraft.entity.player.EntityPlayer;
  3. import net.minecraft.entity.player.InventoryPlayer;
  4. import net.minecraft.inventory.Container;
  5. import net.minecraft.inventory.Slot;
  6. import net.minecraft.item.ItemStack;
  7.  
  8. public class ContainerVendor extends Container
  9. {
  10. private TileVendor tile;
  11. private Boolean lastMode;
  12. private int lastPrice, lastUserCoins, lastOwnerCoins;
  13.  
  14. public ContainerVendor(InventoryPlayer playerInventory, TileVendor tile)
  15. {
  16. this.tile = tile;
  17.  
  18. addSlotToContainer(new SlotGhost(tile, TileVendor.SLOT_TRADE, 9, 17));
  19. addSlotToContainer(new SlotCoinInput(tile, TileVendor.SLOT_OWNER_COIN_INPUT, 35, 55));
  20. addSlotToContainer(new SlotCard(tile, TileVendor.SLOT_OWNER_CARD, 17, 55));
  21. addSlotToContainer(new SlotOutput(tile, TileVendor.SLOT_COIN_OUTPUT, 152, 55));
  22.  
  23. for (int i = 0; i < 9; i++)
  24. addSlotToContainer(new Slot(tile, i, 8 + i * 18, 96));
  25.  
  26. for (int i = 0; i < 3; i++)
  27. for (int j = 0; j < 9; j++)
  28. addSlotToContainer(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 119 + i * 18));
  29.  
  30. for (int i = 0; i < 9; i++)
  31. addSlotToContainer(new Slot(playerInventory, i, 8 + i * 18, 177));
  32. }
  33.  
  34. @Override
  35. public boolean canInteractWith(EntityPlayer player)
  36. {
  37. return tile.isUseableByPlayer(player);
  38. }
  39.  
  40. @Override
  41. public ItemStack transferStackInSlot(EntityPlayer player, int slot)
  42. {
  43. ItemStack stack = null;
  44. Slot slotObject = (Slot) inventorySlots.get(slot);
  45. // null checks and checks if the item can be stacked (maxStackSize > 1)
  46. if (slotObject != null && slotObject.getHasStack())
  47. {
  48. ItemStack stackInSlot = slotObject.getStack();
  49. stack = stackInSlot.copy();
  50.  
  51. // merges the item into player inventory since its in the tileEntity
  52. if (slot < 13)
  53. {
  54. if (!this.mergeItemStack(stackInSlot, 13, 49, true))
  55. {
  56. return null;
  57. }
  58. }
  59. // places it into the tileEntity is possible since its in the player
  60. // inventory
  61. else
  62. {
  63. boolean foundSlot = false;
  64. for (int i = 1; i < 13; i++) // we start at 1 to avoid shift
  65. {
  66. // clicking into trade slot
  67. if (((Slot) inventorySlots.get(i)).isItemValid(stackInSlot)
  68. && this.mergeItemStack(stackInSlot, i, i + 1, false))
  69. {
  70. foundSlot = true;
  71. break;
  72. }
  73. }
  74. if (!foundSlot)
  75. {
  76. return null;
  77. }
  78. }
  79.  
  80. if (stackInSlot.stackSize == 0)
  81. {
  82. slotObject.putStack(null);
  83. } else
  84. {
  85. slotObject.onSlotChanged();
  86. }
  87.  
  88. if (stackInSlot.stackSize == stack.stackSize)
  89. {
  90. return null;
  91. }
  92. slotObject.onPickupFromSlot(player, stackInSlot);
  93. }
  94.  
  95. return stack;
  96. }
  97.  
  98.  
  99.  
  100. @Override
  101. public void detectAndSendChanges()
  102. {
  103. super.detectAndSendChanges();
  104.  
  105. if (lastMode == null || lastMode != tile.sellToUser
  106. || lastUserCoins != tile.userCoins || lastOwnerCoins != tile.ownerCoins || lastPrice != tile.price)
  107. {
  108. tile.scheduleUpdate();
  109. lastMode = tile.sellToUser;
  110. lastUserCoins = tile.userCoins;
  111. lastOwnerCoins = tile.ownerCoins;
  112. lastPrice = tile.price;
  113. }
  114. }
  115.  
  116. @Override
  117. public void onContainerClosed(EntityPlayer player)
  118. {
  119. super.onContainerClosed(player);
  120. tile.onContainerClosed(player);
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment