Skip to content
Open
Show file tree
Hide file tree
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 Oct 7, 2025
0f14cb9
remove adjusted ServerPlaceRecipe#moveItemToGrid copy
MrHell228 Oct 25, 2025
25562d6
move RecipeBridge logic to PlacementInfoBridge
MrHell228 Oct 25, 2025
7bede09
reduce the amount of injections
MrHell228 Oct 26, 2025
ab60d1c
move some things
MrHell228 Oct 26, 2025
f08fc22
follow the review
MrHell228 Oct 26, 2025
1d935e2
fail finding slot when exemplary and craft input stacks can't stack
MrHell228 Oct 26, 2025
17ae6d8
add RecipePlaceTest
MrHell228 Oct 30, 2025
0733598
follow the review
MrHell228 Oct 30, 2025
1a94490
make tests more flexible
MrHell228 Oct 31, 2025
3b01885
Revert "make tests more flexible"
MrHell228 Oct 31, 2025
6fb1659
test option for creative mode, tests for full inventory
MrHell228 Oct 31, 2025
a0bb7ae
move bad layout tests to its own populator
MrHell228 Nov 2, 2025
46cd7a3
Update RecipePlaceTest.java
MrHell228 Nov 2, 2025
e8556bf
Merge branch 'api-14' into api-14-fix/recipe-book-placer
MrHell228 Nov 27, 2025
9130a71
Update SpongeAPI
MrHell228 Nov 27, 2025
6c684fb
Update SpongeAPI
MrHell228 Nov 27, 2025
4d6d606
Merge branch 'api-14-fix/recipe-book-placer' of https://github.com/Mr…
MrHell228 Nov 27, 2025
5f40fb9
Update SpongeAPI
MrHell228 Nov 27, 2025
0cf4640
enable custom shapelss recipe tests
MrHell228 Nov 27, 2025
bf8b5f7
enable custom shapelss recipe tests
MrHell228 Nov 27, 2025
eeb5dee
Merge branch 'api-14-fix/recipe-book-placer' of https://github.com/Mr…
MrHell228 Nov 27, 2025
2b0f447
Update SpongeAPI
MrHell228 Nov 27, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
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);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import net.minecraft.world.item.crafting.ShapelessRecipe;
import net.minecraft.world.level.Level;
import org.spongepowered.common.accessor.world.item.crafting.ShapelessRecipeAccessor;
import org.spongepowered.common.item.recipe.ingredient.SpongeIngredient;
import org.spongepowered.common.bridge.world.item.crafting.PlacementInfoBridge;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -50,8 +50,6 @@
*/
public class SpongeShapelessRecipe extends ShapelessRecipe {

private final boolean onlyVanillaIngredients;

private final Function<CraftingInput, ItemStack> resultFunction;
private final Function<CraftingInput, NonNullList<net.minecraft.world.item.ItemStack>> remainingItemsFunction;

Expand All @@ -62,14 +60,13 @@ public SpongeShapelessRecipe(final String groupIn,
final Function<CraftingInput, net.minecraft.world.item.ItemStack> resultFunction,
final Function<CraftingInput, NonNullList<ItemStack>> remainingItemsFunction) {
super(groupIn, category, spongeResultStack, recipeItemsIn);
this.onlyVanillaIngredients = recipeItemsIn.stream().noneMatch(i -> i instanceof SpongeIngredient);
this.resultFunction = resultFunction;
this.remainingItemsFunction = remainingItemsFunction;
}

@Override
public boolean matches(final CraftingInput $$0, final Level $$1) {
if (this.onlyVanillaIngredients) {
if (!((PlacementInfoBridge) this.placementInfo()).bridge$hasCustomIngredients()) {
return super.matches($$0, $$1);
}
return SpongeShapelessRecipe.matches($$0.items(), ((ShapelessRecipeAccessor) this).accessor$ingredients());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public static org.spongepowered.api.item.recipe.crafting.Ingredient of(ResourceK
return IngredientUtil.fromNative(ingredient);
}



public static boolean isCustom(final @Nullable Ingredient ingredient) {
return ingredient instanceof SpongeIngredient;
}
}
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();
Copy link
Copy Markdown
Member

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:

if (this.impl$stackList.isEmpty()) {
    return original.call(...);
} else {
    final ItemStack input = his.impl$stackList.poll();
    if (!ItemStack.isSameItemSameComponents(craftInputStack, input)) {
        return -1:
    }
    return original.call(..., input);
}

Copy link
Copy Markdown
Contributor Author

@MrHell228 MrHell228 Oct 26, 2025

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

}
Comment thread
MrHell228 marked this conversation as resolved.
}
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;
}
}
2 changes: 2 additions & 0 deletions src/mixins/resources/mixins.sponge.core.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"network.syncher.EntityDataAccessorMixin",
"network.syncher.SynchedEntityData_BuilderMixin",
"network.syncher.SynchedEntityDataMixin",
"recipebook.ServerPlaceRecipeMixin",
"resources.RegistryDataLoader_LoaderMixin",
"resources.RegistryDataLoaderMixin",
"server.MinecraftServerMixin",
Expand Down Expand Up @@ -211,6 +212,7 @@
"world.item.TeleportRandomlyConsumeEffectMixin",
"world.item.crafting.AbstractCookingRecipeMixin",
"world.item.crafting.IngredientMixin",
"world.item.crafting.PlacementInfoMixin",
"world.item.crafting.RecipeManagerMixin",
"world.item.crafting.ShapedRecipeMixin",
"world.item.crafting.ShapelessRecipeMixin",
Expand Down