Guest User

ContainerClass

a guest
Aug 18th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. package kyros.testMod.container;
  2.  
  3. import kyros.testMod.testMod;
  4. import kyros.testMod.crafting.WorlSurfaceCraftingManager;
  5. import net.minecraft.entity.player.EntityPlayer;
  6. import net.minecraft.entity.player.InventoryPlayer;
  7. import net.minecraft.init.Blocks;
  8. import net.minecraft.inventory.Container;
  9. import net.minecraft.inventory.IInventory;
  10. import net.minecraft.inventory.InventoryCraftResult;
  11. import net.minecraft.inventory.InventoryCrafting;
  12. import net.minecraft.inventory.Slot;
  13. import net.minecraft.inventory.SlotCrafting;
  14. import net.minecraft.item.ItemStack;
  15. import net.minecraft.world.World;
  16.  
  17. public class ContainerCCrafter extends Container {
  18.  
  19. public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
  20. public IInventory craftResult = new InventoryCraftResult();
  21. private World worldObj;
  22. private int posX;
  23. private int posY;
  24. private int posZ;
  25.  
  26. public ContainerCCrafter(InventoryPlayer invPlayer, World world, int x, int y, int z) {
  27. worldObj = world;
  28. posX = x;
  29. posY = y;
  30. posZ = z;
  31.  
  32. this.addSlotToContainer(new SlotCrafting(invPlayer.player, craftMatrix, craftResult, 0, 124, 35));
  33.  
  34. this.addSlotToContainer(new Slot(craftMatrix, 0, 48, 17));
  35.  
  36. this.addSlotToContainer(new Slot(craftMatrix, 0, 30, 35));
  37.  
  38. this.addSlotToContainer(new Slot(craftMatrix, 0, 66, 35));
  39.  
  40. this.addSlotToContainer(new Slot(craftMatrix, 0, 48, 53));
  41.  
  42. for (int i = 0; i < 3; ++i) {
  43. for (int k = 0; k < 9; ++k) {
  44. this.addSlotToContainer(new Slot(invPlayer, k + i * 9 + 9, 8 + k * 18, 84 + i * 18));
  45. }
  46. }
  47.  
  48. for (int i = 0; i < 9; ++i) {
  49. this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 142));
  50. }
  51.  
  52. onCraftMatrixChanged(craftMatrix);
  53. }
  54.  
  55. public void onCraftingMatrixChanged(IInventory iinv) {
  56. craftResult.setInventorySlotContents(0,
  57. WorlSurfaceCraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj));
  58. }
  59.  
  60. public boolean canInteractWith(EntityPlayer player) {
  61. return this.worldObj.getBlock(this.posX, this.posY, this.posZ) != Blocks.crafting_table ? false
  62. : player.getDistanceSq((double) this.posX + 0.5D, (double) this.posY + 0.5D,
  63. (double) this.posZ + 0.5D) <= 64.0D;
  64. }
  65.  
  66. public void onContainerClosed(EntityPlayer player) {
  67. super.onContainerClosed(player);
  68.  
  69. if (!this.worldObj.isRemote) {
  70. for (int i = 0; i < 9; ++i) {
  71. ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);
  72.  
  73. if (itemstack != null) {
  74. player.dropPlayerItemWithRandomChoice(itemstack, false);
  75. }
  76. }
  77. }
  78. }
  79.  
  80. public ItemStack transferStackInSlot(EntityPlayer player, int INT1) {
  81. ItemStack itemstack = null;
  82. Slot slot = (Slot) this.inventorySlots.get(INT1);
  83.  
  84. if (slot != null && slot.getHasStack()) {
  85. ItemStack itemstack1 = slot.getStack();
  86. itemstack = itemstack1.copy();
  87.  
  88. if (INT1 == 0) {
  89. if (!this.mergeItemStack(itemstack1, 10, 46, true)) {
  90. return null;
  91. }
  92.  
  93. slot.onSlotChange(itemstack1, itemstack);
  94. } else if (INT1 >= 10 && INT1 < 37) {
  95. if (!this.mergeItemStack(itemstack1, 37, 46, false)) {
  96. return null;
  97. }
  98. } else if (INT1 >= 37 && INT1 < 46) {
  99. if (!this.mergeItemStack(itemstack1, 10, 37, false)) {
  100. return null;
  101. }
  102. } else if (!this.mergeItemStack(itemstack1, 10, 46, false)) {
  103. return null;
  104. }
  105.  
  106. if (itemstack1.stackSize == 0) {
  107. slot.putStack((ItemStack) null);
  108. } else {
  109. slot.onSlotChanged();
  110. }
  111.  
  112. if (itemstack1.stackSize == itemstack.stackSize) {
  113. return null;
  114. }
  115.  
  116. slot.onPickupFromSlot(player, itemstack1);
  117. }
  118.  
  119. return itemstack;
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment