Replace loot context type reflection with a mixin (#3)

* Add item settings

* Restore original build.gradle

* Replace loot context type reflection with a mixin
This commit is contained in:
Juuxel 2019-01-12 02:30:26 +02:00 committed by Shadowfacts
parent 60c785dc2b
commit 126b5b7482
3 changed files with 36 additions and 10 deletions

View File

@ -14,7 +14,7 @@ import net.shadowfacts.simplemultipart.container.*;
import net.shadowfacts.simplemultipart.multipart.Multipart; import net.shadowfacts.simplemultipart.multipart.Multipart;
import net.shadowfacts.simplemultipart.multipart.MultipartState; 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.Consumer;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -35,6 +35,8 @@ public class SimpleMultipart implements ModInitializer {
public static final BlockEntityType<ContainerBlockEntity> containerBlockEntity = createBlockEntityType("container", ContainerBlockEntity::new); public static final BlockEntityType<ContainerBlockEntity> containerBlockEntity = createBlockEntityType("container", ContainerBlockEntity::new);
public static final BlockEntityType<TickableContainerBlockEntity> tickableContainerBlockEntity = createBlockEntityType("tickable_container", TickableContainerBlockEntity::new); public static final BlockEntityType<TickableContainerBlockEntity> tickableContainerBlockEntity = createBlockEntityType("tickable_container", TickableContainerBlockEntity::new);
private static BiFunction<String, Consumer<LootContextType.Builder>, LootContextType> lootContextRegisterFunction;
@Override @Override
public void onInitialize() { public void onInitialize() {
Registry.register(Registry.BLOCK, new Identifier(MODID, "container"), containerBlock); 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)); return Registry.register(Registry.BLOCK_ENTITY, new Identifier(MODID, name), builder.method_11034(null));
} }
@Deprecated
public static void setLootContextRegisterFunction(BiFunction<String, Consumer<LootContextType.Builder>, LootContextType> function) {
lootContextRegisterFunction = function;
}
private static LootContextType createMultipartLootContextType() { private static LootContextType createMultipartLootContextType() {
try { try {
Method register = LootContextTypes.class.getDeclaredMethod("register", String.class, Consumer.class); // Load the function
register.setAccessible(true); Class.forName(LootContextTypes.class.getCanonicalName());
Consumer<LootContextType.Builder> initializer = builder -> { } catch (ClassNotFoundException e) {}
builder.require(MULTIPART_STATE_PARAMETER).require(Parameters.POSITION); return lootContextRegisterFunction.apply(
}; MODID + ":multipart",
return (LootContextType)register.invoke(null, MODID + ":multipart", initializer); builder -> builder.require(MULTIPART_STATE_PARAMETER).require(Parameters.POSITION)
} catch (ReflectiveOperationException e) { );
throw new RuntimeException(e);
}
} }
} }

View File

@ -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<LootContextType.Builder> consumer_1) {
return null;
}
}

View File

@ -3,6 +3,7 @@
"package": "net.shadowfacts.simplemultipart.mixin", "package": "net.shadowfacts.simplemultipart.mixin",
"compatibilityLevel": "JAVA_8", "compatibilityLevel": "JAVA_8",
"mixins": [ "mixins": [
"MixinLootContextTypes"
], ],
"injectors": { "injectors": {
"defaultRequire": 1 "defaultRequire": 1