Jump to content

Recommended Posts

Posted

Hello i have tried to use IRecipe to add NBT to an item when it is crafted.  However the output item is never shown and the items i put in as components disappear when i take them out.  Why is this happening?  Am i doing something wrong?

 

package net.drok.poverhaul.recipe;

import net.drok.poverhaul.ModRegistry;
import net.drok.poverhaul.item.ItemRock;
import net.drok.poverhaul.item.ItemStoneCelt;
import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;

public class RecipeStoneCelt extends net.minecraftforge.fml.common.registry.IForgeRegistryEntry.Impl<IRecipe> implements IRecipe {
	
	@Override
	public boolean matches(InventoryCrafting inv, World worldIn) {
		int rocks = 0;
		int sticks = 0;
		int other = 0;
		
		for(int i = 0; i < inv.getSizeInventory(); i++) {
			
			ItemStack stack = inv.getStackInSlot(i);
			int[] ids = OreDictionary.getOreIDs(stack);
			for (int o = 0; o < ids.length; o++) {
				String ore = OreDictionary.getOreName(ids[o]);
				if(stack.getItem() == ModRegistry.items.get("rock")) {
					rocks++;
				}else if(ore.equals("stick")) {
					sticks++;
				} else {
					other++;
				}
			}
		}
		
		if(rocks == 1 && sticks == 1 && other == 0) return true;
		return false;
	}

	@Override
	public ItemStack getCraftingResult(InventoryCrafting inv) {
		ItemStack rockStack = ItemStack.EMPTY;
		
		for(int i = 0; i < inv.getSizeInventory(); i++) {
			if(inv.getStackInSlot(i).getItem() == ModRegistry.items.get("stonecelt")) {
				rockStack = inv.getStackInSlot(i);
				break;
			}
		}
		
		ItemStack celtStack = new ItemStack(ModRegistry.items.get("stonecelt"));
		ItemStoneCelt celt = (ItemStoneCelt) ModRegistry.items.get("stonecelt");
		ItemRock rock = (ItemRock) ModRegistry.items.get("rock");
		
		celt.setQuality(celtStack, rock.getQuality(rockStack));
		
		return celtStack;
	}

	@Override
	public boolean canFit(int width, int height) {
		return (width >= 2 && height >= 2);
	}

	@Override
	public ItemStack getRecipeOutput() {
		return new ItemStack(ModRegistry.items.get("stonecelt"));
	}

	@Override
	public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) {
		NonNullList<ItemStack> nonnulllist = NonNullList.<ItemStack>withSize(inv.getSizeInventory(), ItemStack.EMPTY);

        for (int i = 0; i < nonnulllist.size(); ++i)
        {
            ItemStack itemstack = inv.getStackInSlot(i);

            nonnulllist.set(i, net.minecraftforge.common.ForgeHooks.getContainerItem(itemstack));
        }

        return nonnulllist;
	}
}

 

Posted

Ok, one, your recipes does dick with NBT data. What you've shown here is that the output stack has the same stack size as one of the input stacks.

Two, ModRegistry.items.get() is not a good way to reference your items, it makes your code very stringly typed.

Three, if you're overriding a method do to the same thing as the parent class, don't override (cough, canFit(), cough).

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

First i am dicking with the nbt when i call 

celt.setQuality(celtStack, rock.getQuality(rockStack));

When I do this i am setting an nbt tag called Quality.  Second I dont see why it is bad to use ModRegistry.items.get() as i can get the item based on the registry name and i can register all of the items in one line of code.

Posted
On 2017. 8. 7. at 3:01 AM, drok0920 said:

for (int o = 0; o < ids.length; o++) {

So for every ore ids, you check for the rocks. So it won't work if the rock has no ore id or more than 2 ids.

Also I guess empty itemstack is thought as 'others'...

Plus, when getting the result you look for the last rock slot, while take all of them later. Moreover, I think you remove the result item as well in ::getRemainingItems, but I'm not sure about this part.

Besides, you can use @ObjectHolder.

https://mcforge.readthedocs.io/en/latest/concepts/registries/

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Posted
10 minutes ago, Abastro said:

So for every ore ids, you check for the rocks. So it won't work if the rock has no ore id or more than 2 ids.

Also I guess empty itemstack is thought as 'others'...

Plus, when getting the result you look for the last rock slot, while take all of them later. Moreover, I think you remove the result item as well in ::getRemainingItems, but I'm not sure about this part.

Besides, you can use @ObjectHolder.

https://mcforge.readthedocs.io/en/latest/concepts/registries/

First I only check if the itemstack's item is the same as a rock so the oredictionary for that should not be a problem.  Second I didnt think that an empty itemstack would be a problem but now that you mention it i see that it is.  And third i knew that @ObjectHolder was a thing but doesn't it still need to be registered individually?  I am using the Map because i can just convert it's values to an Array and register them all at once.

Posted
1 minute ago, drok0920 said:

And third i knew that @ObjectHolder was a thing but doesn't it still need to be registered individually?

No, it's annotation magic. All you do is annotate the field (correctly) and everything else happens automagically.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted (edited)
4 minutes ago, Draco18s said:

No, it's annotation magic. All you do is annotate the field (correctly) and everything else happens automagically.

So if i just created a public static final field then set it to my item it will automatically registered so long as I annotate it?

Edited by drok0920
Posted
2 minutes ago, drok0920 said:

So if i just created a public static final field then set it to my item it will automatically registered so long as I annotate it?

No.

 

You create a field, you annotate it, you register the item normally.

Then forge sets the value of the field for you.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
2 minutes ago, Draco18s said:

No.

 

You create a field, you annotate it, you register the item normally.

Then forge sets the value of the field for you.

Ok then i misunderstood.  If you recommended this instead of the Map that i am currently using then it does not fit what i am looking for.  I am looking for a method where i dont have to manually register each Item via

 

 event.getRegistry().register(value);

 

Is there a better way of doing that or is the only proper way to do each one manually.

Posted
2 hours ago, drok0920 said:

Is there a better way of doing that or is the only proper way to do each one manually.

Oh you can use a map or list for that, that's fine. You're just iterating over the entries and registering them. But that isn't what I was saying not to do.

 

I was saying, don't do this: if(stack.getItem() == ModRegistry.items.get("rock")) { ... }

 

THAT is what you shouldn't do. It makes your code rely on strings that if you change ("dur, 'rock' such a dumb name I'm going to change that to 'stone_gniess' instead"), well now you have to search your code manually for every instance of the string "rock" and change it by hand. You can't refactor it by using Source -> Refactor -> Rename.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Oh ok that makes sense one last question since all of my other problems seem to have been fixed is how do i check what item an item stack is?  Do i do stack.equals(stack2)

Posted

You're already doing it:

stack.getItem() == someItem

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Use the debugger to find out why.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
36 minutes ago, Draco18s said:

Use the debugger to find out why.

I figured it out, if the item stack doesnt have an ore dictionary id it never gets to the code that checks if it is a rock.

 

Posted
9 minutes ago, drok0920 said:

I figured it out, if the item stack doesnt have an ore dictionary id it never gets to the code that checks if it is a rock.

 

Mm, you don't say.

 

5 hours ago, Abastro said:

So for every ore ids, you check for the rocks. So it won't work if the rock has no ore id

 

  • Like 2

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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.