-
-
Notifications
You must be signed in to change notification settings - Fork 216
Make recipe placing from recipe book working with custom ingredients #4241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MrHell228
wants to merge
23
commits into
SpongePowered:api-14
Choose a base branch
from
MrHell228:api-14-fix/recipe-book-placer
base: api-14
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
9a819eb
make placing recipe from recipe book working with custom ingredients
MrHell228 0f14cb9
remove adjusted ServerPlaceRecipe#moveItemToGrid copy
MrHell228 25562d6
move RecipeBridge logic to PlacementInfoBridge
MrHell228 7bede09
reduce the amount of injections
MrHell228 ab60d1c
move some things
MrHell228 f08fc22
follow the review
MrHell228 1d935e2
fail finding slot when exemplary and craft input stacks can't stack
MrHell228 17ae6d8
add RecipePlaceTest
MrHell228 0733598
follow the review
MrHell228 1a94490
make tests more flexible
MrHell228 3b01885
Revert "make tests more flexible"
MrHell228 6fb1659
test option for creative mode, tests for full inventory
MrHell228 a0bb7ae
move bad layout tests to its own populator
MrHell228 46cd7a3
Update RecipePlaceTest.java
MrHell228 e8556bf
Merge branch 'api-14' into api-14-fix/recipe-book-placer
MrHell228 9130a71
Update SpongeAPI
MrHell228 6c684fb
Update SpongeAPI
MrHell228 4d6d606
Merge branch 'api-14-fix/recipe-book-placer' of https://github.com/Mr…
MrHell228 5f40fb9
Update SpongeAPI
MrHell228 0cf4640
enable custom shapelss recipe tests
MrHell228 bf8b5f7
enable custom shapelss recipe tests
MrHell228 eeb5dee
Merge branch 'api-14-fix/recipe-book-placer' of https://github.com/Mr…
MrHell228 2b0f447
Update SpongeAPI
MrHell228 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/org/spongepowered/common/bridge/world/item/crafting/PlacementInfoBridge.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * This file is part of Sponge, licensed under the MIT License (MIT). | ||
| * | ||
| * Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
| * Copyright (c) contributors | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package org.spongepowered.common.bridge.world.item.crafting; | ||
|
|
||
| import net.minecraft.world.entity.player.StackedContents; | ||
| import net.minecraft.world.item.ItemStack; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface PlacementInfoBridge { | ||
|
|
||
| boolean bridge$hasCustomIngredients(); | ||
|
|
||
| List<StackedContents.IngredientInfo<ItemStack>> bridge$getStackIngredientInfos(); | ||
| } |
127 changes: 127 additions & 0 deletions
127
src/main/java/org/spongepowered/common/item/recipe/crafting/SpongeStackedItemContents.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * This file is part of Sponge, licensed under the MIT License (MIT). | ||
| * | ||
| * Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
| * Copyright (c) contributors | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package org.spongepowered.common.item.recipe.crafting; | ||
|
|
||
| import it.unimi.dsi.fastutil.Hash; | ||
| import it.unimi.dsi.fastutil.objects.Object2ReferenceMap; | ||
| import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenCustomHashMap; | ||
| import net.minecraft.core.Holder; | ||
| import net.minecraft.world.entity.player.StackedContents; | ||
| import net.minecraft.world.entity.player.StackedItemContents; | ||
| import net.minecraft.world.item.Item; | ||
| import net.minecraft.world.item.ItemStack; | ||
| import net.minecraft.world.item.crafting.PlacementInfo; | ||
| import net.minecraft.world.item.crafting.Recipe; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
| import org.spongepowered.common.bridge.world.item.crafting.PlacementInfoBridge; | ||
|
|
||
| import java.util.function.Function; | ||
|
|
||
| public final class SpongeStackedItemContents extends StackedItemContents { | ||
|
|
||
| private static final Hash.Strategy<ItemStack> STACK_HASH_STRATEGY = new Hash.Strategy<>() { | ||
| @Override | ||
| public int hashCode(final ItemStack o) { | ||
| return ItemStack.hashItemAndComponents(o); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(final @Nullable ItemStack a, final @Nullable ItemStack b) { | ||
| return (a == b) || (a != null && b != null && ItemStack.isSameItemSameComponents(a, b)); | ||
| } | ||
| }; | ||
|
|
||
| private final Object2ReferenceMap<ItemStack, ItemStack> stackInterner = | ||
| new Object2ReferenceOpenCustomHashMap<>(SpongeStackedItemContents.STACK_HASH_STRATEGY); | ||
| private final StackedContents<ItemStack> stackedContents = new StackedContents<>(); | ||
|
|
||
| private final StackedContents.Output<ItemStack> addCallback; | ||
| private final Runnable clearCallback; | ||
|
|
||
| public SpongeStackedItemContents( | ||
| final StackedContents.Output<ItemStack> addCallback, final Runnable clearCallback | ||
| ) { | ||
| this.addCallback = addCallback; | ||
| this.clearCallback = clearCallback; | ||
| } | ||
|
|
||
| @Override | ||
| public void accountStack(final ItemStack stack, final int maxStackSize) { | ||
| // Account to parent contents because it's used in | ||
| // #canCraft(List<StackedContents.IngredientInfo<Holder<Item>>>, StackedContents.Output<Holder<Item>>) | ||
| // and we can't safely override it to use StackedContents<ItemStack> | ||
| super.accountStack(stack, maxStackSize); | ||
|
|
||
| if (!stack.isEmpty()) { | ||
| // StackedContents works on Reference2IntMap, so if we meet stack which "same" copy | ||
| // has already been accounted we would need to provide the stack that was met first. | ||
| final ItemStack stackToAccount = this.stackInterner.computeIfAbsent(stack, Function.identity()); | ||
| this.stackedContents.account(stackToAccount, Math.min(stack.getCount(), maxStackSize)); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canCraft( | ||
| final Recipe<?> recipe, final int amount, | ||
| final StackedContents.@Nullable Output<Holder<Item>> output | ||
| ) { | ||
| final PlacementInfo placement = recipe.placementInfo(); | ||
| return !placement.isImpossibleToPlace() | ||
| && this.stackedContents.tryPick( | ||
| ((PlacementInfoBridge) placement).bridge$getStackIngredientInfos(), | ||
| amount, this.createStackOutput(output)); | ||
| } | ||
|
|
||
| @Override | ||
| public int getBiggestCraftableStack( | ||
| final Recipe<?> recipe, final int maxCount, | ||
| final StackedContents.@Nullable Output<Holder<Item>> output | ||
| ) { | ||
| return this.stackedContents.tryPickAll( | ||
| ((PlacementInfoBridge) recipe.placementInfo()).bridge$getStackIngredientInfos(), | ||
| maxCount, this.createStackOutput(output)); | ||
| } | ||
|
|
||
| @Override | ||
| public void clear() { | ||
| super.clear(); | ||
| this.stackInterner.clear(); | ||
| this.stackedContents.clear(); | ||
| } | ||
|
|
||
| private StackedContents.@Nullable Output<ItemStack> createStackOutput( | ||
| final StackedContents.@Nullable Output<Holder<Item>> output | ||
| ) { | ||
| if (output == null) { | ||
| return null; | ||
| } | ||
|
|
||
| this.clearCallback.run(); | ||
| return stack -> { | ||
| output.accept(stack.getItemHolder()); | ||
| this.addCallback.accept(stack); | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/mixins/java/org/spongepowered/common/mixin/core/recipebook/ServerPlaceRecipeMixin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /* | ||
| * This file is part of Sponge, licensed under the MIT License (MIT). | ||
| * | ||
| * Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
| * Copyright (c) contributors | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package org.spongepowered.common.mixin.core.recipebook; | ||
|
|
||
| import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
| import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
| import com.llamalad7.mixinextras.sugar.Local; | ||
| import net.minecraft.recipebook.ServerPlaceRecipe; | ||
| import net.minecraft.world.entity.player.Inventory; | ||
| import net.minecraft.world.entity.player.StackedItemContents; | ||
| import net.minecraft.world.inventory.Slot; | ||
| import net.minecraft.world.item.ItemStack; | ||
| import net.minecraft.world.item.crafting.RecipeHolder; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.injection.At; | ||
| import org.spongepowered.asm.mixin.injection.ModifyArg; | ||
| import org.spongepowered.common.bridge.world.item.crafting.PlacementInfoBridge; | ||
| import org.spongepowered.common.item.recipe.crafting.SpongeStackedItemContents; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.Deque; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Makes recipe placing to work with sponge custom ingredients. | ||
| * Fallbacks to the original logic if recipe does not have any custom ingredient. | ||
| */ | ||
| @Mixin(ServerPlaceRecipe.class) | ||
| public abstract class ServerPlaceRecipeMixin { | ||
|
|
||
| private Deque<ItemStack> impl$stackList = new ArrayDeque<>(); | ||
|
|
||
| @WrapOperation( | ||
| method = "placeRecipe(Lnet/minecraft/recipebook/ServerPlaceRecipe$CraftingMenuAccess;IILjava/util/List;Ljava/util/List;Lnet/minecraft/world/entity/player/Inventory;Lnet/minecraft/world/item/crafting/RecipeHolder;ZZ)Lnet/minecraft/world/inventory/RecipeBookMenu$PostPlaceAction;", | ||
| at = @At( | ||
| value = "NEW", | ||
| target = "()Lnet/minecraft/world/entity/player/StackedItemContents;" | ||
| ) | ||
| ) | ||
| private static StackedItemContents impl$useCustomStackedItemContents( | ||
| final Operation<StackedItemContents> original, | ||
| final ServerPlaceRecipe.CraftingMenuAccess<?> menu, | ||
| final int gridWidth, final int gridHeight, | ||
| final List<Slot> inputGridSlots, final List<Slot> slotsToClear, | ||
| final Inventory inventory, final RecipeHolder<?> recipe, | ||
| final boolean useMaxItems, final boolean isCreative, | ||
| final @Local ServerPlaceRecipe<?> placeRecipe | ||
| ) { | ||
| final ServerPlaceRecipeMixin mixed = (ServerPlaceRecipeMixin) (Object) placeRecipe; | ||
| return ((PlacementInfoBridge) recipe.value().placementInfo()).bridge$hasCustomIngredients() | ||
| ? new SpongeStackedItemContents(mixed.impl$stackList::add, mixed.impl$stackList::clear) | ||
| : original.call(); | ||
| } | ||
|
|
||
| @ModifyArg( | ||
| method = "moveItemToGrid", | ||
| at = @At( | ||
| value = "INVOKE", | ||
| target = "Lnet/minecraft/world/entity/player/Inventory;findSlotMatchingCraftingIngredient(Lnet/minecraft/core/Holder;Lnet/minecraft/world/item/ItemStack;)I" | ||
| ) | ||
| ) | ||
| private ItemStack impl$adjustMatchingSlotFinder(final ItemStack craftInputStack) { | ||
| return this.impl$stackList.isEmpty() ? craftInputStack : this.impl$stackList.poll(); | ||
| } | ||
|
MrHell228 marked this conversation as resolved.
|
||
| } | ||
69 changes: 69 additions & 0 deletions
69
...xins/java/org/spongepowered/common/mixin/core/world/item/crafting/PlacementInfoMixin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * This file is part of Sponge, licensed under the MIT License (MIT). | ||
| * | ||
| * Copyright (c) SpongePowered <https://www.spongepowered.org> | ||
| * Copyright (c) contributors | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package org.spongepowered.common.mixin.core.world.item.crafting; | ||
|
|
||
| import net.minecraft.world.entity.player.StackedContents; | ||
| import net.minecraft.world.item.ItemStack; | ||
| import net.minecraft.world.item.crafting.Ingredient; | ||
| import net.minecraft.world.item.crafting.PlacementInfo; | ||
| import org.spongepowered.asm.mixin.Final; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.Shadow; | ||
| import org.spongepowered.asm.mixin.injection.At; | ||
| import org.spongepowered.asm.mixin.injection.Inject; | ||
| import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
| import org.spongepowered.common.bridge.world.item.crafting.PlacementInfoBridge; | ||
| import org.spongepowered.common.item.recipe.ingredient.IngredientUtil; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Mixin(PlacementInfo.class) | ||
| public abstract class PlacementInfoMixin implements PlacementInfoBridge { | ||
|
|
||
| @Shadow @Final private List<Ingredient> ingredients; | ||
|
|
||
| private boolean impl$hasCustomIngredients; | ||
| private List<StackedContents.IngredientInfo<ItemStack>> impl$stackIngredientInfos; | ||
|
|
||
| @Inject(method = "<init>", at = @At("RETURN")) | ||
| private void impl$setSpongeData(final CallbackInfo ci) { | ||
| this.impl$hasCustomIngredients = this.ingredients.stream() | ||
| .anyMatch(IngredientUtil::isCustom); | ||
|
|
||
| this.impl$stackIngredientInfos = this.ingredients.stream() | ||
| .<StackedContents.IngredientInfo<ItemStack>>map(ingredient -> ingredient::test) | ||
| .toList(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean bridge$hasCustomIngredients() { | ||
| return this.impl$hasCustomIngredients; | ||
| } | ||
|
|
||
| @Override | ||
| public List<StackedContents.IngredientInfo<ItemStack>> bridge$getStackIngredientInfos() { | ||
| return this.impl$stackIngredientInfos; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This still needs to account for the non empty
craftInputStack. My initial suggestion was to just use it instead when its non empty, but now that I thought about it more, it actually just pulls more wrong items inside the slot. I think we should just bail when!ItemStack.isSameItemSameComponents(craftInputStack, this.impl$stackList.poll())because theres nothing more we can do.We should hit here when the players inventory is full and they have more stacks in the crafting input than they can fit in their inventory.
I think the following works as intended:
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the condition to fail should be
!craftInputStack.isEmpty() && !ItemStack.isSameItemSameComponents(craftInputStack, input), or we won't be able to move items to empty crafting slots