-
-
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 4 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(); | ||
| } |
55 changes: 55 additions & 0 deletions
55
src/main/java/org/spongepowered/common/item/recipe/book/RecipeBookUtil.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,55 @@ | ||
| /* | ||
| * 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.book; | ||
|
|
||
| import net.minecraft.core.Holder; | ||
| import net.minecraft.world.entity.player.Inventory; | ||
| import net.minecraft.world.item.ItemStack; | ||
|
|
||
| public final class RecipeBookUtil { | ||
|
|
||
| /** | ||
| * Copied from {@link Inventory#findSlotMatchingCraftingIngredient(Holder, ItemStack)} | ||
| * and adjusted to use exemplary {@link ItemStack} instead of just item type. | ||
| */ | ||
| public static int findSlotMatchingCraftingIngredient( | ||
| final Inventory inventory, final ItemStack exemplaryStackToMove, final ItemStack craftInputStack | ||
| ) { | ||
| for (int i = 0; i < inventory.items.size(); i++) { | ||
| final ItemStack stack = inventory.items.get(i); | ||
| if (!stack.isEmpty() | ||
| && Inventory.isUsableForCrafting(stack) | ||
| && ItemStack.isSameItemSameComponents(exemplaryStackToMove, stack) | ||
| && (craftInputStack.isEmpty() || ItemStack.isSameItemSameComponents(craftInputStack, stack))) { | ||
| return i; | ||
| } | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| private RecipeBookUtil() { | ||
| } | ||
| } |
129 changes: 129 additions & 0 deletions
129
src/main/java/org/spongepowered/common/item/recipe/book/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,129 @@ | ||
| /* | ||
| * 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.book; | ||
|
|
||
| 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.List; | ||
| import java.util.function.Function; | ||
|
|
||
| public 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<>(STACK_HASH_STRATEGY); | ||
| private final StackedContents<ItemStack> stackedContents = new StackedContents<>(); | ||
|
|
||
| private StackedContents.@Nullable Output<ItemStack> stackOutput; | ||
|
|
||
| @Override | ||
| public void accountStack(final ItemStack stack, final int 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 boolean canCraft( | ||
| final List<? extends StackedContents.IngredientInfo<Holder<Item>>> ingredients, | ||
| final StackedContents.@Nullable Output<Holder<Item>> output | ||
| ) { | ||
| // By default, this method is not called in the context the instance of this class is created. | ||
| // If this happens, it's either error in Sponge impl or | ||
| // mixin from some mod (which should be inspected instead of silently doing something that impl does not expect). | ||
| throw new UnsupportedOperationException("This method should not have been called, please report about it"); | ||
| } | ||
|
|
||
| @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() { | ||
| this.stackInterner.clear(); | ||
| this.stackedContents.clear(); | ||
| } | ||
|
|
||
| public void setStackOutput(final StackedContents.@Nullable Output<ItemStack> stackOutput) { | ||
| this.stackOutput = stackOutput; | ||
| } | ||
|
|
||
| private StackedContents.@Nullable Output<ItemStack> createStackOutput( | ||
| final StackedContents.@Nullable Output<Holder<Item>> output | ||
| ) { | ||
| if (output == null) { | ||
| return null; | ||
| } else if (this.stackOutput == null) { | ||
| return stack -> output.accept(stack.getItemHolder()); | ||
| } else { | ||
| return stack -> { | ||
| output.accept(stack.getItemHolder()); | ||
| this.stackOutput.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
126 changes: 126 additions & 0 deletions
126
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,126 @@ | ||
| /* | ||
| * 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.wrapmethod.WrapMethod; | ||
| import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
| import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
| import net.minecraft.core.Holder; | ||
| 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.Item; | ||
| import net.minecraft.world.item.ItemStack; | ||
| import net.minecraft.world.item.crafting.RecipeHolder; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
| 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.book.RecipeBookUtil; | ||
| import org.spongepowered.common.item.recipe.book.SpongeStackedItemContents; | ||
|
|
||
| import java.util.ArrayList; | ||
| 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 { | ||
|
|
||
| @Shadow @Final private Inventory inventory; | ||
|
|
||
| private @Nullable List<ItemStack> impl$stackList; | ||
|
|
||
| @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 | ||
| ) { | ||
| return ((PlacementInfoBridge) recipe.value().placementInfo()).bridge$hasCustomIngredients() | ||
| ? new SpongeStackedItemContents() | ||
| : original.call(); | ||
| } | ||
|
|
||
| @WrapMethod(method = "placeRecipe(Lnet/minecraft/world/item/crafting/RecipeHolder;Lnet/minecraft/world/entity/player/StackedItemContents;)V") | ||
| private void impl$handleApplicableStacks( | ||
| final RecipeHolder<?> recipe, final StackedItemContents contents, final Operation<Void> original | ||
| ) { | ||
| if (contents instanceof final SpongeStackedItemContents spongeContents) { | ||
| this.impl$stackList = new ArrayList<>(); | ||
|
MrHell228 marked this conversation as resolved.
Outdated
|
||
| // In wrapped method the used output (if not null) is always List<Holder<Item>>::add | ||
| spongeContents.setStackOutput(this.impl$stackList::add); | ||
| original.call(recipe, contents); | ||
| spongeContents.setStackOutput(null); | ||
| this.impl$stackList = null; | ||
| } else { | ||
| original.call(recipe, contents); | ||
| } | ||
| } | ||
|
|
||
| @Inject( | ||
| method = "placeRecipe(Lnet/minecraft/world/item/crafting/RecipeHolder;Lnet/minecraft/world/entity/player/StackedItemContents;)V", | ||
| at = @At( | ||
| value = "INVOKE", | ||
| target = "Ljava/util/List;clear()V" | ||
|
MrHell228 marked this conversation as resolved.
Outdated
|
||
| ) | ||
| ) | ||
| private void impl$clearStackList(final CallbackInfo ci) { | ||
| if (this.impl$stackList != null) { | ||
| this.impl$stackList.clear(); | ||
| } | ||
| } | ||
|
|
||
| @WrapOperation( | ||
| 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 int impl$adjustMatchingSlotFinder( | ||
| final Inventory instance, final Holder<Item> exemplaryItem, final ItemStack craftInputStack, final Operation<Integer> original | ||
| ) { | ||
| return this.impl$stackList == null | ||
| ? original.call(instance, exemplaryItem, craftInputStack) | ||
| : RecipeBookUtil.findSlotMatchingCraftingIngredient(inventory, this.impl$stackList.removeFirst(), craftInputStack); | ||
| } | ||
|
MrHell228 marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.