Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.8] Recipe using custom bucket that holds contained liquid in NBT


DimensionsInTime
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

@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.

 

Link to comment
Share on other sites

@ 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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

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
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.

 Share



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • So I've gotten this error and I tried fixing it the way another form did, but it came with no success and I am truly lost as this isn't well documented. Doing `gradlew build` shows: My gradlew path is the following: C:/     Gradle         bin         docs         init.d         lib         src         LICENSE         NOTICE         README The bin is added to the system environment variables "Path" list.   Any help? Thanks!
    • I downloaded the Forge Server Installer and installed the server to my root directory, then I tried to launch the server and got nothing but a log file, it did not even generate the files. This is all that is in the log files, I am running the latest Java 8 version and JDK 17 version (Adoptium Temurin 17 LTS). // Debug.txt [28Jan2022 17:09:25.344] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--gameDir, ., --launchTarget, fmlserver, --fml.forgeVersion, 36.2.23, --fml.mcpVersion, 20210115.111550, --fml.mcVersion, 1.16.5, --fml.forgeGroup, net.minecraftforge] [28Jan2022 17:09:25.346] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 8.0.9+86+master.3cf110c starting: java version 17.0.2 by Eclipse Adoptium [28Jan2022 17:09:25.354] [main/DEBUG] [cpw.mods.modlauncher.LaunchServiceHandler/MODLAUNCHER]: Found launch services [minecraft,testharness,fmlclient,fmlserver] // Latest.txt [28Jan2022 17:09:25.344] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher running: args [--gameDir, ., --launchTarget, fmlserver, --fml.forgeVersion, 36.2.23, --fml.mcpVersion, 20210115.111550, --fml.mcVersion, 1.16.5, --fml.forgeGroup, net.minecraftforge] [28Jan2022 17:09:25.346] [main/INFO] [cpw.mods.modlauncher.Launcher/MODLAUNCHER]: ModLauncher 8.0.9+86+master.3cf110c starting: java version 17.0.2 by Eclipse Adoptium  
    • Here is the log file https://drive.google.com/file/d/1Y456kYGNFfgbNDpoE3cW735JKRV-WRt1/view?usp=sharing
    • Here's the new log https://gist.github.com/RichyZ2002/fdaaacb1fd39095d5e15e5d5101542a4
    • I have read the documentation and run the example once, but attach is not executed. When the subscribed Entity is called, attach is called, and then MyCapabilityImplementation is called. fill in the code in the place corresponding to MyCapabilityImplementation. This is how I understand it, am I wrong And I'm not sure what the need for the ResourceLocation string is. It says there are no restrictions, so does that mean it is an ID for identification purposes? Main public void registerCapabilities(RegisterCapabilitiesEvent event) { MyCapability.register(event); }     The problem with not running has been resolved. I had misinterpreted it. MinecraftForge.EVENT_BUS.register(MyCapabilityAttacher.class); So the only thing I'm concerned about is the correctness of future development procedures.  
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.