Guest User

ICCContainer

a guest
Jun 29th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. package com.jam.icc.container;
  2.  
  3. import javax.annotation.Nullable;
  4.  
  5. import com.jam.icc.slots.SlotGhost;
  6. import com.jam.icc.slots.SlotUnstackable;
  7. import com.jam.icc.tileentity.TileEntityICC;
  8.  
  9. import net.minecraft.entity.player.EntityPlayer;
  10. import net.minecraft.inventory.Container;
  11. import net.minecraft.inventory.IInventory;
  12. import net.minecraft.inventory.Slot;
  13. import net.minecraft.item.Item;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraftforge.items.CapabilityItemHandler;
  16. import net.minecraftforge.items.IItemHandler;
  17. import net.minecraftforge.items.SlotItemHandler;
  18.  
  19. public class ICCContainer extends Container{
  20.  
  21. private TileEntityICC te;
  22.  
  23. public ICCContainer(IInventory playerInventory, TileEntityICC te) {
  24. this.te = te;
  25.  
  26. // This container references items out of our own inventory (the 10 slots we hold ourselves)
  27. // as well as the slots from the player inventory so that the user can transfer items between
  28. // both inventories. The two calls below make sure that slots are defined for both inventories.
  29. addOwnSlots();
  30. addPlayerSlots(playerInventory);
  31. }
  32. private void addPlayerSlots(IInventory playerInventory) {
  33. // Slots for the main inventory
  34. for (int row = 0; row < 3; ++row) {
  35. for (int col = 0; col < 9; ++col) {
  36. int x = 8 + col * 18;
  37. int y = row * 18 + 54;
  38. this.addSlotToContainer(new Slot(playerInventory, col + row * 9 + 9, x, y));
  39. }
  40. }
  41.  
  42. // Slots for the hotbar
  43. for (int row = 0; row < 9; ++row) {
  44. int x = 8 + row * 18;
  45. int y = 58 + 54;
  46. this.addSlotToContainer(new Slot(playerInventory, row, x, y));
  47. }
  48. }
  49.  
  50. private void addOwnSlots() {
  51. IItemHandler itemHandler = this.te.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
  52. int x = 8;
  53. int y = 22;
  54.  
  55. // Add our own slots
  56. int slotIndex = 0;
  57. for (int i = 0; i < 9; i++) {
  58. addSlotToContainer(new SlotItemHandler(itemHandler, slotIndex, x, y));
  59. slotIndex++;
  60. x += 18;
  61. }
  62. //add other slots
  63.  
  64. //item filter slot
  65. addSlotToContainer(new SlotGhost(itemHandler, 9, 173, 5));
  66. }
  67.  
  68. public ItemStack getItemInSpecifiedSlot(int specslot)
  69. {
  70. Slot slot = inventorySlots.get(specslot);
  71.  
  72.  
  73.  
  74. return slot.getStack();
  75.  
  76. }
  77. @Nullable
  78. @Override
  79. public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
  80. ItemStack itemstack = ItemStack.EMPTY;
  81. Slot slot = this.inventorySlots.get(index);
  82.  
  83. if (slot != null && slot.getHasStack()) {
  84. ItemStack itemstack1 = slot.getStack();
  85. itemstack = itemstack1.copy();
  86.  
  87. if (index < TileEntityICC.SIZE) {
  88. if (!this.mergeItemStack(itemstack1, TileEntityICC.SIZE, this.inventorySlots.size(), true)) {
  89. return ItemStack.EMPTY;
  90. }
  91. } else if (!this.mergeItemStack(itemstack1, 0, TileEntityICC.SIZE, false)) {
  92. return ItemStack.EMPTY;
  93. }
  94.  
  95. if (itemstack1.isEmpty()) {
  96. slot.putStack(ItemStack.EMPTY);
  97. } else {
  98. slot.onSlotChanged();
  99. }
  100. }
  101.  
  102. return itemstack;
  103. }
  104.  
  105.  
  106.  
  107. @Override
  108. public boolean canInteractWith(EntityPlayer playerIn) {
  109.  
  110. return true;
  111. }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment