Skip to content

Commit

Permalink
fix per-recipe remainders
Browse files Browse the repository at this point in the history
  • Loading branch information
BasiqueEvangelist committed Sep 29, 2023
1 parent 5b120e1 commit c2b7bb5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ archives_base_name=owo-lib
fabric_version=0.88.3+1.20.2

# https://maven.shedaniel.me/me/shedaniel/RoughlyEnoughItems-fabric/
rei_version=13.0.654
rei_version=13.0.666

# https://maven.terraformersmc.com/releases/dev/emi/emi-fabric/
emi_version=1.0.19+1.20.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,36 @@
import com.google.gson.JsonPrimitive;
import com.mojang.serialization.JsonOps;
import io.wispforest.owo.util.RecipeRemainderStorage;
import io.wispforest.owo.util.pond.MixinHooks;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeCodecs;
import net.minecraft.recipe.RecipeManager;
import net.minecraft.recipe.ShapedRecipe;
import net.minecraft.recipe.*;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.Util;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import java.util.HashMap;
import java.util.Optional;

@Mixin(RecipeManager.class)
public abstract class RecipeManagerMixin {
@Inject(method = "getRemainingStacks", at = @At(value = "INVOKE", target = "Lnet/minecraft/recipe/Recipe;getRemainder(Lnet/minecraft/inventory/Inventory;)Lnet/minecraft/util/collection/DefaultedList;"), locals = LocalCapture.CAPTURE_FAILHARD)
private void setRecipe(RecipeType<?> type, Inventory inventory, World world, CallbackInfoReturnable<DefaultedList<ItemStack>> cir, Optional<RecipeEntry<?>> optional) {
MixinHooks.RECIPE_ID.set(optional.map(RecipeEntry::id).orElse(null));
}

@Inject(method = "getRemainingStacks", at = @At("RETURN"))
private void unsetRecipe(RecipeType<?> type, Inventory inventory, World world, CallbackInfoReturnable<DefaultedList<ItemStack>> cir) {
MixinHooks.RECIPE_ID.remove();
}

@Inject(method = "deserialize", at = @At(value = "RETURN"))
private static void deserializeRecipeSpecificRemainders(Identifier id, JsonObject json, CallbackInfoReturnable<Recipe<?>> cir) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
package io.wispforest.owo.mixin.recipe_remainders;

import io.wispforest.owo.util.RecipeRemainderStorage;
import io.wispforest.owo.util.pond.MixinHooks;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Recipe;
import net.minecraft.util.Identifier;
import net.minecraft.util.collection.DefaultedList;
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.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

@Mixin(Recipe.class)
public interface RecipeMixin<C extends Inventory> {
@Inject(method = "getRemainder", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILHARD)
private void addRecipeSpecificRemainders(C inventory, CallbackInfoReturnable<DefaultedList<ItemStack>> cir, DefaultedList<ItemStack> remainders) {
Identifier id = MixinHooks.RECIPE_ID.get();
if (id == null) return;

// TODO: make work
if (!RecipeRemainderStorage.has(id)) return;

// @Shadow
// Identifier getId();
//
// @Inject(method = "getRemainder", at = @At("RETURN"), locals = LocalCapture.CAPTURE_FAILHARD)
// private void addRecipeSpecificRemainders(C inventory, CallbackInfoReturnable<DefaultedList<ItemStack>> cir, DefaultedList<ItemStack> remainders) {
// if (!RecipeRemainderStorage.has(this.getId())) return;
//
// var owoRemainders = RecipeRemainderStorage.get(this.getId());
// for (int i = 0; i < remainders.size(); ++i) {
// var item = inventory.getStack(i).getItem();
// if (!owoRemainders.containsKey(item)) continue;
//
// remainders.set(i, owoRemainders.get(item).copy());
// }
// }
var owoRemainders = RecipeRemainderStorage.get(id);
for (int i = 0; i < remainders.size(); ++i) {
var item = inventory.getStack(i).getItem();
if (!owoRemainders.containsKey(item)) continue;

remainders.set(i, owoRemainders.get(item).copy());
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/io/wispforest/owo/util/pond/MixinHooks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.wispforest.owo.util.pond;

import net.minecraft.util.Identifier;

public class MixinHooks {
public static final ThreadLocal<Identifier> RECIPE_ID = new ThreadLocal<>();
}

0 comments on commit c2b7bb5

Please sign in to comment.