Fix #1822: Return default remainder when recipe is null #12367
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The bug
If the result slot of a crafting grid is taken, and no recipe exists for the craft, the input materials get duplicated.
This happens when, for example,
PrepareItemCraftEvent
is used to override the result slot usinginventory.setResult()
. That is useful for creating dynamic recipes, like dye mixing, something that isn't yet possible to do with regular json recipes.Screen.Recording.mov
Why it happens
The issue is in the
ResultSlot#getRemainingItems
method, which gets called when the craft result is taken. It is used to add remainder items to the crafting grid (e.g. water bucket -> bucket).That's where the dupe happens. A craft is possible, since a result slot was set in
PrepareItemCraftEvent
. However there's no actual recipe, sogetRemainingItems
returns a copy of the input items.In the
onTake
method, those remainder items get added to the crafting grid. If there's already an item with the same components, it will simply add to its quantity, resulting it duplication.And I honestly have no idea why it returns a copy of input items in that case. As far as I know, in vanilla that would never be the case. It's not possible to take an item from the result slot when there is no recipe.
How this PR fixes it
Instead of returning the input items when recipe is null, the default crafting remainder for the items should be returned.
Screen.Recording.1.mov
fixes #1822
thank you