Skip to content

Commit

Permalink
Add support for dynamic registry creation across platforms
Browse files Browse the repository at this point in the history
Introduced a mechanism to register new registries dynamically for both NeoForge and Fabric platforms. This includes implementing `registerNewRegistry` in `RegistrationHelper` and integrating registry registration logic in `common` and platform-specific code. Also added a Fabric test mod module and updated the existing configurations for consistency.
  • Loading branch information
ThePandaOliver committed Dec 22, 2024
1 parent 99ec183 commit 0c2d774
Show file tree
Hide file tree
Showing 14 changed files with 211 additions and 4 deletions.
18 changes: 18 additions & 0 deletions common-testmod/src/main/java/me/pandamods/test/TestMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@

package me.pandamods.test;

import com.mojang.serialization.Lifecycle;
import dev.architectury.event.events.client.ClientLifecycleEvent;
import me.pandamods.pandalib.config.PandaLibConfig;
import me.pandamods.pandalib.config.holders.ClientConfigHolder;
import me.pandamods.pandalib.config.holders.CommonConfigHolder;
import me.pandamods.pandalib.registry.DeferredObject;
import me.pandamods.pandalib.registry.DeferredRegister;
import me.pandamods.pandalib.registry.RegistryRegister;
import me.pandamods.test.config.ClientTestConfig;
import me.pandamods.test.config.CommonTestConfig;
import net.minecraft.core.MappedRegistry;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;

public class TestMod {
Expand All @@ -26,8 +34,16 @@ public class TestMod {
private static final CommonConfigHolder<CommonTestConfig> COMMON_TEST_CONFIG = PandaLibConfig.registerCommon(CommonTestConfig.class);
private static final ClientConfigHolder<ClientTestConfig> CLIENT_TEST_CONFIG = PandaLibConfig.registerClient(ClientTestConfig.class);

public static final ResourceKey<Registry<TestRegistry>> TEST_REGISTRY_KEY = ResourceKey.createRegistryKey(TestMod.resourceLocation("test_registry"));
public static final Registry<TestRegistry> TEST_REGISTRY = RegistryRegister.register(new MappedRegistry<>(TEST_REGISTRY_KEY, Lifecycle.stable()));

public static final DeferredRegister<TestRegistry> TEST_REGISTER = DeferredRegister.create(MOD_ID, TEST_REGISTRY);
public static final DeferredObject<TestRegistry> TEST1 = TEST_REGISTER.register("test1", TestRegistry::new);
public static final DeferredObject<TestRegistry> TEST2 = TEST_REGISTER.register("test2", TestRegistry::new);

public TestMod() {
instance = this;
TEST_REGISTER.register();
}

public static ResourceLocation resourceLocation(String path) {
Expand All @@ -37,4 +53,6 @@ public static ResourceLocation resourceLocation(String path) {
public static TestMod getInstance() {
return instance;
}

public static class TestRegistry { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
package me.pandamods.pandalib.platform.services;

import me.pandamods.pandalib.registry.DeferredObject;
import net.minecraft.core.Registry;

import java.util.function.Supplier;

public interface RegistrationHelper {
<T> void register(DeferredObject<? extends T> deferredObject, Supplier<? extends T> supplier);

<T> void registerNewRegistry(Registry<T> registry);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

@SuppressWarnings("unused")
public class DeferredRegister<T> {
private final String namespace;
private final ResourceKey<? extends Registry<T>> registryKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2024 Oliver Froberg (The Panda Oliver)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package me.pandamods.pandalib.registry;

import me.pandamods.pandalib.platform.Services;
import net.minecraft.core.Registry;

@SuppressWarnings("unused")
public class RegistryRegister<T> {
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> Registry<T> register(Registry<T> registry) {
Services.REGISTRATION.registerNewRegistry(registry);
return registry;
}
}
61 changes: 61 additions & 0 deletions fabric-testmod/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import net.fabricmc.loom.task.RemapJarTask

architectury {
platformSetupLoomIde()
fabric()
}

//loom.accessWidenerPath.set(project(":common").loom.accessWidenerPath)

configurations {
getByName("developmentFabric").extendsFrom(configurations["common"])
}

repositories {
maven("https://maven.terraformersmc.com/releases/")
}

dependencies {
modImplementation("net.fabricmc:fabric-loader:${properties["fabric_version"]}")
modApi("net.fabricmc.fabric-api:fabric-api:${properties["fabric_api_version"]}")

modApi("dev.architectury:architectury-fabric:${properties["deps_architectury_version"]}")
modApi("com.terraformersmc:modmenu:${properties["deps_modmenu_version"]}")

implementation(project(":fabric", "namedElements")) { isTransitive = false }
common(project(":common", "namedElements")) { isTransitive = false }
common(project(":common-testmod", "namedElements")) { isTransitive = false }
}

tasks.remapJar {
injectAccessWidener.set(true)
}

tasks.withType<RemapJarTask> {
val shadowJar = tasks.getByName<ShadowJar>("shadowJar")
inputFile.set(shadowJar.archiveFile)
}

publishing {
publications {
register("mavenJava", MavenPublication::class) {
groupId = properties["maven_group"] as String
artifactId = "${properties["mod_id"]}-${project.name}"
version = "${project.version}-build.${project.findProperty("buildNumber") ?: "-1"}"

from(components["java"])
}
}

repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/PandaMods-Dev/PandaLib")
credentials {
username = project.findProperty("gpr.user") as String? ?: System.getenv("USERNAME")
password = project.findProperty("gpr.key") as String? ?: System.getenv("TOKEN")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2024 Oliver Froberg (The Panda Oliver)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package me.pandamods.testmod.fabric;

import me.pandamods.test.TestMod;
import net.fabricmc.api.ModInitializer;

public class TestModFabric implements ModInitializer {
@Override
public void onInitialize() {
new TestMod();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (C) 2024 Oliver Froberg (The Panda Oliver)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package me.pandamods.testmod.fabric.client;

import me.pandamods.test.client.TestModClient;
import net.fabricmc.api.ClientModInitializer;

public class TestModClientFabric implements ClientModInitializer {
@Override
public void onInitializeClient() {
new TestModClient();
}
}
27 changes: 27 additions & 0 deletions fabric-testmod/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"schemaVersion": 1,
"id": "testmod",
"version": "${mod_version}",
"name": "Test Mod",
"description": "Testing environment for mod development and testing purposes",
"authors": [
"${mod_author}"
],
"contact": {},
"license": "${mod_license}",
"icon": "assets/${mod_id}/icon.png",
"environment": "*",
"entrypoints": {
"main": [
"me.pandamods.testmod.fabric.TestModFabric"
],
"client": [
"me.pandamods.testmod.fabric.client.TestModClientFabric"
]
},
"mixins": [],
"depends": {
"fabric": "*",
"minecraft": "${fabric_version_range}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

import me.pandamods.pandalib.platform.services.RegistrationHelper;
import me.pandamods.pandalib.registry.DeferredObject;
import net.minecraft.core.RegistrationInfo;
import net.minecraft.core.Registry;
import net.minecraft.core.WritableRegistry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;

import java.util.function.Supplier;

Expand All @@ -24,4 +28,13 @@ public class RegistrationHelperImpl implements RegistrationHelper {
public <T> void register(DeferredObject<? extends T> deferredObject, Supplier<? extends T> supplier) {
Registry.register((Registry<T>) deferredObject.getRegistry(), deferredObject.getId(), supplier.get());
}

@Override
public <T> void registerNewRegistry(Registry<T> registry) {
ResourceLocation registryName = registry.key().location();
if (BuiltInRegistries.REGISTRY.containsKey(registryName))
throw new IllegalStateException("Attempted duplicate registration of registry " + registryName);

((WritableRegistry) BuiltInRegistries.REGISTRY).register(registry.key(), registry, RegistrationInfo.BUILT_IN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

package me.pandamods.testmod.neoforge;

import me.pandamods.pandalib.PandaLib;
import me.pandamods.test.TestMod;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.common.Mod;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

package me.pandamods.testmod.neoforge.client;

import me.pandamods.pandalib.PandaLib;
import me.pandamods.test.TestMod;
import me.pandamods.test.client.TestModClient;
import net.neoforged.api.distmarker.Dist;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ public PandaLibNeoForge(IEventBus eventBus) {
new PandaLib();

eventBus.addListener(NetworkHelperImpl::registerPackets);
if (Services.REGISTRATION instanceof RegistrationHelperImpl helper)
if (Services.REGISTRATION instanceof RegistrationHelperImpl helper) {
eventBus.addListener(helper::registerEvent);
eventBus.addListener(helper::registerNewRegistryEvent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
import me.pandamods.pandalib.registry.DeferredObject;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.neoforged.neoforge.registries.NewRegistryEvent;
import net.neoforged.neoforge.registries.RegisterEvent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

public class RegistrationHelperImpl implements RegistrationHelper {
private final Map<ResourceKey<? extends Registry<?>>, PendingRegistries<?>> pendingRegistries = new HashMap<>();
private final List<Registry<?>> pendingRegistryTypes = new ArrayList<>();

@Override
@SuppressWarnings("unchecked")
Expand All @@ -34,9 +38,18 @@ public <T> void register(DeferredObject<? extends T> deferredObject, Supplier<?
pending.add(deferredObject, supplier);
}

@Override
public <T> void registerNewRegistry(Registry<T> registry) {
pendingRegistryTypes.add(registry);
}

public void registerEvent(RegisterEvent event) {
pendingRegistries.values().forEach(pending -> pending.register(event));
}

public void registerNewRegistryEvent(NewRegistryEvent event) {
pendingRegistryTypes.forEach(event::register);
}

private static class PendingRegistries<T> {
private final ResourceKey<? extends Registry<T>> registryKey;
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ pluginManagement.repositories {
}

include("common", "common-testmod")
include("fabric")
include("fabric", "fabric-testmod")
include("neoforge", "neoforge-testmod")

0 comments on commit 0c2d774

Please sign in to comment.