Skip to content

Commit 8ea696e

Browse files
committed
Add Reflector for 1.20.5-1.20.6
1 parent 5b0735c commit 8ea696e

5 files changed

Lines changed: 120 additions & 6 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
org.gradle.jvmargs=-Xmx1G
33

44
# Mod Properties
5-
projectVersion=4.3.1
5+
projectVersion=4.3.2
66
maven_group=gay.ampflower.mod
77

88
curseforgeId=914551

libs.versions.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
# Minecraft
44
minecraft_version = "1.20.4"
5-
minecraft_required = "1.20.3-"
6-
minecraft_compatible = "1.20.3,1.20.4"
5+
minecraft_required = "~1.20.3-"
6+
minecraft_compatible = "1.20.3,1.20.4,1.20.5,1.20.6"
77

88
fabric_loader = "0.15.+"
99

src/main/java/tfar/fastbench/MixinHooks.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022-2023 Ampflower
2+
* Copyright (c) 2022-2024 Ampflower
33
* Copyright (c) 2020-2021 Tfarcenim
44
* Copyright (c) 2018-2021 Brennan Ward
55
*
@@ -26,6 +26,7 @@
2626

2727
package tfar.fastbench;
2828

29+
import net.minecraft.core.RegistryAccess;
2930
import net.minecraft.world.Container;
3031
import net.minecraft.world.entity.player.Player;
3132
import net.minecraft.world.inventory.AbstractContainerMenu;
@@ -40,10 +41,15 @@
4041
import net.minecraft.world.level.Level;
4142
import tfar.fastbench.interfaces.CraftingInventoryDuck;
4243
import tfar.fastbench.mixin.ContainerAccessor;
44+
import tfar.fastbench.quickbench.internal.Reflector;
4345

46+
import java.lang.invoke.MethodHandle;
47+
import java.lang.invoke.MethodType;
4448
import java.util.Collections;
4549

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

4854
public static boolean hascachedrecipe = false;
4955

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

6066
if (recipe != null) {
61-
itemstack = recipe.value().assemble(inv, level.registryAccess());
67+
try {
68+
itemstack = (ItemStack) recipe$assemble.invoke(recipe.value(), inv, level.registryAccess());
69+
} catch (Throwable t) {
70+
throw new AssertionError(t);
71+
}
6272
}
6373

6474
result.setItem(0, itemstack);
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright (c) 2024 Ampflower
3+
*
4+
* This software is subject to the terms of either the MIT or CC0-1.0 Licenses.
5+
* If a copy was not distributed with this file, you can obtain one at
6+
* https://github.com/Modflower/QuickBench/blob/trunk/LICENSE-MIT
7+
* https://github.com/Modflower/QuickBench/blob/trunk/LICENSE-CC0
8+
*
9+
* Sources:
10+
* - https://github.com/Modflower/QuickBench
11+
*
12+
* SPDX-License-Identifier: MIT OR CC0-1.0
13+
*
14+
* Additional details are outlined in LICENSE.md, which you can obtain at
15+
* https://github.com/Modflower/QuickBench/blob/trunk/LICENSE.md
16+
*/
17+
18+
package tfar.fastbench.quickbench.internal;
19+
20+
import net.fabricmc.loader.api.FabricLoader;
21+
import net.fabricmc.loader.api.MappingResolver;
22+
23+
import java.lang.invoke.MethodHandle;
24+
import java.lang.invoke.MethodHandles;
25+
import java.lang.invoke.MethodType;
26+
import java.lang.reflect.Method;
27+
import java.lang.reflect.Modifier;
28+
29+
/**
30+
* @author Ampflower
31+
* @since 4.3.2
32+
**/
33+
public final class Reflector {
34+
private static final String namespace = "intermediary";
35+
36+
private static final MethodHandles.Lookup lookup = MethodHandles.lookup();
37+
private static final MappingResolver resolver = FabricLoader.getInstance().getMappingResolver();
38+
private static final boolean production = namespace.equals(resolver.getCurrentRuntimeNamespace());
39+
40+
private static String unmap(Class<?> clazz) {
41+
if (clazz.isPrimitive()) {
42+
return clazz.descriptorString();
43+
}
44+
return 'L' + unmapClassName(clazz).replace('.', '/') + ';';
45+
}
46+
47+
private static String unmapClassName(Class<?> clazz) {
48+
return resolver.unmapClassName(namespace, clazz.getName());
49+
}
50+
51+
private static boolean virtual(Method method, String reference, MethodType signature) {
52+
if (!signature(method, signature)) {
53+
return false;
54+
}
55+
if (production) {
56+
return reference.equals(method.getName());
57+
}
58+
59+
final var parameters = method.getParameterTypes();
60+
final var unmap = new String[method.getParameterCount()];
61+
for (int i = 0; i < unmap.length; i++) {
62+
unmap[i] = unmap(parameters[i]);
63+
}
64+
65+
return resolver.mapMethodName(namespace,
66+
unmapClassName(method.getDeclaringClass()),
67+
reference,
68+
'(' + String.join("", unmap) + ')' + unmap(method.getReturnType()))
69+
.equals(method.getName());
70+
}
71+
72+
private static boolean signature(Method method, MethodType signature) {
73+
if (signature.parameterCount() != method.getParameterCount()) {
74+
return false;
75+
}
76+
if (!signature.returnType().equals(method.getReturnType())) {
77+
return false;
78+
}
79+
final var parameters = method.getParameterTypes();
80+
for (int i = 0; i < signature.parameterCount(); i++) {
81+
if (!parameters[i].isAssignableFrom(signature.parameterType(i))) {
82+
return false;
83+
}
84+
}
85+
return true;
86+
}
87+
88+
public static MethodHandle virtual(Class<?> clazz, String reference, MethodType signature) {
89+
try {
90+
for (final var method : clazz.getMethods()) {
91+
if (Modifier.isStatic(method.getModifiers())) {
92+
continue;
93+
}
94+
if (!virtual(method, reference, signature)) {
95+
continue;
96+
}
97+
return lookup.unreflect(method);
98+
}
99+
} catch (IllegalAccessException e) {
100+
throw new AssertionError(e);
101+
}
102+
throw new AssertionError(clazz + " has no such method: " + reference + signature);
103+
}
104+
}

src/main/resources/fabric.mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
],
2727
"depends": {
2828
"fabricloader": ">=0.15.11",
29-
"minecraft": ">=${minecraftRequired}"
29+
"minecraft": "${minecraftRequired}"
3030
},
3131
"custom": {
3232
"modmenu": {

0 commit comments

Comments
 (0)