Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
import com.google.common.base.Suppliers;
import games.moegirl.sinocraft.sinocore.api.registry.IRegRef;
import games.moegirl.sinocraft.sinocore.api.registry.IRegistry;
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
import net.fabricmc.fabric.api.event.registry.RegistryAttribute;
import games.moegirl.sinocraft.sinocore.api.registry.RegistryManager;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceKey;
Expand All @@ -26,13 +25,7 @@ public FabricRegistry(String modId, ResourceKey<Registry<T>> key) {
this.modId = modId;
this.key = key;

registry = (Registry<T>) BuiltInRegistries.REGISTRY.get(key.location());
if (registry == null) {
// 不存在的注册表 -- 创建自定义注册表
registry = FabricRegistryBuilder.createSimple(key)
.attribute(RegistryAttribute.SYNCED)
.buildAndRegister();
}
registry = RegistryManager.getOrCreateRegistry(key);
sup = Suppliers.memoize(() -> (Registry<T>) BuiltInRegistries.REGISTRY.get(key.location()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

import games.moegirl.sinocraft.sinocore.api.registry.*;
import games.moegirl.sinocraft.sinocore.fabric.registry.*;
import net.fabricmc.fabric.api.event.registry.FabricRegistryBuilder;
import net.fabricmc.fabric.api.event.registry.RegistryAttribute;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public class RegistryPlatformImpl {
public static <T> Registry<T> createRegistry(RegistryBuilder<T> builder) {
var b = FabricRegistryBuilder.createSimple(builder.getKey());
if (builder.isSync()) {
b.attribute(RegistryAttribute.SYNCED);
}
return b.buildAndRegister();
}

public static <T> IRegistry<T> create(String modId, ResourceKey<Registry<T>> key) {
return new FabricRegistry<>(modId, key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

import games.moegirl.sinocraft.sinocore.api.registry.IRegRef;
import games.moegirl.sinocraft.sinocore.api.registry.IRegistry;
import games.moegirl.sinocraft.sinocore.api.registry.RegistryManager;
import games.moegirl.sinocraft.sinocore.neoforge.util.ModBusHelper;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredRegister;
import net.neoforged.neoforge.registries.NewRegistryEvent;
import net.neoforged.neoforge.registries.RegistryBuilder;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class NeoForgeRegistry<T> implements IRegistry<T> {
Expand All @@ -31,11 +27,10 @@ public NeoForgeRegistry(String modId, ResourceKey<Registry<T>> key) {
this.modId = modId;
this.dr = DeferredRegister.create(key, modId);

if (!BuiltInRegistries.REGISTRY.containsKey(key.location())) {
bus.addListener((Consumer<NewRegistryEvent>) event -> event.register(new RegistryBuilder<>(key)
.sync(true)
.create()));
if (!RegistryManager.hasRegistry(key)) {
RegistryManager.createRegistry(key);
}

registered = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
public class ModBusHelper {
public static IEventBus getModBus(String modId) {
if ("minecraft".equals(modId)) {
return getModBus("sinocore");
throw new RuntimeException("Hey, Minecraft has no event bus!");
}

return Objects.requireNonNull(ModList.get().getModContainerById(modId).get().getEventBus());
return Objects.requireNonNull(ModList.get().getModContainerById(modId).orElseThrow().getEventBus());
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
package games.moegirl.sinocraft.sinocore.platform.neoforge;

import games.moegirl.sinocraft.sinocore.SinoCore;
import games.moegirl.sinocraft.sinocore.api.registry.*;
import games.moegirl.sinocraft.sinocore.neoforge.registry.*;
import games.moegirl.sinocraft.sinocore.neoforge.util.ModBusHelper;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
import net.neoforged.neoforge.registries.NewRegistryEvent;

import java.util.function.Consumer;

public class RegistryPlatformImpl {
public static <T> Registry<T> createRegistry(RegistryBuilder<T> builder) {
var b = new net.neoforged.neoforge.registries.RegistryBuilder<T>(builder.getKey());
b.sync(builder.isSync());
var registry = b.create();

var bus = ModBusHelper.getModBus(SinoCore.MODID);
bus.addListener((Consumer<NewRegistryEvent>) event -> event.register(registry));

return registry;
}

public static <T> IRegistry<T> create(String modId, ResourceKey<Registry<T>> key) {
return new NeoForgeRegistry<>(modId, key);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package games.moegirl.sinocraft.sinocore.api.registry;

import lombok.Getter;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;

/**
* A builder for creating custom registries.
*
* @param <T> Registry entry type.
* @see RegistryManager#createRegistry(RegistryBuilder)
*/
@Getter
public class RegistryBuilder<T> {
private final ResourceKey<Registry<T>> key;

private boolean sync = false;

public RegistryBuilder(ResourceKey<Registry<T>> key) {
this.key = key;
}

/**
* Set the registry to be synchronized to client.
*
* @return this
*/
public RegistryBuilder<T> sync() {
return sync(true);
}

/**
* Set whether the registry should be synchronized to client.
*
* @param value Whether to sync.
* @return this
*/
public RegistryBuilder<T> sync(boolean value) {
this.sync = value;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.mojang.logging.LogUtils;
import games.moegirl.sinocraft.sinocore.platform.RegistryPlatform;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;

import java.util.ArrayList;
Expand Down Expand Up @@ -41,6 +43,15 @@ private static <T, U extends IRegistrable<T>> U appendRegistrable(Map<String, Li
return registrable;
}

/**
* Get all registries created by SinoCore for a mod and registry key.
*
* @param modId Mod Id.
* @param key Registry key.
* @param <T> Registry entry type.
* @param <U> Registry type.
* @return List of registries.
*/
@SuppressWarnings("unchecked")
public static <T, U extends IRegistryBase<T>> List<U> getRegistries(String modId, ResourceKey<Registry<T>> key) {
if (!REGISTRIES.containsKey(modId)) {
Expand All @@ -57,6 +68,70 @@ public static <T, U extends IRegistryBase<T>> List<U> getRegistries(String modId
.build();
}

/**
* Check if a registry exists.
*
* @param key Registry key.
* @param <T> Registry entry type.
* @return true if exists.
*/
public static <T> boolean hasRegistry(ResourceKey<Registry<T>> key) {
if (!key.isFor(BuiltInRegistries.REGISTRY.key())) {
return false;
}
return BuiltInRegistries.REGISTRY.containsKey(key.location());
}

/**
* Get a registry by its key.
*
* @param key Registry key.
* @param <T> Registry entry type.
* @return The registry, or null if not found.
*/
@Nullable
@SuppressWarnings("unchecked")
public static <T> Registry<T> getRegistry(ResourceKey<Registry<T>> key) {
return (Registry<T>) BuiltInRegistries.REGISTRY.get(key.location());
}

/**
* Create a new registry (sync by default).
*
* @param key Registry key.
* @param <T> Registry entry type.
* @return The created registry.
*/
public static <T> Registry<T> createRegistry(ResourceKey<Registry<T>> key) {
return createRegistry(new RegistryBuilder<>(key).sync());
}

/**
* Create a new registry.
*
* @param builder Registry builder.
* @param <T> Registry entry type.
* @return The created registry.
*/
public static <T> Registry<T> createRegistry(RegistryBuilder<T> builder) {
return RegistryPlatform.createRegistry(builder);
}

/**
* Get if the registry exists, otherwise create a new one (sync by default).
*
* @param key Registry key.
* @param <T> Registry entry type.
* @return The registry.
*/
public static <T> Registry<T> getOrCreateRegistry(ResourceKey<Registry<T>> key) {
Registry<T> registry = getRegistry(key);
if (registry != null) {
return registry;
}
return createRegistry(key);
}

/**
* 创建一个新的 SinoCore 注册表
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

@ApiStatus.Internal
public class RegistryPlatform {
@ExpectPlatform
public static <T> Registry<T> createRegistry(RegistryBuilder<T> builder) {
throw new AssertionError();
}

@ExpectPlatform
public static <T> IRegistry<T> create(String modId, ResourceKey<Registry<T>> key) {
throw new AssertionError();
Expand Down