Jump to content

Recommended Posts

Posted

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?

Posted

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;
}
}

Posted

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.

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.