Jump to content

Recommended Posts

Posted

I'm working on a custom buckets mod, and the mod contains two base item classes: BucketEmpty and BucketFull. BucketFull stores the contained liquid in a private Block var called "containedBlock". So far things are working great, but I just ran into my first hitch with my planned design - recipes.

 

I want an instance of a FullBucket item that contains water to be used in a recipe to make clay. A FullBucket item + a dirt block + a gravel block = a clay block. But the following tells the game nothing about what FullBucket contains:

 

GameRegistry.addRecipe(new ShapelessOreRecipe(new ItemStack(Blocks.clay), new Object[]{ModItems.bucketWoodFull, Blocks.dirt, Blocks.gravel}));

 

FYI, ModItems.bucketWoodFull is a class that extends FullBucket. I tried adding another var to the FullBucket class called "containedBlockName" and setting it to "water" if any kind of water was set as the containedBlock, plus creating a getter for it. This throws an exception (as I thought it would):

 

GameRegistry.addRecipe(new ShapelessOreRecipe(new ItemStack(Blocks.clay), new Object[]{ModItems.bucketWoodFull.getContainedBlockName().contains("water"), Blocks.dirt, Blocks.gravel}));

 

So, is there a way to create a recipe that checks for value like I am trying to do? I looked at RecipesFood where cookies use a dye color, but not quite what I want.

Posted

Unless you have a version of the BucketFull item for EVERY fluid (in which case the 'containedBlock' class member would be final and could then easily designate what the recipe output was), then your design is completely broken.

 

Assuming you have one version of the full bucket for every fluid, you would do this:

// new Object[]{} is added automatically by Java, but can (and should) be omitted
GameRegistry.addRecipe(new ShapelessOreRecipe(new ItemStack(Blocks.clay), ModItems.bucketWoodFullWater, Blocks.dirt, Blocks.gravel));

 

If that's not the case, then as I said, your design is broken: Items are singletons, so if you have one and only one bucketWoodFull Item, when you call 'setConatainedBlock(Blocks.water)' or whatever, EVERY SINGLE ONE of those wooden buckets in the entire world will now contain water, even if before they contained something else. Next time someone clicks an empty bucket on, say, sludge, now they all contain sludge.

 

You should probably be storing the contained block type in the ItemStack's NBT, then it will be unique for every stack, meaning I can have a wooden bucket full of water and you filling up yours with sludge won't bother me in the least.

 

To use an ItemStack with NBT in a recipe is possible, but you probably need to create a class that implements IRecipe and register that - in your implementation, you will be able to check the NBT data of the ingredients when determining the outcome.

Posted

You should probably be storing the contained block type in the ItemStack's NBT, then it will be unique for every stack, meaning I can have a wooden bucket full of water and you filling up yours with sludge won't bother me in the least.

 

To use an ItemStack with NBT in a recipe is possible, but you probably need to create a class that implements IRecipe and register that - in your implementation, you will be able to check the NBT data of the ingredients when determining the outcome.

 

Yes! Storing bucket data for each stack in NBT is exactly what I've done (my title says NBT, sorry I didn't make that clear in the text of my post). I thought that I might have to create my own recipe class, I just thought I remembered seeing a post long ago where someone set recipes using NBT without creating their own class.

 

I was also toying with the idea of creating something akin to the Cauldron that you could drop water and other blocks like dirt, cobble and gravel into to get clay. The more I think about a recipe for it, the more I feel it might be too easy.

Posted

I have a similar need, if you get it done please post the code or PM me. I have a custom IRecipe class but I don't think I did much outside vanilla except using my inventories on my container instead of IInventory, or something like that.

www.YouTube.com/WeiseGamer

www.twitter.com/WeiseGamer

Posted

@OP Lol, so your title proclaims indeed! Totally missed that, and didn't read anything about it in the main thread. :P

 

As for recipes with NBT, that can be done, but ONLY for the output, e.g.:

ItemStack output = new ItemStack(Items.wooden_sword); // create desired ItemStack
output.addEnchantment(Enchantment.sharpness, 5); // add NBT data, e.g. enchantments
GameRegistry.addShapelessRecipe(output, Blocks.dirt, Blocks.gravel)); // get a sharpness 5 sword with dirt and gravel! yay!

The only way to handle NBT on the inputs, however, is to implement IRecipe and check for it yourself.

 

@WeiseGuy - you should start your own topic, post your code, and explain more thoroughly what you are trying to accomplish.

 

Posted

@ coolAlias  Thanks again!

 

@ WeiseGuy  Not sure we're looking to do the same thing, but I'll let you know when I get the solution for my problem done.

 

@ DimensionsInTime - Thanks! It's actually REALLY similar to what I'm looking to do.

 

@ coolAlias - I have a topic that is asking about this, but wasn't originally intended to look into NBT in input/output items used in a recipe. Additionally, proper forum etiquette should be "search before you post" and I found this topic that was asking what I'm asking. It's not a thread hijack to say "if you figure this out let me know as I'm doing something similar." Finally, I like your tutorials, thank you!

www.YouTube.com/WeiseGamer

www.twitter.com/WeiseGamer

Posted

As promised, here's the solution I just wrote for my recipe need:

 

package info.dimensionsintime.additionalbuckets.recipe;

import com.sun.istack.internal.NotNull;
import info.dimensionsintime.additionalbuckets.utility.NBTData;
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.world.World;
import net.minecraftforge.oredict.OreDictionary;

import java.util.ArrayList;
import java.util.Iterator;

public class ShapelessNBTRecipe implements IRecipe {

private ItemStack output = null;
private ArrayList<Object> input = new ArrayList<Object>();

public ShapelessNBTRecipe(Block result, @NotNull Object... recipe){
	this(new ItemStack(result), recipe);
}
public ShapelessNBTRecipe(Item result, @NotNull Object... recipe){
	this(new ItemStack(result), recipe);
}

public ShapelessNBTRecipe(ItemStack result, Object... recipe) {
	output = result.copy();
	for (Object in : recipe){
		if (in instanceof ItemStack){
			input.add(((ItemStack)in).copy());
		}else if (in instanceof Item){
			input.add(new ItemStack((Item)in));
		}else if (in instanceof Block){
			input.add(new ItemStack((Block)in));
		}else if (in instanceof String){
			input.add(OreDictionary.getOres((String) in));
		}else{
			String ret = "Invalid NBT shapeless ore recipe: ";
			for (Object tmp :  recipe){
				ret += tmp + ", ";
			}
			ret += output;
			throw new RuntimeException(ret);
		}
	}
}


@Override
public boolean matches(InventoryCrafting inv, World worldIn){
	ArrayList<Object> required = new ArrayList<Object>(input);
	for(int s = 0; s < inv.getSizeInventory(); s++){
		ItemStack slot = inv.getStackInSlot(s);
		if(slot != null){
			boolean inRecipe = false;
			Iterator<Object> req = required.iterator();
			while (req.hasNext()){
				boolean match = false;
				Object next = req.next();
				if (next instanceof ItemStack){
					if(NBTData.hasTag(slot, "containedBlock")){
						if(NBTData.getString(slot, "containedBlock").equals(NBTData.getString((ItemStack)next, "containedBlock"))){
							match = true;
						}
					}else{
						if(slot.getItem() == ((ItemStack) next).getItem()){
							match = true;
						}
					}
				}
				if(match){
					inRecipe = true;
					required.remove(next);
					break;
				}
			}
			if (!inRecipe){
				return false;
			}
		}
	}
	return required.isEmpty();
}

@Override
public ItemStack getCraftingResult(InventoryCrafting inventoryCrafting){
	return this.output.copy();
}

@Override
public int getRecipeSize(){
	return this.input.size();
}

@Override
public ItemStack getRecipeOutput(){
	return this.output;
}

public ItemStack[] getRemainingItems(InventoryCrafting inventoryCrafting)	{
	ItemStack[] aitemstack = new ItemStack[inventoryCrafting.getSizeInventory()];

	for (int i = 0; i < aitemstack.length; ++i) {
		ItemStack itemstack = inventoryCrafting.getStackInSlot(i);
		aitemstack[i] = net.minecraftforge.common.ForgeHooks.getContainerItem(itemstack);
	}

	return aitemstack;
}

}

 

As I said before, the buckets in the mod have NBT set to save the Block the bucket contains in a string ("containedBlock") that actually is the Block's unlocalized name in a string. Keep in mind this is for shapeless recipes, and it's based off the Minecraftforge ShapelessOreRecipe class. The NBTData class in the utility package is just a helper class that checks and fetches NBT data from ItemStacks.

 

And here's how you set the recipe. You create a holder instance of the correct ItemStack and set the desired match NBT on it, then register your recipe like a normal shapeless (EDIT: except it's ShapelessNBTRecipe instead of ShapelessOreRecipe - thought I should point that out!).

 

ItemStack stackHolder = new ItemStack(ModItems.bucketWoodFull);
NBTData.setString(stackHolder, "containedBlock", "tile.water");
GameRegistry.addRecipe(new ShapelessNBTRecipe(new ItemStack(Blocks.clay), stackHolder, Blocks.dirt, Blocks.gravel, Blocks.sand));

 

The result of this recipe is one clay block when you put one of my Wood Buckets (stackHolder) that is filled with water plus one each of dirt, gravel and sand into the table.

 

Hope this helps you, and maybe others!

Posted

As promised, here's the solution I just wrote for my recipe need:

 

package info.dimensionsintime.additionalbuckets.recipe;

import com.sun.istack.internal.NotNull;
import info.dimensionsintime.additionalbuckets.utility.NBTData;
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.world.World;
import net.minecraftforge.oredict.OreDictionary;

import java.util.ArrayList;
import java.util.Iterator;

public class ShapelessNBTRecipe implements IRecipe {

private ItemStack output = null;
private ArrayList<Object> input = new ArrayList<Object>();

public ShapelessNBTRecipe(Block result, @NotNull Object... recipe){
	this(new ItemStack(result), recipe);
}
public ShapelessNBTRecipe(Item result, @NotNull Object... recipe){
	this(new ItemStack(result), recipe);
}

public ShapelessNBTRecipe(ItemStack result, Object... recipe) {
	output = result.copy();
	for (Object in : recipe){
		if (in instanceof ItemStack){
			input.add(((ItemStack)in).copy());
		}else if (in instanceof Item){
			input.add(new ItemStack((Item)in));
		}else if (in instanceof Block){
			input.add(new ItemStack((Block)in));
		}else if (in instanceof String){
			input.add(OreDictionary.getOres((String) in));
		}else{
			String ret = "Invalid NBT shapeless ore recipe: ";
			for (Object tmp :  recipe){
				ret += tmp + ", ";
			}
			ret += output;
			throw new RuntimeException(ret);
		}
	}
}


@Override
public boolean matches(InventoryCrafting inv, World worldIn){
	ArrayList<Object> required = new ArrayList<Object>(input);
	for(int s = 0; s < inv.getSizeInventory(); s++){
		ItemStack slot = inv.getStackInSlot(s);
		if(slot != null){
			boolean inRecipe = false;
			Iterator<Object> req = required.iterator();
			while (req.hasNext()){
				boolean match = false;
				Object next = req.next();
				if (next instanceof ItemStack){
					if(NBTData.hasTag(slot, "containedBlock")){
						if(NBTData.getString(slot, "containedBlock").equals(NBTData.getString((ItemStack)next, "containedBlock"))){
							match = true;
						}
					}else{
						if(slot.getItem() == ((ItemStack) next).getItem()){
							match = true;
						}
					}
				}
				if(match){
					inRecipe = true;
					required.remove(next);
					break;
				}
			}
			if (!inRecipe){
				return false;
			}
		}
	}
	return required.isEmpty();
}

@Override
public ItemStack getCraftingResult(InventoryCrafting inventoryCrafting){
	return this.output.copy();
}

@Override
public int getRecipeSize(){
	return this.input.size();
}

@Override
public ItemStack getRecipeOutput(){
	return this.output;
}

public ItemStack[] getRemainingItems(InventoryCrafting inventoryCrafting)	{
	ItemStack[] aitemstack = new ItemStack[inventoryCrafting.getSizeInventory()];

	for (int i = 0; i < aitemstack.length; ++i) {
		ItemStack itemstack = inventoryCrafting.getStackInSlot(i);
		aitemstack[i] = net.minecraftforge.common.ForgeHooks.getContainerItem(itemstack);
	}

	return aitemstack;
}

}

 

As I said before, the buckets in the mod have NBT set to save the Block the bucket contains in a string ("containedBlock") that actually is the Block's unlocalized name in a string. Keep in mind this is for shapeless recipes, and it's based off the Minecraftforge ShapelessOreRecipe class. The NBTData class in the utility package is just a helper class that checks and fetches NBT data from ItemStacks.

 

And here's how you set the recipe. You create a holder instance of the correct ItemStack and set the desired match NBT on it, then register your recipe like a normal shapeless (EDIT: except it's ShapelessNBTRecipe instead of ShapelessOreRecipe - thought I should point that out!).

 

ItemStack stackHolder = new ItemStack(ModItems.bucketWoodFull);
NBTData.setString(stackHolder, "containedBlock", "tile.water");
GameRegistry.addRecipe(new ShapelessNBTRecipe(new ItemStack(Blocks.clay), stackHolder, Blocks.dirt, Blocks.gravel, Blocks.sand));

 

The result of this recipe is one clay block when you put one of my Wood Buckets (stackHolder) that is filled with water plus one each of dirt, gravel and sand into the table.

 

Hope this helps you, and maybe others!

 

That's awesome thank you!

www.YouTube.com/WeiseGamer

www.twitter.com/WeiseGamer

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.

×
×
  • Create New...

Important Information

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