Jump to content

Check if player is able to craft item which has multiple possible inputs


Fubar

Recommended Posts

Hello there.

I'm currently trying to check if a player would be able to craft an item based on the items the player has in their inventory.

This should be done without using a crafting table or gui. The check is triggered automatically after a specific event (for example right-clicking with an item).

I'm using a 3x3 "InventoryCrafting" matrix to craft items.

 

It's pretty simple if every slot of a crafting recipe has only 1 valid input. I loop through all slots of the recipe and through the player's inventory to check if all items are available.

The problem occurs if a recipe uses the ore dictionary and has multiple valid inputs for a single slot.

 

Let's say there is a recipe which requires "anyFoodItem" in Slot 1 and an "apple" in the 2nd slot.

The player has a bread and an apple in it's inventory. Now I loop through the recipe slots + inventory.

It looks for any food item, finds the bread and puts it into the first slot. Afterwards it finds the apple and puts it into the second slot => The recipe works.

But there's a problem. It will not work if both items in the player's inventory are swapped. So instead of "bread, apple" => "apple, bread"

It looks for any food item, finds the apple and puts it into the first slot. Now it finds the bread, but is unable to put it into the second slot since apples are the only valid item for that slot.

This results into a non-working recipe even if the player would be able to craft the item.

 

To fix this problem I would need to arrange the items in a smart way.

My idea was to check every slot in the crafting matrix and fill the ones which have the least possible items first. But I'm not sure if that works well or if there's a better method.

Link to comment
Share on other sites

You're probably better off doing this the other way around:

Loop over the recipes list and see if the player has the required items.

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.

Link to comment
Share on other sites

The result + problem would be the same.

If it finds an item which can go into slot1 + slot2 and afterwards an item which can only go into slot1 it doesn't work, since the first item was already sent to slot1 and occupies it.

 

That's why I was thinking about some kind of priority system so the second item goes into slot1 first (it has less possibilities) and then the first item goes into the second slot, since it can go into either of them.

Link to comment
Share on other sites

Are you trying to fill actual grid slots, or are you just checking to see if the player can craft a given item?

(Also, don't forget you can split stacks: that bread in your example could actually fill both slots!)

Edited by Draco18s

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.

Link to comment
Share on other sites

It could fill both, but it should only do if the player has enough items. If the player had one of each it wouldn't work.

 

Currently I'm:

- Creating a copy of the inventory

- Create an InventoryMatrix (3x3 slots)

- Fill the matrix with valid input items from the copied inventory

- Reduce the stack size from pulled ItemStacks by one or set them to null if they reach zero

- Use the "findMatchingRecipe" function from the CraftingManager class to check if the matrix would output an item

- If yes: Do changes to original inventory and give the output ItemStack to the player

Edited by Fubar
Link to comment
Share on other sites

I'm not sure what your use-case is, still. Like, why are you checking every item in the player's inventory to see if that results in a valid craft? What's the point?

 

I don't see any reason why the player would ever use an item that "automatically crafts everything in their inventory into whatever it can."

Edited by Draco18s

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.

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.

×
×
  • Create New...

Important Information

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