Jump to content

[1.7.10] Passing NBT Data through a recipe?


61352151511

Recommended Posts

I tried searching and couldn't find the answer anywhere, sorry if this has been answered before.

 

Basically I've created this heart canister that stores health for later, right clicking takes health away leaving you at one heart, shift right clicking gives you the health back, the amount of health the canister has is stored via NBT Data. I've created a second kind of canister, an upgraded one that automatically fills/drains the canister based on how much health you have, having less than 9 hearts will take health from the canister and fill you back to 9 hearts, having more than 9 hearts will take whatever you have over 9, put it in the canister, and leave you at 9 hearts.

 

I want to make it so combining a normal heart canister with a nether star and a bucket will create an auto drain/fill canister, However the part I'm wondering is about saving the hearts in the canister, say the canister has 50 hearts stored in it, if I were to put it in a crafting table with the nether star and bucket I'd want the upgraded one to have 50 hearts as well.

 

So anyone know how I can detect the NBT Data of the item in the crafting table?

Link to comment
Share on other sites

Ok that works, two things however. First the log says this when launching

[08:29:54] [Client thread/INFO] [FML]:  Unknown recipe class! com.sixonethree.snapshot.recipes.RecipesHeartStorageUpgrading Modder please refer to net.minecraftforge.oredict.RecipeSorter

Again the recipes work fine I'm just not sure if this will cause issues in the future or not.

Second I attempted to make a lunchbox like my heart canister, placing it in a crafting grid with any food is supposed to make the crafting result have the same food level of the lunchbox plus the food level of the food item you put in. This kinda works, as well as the crafting result having the correct NBT Data the lunchbox you put in the crafting grid gets changed. So if I put in a lunchbox with 0/100 food stored and a carrot, the crafting result will say 4/100 food stored, but the lunchbox you're crafting with will get changed as well causing it to have 4/100 food stored and not actually having to use up the carrot. Here's the crafting code.

 

package com.sixonethree.snapshot.recipes;

import com.sixonethree.snapshot.init.ModItems;

import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;

public class RecipesLunchboxFeeding implements IRecipe {

@Override
public boolean matches(InventoryCrafting craftingwindow, World world) {
	int NumberOfLunchboxes = 0;
	for (int j = 0; j < craftingwindow.getSizeInventory(); j++) {
		if (craftingwindow.getStackInSlot(j) != null) {
			if (craftingwindow.getStackInSlot(j).getItem() == ModItems.Lunchbox) {
				NumberOfLunchboxes++;
			}
		}
	}
	if (NumberOfLunchboxes == 1) {
		ItemStack lunchbox = null;
		for (int j = 0; j < craftingwindow.getSizeInventory(); j++) {
			if (craftingwindow.getStackInSlot(j) != null) {
				if (craftingwindow.getStackInSlot(j).getItem() == ModItems.Lunchbox) {
					lunchbox = craftingwindow.getStackInSlot(j);
				}
			}
		}
		if (lunchbox != null) {
			float FoodStored = 0F;
			if (lunchbox.getTagCompound() == null) {
				lunchbox.setTagCompound(new NBTTagCompound());
			}
			try {
				FoodStored = lunchbox.getTagCompound().getFloat("Food_Stored");
			} catch (NullPointerException e) { FoodStored = 0F; }
			float FoodPointsToAdd = 0F;
			boolean NonFood = false;
			for (int j = 0; j < craftingwindow.getSizeInventory(); j++) {
				if (craftingwindow.getStackInSlot(j) != null) {
					ItemStack StackInSlot = craftingwindow.getStackInSlot(j);
					if (StackInSlot.getItem() instanceof ItemFood) {
						ItemFood AFood = (ItemFood) StackInSlot.getItem();
						FoodPointsToAdd = FoodPointsToAdd + AFood.func_150905_g(StackInSlot);
					} else {
						if (StackInSlot.getItem() != ModItems.Lunchbox) {
							NonFood = true;
						}
					}
				}
			}
			if (FoodStored + FoodPointsToAdd <= 100F && NonFood == false) { // 100F = Maximum Storage
				return true;
			}
		}
	}
	return false;
}

@Override
public ItemStack getCraftingResult(InventoryCrafting craftingwindow) {
	int NumberOfLunchboxes = 0;
	for (int j = 0; j < craftingwindow.getSizeInventory(); j++) {
		if (craftingwindow.getStackInSlot(j) != null) {
			if (craftingwindow.getStackInSlot(j).getItem() == ModItems.Lunchbox) {
				NumberOfLunchboxes++;
			}
		}
	}
	if (NumberOfLunchboxes == 1) {
		ItemStack lunchbox = null;
		for (int j = 0; j < craftingwindow.getSizeInventory(); j++) {
			if (craftingwindow.getStackInSlot(j) != null) {
				if (craftingwindow.getStackInSlot(j).getItem() == ModItems.Lunchbox) {
					lunchbox = craftingwindow.getStackInSlot(j);
				}
			}
		}
		if (lunchbox != null) {
			float FoodStored = 0F;
			if (lunchbox.getTagCompound() == null) {
				lunchbox.setTagCompound(new NBTTagCompound());
			}
			try {
				FoodStored = lunchbox.getTagCompound().getFloat("Food_Stored");
			} catch (NullPointerException e) { FoodStored = 0F; }
			float FoodPointsToAdd = 0F;
			boolean NonFood = false;
			for (int j = 0; j < craftingwindow.getSizeInventory(); j++) {
				if (craftingwindow.getStackInSlot(j) != null) {
					ItemStack StackInSlot = craftingwindow.getStackInSlot(j);
					if (StackInSlot.getItem() instanceof ItemFood) {
						ItemFood AFood = (ItemFood) StackInSlot.getItem();
						FoodPointsToAdd = FoodPointsToAdd + AFood.func_150905_g(StackInSlot);
					} else {
						if (StackInSlot.getItem() != ModItems.Lunchbox) {
							NonFood = true;
						}
					}
				}
			}
			if (FoodStored + FoodPointsToAdd <= 100F && NonFood == false) { // 100F = Maximum Storage
				ItemStack res = new ItemStack(ModItems.Lunchbox, 1, lunchbox.getItemDamage());
				res.setTagCompound(lunchbox.getTagCompound());
				res.getTagCompound().setFloat("Food_Stored", FoodStored + FoodPointsToAdd);
				return res;
			}
		}
	}
	return null;
}

@Override
public int getRecipeSize() {
	return 9;
}

@Override
public ItemStack getRecipeOutput() {
	return null;
}
}

Link to comment
Share on other sites

I have copied it, as I said it adds the food points just fine, however for some reason it affects both the crafting result, and the lunchbox in the crafting grid. Here's the main part of the code.

 

ItemStack lunchbox = null;

for (int j = 0; j < craftingwindow.getSizeInventory(); j++) {

if (craftingwindow.getStackInSlot(j) != null) {

if (craftingwindow.getStackInSlot(j).getItem() == ModItems.Lunchbox) {

lunchbox = craftingwindow.getStackInSlot(j);

}

}

}

if (lunchbox != null) {

float FoodStored = 0F;

if (lunchbox.getTagCompound() == null) {

lunchbox.setTagCompound(new NBTTagCompound());

}

try {

FoodStored = lunchbox.getTagCompound().getFloat("Food_Stored");

} catch (NullPointerException e) { FoodStored = 0F; }

float FoodPointsToAdd = 0F;

boolean NonFood = false;

for (int j = 0; j < craftingwindow.getSizeInventory(); j++) {

if (craftingwindow.getStackInSlot(j) != null) {

ItemStack StackInSlot = craftingwindow.getStackInSlot(j);

if (StackInSlot.getItem() instanceof ItemFood) {

ItemFood AFood = (ItemFood) StackInSlot.getItem();

FoodPointsToAdd = FoodPointsToAdd + AFood.func_150905_g(StackInSlot);

} else {

if (StackInSlot.getItem() != ModItems.Lunchbox) {

NonFood = true;

}

}

}

}

if (FoodStored + FoodPointsToAdd <= 100F && NonFood == false) { // 100F = Maximum Storage

ItemStack res = new ItemStack(ModItems.Lunchbox, 1, lunchbox.getItemDamage());

res.setTagCompound(lunchbox.getTagCompound());

res.getTagCompound().setFloat("Food_Stored", FoodStored + FoodPointsToAdd);

return res;

}

First it goes through all the slots of the crafting grid and sets the ItemStack "lunchbox" to the one in the slot

Then if lunchbox isn't null it attempts to set the FoodStored variable to whatever the NBT Data of the lunchbox is (Succeeds)

Then it goes through all slots, if the item in the current slot is a food it adds to a "FoodToAddVariable"

Finally if the current food stored and the foodpoundstoadd variable are less than 100 it

- Creates a new itemstack with the same item damage as the lunchbox used

- sets the tag compound of that to the lunchbox's tag compound

- sets the float of it to the correct number

- returns the res ItemStack

 

However the part I'm really confused about is why it affects the lunchbox in the crafting grid. If you don't understand what I'm saying I'll try to explain it better

 

Nevermind, doing lunchbox.copy() then setting the NBT worked, which is weird because that's not how I did it for the heart canister's and they worked fine.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • While living in Copenhagen, I became deeply involved in a vibrant local crypto community. The energy was palpable; people were passionate about the potential of blockchain technology and the myriad of opportunities it offered. I was initially skeptical but soon found myself captivated by the discussions surrounding various cryptocurrencies. After much deliberation, I decided to invest 4,000 DKK in Dogecoin, motivated by both the community's enthusiasm and the coin’s unique charm as a meme-driven currency. To my astonishment, my investment blossomed into a staggering 100,000 DKK within a few months. The thrill of watching my investment grow was exhilarating, and I felt a sense of belonging and achievement among my peers. However, as the community grew, so did the differences in opinion about the direction of our projects and investments. I found myself caught in a heated disagreement with a prominent member regarding strategic decisions, which escalated tensions within the group. Things took a turn for the worse when, following this disagreement, I was locked out of my email account. This was more than just a mere inconvenience; it felt like a nightmare unfolding. My email was the gateway to my crypto assets and exchanges, and without access, I was paralyzed. I frantically attempted to regain control but found myself hitting dead ends. The thought of losing my investment sent waves of anxiety through me. Desperate for a solution, I reached out to friends in the community, hoping they could offer guidance. To my relief, several members were sympathetic to my plight and shared their experiences of similar challenges. One name kept surfacing: ADRIAN LAMO HACKER. I Consult ADRIAN LAMO HACKER Via email: Adrianlamo@ consultant. com / Telegram: @ADRIANLAMOHACKERTECH they also have an active Whats App: ‪+1 (90 9) 73 9‑0 2 69‬ It was touted as a reliable service for recovering compromised accounts, and I was eager to give it a try. With their swift assistance, I provided the necessary information, and the team quickly went to work. They were professional and responsive, keeping me updated throughout the process. Within a short period, I received the long-awaited notification that my email account had been restored. A wave of relief washed over me; I could finally access my assets and continue participating in the community. Regaining control not only allowed me to protect my investment but also reinforced the importance of security in the crypto space. It served as a valuable lesson about the risks and challenges inherent in this rapidly evolving world. With newfound confidence, I re-engaged with the community, more vigilant than ever about safeguarding my digital assets.
    • As the title says, I'm trying to install Forge on Linux. Whenever I load up the installer, and let it run, I end up getting an error that says: "Processor failed, invalid outputs:" Then, it shows the .jar file I used for the installer, and some codes that don't make any sense to my pea-sized brain. All I can tell however, is that they're different and they aren't supposed to be. (Mostly because it tells me.) I don't know how to fix this, and I've encountered this for every file I've tried so far.
    • Descargo un mod y luego, en archivo jar, lo mando a la carpeta "mods" en minecraft, pero al entrar a minecraft forge no me aparecen los mods por ningun lado, si pongo un mod incompatible me sale un error pero no me aparecen los demas mods, ¿Que puedo hacer?  
    • Descargo un mod y luego, en archivo jar, lo mando a la carpeta "mods" en minecraft, pero al entrar a minecraft forge no me aparecen los mods por ningun lado, si pongo un mod incompatible me sale un error pero no me aparecen los demas mods, ¿Que puedo hacer?  
  • Topics

×
×
  • Create New...

Important Information

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