TristanvO

ContainerMoriumWorkbench.java

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