Jump to content

Multiple crafting recipe ingredients


peter1745

Recommended Posts

Hello, i'm currently having an issue with a crafting recipe, i want the recipe to require more than 1 of a specific ingredient, so you would require say 3x potatoes, 4x carrots and 7x apples.

 

I haven't found a way to do this using the JSON format, so I figured that i would implement an IRecipe, however i can't make it work, logically it seems like it should work. So i was wondering if anyone have any examples of a mod that does this, or if anyone know how to do it.

 

The code that i currently have is below:

 

private static class RecipeMysticalSaladEssence extends net.minecraftforge.registries.IForgeRegistryEntry.Impl<IRecipe> implements IRecipe
	{

		private static final Map<Item, Integer> REQUIRED_AMOUNTS = Maps.newHashMap();
		private static final Map<Item, Boolean> FOUND_ITEMS = Maps.newHashMap();

		public RecipeMysticalSaladEssence()
		{
			FOUND_ITEMS.put(MFItems.CUCUMBER_SLICE, false);
			FOUND_ITEMS.put(MFItems.TOMATO_SLICE, false);
			FOUND_ITEMS.put(MFItems.CARROT_SLICE, false);
			FOUND_ITEMS.put(MFItems.POTATO_SLICE, false);

			REQUIRED_AMOUNTS.put(MFItems.CUCUMBER_SLICE, 7);
			REQUIRED_AMOUNTS.put(MFItems.TOMATO_SLICE, 5);
			REQUIRED_AMOUNTS.put(MFItems.CARROT_SLICE, 2);
			REQUIRED_AMOUNTS.put(MFItems.POTATO_SLICE, 3);
		}

		@Override
		public boolean matches(InventoryCrafting inv, World worldIn)
		{
			int matchingItems = 0;

			for (int i = 0; i < inv.getSizeInventory(); i++)
			{
				ItemStack stack = inv.getStackInSlot(i);
				if (!stack.isEmpty())
				{
					if (this.hasRequiredAmount(stack) && !this.hasBeenFound(stack))
					{
						matchingItems++;
						FOUND_ITEMS.replace(stack.getItem(), true);
					}
				}
			}

			return matchingItems == 4;
		}

		private boolean hasBeenFound(ItemStack stack)
		{
			return FOUND_ITEMS.containsKey(stack.getItem()) && FOUND_ITEMS.get(stack.getItem());
		}

		private boolean hasRequiredAmount(ItemStack stack)
		{
			return REQUIRED_AMOUNTS.containsKey(stack.getItem()) && stack.getCount() >= REQUIRED_AMOUNTS.get(stack.getItem());
		}

		private int getAmountForItem(ItemStack stack)
		{
			if (!REQUIRED_AMOUNTS.containsKey(stack.getItem()))
				return -1;

			return REQUIRED_AMOUNTS.get(stack.getItem());
		}

		@Override
		public ItemStack getCraftingResult(InventoryCrafting inv)
		{
			return new ItemStack(MFItems.MYSTICAL_SALAD_ESSENCE);
		}

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

		@Override
		public ItemStack getRecipeOutput()
		{
			return new ItemStack(MFItems.MYSTICAL_SALAD_ESSENCE);
		}

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

			for (int i = 0; i < inv.getSizeInventory(); i++)
			{
				ItemStack stack = inv.getStackInSlot(i);

				if (!stack.isEmpty())
				{
					int amount = this.getAmountForItem(stack);

					if (amount > 0)
					{
						ItemStack copy = stack.copy();
						copy.shrink(amount);
						items.add(i, copy);
					}
				}

			}

			inv.clear();

			return items;
		}

	}

 

Link to comment
Share on other sites

You are not resetting your FOUND_ITEMS map's state, thus the matches will never return true.

IList#add inserts the entry into a given position in the list, shifting all entries with a higher or equal position to the right. This will crash the NonNullList created with the [int, T] constructor since the add operation in that case is not supported. You need to use set, not add.

Link to comment
Share on other sites

 @V0idWa1k3r That fixed one issue, however i've determined that the major issue isn't with my IRecipe, apparently Minecraft/Minecraft Forge doesn't update the crafting matrix when you right-click to add 1x item to the crafting grid. Personally i think this is a major flaw, since it prevents us from accurately determining how many of an item there is in a stack, since IRecipe's matching method isn't called when you right-click. Thanks for pointing out the smaller flawes in my code btw :)

Link to comment
Share on other sites

7 minutes ago, peter1745 said:

Minecraft/Minecraft Forge doesn't update the crafting matrix when you right-click to add 1x item to the crafting grid. Personally i think this is a major flaw, since it prevents us from accurately determining how many of an item there is in a stack, since IRecipe's matching method isn't called when you right-click.

Well, that happens because in vanilla there is no reason to. All recipes in minecraft take one item at a time, not many, and as such the system just isn't built with requiring more than one item in a stack at a time. This isn't a "major flaw", this is you using the system in a way it was never meant to be used. 

Your best solution is to have your own implementation of ContainerWorkbench that does the check every time a stack size is changed aswell. Of course that means that you need your own workbench.

Edited by V0idWa1k3r
Link to comment
Share on other sites

2 minutes ago, V0idWa1k3r said:

Well, that happens because in vanilla there is no reason to. All recipes in minecraft take one item at a time, not many, and as such the system just isn't built with requiring more than one item in a stack at a time. This isn't a "major flaw", this is you using the system in a way it was never meant to be used. 

Your best solution is to have your own implementation of ContainerWorkbench that does the check every time a stack size is changed aswell. Of course that means that you need your own workbench.

True it wasn't built to support that, however i think it should've been, it would allow for more flexibility, however i understand why it wasn't built to support that kind of implementation

Link to comment
Share on other sites

You could have your own version of ContainerWorkbench that does the check and replace the default one with yours when the container is opened. It won't work for any of the modded crafting tables though.

I suppose you could make an issue/submit a pull request at forge's github about that but I don't know how likely it is that forge would accept it, since you are the only one doing this.

I guess you could also contact Mojang directly and ask them to add the ability to have recipes support more than one item from a stack per craft. They might be willing to do this since they are already doing the whole data-driven recipes for map makers thing, but good luck contacting them in the first place.

Link to comment
Share on other sites

16 minutes ago, V0idWa1k3r said:

You could have your own version of ContainerWorkbench that does the check and replace the default one with yours when the container is opened. It won't work for any of the modded crafting tables though.

I suppose you could make an issue/submit a pull request at forge's github about that but I don't know how likely it is that forge would accept it, since you are the only one doing this.

I guess you could also contact Mojang directly and ask them to add the ability to have recipes support more than one item from a stack per craft. They might be willing to do this since they are already doing the whole data-driven recipes for map makers thing, but good luck contacting them in the first place.

Alright, might try to make an issue or pull request

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



×
×
  • Create New...

Important Information

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