From 7ac2c5fa689dbdd2b67babb9e0428473858672bb Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Fri, 2 May 2025 14:29:54 -0400 Subject: [PATCH] Fix several mistakes in porting mixin.perf.faster_ingredients --- .../faster_ingredients/IngredientMixin.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/neoforge/src/main/java/org/embeddedt/modernfix/neoforge/mixin/perf/faster_ingredients/IngredientMixin.java b/neoforge/src/main/java/org/embeddedt/modernfix/neoforge/mixin/perf/faster_ingredients/IngredientMixin.java index 7c3eb208..f372b031 100644 --- a/neoforge/src/main/java/org/embeddedt/modernfix/neoforge/mixin/perf/faster_ingredients/IngredientMixin.java +++ b/neoforge/src/main/java/org/embeddedt/modernfix/neoforge/mixin/perf/faster_ingredients/IngredientMixin.java @@ -5,7 +5,9 @@ import it.unimi.dsi.fastutil.ints.IntComparators; import it.unimi.dsi.fastutil.ints.IntList; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.ItemStackLinkedSet; import net.minecraft.world.item.crafting.Ingredient; +import net.neoforged.neoforge.common.crafting.ICustomIngredient; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; @@ -17,6 +19,7 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import java.util.ArrayList; +import java.util.stream.Collectors; @Mixin(value = Ingredient.class, priority = 700) public abstract class IngredientMixin { @@ -29,6 +32,8 @@ public abstract class IngredientMixin { @Shadow public abstract boolean isCustom(); + @Shadow private ICustomIngredient customIngredient; + @Unique private boolean isVanilla() { return !this.isCustom(); @@ -40,7 +45,7 @@ public abstract class IngredientMixin { */ @Inject(method = "test(Lnet/minecraft/world/item/ItemStack;)Z", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/crafting/Ingredient;getItems()[Lnet/minecraft/world/item/ItemStack;"), cancellable = true) private void modernfix$fasterTagIngredientTest(ItemStack stack, CallbackInfoReturnable cir) { - if (this.isCustom() && this.values.length == 1 && this.values[0] instanceof Ingredient.TagValue tagValue) { + if (this.isVanilla() && this.values.length == 1 && this.values[0] instanceof Ingredient.TagValue tagValue) { cir.setReturnValue(stack.getItemHolder().is(tagValue.tag())); } } @@ -49,9 +54,11 @@ public abstract class IngredientMixin { * @author embeddedt * @reason exploding the stack list is unnecessary */ - @Overwrite(remap = false) - public boolean hasNoItems() { - return !this.containsItems(); + @Inject(method = "hasNoItems", at = @At("HEAD"), cancellable = true, remap = false) + public void hasNoItems(CallbackInfoReturnable cir) { + if (this.isVanilla()) { + cir.setReturnValue(!this.containsItems()); + } } @Unique @@ -112,6 +119,11 @@ public abstract class IngredientMixin { if (this.itemStacks != null) { return this.itemStacks; } + if (this.customIngredient != null) { + // We probably have to cache this as mods won't make it fast if they expect Neo to cache it + this.itemStacks = this.customIngredient.getItems().collect(Collectors.toCollection(ItemStackLinkedSet::createTypeAndComponentsSet)).toArray(ItemStack[]::new); + return this.itemStacks; + } // Fast path for case with one item if (this.values.length == 1) { if (this.values[0] instanceof Ingredient.ItemValue itemValue) {