From fa80027d841d55651d3febdbd2d05039933471c2 Mon Sep 17 00:00:00 2001 From: SD Date: Wed, 3 Mar 2021 20:02:26 +0530 Subject: [PATCH] And that's why fabric api is important --- build.gradle | 14 +++++++ src/main/java/org/dimdev/matrix/Matrix.java | 41 +++++++++++-------- .../matrix/{Register.java => Registrar.java} | 4 +- .../org/dimdev/matrix/test/ModBlocks.java | 17 ++++++++ .../java/org/dimdev/matrix/test/ModItems.java | 14 +++++++ .../java/org/dimdev/matrix/test/Test.java | 15 +++++++ src/testmod/resources/fabric.mod.json | 12 ++++++ 7 files changed, 99 insertions(+), 18 deletions(-) rename src/main/java/org/dimdev/matrix/{Register.java => Registrar.java} (89%) create mode 100644 src/testmod/java/org/dimdev/matrix/test/ModBlocks.java create mode 100644 src/testmod/java/org/dimdev/matrix/test/ModItems.java create mode 100644 src/testmod/java/org/dimdev/matrix/test/Test.java create mode 100644 src/testmod/resources/fabric.mod.json diff --git a/build.gradle b/build.gradle index 7137caf..271dd9b 100644 --- a/build.gradle +++ b/build.gradle @@ -8,16 +8,30 @@ apply plugin: "java" sourceCompatibility = 1.8 targetCompatibility = 1.8 +sourceSets { + testmod +} + dependencies { minecraft "com.mojang:minecraft:21w08b" mappings "net.fabricmc:yarn:21w08b+build.8:v2" modImplementation "net.fabricmc:fabric-loader:0.11.1" + testmodImplementation sourceSets.main.output + testmodCompileOnly sourceSets.main.compileClasspath + testmodRuntimeOnly sourceSets.main.runtimeClasspath + modRuntime("net.fabricmc.fabric-api:fabric-api:0.31.2+1.17") { + exclude module: "fabric-loader" + } } version = "1.0.0" archivesBaseName = "Matrix" group = "org.dimdev" +license { + include '**/*.java' +} + processResources { filesMatching("fabric.mod.json") { expand "version": project.version diff --git a/src/main/java/org/dimdev/matrix/Matrix.java b/src/main/java/org/dimdev/matrix/Matrix.java index 6f04a00..74ad732 100644 --- a/src/main/java/org/dimdev/matrix/Matrix.java +++ b/src/main/java/org/dimdev/matrix/Matrix.java @@ -2,8 +2,9 @@ import java.lang.reflect.Modifier; import java.util.Arrays; -import java.util.Optional; +import net.minecraft.item.BlockItem; +import net.minecraft.item.Item; import net.minecraft.util.Identifier; import net.minecraft.util.registry.Registry; @@ -25,28 +26,34 @@ public class Matrix { * * @param clazz The class that should be scanned for registry entries. * @param registry The registry that entries should be registered to. - * @param entryClass The class that each registry entry should be an instance of. - * @param The registry's container type. */ - public static void register(Class clazz, Registry registry, Class entryClass) { - String modId = Optional.ofNullable(clazz.getAnnotation(Register.class)).orElseThrow(() -> new UnsupportedOperationException(clazz.getName() + " did not have the Register annotation!")).modid(); + @SuppressWarnings({"unchecked", "rawtypes"}) + public static void register(Class clazz, Registry registry) { + Registrar registrar = clazz.getAnnotation(Registrar.class); + if (registrar == null) { + return; + } + + String modid = registrar.modid(); + Class element = registrar.element(); + Arrays.stream(clazz.getFields()) - .filter(field -> { - int modifiers = field.getModifiers(); - return Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers) && Modifier.isFinal(modifiers); - }) - .filter(field -> field.isAnnotationPresent(RegistryEntry.class)) - .filter(field -> entryClass.isAssignableFrom(field.getType())) + .filter(field -> field.isAnnotationPresent(RegistryEntry.class) + && Modifier.isPublic(field.getModifiers()) + && Modifier.isStatic(field.getModifiers()) + && Modifier.isFinal(field.getModifiers()) + && element.isAssignableFrom(field.getType()) + ) .forEach(field -> { - T value; try { - //noinspection unchecked - value = (T) field.get(null); + Object value = field.get(null); + Registry.register((Registry) registry, new Identifier(modid, field.getAnnotation(RegistryEntry.class).value()), element.cast(value)); + if (value instanceof BlockItem) { + Item.BLOCK_ITEMS.put(((BlockItem) value).getBlock(), (Item) value); + } } catch (IllegalAccessException e) { - throw new AssertionError(e); // cant happen + throw new AssertionError(e); } - Identifier id = new Identifier(modId, field.getAnnotation(RegistryEntry.class).value()); - Registry.register(registry, id, value); }); } } diff --git a/src/main/java/org/dimdev/matrix/Register.java b/src/main/java/org/dimdev/matrix/Registrar.java similarity index 89% rename from src/main/java/org/dimdev/matrix/Register.java rename to src/main/java/org/dimdev/matrix/Registrar.java index f344160..26afcc4 100644 --- a/src/main/java/org/dimdev/matrix/Register.java +++ b/src/main/java/org/dimdev/matrix/Registrar.java @@ -13,6 +13,8 @@ */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) -public @interface Register { +public @interface Registrar { + Class element(); + String modid(); } diff --git a/src/testmod/java/org/dimdev/matrix/test/ModBlocks.java b/src/testmod/java/org/dimdev/matrix/test/ModBlocks.java new file mode 100644 index 0000000..6990735 --- /dev/null +++ b/src/testmod/java/org/dimdev/matrix/test/ModBlocks.java @@ -0,0 +1,17 @@ +package org.dimdev.matrix.test; + +import org.dimdev.matrix.Registrar; +import org.dimdev.matrix.RegistryEntry; + +import net.minecraft.block.AbstractBlock; +import net.minecraft.block.Block; +import net.minecraft.block.Blocks; +import net.minecraft.item.BlockItem; +import net.minecraft.item.Item; +import net.minecraft.item.ItemGroup; + +@Registrar(element = Block.class, modid = "matrix-test") +public class ModBlocks { + @RegistryEntry("test_block") + public static final Block BLOCK = new Block(AbstractBlock.Settings.copy(Blocks.DIRT)); +} diff --git a/src/testmod/java/org/dimdev/matrix/test/ModItems.java b/src/testmod/java/org/dimdev/matrix/test/ModItems.java new file mode 100644 index 0000000..4badece --- /dev/null +++ b/src/testmod/java/org/dimdev/matrix/test/ModItems.java @@ -0,0 +1,14 @@ +package org.dimdev.matrix.test; + +import org.dimdev.matrix.Registrar; +import org.dimdev.matrix.RegistryEntry; + +import net.minecraft.item.BlockItem; +import net.minecraft.item.Item; +import net.minecraft.item.ItemGroup; + +@Registrar(element = Item.class, modid = "matrix-test") +public class ModItems { + @RegistryEntry("test_item") + public static final Item ITEM = new BlockItem(ModBlocks.BLOCK, new Item.Settings().group(ItemGroup.FOOD)); +} diff --git a/src/testmod/java/org/dimdev/matrix/test/Test.java b/src/testmod/java/org/dimdev/matrix/test/Test.java new file mode 100644 index 0000000..a41063b --- /dev/null +++ b/src/testmod/java/org/dimdev/matrix/test/Test.java @@ -0,0 +1,15 @@ +package org.dimdev.matrix.test; + +import org.dimdev.matrix.Matrix; + +import net.minecraft.util.registry.Registry; + +import net.fabricmc.api.ModInitializer; + +public class Test implements ModInitializer { + @Override + public void onInitialize() { + Matrix.register(ModBlocks.class, Registry.BLOCK); + Matrix.register(ModItems.class, Registry.ITEM); + } +} diff --git a/src/testmod/resources/fabric.mod.json b/src/testmod/resources/fabric.mod.json new file mode 100644 index 0000000..7c80504 --- /dev/null +++ b/src/testmod/resources/fabric.mod.json @@ -0,0 +1,12 @@ +{ + "schemaVersion": 1, + "version": "${version}", + "id": "matrix-test", + "name": "Matrix Test", + "description": "Testmod", + "entrypoints": { + "main": [ + "org.dimdev.matrix.test.Test" + ] + } +}