From 5325a6795275b601ba2d98c2f65f14ddf2ae5eff Mon Sep 17 00:00:00 2001 From: Juuxel Date: Fri, 11 Jan 2019 21:08:51 +0200 Subject: [PATCH] Replace loot context type reflection with a mixin --- .../simplemultipart/SimpleMultipart.java | 25 +++++++++++-------- .../mixin/MixinLootContextTypes.java | 20 +++++++++++++++ .../resources/simplemultipart.common.json | 1 + 3 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 src/main/java/net/shadowfacts/simplemultipart/mixin/MixinLootContextTypes.java diff --git a/src/main/java/net/shadowfacts/simplemultipart/SimpleMultipart.java b/src/main/java/net/shadowfacts/simplemultipart/SimpleMultipart.java index 2343cfd..422ab64 100644 --- a/src/main/java/net/shadowfacts/simplemultipart/SimpleMultipart.java +++ b/src/main/java/net/shadowfacts/simplemultipart/SimpleMultipart.java @@ -14,7 +14,7 @@ import net.shadowfacts.simplemultipart.container.*; import net.shadowfacts.simplemultipart.multipart.Multipart; import net.shadowfacts.simplemultipart.multipart.MultipartState; -import java.lang.reflect.Method; +import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Supplier; @@ -35,6 +35,8 @@ public class SimpleMultipart implements ModInitializer { public static final BlockEntityType containerBlockEntity = createBlockEntityType("container", ContainerBlockEntity::new); public static final BlockEntityType tickableContainerBlockEntity = createBlockEntityType("tickable_container", TickableContainerBlockEntity::new); + private static BiFunction, LootContextType> lootContextRegisterFunction; + @Override public void onInitialize() { Registry.register(Registry.BLOCK, new Identifier(MODID, "container"), containerBlock); @@ -54,17 +56,20 @@ public class SimpleMultipart implements ModInitializer { return Registry.register(Registry.BLOCK_ENTITY, new Identifier(MODID, name), builder.method_11034(null)); } + @Deprecated + public static void setLootContextRegisterFunction(BiFunction, LootContextType> function) { + lootContextRegisterFunction = function; + } + private static LootContextType createMultipartLootContextType() { try { - Method register = LootContextTypes.class.getDeclaredMethod("register", String.class, Consumer.class); - register.setAccessible(true); - Consumer initializer = builder -> { - builder.require(MULTIPART_STATE_PARAMETER).require(Parameters.POSITION); - }; - return (LootContextType)register.invoke(null, MODID + ":multipart", initializer); - } catch (ReflectiveOperationException e) { - throw new RuntimeException(e); - } + // Load the function + Class.forName(LootContextTypes.class.getCanonicalName()); + } catch (ClassNotFoundException e) {} + return lootContextRegisterFunction.apply( + MODID + ":multipart", + builder -> builder.require(MULTIPART_STATE_PARAMETER).require(Parameters.POSITION) + ); } } diff --git a/src/main/java/net/shadowfacts/simplemultipart/mixin/MixinLootContextTypes.java b/src/main/java/net/shadowfacts/simplemultipart/mixin/MixinLootContextTypes.java new file mode 100644 index 0000000..6afdae3 --- /dev/null +++ b/src/main/java/net/shadowfacts/simplemultipart/mixin/MixinLootContextTypes.java @@ -0,0 +1,20 @@ +package net.shadowfacts.simplemultipart.mixin; + +import net.minecraft.world.loot.context.LootContextType; +import net.minecraft.world.loot.context.LootContextTypes; +import net.shadowfacts.simplemultipart.SimpleMultipart; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import java.util.function.Consumer; + +@Mixin(LootContextTypes.class) +public class MixinLootContextTypes { + static { + SimpleMultipart.setLootContextRegisterFunction(MixinLootContextTypes::register); + } + + @Shadow private static LootContextType register(String string_1, Consumer consumer_1) { + return null; + } +} diff --git a/src/main/resources/simplemultipart.common.json b/src/main/resources/simplemultipart.common.json index 89c2b25..6511add 100644 --- a/src/main/resources/simplemultipart.common.json +++ b/src/main/resources/simplemultipart.common.json @@ -3,6 +3,7 @@ "package": "net.shadowfacts.simplemultipart.mixin", "compatibilityLevel": "JAVA_8", "mixins": [ + "MixinLootContextTypes" ], "injectors": { "defaultRequire": 1