work around INVOKEDYNAMIC usage in LootEntryTypeRegistry

This commit is contained in:
asie 2019-01-26 19:19:06 +01:00
parent 77928a3d65
commit dc75d45c51
3 changed files with 26 additions and 48 deletions

View file

@ -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<LootEntry.Serializer<?>> 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);
try {
REGISTER_METHOD.invoke(null, serializer);
} catch (Throwable t) {
throw new RuntimeException(t);
}
public static void setRegisterFunction(Consumer<LootEntry.Serializer<?>> registerFunction) {
LootEntryTypeRegistryImpl.registerFunction = registerFunction;
}
private static void loadLootEntries() {
try { Class.forName(LootEntries.class.getCanonicalName()); } catch (ClassNotFoundException e) {}
}
}

View file

@ -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 = "<clinit>", at = @At("RETURN"))
private static void onClinit(CallbackInfo info) {
LootEntryTypeRegistryImpl.setRegisterFunction(MixinLootEntries::register);
}
}

View file

@ -19,7 +19,6 @@
"events.tick.MixinWorld",
"item.MixinAbstractFurnaceBlockEntity",
"itemgroup.MixinItemGroup",
"loot.MixinLootEntries",
"misc.MixinCrashReport",
"networking.MixinServerPlayNetworkHandler",
"networking.MixinSPacketCustomPayload",