TristanvO

ContainerWorkbench.java

Apr 10th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | None | 0 0
  1. package com.tristanvo.mod.gui;
  2.  
  3. import com.tristanvo.mod.mod;
  4.  
  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.item.crafting.CraftingManager;
  16. import net.minecraft.world.World;
  17.  
  18. public class ContainerMoriumWorkbench extends Container
  19. {
  20.     public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3);
  21.     public IInventory craftResult = new InventoryCraftResult();
  22.     private World worldObj;
  23.     private int posX;
  24.     private int posY;
  25.     private int posZ;
  26.  
  27.     public ContainerMoriumWorkbench(InventoryPlayer inventory, World world, int par3, int par4, int par5)
  28.     {
  29.         this.worldObj = world;
  30.         this.posX = par3;
  31.         this.posY = par4;
  32.         this.posZ = par5;
  33.         this.addSlotToContainer(new SlotCrafting(inventory.player, this.craftMatrix, this.craftResult, 0, 124, 35));
  34.         int l;
  35.         int i1;
  36.  
  37.         for (l = 0; l < 3; ++l)
  38.         {
  39.             for (i1 = 0; i1 < 3; ++i1)
  40.             {
  41.                 this.addSlotToContainer(new Slot(this.craftMatrix, i1 + l * 3, 30 + i1 * 18, 17 + l * 18));
  42.             }
  43.         }
  44.  
  45.         for (l = 0; l < 3; ++l)
  46.         {
  47.             for (i1 = 0; i1 < 9; ++i1)
  48.             {
  49.                 this.addSlotToContainer(new Slot(inventory, i1 + l * 9 + 9, 8 + i1 * 18, 84 + l * 18));
  50.             }
  51.         }
  52.  
  53.         for (l = 0; l < 9; ++l)
  54.         {
  55.             this.addSlotToContainer(new Slot(inventory, l, 8 + l * 18, 142));
  56.         }
  57.  
  58.         this.onCraftMatrixChanged(this.craftMatrix);
  59.     }
  60.  
  61.     /**
  62.      * Callback for when the crafting matrix is changed.
  63.      */
  64.     public void onCraftMatrixChanged(IInventory inventory)
  65.     {
  66.         this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj));
  67.     }
  68.  
  69.     /**
  70.      * Called when the container is closed.
  71.      */
  72.     public void onContainerClosed(EntityPlayer entity)
  73.     {
  74.         super.onContainerClosed(entity);
  75.  
  76.         if (!this.worldObj.isRemote)
  77.         {
  78.             for (int i = 0; i < 9; ++i)
  79.             {
  80.                 ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);
  81.  
  82.                 if (itemstack != null)
  83.                 {
  84.                     entity.dropPlayerItemWithRandomChoice(itemstack, false);
  85.                 }
  86.             }
  87.         }
  88.     }
  89.  
  90.     public boolean canInteractWith(EntityPlayer entity)
  91.     {
  92.         return this.worldObj.getBlock(this.posX, this.posY, this.posZ) != mod.moriumWorkbench ? false : entity.getDistanceSq((double)this.posX + 0.5D, (double)this.posY + 0.5D, (double)this.posZ + 0.5D) <= 64.0D;
  93.     }
  94.  
  95.     /**
  96.      * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
  97.      */
  98.     public ItemStack transferStackInSlot(EntityPlayer entity, int par2)
  99.     {
  100.         ItemStack itemstack = null;
  101.         Slot slot = (Slot)this.inventorySlots.get(par2);
  102.  
  103.         if (slot != null && slot.getHasStack())
  104.         {
  105.             ItemStack itemstack1 = slot.getStack();
  106.             itemstack = itemstack1.copy();
  107.  
  108.             if (par2 == 0)
  109.             {
  110.                 if (!this.mergeItemStack(itemstack1, 10, 46, true))
  111.                 {
  112.                     return null;
  113.                 }
  114.  
  115.                 slot.onSlotChange(itemstack1, itemstack);
  116.             }
  117.             else if (par2 >= 10 && par2 < 37)
  118.             {
  119.                 if (!this.mergeItemStack(itemstack1, 37, 46, false))
  120.                 {
  121.                     return null;
  122.                 }
  123.             }
  124.             else if (par2 >= 37 && par2 < 46)
  125.             {
  126.                 if (!this.mergeItemStack(itemstack1, 10, 37, false))
  127.                 {
  128.                     return null;
  129.                 }
  130.             }
  131.             else if (!this.mergeItemStack(itemstack1, 10, 46, false))
  132.             {
  133.                 return null;
  134.             }
  135.  
  136.             if (itemstack1.stackSize == 0)
  137.             {
  138.                 slot.putStack((ItemStack)null);
  139.             }
  140.             else
  141.             {
  142.                 slot.onSlotChanged();
  143.             }
  144.  
  145.             if (itemstack1.stackSize == itemstack.stackSize)
  146.             {
  147.                 return null;
  148.             }
  149.  
  150.             slot.onPickupFromSlot(entity, itemstack1);
  151.         }
  152.  
  153.         return itemstack;
  154.     }
  155.  
  156.     public boolean func_94530_a(ItemStack item, Slot slot)
  157.     {
  158.         return slot.inventory != this.craftResult && super.func_94530_a(item, slot);
  159.     }
  160. }
Add Comment
Please, Sign In to add comment