Jump to content

gettingRecipeList


Gnomorian

Recommended Posts

Hi, essentualy making a clone of Crafting Bench 2/3 as they both seem dead.

im having trouble with checking if the player has the right items in their inventory or not.

i have a function that runs during postinit to get the list of reciepes from CraftingManager and sort them into 2 lists, ShapedRecipe and ShapelessRecipe

 

public class GGURecipeManager 
{
/**
 * list of all shaped recipes found in the CraftingManager's Recipe List.
 */
private static List<ShapedRecipes> shapedRecipes = new ArrayList<ShapedRecipes>();
/**
 * list of all shapeless recipes found in the CraftingManager's Recipe List.
 */
private static List<ShapelessRecipes> shapelessRecipes = new ArrayList<ShapelessRecipes>();

/**
 * gets the list of recipes from the crafting manager and sorts them into multiple lists to 
 * reduse lookup time during the game.
 * @param recipeList list of recipes from CraftingManager
 */
public static void init(List recipeList) 
{
	for(int i = 0; i<recipeList.size();i++)
	{
		if(recipeList.get(i) instanceof ShapedRecipes) 
		{
			//if(((ShapedRecipes)recipeList.get(i)).getRecipeOutput() != null)
				shapedRecipes.add(((ShapedRecipes)recipeList.get(i)));
		}
		else if(recipeList.get(i) instanceof ShapelessRecipes) 
		{
			if(((ShapelessRecipes)recipeList.get(i)).getRecipeOutput() != null)
				shapelessRecipes.add(((ShapelessRecipes)recipeList.get(i)));
		}
	}
}
/**
 * returns the list of shaped recipes.
 */
public static List<ShapedRecipes> getShaped() 
{
	return shapedRecipes;

}
/**
 * returns the list of shaped recipes.
 */
public static List<ShapelessRecipes> getShapeless() 
{
	return shapelessRecipes;

}
}

 

i then have a block, which when you right click on it calls the method that loops through your inventory and checks if you have the items required for the stored ShapedRecipe and ShapelessReicpe stored in the lists

 

/*
* Copyright (c) 2014, William <[email protected]>
* 
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package nz.co.crookedhill.ggutils.util;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;

/**
*	recipe instances:
* 	net.minecraft.item.crafting.ShapedRecipes
*	net.minecraftforge.oredict.ShapedOreRecipe
*	net.minecraft.item.crafting.RecipesMapExtending
*	net.minecraft.item.crafting.RecipesArmorDyes
*	net.minecraft.item.crafting.RecipeFireworks
*	net.minecraft.item.crafting.RecipeBookCloning
*	net.minecraft.item.crafting.RecipesMapCloning
*	net.minecraft.item.crafting.ShapelessRecipes
*	net.minecraftforge.oredict.ShapelessOreRecipe
*	net.minecraft.item.crafting.ShapedRecipes
*/

/**
* new packet needs to be added that sends the itemstack 
* the player wants to make to the server. possibly as well
* as the itemstacks used to make it as the items used to
* make it to avoid the server looping through all the recipes.
* @author william-cameron1994
*
*/

public class GGURecipeFilter 
{
public static List filter(List<ItemStack> inventoryItems) 
{
	List avalableRecipes = new ArrayList();

	//for shaped recipes
	for(ShapedRecipes recipe : GGURecipeManager.getShaped()) 
	{
		/*
		 * if the player has an item required for the recipe, 
		 * incriment by one, if its the size of required items 
		 * at the end, player has all items
		 */
		int gotRequiredItems = 0;
		for(ItemStack requiredItem : recipe.recipeItems) 
		{
			if(requiredItem == null)
				continue;
			for(ItemStack invItem : inventoryItems) 
			{
				if(invItem == null)
					continue;
				if(requiredItem.getItem() == (invItem.getItem()) &&requiredItem.stackSize <= invItem.stackSize)
					gotRequiredItems++;
			}
		}
		if(recipe.recipeItems.length == gotRequiredItems)
			avalableRecipes.add(recipe);
	}

	//for shapeless recipes
	for(ShapelessRecipes recipe : GGURecipeManager.getShapeless()) 
	{
		/*
		 * if the player has an item required for the recipe, 
		 * incriment by one, if its the size of required items 
		 * at the end, player has all items
		 */
		int gotRequiredItems = 0;
		for(int i = 0; i < recipe.recipeItems.size(); i++) {
			ItemStack requiredItem = (ItemStack)recipe.recipeItems.get(i);
			if(requiredItem == null)
				continue;
			for(ItemStack invItem : inventoryItems) 
			{
				if(invItem == null)
					continue;
				if(requiredItem.getItem() == (invItem.getItem())&&requiredItem.stackSize <= invItem.stackSize)
					gotRequiredItems++;
			}
		}
		if(recipe.recipeItems.size() == gotRequiredItems)
			avalableRecipes.add(recipe);
	}
	System.out.println(avalableRecipes.size());
	return avalableRecipes;
}
}

 

the last method is called by the block, it then loops through the filtered out "valid" recipes and prints out what you can make.

 

public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int meta, float float1, float float2, float float3) 
{
	player.addStat(GGUAchievements.usedlazyCrafter, 1);
	//open the gui.
	//get the players inventory.
	//display the inventory and the items avalable 
	//to craft with current items.
	if(!world.isRemote)
	{
		IInventory inventory = player.inventory;
		List invItems = new ArrayList();
		//36-39=armour
		for(int i = 0; i < 35; i++) 
		{
			ItemStack items = inventory.getStackInSlot(i);
			if(items == null)
				continue;
			invItems.add(items);
		}
		List recipes = GGURecipeFilter.filter(invItems);
		for(int i = 0; i < recipes.size(); i++) 
		{
			if(recipes.get(i)instanceof ShapedRecipes) 
			{
				System.out.println(player.getDisplayName()+" can craft a shaped "+((ShapedRecipes)recipes.get(i)).getRecipeOutput().getDisplayName());
			}
			else if(recipes.get(i)instanceof ShapelessRecipes) {
				System.out.println(player.getDisplayName()+" can craft a shapeless "+((ShapelessRecipes)recipes.get(i)).getRecipeOutput().getDisplayName());
			}

		}

		return true;
	}
	return false;
}

}

 

 

it sort of seems to work, it finds some recipes, like it knows when you have 3+ planks, you can make slabs, but it doesn't recognize you can make sticks.

if anyone can make heads or tails of what im mucking up here, your help would be apreciated, thanks.

<iframe src="http://widget.mcf.li/mc-mods/minecraft/225523-gnomgnoms-utils" width="100%" style="border: none;"></iframe>

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.