From a1d82712a9a6668a4fbf36e05ce450c88785963a Mon Sep 17 00:00:00 2001 From: Pyrofab Date: Wed, 5 May 2021 16:35:01 +0200 Subject: [PATCH] Fix unholy FAPI hack on older versions --- .../cca/internal/base/ComponentsInternals.java | 17 +++++++++++++++-- changelog.md | 6 ++++++ gradle.properties | 2 +- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/cardinal-components-base/src/main/java/dev/onyxstudios/cca/internal/base/ComponentsInternals.java b/cardinal-components-base/src/main/java/dev/onyxstudios/cca/internal/base/ComponentsInternals.java index 89d68900..b9b60c27 100644 --- a/cardinal-components-base/src/main/java/dev/onyxstudios/cca/internal/base/ComponentsInternals.java +++ b/cardinal-components-base/src/main/java/dev/onyxstudios/cca/internal/base/ComponentsInternals.java @@ -42,6 +42,7 @@ 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>, MethodHandle> FACTORY_CACHE = new HashMap<>(); @@ -49,7 +50,15 @@ public final class ComponentsInternals { 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) { @@ -75,7 +84,11 @@ private static void initComponents(Compone @SuppressWarnings("unchecked") private static MethodHandle createCallbackFactory(Event event) { try { - Class> eventType = (Class>) EVENT$HANDLERS.get(event).getClass().getComponentType(); + Class> eventType = (Class>) ( + 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, diff --git a/changelog.md b/changelog.md index 65d89144..220f56da 100644 --- a/changelog.md +++ b/changelog.md @@ -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 ------------------------------------------------------ diff --git a/gradle.properties b/gradle.properties index 01ba4c29..c03da136 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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