Skip to content

Commit

Permalink
fix: potentially fix recipe api implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Harris-Rouquette <[email protected]>
  • Loading branch information
gabizou committed Oct 10, 2024
1 parent 411f9f5 commit 805eb43
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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 net.minecraft.util.context.ContextMap;
import net.minecraft.world.item.crafting.display.DisplayContentsFactory;
import net.minecraft.world.item.crafting.display.SlotDisplayContext;
import org.spongepowered.api.Sponge;
import org.spongepowered.common.SpongeCommon;

import java.util.List;

public final class RecipeUtil {

public static ContextMap serverBasedContextMap() {
if (!Sponge.isServerAvailable()) {
return new ContextMap.Builder().create(SlotDisplayContext.CONTEXT);
}
final var server = SpongeCommon.server();
return new ContextMap.Builder()
.withParameter(SlotDisplayContext.FUEL_VALUES, server.fuelValues())
.withParameter(SlotDisplayContext.REGISTRIES, server.registryAccess())
.create(SlotDisplayContext.CONTEXT);
}

public class RemainderResolver<T> implements DisplayContentsFactory.ForRemainders<T> {
@Override
public T addRemainder(T var1, List<T> var2) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
*/
package org.spongepowered.common.mixin.api.minecraft.world.item.crafting;

import static org.spongepowered.common.inventory.util.InventoryUtil.toCraftingInputOrThrow;

import net.minecraft.core.HolderLookup;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeInput;
import net.minecraft.world.item.crafting.display.RecipeDisplay;
import net.minecraft.world.item.crafting.display.SlotDisplay;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.item.recipe.Recipe;
Expand All @@ -37,9 +37,12 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.inventory.util.InventoryUtil;
import org.spongepowered.common.item.recipe.crafting.RecipeUtil;
import org.spongepowered.common.item.util.ItemStackUtil;

import java.util.List;
import java.util.function.Predicate;

@Mixin(net.minecraft.world.item.crafting.Recipe.class)
public interface RecipeMixin_API<I extends RecipeInput, I2 extends org.spongepowered.api.item.recipe.crafting.RecipeInput> extends Recipe<I2> {
Expand All @@ -49,30 +52,44 @@ public interface RecipeMixin_API<I extends RecipeInput, I2 extends org.spongepow
@Shadow boolean shadow$isSpecial();
@Shadow boolean shadow$matches(I inv, net.minecraft.world.level.Level worldIn);
@Shadow net.minecraft.world.item.crafting.RecipeType<?> shadow$getType();
// @formatter:on
@Shadow List<RecipeDisplay> shadow$display();
// @formatter:on

@NonNull
@Override
default ItemStackSnapshot exemplaryResult() {
// TODO - Do we want to start exposing slot displays in some way?
return ItemStackSnapshot.empty();
return this.shadow$display().stream()
.map(RecipeDisplay::result)
.map(sd -> sd.resolveForFirstStack(RecipeUtil.serverBasedContextMap()))
.filter(Predicate.not(ItemStack::isEmpty))
.map(ItemStackUtil::snapshotOf)
.findFirst()
.orElseGet(ItemStackSnapshot::empty);
}

@Override
default boolean isValid(@NonNull final I2 inv, @NonNull final ServerWorld world) {
return this.shadow$matches((I) toCraftingInputOrThrow(inv), (net.minecraft.world.level.Level) world);
return this.shadow$matches((I) InventoryUtil.toCraftingInputOrThrow(inv), (net.minecraft.world.level.Level) world);
}

@NonNull
@Override
default ItemStackSnapshot result(@NonNull final I2 inv) {
return ItemStackUtil.snapshotOf(this.shadow$assemble((I) toCraftingInputOrThrow(inv), SpongeCommon.server().registryAccess()));
return ItemStackUtil.snapshotOf(this.shadow$assemble((I) InventoryUtil.toCraftingInputOrThrow(inv), SpongeCommon.server().registryAccess()));
}

@NonNull
@Override
default List<ItemStackSnapshot> remainingItems(@NonNull final I2 inv) {
return List.of();
return this.shadow$display().stream()
.map(RecipeDisplay::result)
.filter(sd -> sd instanceof SlotDisplay.WithRemainder)
.map(SlotDisplay.WithRemainder.class::cast)
.map(SlotDisplay.WithRemainder::remainder)
.map(sd -> sd.resolveForFirstStack(RecipeUtil.serverBasedContextMap()))
.filter(Predicate.not(ItemStack::isEmpty))
.map(ItemStackUtil::snapshotOf)
.toList();
}

@Override
Expand Down

0 comments on commit 805eb43

Please sign in to comment.