Jump to content

Custom Rendered Block (TileEntity) as Crafting Table Help!


Nodnarb3

Recommended Posts

I'm new to the forums, so hopefully all goes well. I am having trouble taking a custom block model and using it as a crafting table.

So far I have gotten it to a point where I can open the gui, interact with it, but I can't get it to actually complete the recipe corresponding to the items placed inside. I'm really not sure where to look for the problem, any help would be great, or a tutorial on making a crafting table from a Tile Entity, not a furnace, would be helpful. Thanks!

 

Crafting Manager

 

package net.survivalplus.mod.crafting;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;
import net.survivalplus.mod.SurvivalPlus;

public class SkinningTableCraftingManager
{
    /** The static instance of this class */
    private static final SkinningTableCraftingManager instance = new SkinningTableCraftingManager();
    /** A list of all the recipes added */
    private List recipes = new ArrayList();
    private static final String __OBFID = "CL_00000090";

    /**
     * Returns the static instance of this class
     */
    public static final SkinningTableCraftingManager getInstance()
    {
        /** The static instance of this class */
        return instance;
    }

    private SkinningTableCraftingManager()
    {
    	
    	recipes = new ArrayList();
    	this.addShapelessRecipe(new ItemStack(SurvivalPlus.itemSkinnedCowCarcass, 1), new Object[]{SurvivalPlus.itemCowCarcass, new ItemStack(SurvivalPlus.itemSkinningKnife, 1, OreDictionary.WILDCARD_VALUE)});
    	//this.addRecipe(new ItemStack(SurvivalPlus.itemSkinnedCowCarcass, 1), new Object[]{"CK", 'C', SurvivalPlus.itemCowCarcass, 'K', new ItemStack(SurvivalPlus.itemSkinningKnife, 1, OreDictionary.WILDCARD_VALUE)});
    	
        Collections.sort(this.recipes, new SkinningTableRecipeSorter(this));
  
    }

    public SkinningTableShapedRecipes addRecipe(ItemStack par1ItemStack, Object ... par2ArrayOfObj)
    {
        String s = "";
        int i = 0;
        int j = 0;
        int k = 0;

        if (par2ArrayOfObj[i] instanceof String[])
        {
            String[] astring = (String[])((String[])par2ArrayOfObj[i++]);

            for (int l = 0; l < astring.length; ++l)
            {
                String s1 = astring[l];
                ++k;
                j = s1.length();
                s = s + s1;
            }
        }
        else
        {
            while (par2ArrayOfObj[i] instanceof String)
            {
                String s2 = (String)par2ArrayOfObj[i++];
                ++k;
                j = s2.length();
                s = s + s2;
            }
        }

        HashMap hashmap;

        for (hashmap = new HashMap(); i < par2ArrayOfObj.length; i += 2)
        {
            Character character = (Character)par2ArrayOfObj[i];
            ItemStack itemstack1 = null;

            if (par2ArrayOfObj[i + 1] instanceof Item)
            {
                itemstack1 = new ItemStack((Item)par2ArrayOfObj[i + 1]);
            }
            else if (par2ArrayOfObj[i + 1] instanceof Block)
            {
                itemstack1 = new ItemStack((Block)par2ArrayOfObj[i + 1], 1, 32767);
            }
            else if (par2ArrayOfObj[i + 1] instanceof ItemStack)
            {
                itemstack1 = (ItemStack)par2ArrayOfObj[i + 1];
            }

            hashmap.put(character, itemstack1);
        }

        ItemStack[] aitemstack = new ItemStack[j * k];

        for (int i1 = 0; i1 < j * k; ++i1)
        {
            char c0 = s.charAt(i1);

            if (hashmap.containsKey(Character.valueOf(c0)))
            {
                aitemstack[i1] = ((ItemStack)hashmap.get(Character.valueOf(c0))).copy();
            }
            else
            {
                aitemstack[i1] = null;
            }
        }

        SkinningTableShapedRecipes shapedrecipes = new SkinningTableShapedRecipes(j, k, aitemstack, par1ItemStack);
        this.recipes.add(shapedrecipes);
        return shapedrecipes;
    }

    public void addShapelessRecipe(ItemStack par1ItemStack, Object ... par2ArrayOfObj)
    {
        ArrayList arraylist = new ArrayList();
        Object[] aobject = par2ArrayOfObj;
        int i = par2ArrayOfObj.length;

        for (int j = 0; j < i; ++j)
        {
            Object object1 = aobject[j];

            if (object1 instanceof ItemStack)
            {
                arraylist.add(((ItemStack)object1).copy());
            }
            else if (object1 instanceof Item)
            {
                arraylist.add(new ItemStack((Item)object1));
            }
            else
            {
                if (!(object1 instanceof Block))
                {
                    throw new RuntimeException("Invalid shapeless recipe!");
                }

                arraylist.add(new ItemStack((Block)object1));
            }
        }

        this.recipes.add(new ShapelessRecipes(par1ItemStack, arraylist));
    }

    public ItemStack findMatchingRecipe(InventoryCrafting par1InventoryCrafting, World par2World)
    {
        int i = 0;
        ItemStack itemstack = null;
        ItemStack itemstack1 = null;
        int j;

        for (j = 0; j < par1InventoryCrafting.getSizeInventory(); ++j)
        {
            ItemStack itemstack2 = par1InventoryCrafting.getStackInSlot(j);

            if (itemstack2 != null)
            {
                if (i == 0)
                {
                    itemstack = itemstack2;
                }

                if (i == 1)
                {
                    itemstack1 = itemstack2;
                }

                ++i;
            }
        }

        if (i == 2 && itemstack.getItem() == itemstack1.getItem() && itemstack.stackSize == 1 && itemstack1.stackSize == 1 && itemstack.getItem().isRepairable())
        {
            Item item = itemstack.getItem();
            int j1 = item.getMaxDamage() - itemstack.getItemDamageForDisplay();
            int k = item.getMaxDamage() - itemstack1.getItemDamageForDisplay();
            int l = j1 + k + item.getMaxDamage() * 5 / 100;
            int i1 = item.getMaxDamage() - l;

            if (i1 < 0)
            {
                i1 = 0;
            }

            return new ItemStack(itemstack.getItem(), 1, i1);
        }
        else
        {
            for (j = 0; j < this.recipes.size(); ++j)
            {
                IRecipe irecipe = (IRecipe)this.recipes.get(j);

                if (irecipe.matches(par1InventoryCrafting, par2World))
                {
                    return irecipe.getCraftingResult(par1InventoryCrafting);
                }
            }

            return null;
        }
    }

    /**
     * returns the List<> of all recipes
     */
    public List getRecipeList()
    {
        return this.recipes;
    }
}

 

 

Tile Entity:

 

package net.survivalplus.mod.tileentity;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;

public class TileEntitySkinningTable extends TileEntity implements ISidedInventory
{

private String localizedName;

private ItemStack[] slots = new ItemStack[3];

public void setGuiDisplayName(String displayName)
{
	this.localizedName = displayName;
}

@Override
public int getSizeInventory() 
{
	return this.slots.length;
}

@Override
public ItemStack getStackInSlot(int i)
{
	return this.slots[i];
}

@Override
public ItemStack decrStackSize(int var1, int var2)
{
	return null;
}

@Override
public ItemStack getStackInSlotOnClosing(int var1)
{
	return null;
}

@Override
public void setInventorySlotContents(int i, ItemStack itemstack)
{
	this.slots[i] = itemstack;

	if(itemstack != null && itemstack.stackSize > this.getInventoryStackLimit()) {
		itemstack.stackSize = this.getInventoryStackLimit();
	}
}

@Override
public String getInventoryName()
{
	return this.hasCustomInventoryName() ? this.localizedName : "container.skinningTable";
}

@Override
public boolean hasCustomInventoryName()
{
	return this.localizedName != null && this.localizedName.length() > 0;
}

@Override
public int getInventoryStackLimit()
{
	return 64;
}

@Override
public boolean isUseableByPlayer(EntityPlayer var1)
{
	return false;
}

@Override
public void openInventory()
{

}

@Override
public void closeInventory()
{

}

@Override
public boolean isItemValidForSlot(int var1, ItemStack var2)
{
	return false;
}

@Override
public int[] getAccessibleSlotsFromSide(int var1)
{
	return null;
}

@Override
public boolean canInsertItem(int var1, ItemStack var2, int var3)
{
	return false;
}

@Override
public boolean canExtractItem(int var1, ItemStack var2, int var3)
{
	return false;
}
}

 

 

Container:

 

package net.survivalplus.mod.container;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
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.world.World;
import net.survivalplus.mod.SurvivalPlus;
import net.survivalplus.mod.crafting.SkinningTableCraftingManager;
import net.survivalplus.mod.tileentity.TileEntitySkinningTable;

public class ContainerSkinningTable extends Container
{

private TileEntitySkinningTable skinningTable;

public InventoryCrafting craftMatrix;
public IInventory craftResult1;
private World worldObj;
private int posX;
private int posY;
private int posZ;
private int verticalSlots = 1;
private int horizontalSlots = 2;

public ContainerSkinningTable(InventoryPlayer inventory, TileEntitySkinningTable entity)
{
	this.skinningTable = entity;

	craftMatrix = new InventoryCrafting(this, verticalSlots, horizontalSlots);
	craftResult1 = new InventoryCraftResult();
	worldObj = entity.getWorldObj();
	posX = entity.xCoord;
	posY = entity.yCoord;
	posZ = entity.zCoord;

	//Output Slot
	this.addSlotToContainer(new SlotCrafting(inventory.player, craftMatrix, craftResult1, 0, 124, 35));

	//Crafting Grid
	for (int i = 0; i < verticalSlots; i++)
	{
		for (int k = 0; k < horizontalSlots; k++)
		{
			this.addSlotToContainer(new Slot(craftMatrix, k + i * 5, 41 + k * 18, 34 + i * 18));
		}
	}

	//Inventory
	for(int i = 0; i < 3; i++)
	{
		for (int k = 0; k < 9; k++)
		{
			this.addSlotToContainer(new Slot(inventory, k + i * 9 + 9, 8 + k * 18, 84 + i * 18));
		}
	}

	//Hot bar
	for (int i = 0; i < 9; i++)
	{
		this.addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 142)); //?, x, y
	}

	onCraftMatrixChanged(craftMatrix);
}

public void onCraftMatrixChange(IInventory iinventory)
{
	craftResult1.setInventorySlotContents(0, SkinningTableCraftingManager.getInstance().findMatchingRecipe(craftMatrix, worldObj));
}

@Override
public boolean canInteractWith(EntityPlayer player)
{
	if(worldObj.getBlock(posX, posY, posZ) != SurvivalPlus.tableSkinningTable)
	{
		return false;
	}
	else
	{
		return player.getDistanceSq((double)posX + 0.5d, (double)posY + 0.5d, (double)posZ + 0.5d) <= 64.0d;
	}
}

public void onContainerClosed(EntityPlayer par1EntityPlayer)
{
        super.onContainerClosed(par1EntityPlayer);

        if (!this.worldObj.isRemote)
        {
            for (int i = 0; i < (verticalSlots * horizontalSlots); i++)
            {
                ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);

                if (itemstack != null)
                {
                    par1EntityPlayer.dropPlayerItemWithRandomChoice(itemstack, false);
                }
            }
        }
    }

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, 3, 39, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if (par2 >= 3 && par2 < 30)
            {
                if (!this.mergeItemStack(itemstack1, 30, 39, false))
                {
                    return null;
                }
            }
            else if (par2 >= 30 && par2 < 39)
            {
                if (!this.mergeItemStack(itemstack1, 3, 30, false))
                {
                    return null;
                }
            }
            else if (!this.mergeItemStack(itemstack1, 3, 39, 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;
    }

}

 

 

I can provide any other classes upon request.

 

Any help is welcome!

Thank you,

Nodnarb3

//Nodnarb3

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.