Skip to content

Commit

Permalink
Add Reflector for 1.20.5-1.20.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Ampflower committed Sep 28, 2024
1 parent 5b0735c commit 8ea696e
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
org.gradle.jvmargs=-Xmx1G

# Mod Properties
projectVersion=4.3.1
projectVersion=4.3.2
maven_group=gay.ampflower.mod

curseforgeId=914551
Expand Down
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"
minecraft_required = "~1.20.3-"
minecraft_compatible = "1.20.3,1.20.4,1.20.5,1.20.6"

fabric_loader = "0.15.+"

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/tfar/fastbench/MixinHooks.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Ampflower
* Copyright (c) 2022-2024 Ampflower
* Copyright (c) 2020-2021 Tfarcenim
* Copyright (c) 2018-2021 Brennan Ward
*
Expand All @@ -26,6 +26,7 @@

package tfar.fastbench;

import net.minecraft.core.RegistryAccess;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
Expand All @@ -40,10 +41,15 @@
import net.minecraft.world.level.Level;
import tfar.fastbench.interfaces.CraftingInventoryDuck;
import tfar.fastbench.mixin.ContainerAccessor;
import tfar.fastbench.quickbench.internal.Reflector;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodType;
import java.util.Collections;

public class MixinHooks {
private static final MethodHandle recipe$assemble = Reflector.virtual(Recipe.class, "method_8116",
MethodType.methodType(ItemStack.class, Container.class, RegistryAccess.class));

public static boolean hascachedrecipe = false;

Expand All @@ -58,7 +64,11 @@ public static void slotChangedCraftingGrid(Level level, CraftingContainer inv, R
if (recipe == null || !recipe.value().matches(inv, level)) recipe = findRecipe(inv, level);

if (recipe != null) {
itemstack = recipe.value().assemble(inv, level.registryAccess());
try {
itemstack = (ItemStack) recipe$assemble.invoke(recipe.value(), inv, level.registryAccess());
} catch (Throwable t) {
throw new AssertionError(t);
}
}

result.setItem(0, itemstack);
Expand Down
104 changes: 104 additions & 0 deletions src/main/java/tfar/fastbench/quickbench/internal/Reflector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2024 Ampflower
*
* This software is subject to the terms of either the MIT or CC0-1.0 Licenses.
* If a copy was not distributed with this file, you can obtain one at
* https://github.com/Modflower/QuickBench/blob/trunk/LICENSE-MIT
* https://github.com/Modflower/QuickBench/blob/trunk/LICENSE-CC0
*
* Sources:
* - https://github.com/Modflower/QuickBench
*
* SPDX-License-Identifier: MIT OR CC0-1.0
*
* Additional details are outlined in LICENSE.md, which you can obtain at
* https://github.com/Modflower/QuickBench/blob/trunk/LICENSE.md
*/

package tfar.fastbench.quickbench.internal;

import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.MappingResolver;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
* @author Ampflower
* @since 4.3.2
**/
public final class Reflector {
private static final String namespace = "intermediary";

private static final MethodHandles.Lookup lookup = MethodHandles.lookup();
private static final MappingResolver resolver = FabricLoader.getInstance().getMappingResolver();
private static final boolean production = namespace.equals(resolver.getCurrentRuntimeNamespace());

private static String unmap(Class<?> clazz) {
if (clazz.isPrimitive()) {
return clazz.descriptorString();
}
return 'L' + unmapClassName(clazz).replace('.', '/') + ';';
}

private static String unmapClassName(Class<?> clazz) {
return resolver.unmapClassName(namespace, clazz.getName());
}

private static boolean virtual(Method method, String reference, MethodType signature) {
if (!signature(method, signature)) {
return false;
}
if (production) {
return reference.equals(method.getName());
}

final var parameters = method.getParameterTypes();
final var unmap = new String[method.getParameterCount()];
for (int i = 0; i < unmap.length; i++) {
unmap[i] = unmap(parameters[i]);
}

return resolver.mapMethodName(namespace,
unmapClassName(method.getDeclaringClass()),
reference,
'(' + String.join("", unmap) + ')' + unmap(method.getReturnType()))
.equals(method.getName());
}

private static boolean signature(Method method, MethodType signature) {
if (signature.parameterCount() != method.getParameterCount()) {
return false;
}
if (!signature.returnType().equals(method.getReturnType())) {
return false;
}
final var parameters = method.getParameterTypes();
for (int i = 0; i < signature.parameterCount(); i++) {
if (!parameters[i].isAssignableFrom(signature.parameterType(i))) {
return false;
}
}
return true;
}

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);
}
} catch (IllegalAccessException e) {
throw new AssertionError(e);
}
throw new AssertionError(clazz + " has no such method: " + reference + signature);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
],
"depends": {
"fabricloader": ">=0.15.11",
"minecraft": ">=${minecraftRequired}"
"minecraft": "${minecraftRequired}"
},
"custom": {
"modmenu": {
Expand Down

0 comments on commit 8ea696e

Please sign in to comment.