From 89d4dd1fcaf74c09ee5542aa20f597dee57bf07a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ampflower=20=F0=9F=8C=BA?= Date: Tue, 24 Dec 2024 11:53:16 -0800 Subject: [PATCH] Add 1.20.2 support --- libs.versions.toml | 4 +- src/main/java/tfar/fastbench/MixinHooks.java | 22 ++++++++--- .../quickbench/internal/Reflector.java | 39 +++++++++++++++---- 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/libs.versions.toml b/libs.versions.toml index d99b5c7..c860b44 100644 --- a/libs.versions.toml +++ b/libs.versions.toml @@ -2,8 +2,8 @@ # Minecraft minecraft_version = "1.20.4" -minecraft_required = "~1.20.3-" -minecraft_compatible = "1.20.3,1.20.4,1.20.5,1.20.6" +minecraft_required = "~1.20.2-" +minecraft_compatible = "1.20.2,1.20.3,1.20.4,1.20.5,1.20.6" fabric_loader = "0.15.+" diff --git a/src/main/java/tfar/fastbench/MixinHooks.java b/src/main/java/tfar/fastbench/MixinHooks.java index cc2e651..634aa6e 100644 --- a/src/main/java/tfar/fastbench/MixinHooks.java +++ b/src/main/java/tfar/fastbench/MixinHooks.java @@ -33,6 +33,7 @@ import net.minecraft.world.inventory.CraftingContainer; import net.minecraft.world.inventory.ResultContainer; import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.CraftingRecipe; import net.minecraft.world.item.crafting.Recipe; @@ -51,6 +52,11 @@ public final class MixinHooks { private static final MethodHandle recipe$assemble = Reflector.virtual(Recipe.class, "method_8116", MethodType.methodType(ItemStack.class, Container.class, RegistryAccess.class)); + private static final MethodHandle item$onCraftedBy = Reflector.virtual( + Item.class, MethodType.methodType(void.class, ItemStack.class, Level.class, Player.class), + "method_54465", "method_7843" + ); + public static boolean hascachedrecipe = false; public static Recipe lastRecipe; @@ -64,11 +70,7 @@ public static void slotChangedCraftingGrid(Level level, CraftingContainer inv, R if (recipe == null || !recipe.value().matches(inv, level)) recipe = findRecipe(inv, level); if (recipe != null) { - try { - itemstack = (ItemStack) recipe$assemble.invoke(recipe.value(), inv, level.registryAccess()); - } catch (Throwable t) { - throw new AssertionError(t); - } + itemstack = durian(recipe$assemble, recipe.value(), inv, level.registryAccess()); } result.setItem(0, itemstack); @@ -89,7 +91,7 @@ public static ItemStack handleShiftCraft(Player player, AbstractContainerMenu co ItemStack recipeOutput = resultSlot.getItem().copy(); outputCopy = recipeOutput.copy(); - recipeOutput.getItem().onCraftedBy(recipeOutput, player.level(), player); + durian(item$onCraftedBy, recipeOutput.getItem(), recipeOutput, player.level(), player); if (!player.level().isClientSide && !((ContainerAccessor) container).insert(recipeOutput, outStart, outEnd, true)) { duck.setCheckMatrixChanges(true); @@ -128,4 +130,12 @@ public static RecipeHolder findRecipe(CraftingContainer inv, Lev public static > RecipeHolder coerce(RecipeHolder in) { return (RecipeHolder) in; } + + public static T durian(MethodHandle handle, Object... objects) { + try { + return (T) handle.invokeWithArguments(objects); + } catch (Throwable t) { + throw new AssertionError(t); + } + } } diff --git a/src/main/java/tfar/fastbench/quickbench/internal/Reflector.java b/src/main/java/tfar/fastbench/quickbench/internal/Reflector.java index 6e96d3a..8471692 100644 --- a/src/main/java/tfar/fastbench/quickbench/internal/Reflector.java +++ b/src/main/java/tfar/fastbench/quickbench/internal/Reflector.java @@ -25,6 +25,7 @@ import java.lang.invoke.MethodType; import java.lang.reflect.Method; import java.lang.reflect.Modifier; +import java.util.Arrays; /** * @author Ampflower @@ -85,20 +86,42 @@ private static boolean signature(Method method, MethodType signature) { return true; } + private static MethodHandle virtual(MethodType signature, Class clazz, String reference) throws IllegalAccessException { + for (final var method : clazz.getMethods()) { + if (Modifier.isStatic(method.getModifiers())) { + continue; + } + if (!virtual(method, reference, signature)) { + continue; + } + return lookup.unreflect(method); + } + return null; + } + public static MethodHandle virtual(Class clazz, String reference, MethodType signature) { try { - for (final var method : clazz.getMethods()) { - if (Modifier.isStatic(method.getModifiers())) { - continue; - } - if (!virtual(method, reference, signature)) { - continue; - } - return lookup.unreflect(method); + final var method = virtual(signature, clazz, reference); + if (method != null) { + return method; } } catch (IllegalAccessException e) { throw new AssertionError(e); } throw new AssertionError(clazz + " has no such method: " + reference + signature); } + + public static MethodHandle virtual(Class clazz, MethodType signature, String... reference) { + try { + for (final var i : reference) { + final var method = virtual(signature, clazz, i); + if (method != null) { + return method; + } + } + } catch (IllegalAccessException e) { + throw new AssertionError(e); + } + throw new AssertionError(clazz + " has no such method: " + Arrays.toString(reference) + signature); + } }