Skip to content

Commit

Permalink
✨ Added ability to create custom registrars
Browse files Browse the repository at this point in the history
  • Loading branch information
XyperCode committed Jan 24, 2025
1 parent 28d6bcc commit 06d6c22
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,12 @@ public interface RegistrarManager {
/// @param key The key of the registry
/// @return The registrar
<T> Registrar<T> getRegistrar(ResourceKey<Registry<T>> key);

/// Creates a new registrar for the given class and resource key
///
/// @param key The key to use for the registry
/// @param clazz The class which represents the type of the registry
/// @return The built registrar
<T> Registrar<T> createRegistrar(ResourceKey<Registry<T>> key, Class<T> clazz);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.ultreon.mods.xinexlib.registrar;

import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
import net.fabricmc.fabric.api.event.registry.RegistryAttribute;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceKey;
Expand All @@ -14,14 +16,13 @@
public class FabricRegistrar<T> implements Registrar<T> {
private final ResourceKey<Registry<T>> key;
private final String modId;
private final Registry<T> registry;
private Registry<T> registry;
private final List<RegistrySupplier<?, T>> suppliers = new ArrayList<>();

@SuppressWarnings("unchecked")
public FabricRegistrar(ResourceKey<Registry<T>> key, String modId) {
this.key = key;
this.modId = modId;
this.registry = BuiltInRegistries.REGISTRY.get((ResourceKey) key);
}

@Override
Expand Down Expand Up @@ -59,4 +60,14 @@ public Registry<T> registry() {
public @NotNull Iterator<RegistrySupplier<?, T>> iterator() {
return suppliers.iterator();
}

public Registrar<T> create() {
this.registry = FabricRegistryBuilder.createSimple(key).attribute(RegistryAttribute.MODDED).buildAndRegister();
return this;
}

public Registrar<T> retrieve() {
this.registry = BuiltInRegistries.REGISTRY.get((ResourceKey) key);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public FabricRegistrarManager(String modId) {

@Override
public <T> Registrar<T> getRegistrar(ResourceKey<Registry<T>> key) {
return new FabricRegistrar<>(key, modId);
return new FabricRegistrar<>(key, modId).retrieve();
}

@Override
public <T> Registrar<T> createRegistrar(ResourceKey<Registry<T>> key, Class<T> clazz) {
return new FabricRegistrar<>(key, modId).create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.RegistryBuilder;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
Expand Down Expand Up @@ -57,4 +58,16 @@ public Registry<T> registry() {
public @NotNull Iterator<RegistrySupplier<?, T>> iterator() {
return registryObjects.iterator();
}

public void create() {
deferredRegister.makeRegistry(ForgeRegistrar::builder);
}

public void createDefault(Supplier<T> fallback) {
deferredRegister.makeRegistry(() -> ForgeRegistrar.<T>builder().missing((key, isNetwork) -> fallback.get()));
}

private static <T> RegistryBuilder<T> builder() {
return new RegistryBuilder<T>().disableOverrides().allowModification();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@ public ForgeRegistrarManager(String modId) {
public <T> Registrar<T> getRegistrar(ResourceKey<Registry<T>> key) {
return new ForgeRegistrar<>(DeferredRegister.create(key, modId), modEventBus, modId);
}

@Override
public <T> Registrar<T> createRegistrar(ResourceKey<Registry<T>> key, Class<T> clazz) {
ForgeRegistrar<T> registrar = new ForgeRegistrar<>(DeferredRegister.create(key, modId), modEventBus, modId);
registrar.create();
return registrar;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.resources.ResourceLocation;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredRegister;
import net.neoforged.neoforge.registries.RegistryBuilder;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
Expand Down Expand Up @@ -45,4 +46,9 @@ public Registry<T> registry() {
public @NotNull Iterator<RegistrySupplier<?, T>> iterator() {
return this.values.iterator();
}

public NeoForgeRegistrar<T> create() {
this.deferredRegister.makeRegistry(RegistryBuilder::create);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public NeoForgeRegistrarManager(String modId, IEventBus modEventBus) {
public <T> Registrar<T> getRegistrar(ResourceKey<Registry<T>> key) {
return new NeoForgeRegistrar<>(DeferredRegister.create(key, modId), modEventBus);
}

@Override
public <T> Registrar<T> createRegistrar(ResourceKey<Registry<T>> key, Class<T> clazz) {
return new NeoForgeRegistrar<>(DeferredRegister.create(key, modId), modEventBus).create();
}
}

0 comments on commit 06d6c22

Please sign in to comment.