Skip to content

Commit

Permalink
Add 1.20.2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ampflower committed Dec 24, 2024
1 parent e8d05b5 commit 89d4dd1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
4 changes: 2 additions & 2 deletions libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.+"

Expand Down
22 changes: 16 additions & 6 deletions src/main/java/tfar/fastbench/MixinHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<CraftingContainer> lastRecipe;
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -128,4 +130,12 @@ public static RecipeHolder<CraftingRecipe> findRecipe(CraftingContainer inv, Lev
public static <C extends Container, T extends Recipe<C>> RecipeHolder<T> coerce(RecipeHolder<?> in) {
return (RecipeHolder<T>) in;
}

public static <T> T durian(MethodHandle handle, Object... objects) {
try {
return (T) handle.invokeWithArguments(objects);
} catch (Throwable t) {
throw new AssertionError(t);
}
}
}
39 changes: 31 additions & 8 deletions src/main/java/tfar/fastbench/quickbench/internal/Reflector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 89d4dd1

Please sign in to comment.