Jump to content

Recommended Posts

Posted

So, as the title says, I am making a custom crafting table.

 

My issues are as follows:

 

1. Shift clicking a stack of items crashes the game

2. When you open the crafting table, your inventory moves all over the place, and sometimes items duplicate or move to the crafting grid by themselves

3. Custom crafting recipes don't work

 

Here is the code for all my related classes (Sorry about the copy pasta):

 

ContainerWorkbench:

package com.descon.container;

import com.descon.mod.WorkBenchCraftingManager;
import com.descon.propsmain.PropsGeneral;
import com.descon.tileentity.TileEntityWorkbench;

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;

public class ContainerWorkbench extends Container {

public InventoryCrafting craftMatrix;
public IInventory craftResult;
private World worldObj;
private int posX;
private int posY;
private int posZ;

public ContainerWorkbench (InventoryPlayer inventory, TileEntityWorkbench entity, World world, int x, int y, int z) {
	craftMatrix = new InventoryCrafting(this, 3, 3);
	craftResult = new InventoryCraftResult();
	worldObj = world;
	posX = x;
	posY = y;
	posZ = z;
	int l;
        int i1;

        for (l = 0; l < 3; ++l)
        {
            for (i1 = 0; i1 < 3; ++i1)
            {
                this.addSlotToContainer(new Slot(this.craftMatrix, i1 + l * 3, 30 + i1 * 18, 17 + l * 18));
            }
        }

        for (l = 0; l < 3; ++l)
        {
            for (i1 = 0; i1 < 9; ++i1)
            {
                this.addSlotToContainer(new Slot(inventory, i1 + l * 9 + 9, 8 + i1 * 18, 84 + l * 18));
            }
        }

        for (l = 0; l < 9; ++l)
        {
            this.addSlotToContainer(new Slot(inventory, l, 8 + l * 18, 142));
        }

        this.onCraftMatrixChanged(this.craftMatrix);
    }


public void onCraftMatricChanged(IInventory iiventory) {
	craftResult.setInventorySlotContents(0, WorkBenchCraftingManager.getInstance().findMatchingRecipe(craftMatrix, worldObj));
}

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

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

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

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


public ItemStack transferStackInSlot(EntityPlayer entityplayer, int q)
{
	ItemStack itemstack = null;
	Slot slot = (Slot)this.inventorySlots.get(q);

	if (slot != null && slot.getHasStack())
	{
		ItemStack itemstack1 = slot.getStack();
		itemstack = itemstack1.copy();

		if (q == 0)
		{
			if (!this.mergeItemStack(itemstack1, 10, 46, true))
			{
				return null;
			}

			slot.onSlotChange(itemstack1, itemstack);
		}
		else if (q >= 10 && q < 37)
		{
			if (!this.mergeItemStack(itemstack1, 37, 46, false))
			{
				return null;
			}
		}
		else if (q >= 37 && q < 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(entityplayer, itemstack1);
	}

	return itemstack;
}

}

 

GuiHandler:

package com.descon.gui;

import com.descon.container.ContainerWorkbench;
import com.descon.propsmain.PropsGeneral;
import com.descon.tileentity.TileEntityWorkbench;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IGuiHandler;

public class GuiHandler implements IGuiHandler {

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world,	int x, int y, int z) {
	TileEntity entity = world.getTileEntity(x, y, z);

	if(entity != null) {
		switch(ID) {
		case PropsGeneral.guiIDdesignersworkbench:
			if (entity instanceof TileEntityWorkbench) {
				return new ContainerWorkbench(player.inventory, (TileEntityWorkbench) entity, world, x, y, z);
			}
			return null;

		}
	}
	return null;

}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world,	int x, int y, int z) {
	TileEntity entity = world.getTileEntity(x, y, z);

	if(entity != null) {
		switch(ID) {
		case PropsGeneral.guiIDdesignersworkbench:
			if (entity instanceof TileEntityWorkbench) {
				return new GuiWorkbench(player.inventory, world, x, z, z);
			}
		}
	}
	return null;
}
}

 

GuiWorkbench:

package com.descon.gui;

import org.lwjgl.opengl.GL11;

import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ContainerWorkbench;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import net.minecraft.world.World;

import com.descon.mod.Main;

public class GuiWorkbench extends GuiContainer{

private ResourceLocation texture = new ResourceLocation(Main.modID + ":" + "textures/gui/WorkbenchTexture.png");

public GuiWorkbench(InventoryPlayer invPlayer, World world, int x, int y, int z) {
	super(new ContainerWorkbench(invPlayer, world, x, y, z));
	// TODO Auto-generated constructor stub

	this.xSize = 176;
	this.ySize = 166;
}

public void onGuiClosed() {
	super.onGuiClosed();
}

protected void drawGuiContainerForegroundLayer(int i, int j) {

	this.fontRendererObj.drawString(StatCollector.translateToLocal("Designer's Workbench"), 100, 5, 0x000000);
}

@Override
protected void drawGuiContainerBackgroundLayer(float var1, int var2, int var3) {

	GL11.glColor4f(1F, 1F, 1F, 1F);

	Minecraft.getMinecraft().getTextureManager().bindTexture(texture);

	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
}

}

 

Main class FMLInitializationEvent:

@EventHandler
public void init(FMLInitializationEvent preEvent) {

	//Renderers
	desconProxy.registerRenderThing();
	desconProxy.registerItemRenderers();

	NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandler());

 

WorkBenchCraftingManager:

package com.descon.mod;

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

import com.descon.crafting.WorkBenchShapedRecipes;
import com.descon.propsmain.PropsModern;

import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.RecipeBookCloning;
import net.minecraft.item.crafting.RecipeFireworks;
import net.minecraft.item.crafting.RecipesArmor;
import net.minecraft.item.crafting.RecipesArmorDyes;
import net.minecraft.item.crafting.RecipesCrafting;
import net.minecraft.item.crafting.RecipesDyes;
import net.minecraft.item.crafting.RecipesFood;
import net.minecraft.item.crafting.RecipesIngots;
import net.minecraft.item.crafting.RecipesMapCloning;
import net.minecraft.item.crafting.RecipesMapExtending;
import net.minecraft.item.crafting.RecipesTools;
import net.minecraft.item.crafting.RecipesWeapons;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraft.world.World;

public class WorkBenchCraftingManager {
/** The static instance of this class */
private static final WorkBenchCraftingManager instance = new WorkBenchCraftingManager();
/** 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 WorkBenchCraftingManager getInstance()
{
	/** The static instance of this class */
	return instance;
}

private WorkBenchCraftingManager()
{
	recipes = new ArrayList();

	this.addRecipe(new ItemStack(PropsModern.laptop, 1), new Object[] {"SSS", "SSS", "SSS", 'S', Items.stick});

	Collections.sort(this.recipes, new WorkBenchRecipeSorter(this));

}

public WorkBenchShapedRecipes addRecipe(ItemStack p_92103_1_, Object ... p_92103_2_)
{
	String s = "";
	int i = 0;
	int j = 0;
	int k = 0;

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

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

	HashMap hashmap;

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

		if (p_92103_2_[i + 1] instanceof Item)
		{
			itemstack1 = new ItemStack((Item)p_92103_2_[i + 1]);
		}
		else if (p_92103_2_[i + 1] instanceof Block)
		{
			itemstack1 = new ItemStack((Block)p_92103_2_[i + 1], 1, 32767);
		}
		else if (p_92103_2_[i + 1] instanceof ItemStack)
		{
			itemstack1 = (ItemStack)p_92103_2_[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;
		}
	}

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

public void addShapelessRecipe(ItemStack p_77596_1_, Object ... p_77596_2_)
{
	ArrayList arraylist = new ArrayList();
	Object[] aobject = p_77596_2_;
	int i = p_77596_2_.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(p_77596_1_, arraylist));
}

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

	for (j = 0; j < p_82787_1_.getSizeInventory(); ++j)
	{
		ItemStack itemstack2 = p_82787_1_.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(p_82787_1_, p_82787_2_))
			{
				return irecipe.getCraftingResult(p_82787_1_);
			}
		}

		return null;
	}
}

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

 

WorkBenchRecipeSorter:

package com.descon.mod;

import java.util.Comparator;

import com.descon.crafting.WorkBenchShapedRecipes;
import com.descon.crafting.WorkBenchShapelessRecipes;

import net.minecraft.item.crafting.IRecipe;

public class WorkBenchRecipeSorter implements Comparator {

final WorkBenchCraftingManager workSurface;

public WorkBenchRecipeSorter(WorkBenchCraftingManager workbenchcraftingmanager) {
	this.workSurface = workbenchcraftingmanager;
}

public int compareRecipes(IRecipe irecipe1, IRecipe irecipe2) {
	return irecipe1 instanceof WorkBenchShapelessRecipes && irecipe2 instanceof WorkBenchShapedRecipes ? 1: (irecipe2 instanceof WorkBenchShapelessRecipes && irecipe1 instanceof WorkBenchShapedRecipes ? -1 :(irecipe2.getRecipeSize() < irecipe1.getRecipeSize() ? -1 : (irecipe2.getRecipeSize() > irecipe1.getRecipeSize() ? 1 : 0)));
}

@Override
public int compare(Object o1, Object o2) {
	return this.compareRecipes((IRecipe)o1, (IRecipe)o2);
}

}

 

Workbench Block Class:

package com.descon.propsmain;

import com.descon.mod.DesconCreativeTabs;
import com.descon.mod.Main;
import com.descon.tileentity.TileEntityPlasmaTubeLight;
import com.descon.tileentity.TileEntityWorkbench;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.BlockContainer;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public class Workbench extends BlockContainer {

public Workbench(Material material) {
	super(material);

	this.setCreativeTab(DesconCreativeTabs.tabGeneral);
	this.setHarvestLevel("axe", 1);
	this.setHardness(3F);
	this.setBlockBounds(0F, 0F, 0F, 1F, 1F, 2F);	
	}

public int getRenderType() {
	return -1;
}

public boolean isOpaqueCube() {
	return false;
}

public boolean renderAsNormalBlock() {
	return false;
}

public TileEntity createNewTileEntity(World var1, int var2) {
	return new TileEntityWorkbench();
}

@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister iconRegister) {
	this.blockIcon = iconRegister.registerIcon(Main.modID + ":" + this.getUnlocalizedName().substring(5));
}

//Directional Bollocks

public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase player, ItemStack itemstack)
{
	int l = MathHelper.floor_double((double)(player.rotationYaw * 4.0F / 360.0F) + 0.5D) & 3;

	world.setBlockMetadataWithNotify(x, y, z, l, 3);

}


public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int q, float a, float b, float c) {
	if(!player.isSneaking()) {
		player.openGui(Main.instance, PropsGeneral.guiIDdesignersworkbench, world, x, y, z);
		return true;
	}else{
		return false;
	}
} 
} 

 

I think that's it. Any help would be greatly appreciated

 

-Whyneb360

Posted

You understand that variable names like "p_92103_1_", "k", "l", etc. are as bad as it can get, right? Nobody should be confronted with such code to debug it... also I agree with diesieben, if you copy code, at least tell us before I read half of the code.

Posted

Thanks, now I know for next time, but it seems like all of my issues are related to the inventory being messed up. It makes sense that the crafting would be off if all the the inventory slots are messed up. Going from the original crafting table to my own, the inventory switches around and goes all out of order and duplicated.

 

Sorry about my previous post, but whereabouts would this code be?

 

Thanks,

 

-Whyneb360

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.