Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.minecolonies.coremod.inventory;
- import net.minecraft.entity.player.EntityPlayer;
- import net.minecraft.entity.player.InventoryPlayer;
- import net.minecraft.init.Blocks;
- import net.minecraft.inventory.*;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.crafting.CraftingManager;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.World;
- import javax.annotation.Nullable;
- public class CraftingGUIBuilding extends Container
- {
- /**
- * The crafting matrix inventory (3x3).
- */
- public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
- public IInventory craftResult = new InventoryCraftResult();
- private final World worldObj;
- /**
- * Position of the workbench
- */
- private final BlockPos pos;
- public CraftingGUIBuilding(InventoryPlayer playerInventory, World worldIn, BlockPos posIn)
- {
- this.worldObj = worldIn;
- this.pos = posIn;
- this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, 124, 35));
- for (int i = 0; i < 2; ++i)
- {
- for (int j = 0; j < 2; ++j)
- {
- this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 2, 30 + j * 18, 17 + i * 18));
- }
- }
- for (int l = 0; l < 3; ++l)
- {
- for (int j1 = 0; j1 < 9; ++j1)
- {
- this.addSlotToContainer(new Slot(playerInventory, j1 + l * 9 + 9, 8 + j1 * 18, 84 + l * 18));
- }
- }
- for (int i1 = 0; i1 < 9; ++i1)
- {
- this.addSlotToContainer(new Slot(playerInventory, i1, 8 + i1 * 18, 142));
- }
- this.onCraftMatrixChanged(this.craftMatrix);
- }
- /**
- * Callback for when the crafting matrix is changed.
- */
- public void onCraftMatrixChanged(IInventory inventoryIn)
- {
- //todo can use this to get the recipe on button click
- this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj));
- }
- /**
- * Called when the container is closed.
- */
- public void onContainerClosed(EntityPlayer playerIn)
- {
- super.onContainerClosed(playerIn);
- if (!this.worldObj.isRemote)
- {
- for (int i = 0; i < 9; ++i)
- {
- ItemStack itemstack = this.craftMatrix.removeStackFromSlot(i);
- if (itemstack != null)
- {
- playerIn.dropItem(itemstack, false);
- }
- }
- }
- }
- public boolean canInteractWith(EntityPlayer playerIn)
- {
- return this.worldObj.getBlockState(this.pos).getBlock() != Blocks.CRAFTING_TABLE
- ? false
- : playerIn.getDistanceSq((double) this.pos.getX() + 0.5D, (double) this.pos.getY() + 0.5D, (double) this.pos.getZ() + 0.5D) <= 64.0D;
- }
- /**
- * Take a stack from the specified inventory slot.
- */
- @Nullable
- public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
- {
- ItemStack itemstack = null;
- Slot slot = (Slot) this.inventorySlots.get(index);
- if (slot != null && slot.getHasStack())
- {
- ItemStack itemstack1 = slot.getStack();
- itemstack = itemstack1.copy();
- if (index == 0)
- {
- if (!this.mergeItemStack(itemstack1, 10, 46, true))
- {
- return null;
- }
- slot.onSlotChange(itemstack1, itemstack);
- }
- else if (index >= 5 && index < 32)
- {
- if (!this.mergeItemStack(itemstack1, 32, 41, false))
- {
- return null;
- }
- }
- else if (index >= 32 && index < 41)
- {
- if (!this.mergeItemStack(itemstack1, 5, 32, false))
- {
- return null;
- }
- }
- else if (!this.mergeItemStack(itemstack1, 5, 41, false))
- {
- return null;
- }
- if (itemstack1.stackSize == 0)
- {
- slot.putStack((ItemStack) null);
- }
- else
- {
- slot.onSlotChanged();
- }
- if (itemstack1.stackSize == itemstack.stackSize)
- {
- return null;
- }
- slot.onPickupFromSlot(playerIn, itemstack1);
- }
- return itemstack;
- }
- /**
- * Called to determine if the current slot is valid for the stack merging (double-click) code. The stack passed in
- * is null for the initial slot that was double-clicked.
- */
- public boolean canMergeSlot(ItemStack stack, Slot slotIn)
- {
- return slotIn.inventory != this.craftResult && super.canMergeSlot(stack, slotIn);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement