Skip to content

Commit

Permalink
Fix unholy FAPI hack on older versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed May 5, 2021
1 parent d697aed commit a1d8271
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,23 @@
public final class ComponentsInternals {
public static final Logger LOGGER = LogManager.getLogger("Cardinal Components API");

private static final Field EVENT$TYPE;
private static final Field EVENT$HANDLERS;
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup();
private static final Map<Event<? extends ComponentCallback<?, ?>>, MethodHandle> FACTORY_CACHE = new HashMap<>();
private static final MethodHandle IMPL_HANDLE;

static {
try {
EVENT$HANDLERS = Class.forName("net.fabricmc.fabric.impl.base.event.ArrayBackedEvent").getDeclaredField("handlers");
Class<?> impl = Class.forName("net.fabricmc.fabric.impl.base.event.ArrayBackedEvent");
Field eventType;
try {
eventType = impl.getDeclaredField("type"); // FAPI < 0.34.0
} catch (NoSuchFieldException e) {
eventType = null; // FAPI >= 0.34.0
}
EVENT$TYPE = eventType;
EVENT$HANDLERS = impl.getDeclaredField("handlers");
EVENT$HANDLERS.setAccessible(true);
IMPL_HANDLE = LOOKUP.findStatic(ComponentsInternals.class, "initComponents", MethodType.methodType(void.class, ComponentType.class, Function.class, Object.class, ComponentContainer.class));
} catch (NoSuchFieldException | ClassNotFoundException e) {
Expand All @@ -75,7 +84,11 @@ private static <T extends Component, P, C extends T> void initComponents(Compone
@SuppressWarnings("unchecked")
private static MethodHandle createCallbackFactory(Event<?> event) {
try {
Class<? extends ComponentCallback<?, ?>> eventType = (Class<? extends ComponentCallback<?, ?>>) EVENT$HANDLERS.get(event).getClass().getComponentType();
Class<? extends ComponentCallback<?, ?>> eventType = (Class<? extends ComponentCallback<?, ?>>) (
EVENT$TYPE == null
? EVENT$HANDLERS.get(event).getClass().getComponentType() // FAPI >= 0.34.0
: EVENT$TYPE.get(event) // FAPI < 0.34.0
);
MethodType eventSamType = findSam(eventType);
return LambdaMetafactory.metafactory(
LOOKUP,
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
------------------------------------------------------
Version 2.8.2
------------------------------------------------------
Fixes
- Fixed a crash with mods using the deprecated API on Fabric API versions older than 0.34.0

------------------------------------------------------
Version 2.8.1
------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ loader_version=0.10.6+build.214
fabric_api_version=0.34.0+1.16

#Publishing
mod_version = 2.8.1
mod_version = 2.8.2
curseforge_id = 318449
curseforge_versions = 1.16.2; 1.16.3; 1.16.4; 1.16.5
changelog_url = https://github.com/OnyxStudios/Cardinal-Components-API/blob/master/changelog.md
Expand Down

0 comments on commit a1d8271

Please sign in to comment.