Skip to content

Commit

Permalink
And that's why fabric api is important
Browse files Browse the repository at this point in the history
  • Loading branch information
BoogieMonster1O1 committed Mar 3, 2021
1 parent 5435c75 commit fa80027
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 18 deletions.
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 24 additions & 17 deletions src/main/java/org/dimdev/matrix/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 <T> The registry's container type.
*/
public static <T> void register(Class<?> clazz, Registry<T> registry, Class<T> 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);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Register {
public @interface Registrar {
Class<?> element();

String modid();
}
17 changes: 17 additions & 0 deletions src/testmod/java/org/dimdev/matrix/test/ModBlocks.java
Original file line number Diff line number Diff line change
@@ -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));
}
14 changes: 14 additions & 0 deletions src/testmod/java/org/dimdev/matrix/test/ModItems.java
Original file line number Diff line number Diff line change
@@ -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));
}
15 changes: 15 additions & 0 deletions src/testmod/java/org/dimdev/matrix/test/Test.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
12 changes: 12 additions & 0 deletions src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"schemaVersion": 1,
"version": "${version}",
"id": "matrix-test",
"name": "Matrix Test",
"description": "Testmod",
"entrypoints": {
"main": [
"org.dimdev.matrix.test.Test"
]
}
}

0 comments on commit fa80027

Please sign in to comment.