From dc75d45c511948abb507666da19303cfd71cc453 Mon Sep 17 00:00:00 2001 From: asie Date: Sat, 26 Jan 2019 19:19:06 +0100 Subject: [PATCH] work around INVOKEDYNAMIC usage in LootEntryTypeRegistry --- .../registry/LootEntryTypeRegistryImpl.java | 36 +++++++++++++----- .../fabric/mixin/loot/MixinLootEntries.java | 37 ------------------- .../net.fabricmc.fabric.mixins.common.json | 1 - 3 files changed, 26 insertions(+), 48 deletions(-) delete mode 100644 src/main/java/net/fabricmc/fabric/mixin/loot/MixinLootEntries.java diff --git a/src/main/java/net/fabricmc/fabric/impl/registry/LootEntryTypeRegistryImpl.java b/src/main/java/net/fabricmc/fabric/impl/registry/LootEntryTypeRegistryImpl.java index 4b28b364b..fa3c0f0ce 100644 --- a/src/main/java/net/fabricmc/fabric/impl/registry/LootEntryTypeRegistryImpl.java +++ b/src/main/java/net/fabricmc/fabric/impl/registry/LootEntryTypeRegistryImpl.java @@ -20,28 +20,44 @@ import net.fabricmc.fabric.api.registry.LootEntryTypeRegistry; import net.minecraft.world.loot.entry.LootEntries; import net.minecraft.world.loot.entry.LootEntry; +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.reflect.Method; import java.util.function.Consumer; public final class LootEntryTypeRegistryImpl implements LootEntryTypeRegistry { private static Consumer> registerFunction; public static final LootEntryTypeRegistryImpl INSTANCE = new LootEntryTypeRegistryImpl(); + private static final Method REGISTER_METHOD; static { - loadLootEntries(); + Method target = null; + for (Method m : LootEntries.class.getDeclaredMethods()) { + if (m.getParameterCount() == 1 && m.getParameterTypes()[0] == LootEntry.Serializer.class) { + if (target != null) { + throw new RuntimeException("More than one register-like method found in LootEntries!"); + } else { + target = m; + } + } + } + + if (target == null) { + throw new RuntimeException("Could not find register-like method in LootEntries!"); + } else { + REGISTER_METHOD = target; + REGISTER_METHOD.setAccessible(true); + } } private LootEntryTypeRegistryImpl() {} @Override public void register(LootEntry.Serializer serializer) { - registerFunction.accept(serializer); - } - - public static void setRegisterFunction(Consumer> registerFunction) { - LootEntryTypeRegistryImpl.registerFunction = registerFunction; - } - - private static void loadLootEntries() { - try { Class.forName(LootEntries.class.getCanonicalName()); } catch (ClassNotFoundException e) {} + try { + REGISTER_METHOD.invoke(null, serializer); + } catch (Throwable t) { + throw new RuntimeException(t); + } } } diff --git a/src/main/java/net/fabricmc/fabric/mixin/loot/MixinLootEntries.java b/src/main/java/net/fabricmc/fabric/mixin/loot/MixinLootEntries.java deleted file mode 100644 index ef16ff074..000000000 --- a/src/main/java/net/fabricmc/fabric/mixin/loot/MixinLootEntries.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2016, 2017, 2018 FabricMC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package net.fabricmc.fabric.mixin.loot; - -import net.fabricmc.fabric.impl.registry.LootEntryTypeRegistryImpl; -import net.minecraft.world.loot.entry.LootEntries; -import net.minecraft.world.loot.entry.LootEntry; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -@Mixin(LootEntries.class) -public class MixinLootEntries { - @Shadow - private static void register(LootEntry.Serializer lootEntry$Serializer_1) {} - - @Inject(method = "", at = @At("RETURN")) - private static void onClinit(CallbackInfo info) { - LootEntryTypeRegistryImpl.setRegisterFunction(MixinLootEntries::register); - } -} diff --git a/src/main/resources/net.fabricmc.fabric.mixins.common.json b/src/main/resources/net.fabricmc.fabric.mixins.common.json index 283f7cc0a..137fd6dfa 100644 --- a/src/main/resources/net.fabricmc.fabric.mixins.common.json +++ b/src/main/resources/net.fabricmc.fabric.mixins.common.json @@ -19,7 +19,6 @@ "events.tick.MixinWorld", "item.MixinAbstractFurnaceBlockEntity", "itemgroup.MixinItemGroup", - "loot.MixinLootEntries", "misc.MixinCrashReport", "networking.MixinServerPlayNetworkHandler", "networking.MixinSPacketCustomPayload",