Skip to content

Instantly share code, notes, and snippets.

@NikolaTheProgrammingNoob
Created November 17, 2015 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikolaTheProgrammingNoob/82647c51dfdda8108494 to your computer and use it in GitHub Desktop.
Save NikolaTheProgrammingNoob/82647c51dfdda8108494 to your computer and use it in GitHub Desktop.
Block Container Class
package com.nikola.carbonmod.container;
import com.nikola.carbonmod.crafting.CraftingManager;
import com.nikola.carbonmod.init.CarbonBlocks;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.InventoryCraftResult;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.inventory.Slot;
import net.minecraft.inventory.SlotCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
public class ContainerIronForge extends Container {
public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3);
public IInventory craftResult = new InventoryCraftResult();;
private World worldObj;
private BlockPos field_178145_h;
private static final String __OBFID = "CL_00001744";
public ContainerIronForge(InventoryPlayer invPlayer, World worldIn, BlockPos p_i45800_3) {
this.worldObj = worldIn;
this.field_178145_h = p_i45800_3;
this.addSlotToContainer(new SlotCrafting(invPlayer.player, this.craftMatrix, this.craftResult, 0, 124, 35));
int i;
int j;
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 3; ++j)
{
this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 3, 30 + j * 18, 17 + i * 18));
}
}
for (i = 0; i < 3; ++i)
{
for (j = 0; j < 9; ++j)
{
this.addSlotToContainer(new Slot(invPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
}
}
for (i = 0; i < 9; ++i)
{
this.addSlotToContainer(new Slot(invPlayer, i, 8 + i * 18, 142));
}
this.onCraftMatrixChanged(this.craftMatrix);
}
public ContainerIronForge(InventoryPlayer playerInv, World worldIn, int x, int y, int z) {
}
@Override
public void onCraftMatrixChanged(IInventory inventoryIn) {
this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.worldObj));
}
public void onContainerClosed(EntityPlayer playerIn) {
super.onContainerClosed(playerIn);
if (!this.worldObj.isRemote) {
for (int i = 0; i < 9; ++i) {
ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);
if (itemstack != null) {
playerIn.dropPlayerItemWithRandomChoice(itemstack, false);
}
}
}
}
@Override
public boolean canInteractWith(EntityPlayer playerIn)
{
return this.worldObj.getBlockState(this.field_178145_h).getBlock() != CarbonBlocks.ironForge ? false : playerIn.getDistanceSq((double)this.field_178145_h.getX() + 0.5D, (double)this.field_178145_h.getY() + 0.5D, (double)this.field_178145_h.getZ() + 0.5D) <= 64.0D;
}
@Override
public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2) {
ItemStack itemstack = null;
Slot slot = (Slot)this.inventorySlots.get(par2);
if (slot != null && slot.getHasStack())
{
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();
if (par2 == 0)
{
if (!this.mergeItemStack(itemstack1, 10, 46, true))
{
return null;
}
slot.onSlotChange(itemstack1, itemstack);
}
else if (par2 >= 10 && par2 < 37)
{
if (!this.mergeItemStack(itemstack1, 37, 46, false))
{
return null;
}
}
else if (par2 >= 37 && par2 < 46)
{
if (!this.mergeItemStack(itemstack1, 10, 37, false))
{
return null;
}
}
else if (!this.mergeItemStack(itemstack1, 10, 46, false))
{
return null;
}
if (itemstack1.stackSize == 0)
{
slot.putStack((ItemStack)null);
}
else
{
slot.onSlotChanged();
}
if (itemstack1.stackSize == itemstack.stackSize)
{
return null;
}
slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
}
return itemstack;
}
@Override
public boolean canMergeSlot(ItemStack p_94530_1_, Slot p_94530_2_)
{
return p_94530_2_.inventory != this.craftResult && super.canMergeSlot(p_94530_1_, p_94530_2_);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment