[1.18.x] Allow Modders to add custom data-pack registries through RegistryBuilder (#8630)

Co-authored-by: Silverminer007 <[email protected]>
Co-authored-by: Curle <[email protected]>
This commit is contained in:
authored and GitHub committed 2022-06-05 15:27:30 +01:00
1 parent 15bd5f0d30
commit 997d8e0aa2
21 files changed
+525 -10

No files matched your search

@@ -0,0 +1,60 @@
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.registries;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import com.google.common.collect.ImmutableMap;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceKey;
@ApiStatus.Internal
public final class DataPackRegistriesHooks
{
private DataPackRegistriesHooks() {} // utility class
private static Map<ResourceKey<? extends Registry<?>>, RegistryAccess.RegistryData<?>> REGISTRY_ACCESS_REGISTRIES_COPY;
private static final Set<ResourceKey<? extends Registry<?>>> SYNCED_CUSTOM_REGISTRIES = new HashSet<>();
private static final Set<ResourceKey<? extends Registry<?>>> SYNCED_CUSTOM_REGISTRIES_VIEW = Collections.unmodifiableSet(SYNCED_CUSTOM_REGISTRIES);
/* Internal forge hook for retaining mutable access to RegistryAccess's codec registry when it bootstraps. */
public static Map<ResourceKey<? extends Registry<?>>, RegistryAccess.RegistryData<?>> grabBuiltinRegistries(ImmutableMap.Builder<ResourceKey<? extends Registry<?>>, RegistryAccess.RegistryData<?>> builder)
{
REGISTRY_ACCESS_REGISTRIES_COPY = new HashMap<>(builder.build());
SYNCED_CUSTOM_REGISTRIES.clear();
return Collections.unmodifiableMap(REGISTRY_ACCESS_REGISTRIES_COPY);
}
/* Internal forge method, registers a datapack registry codec and folder. */
public static <T> void addRegistryCodec(@NotNull RegistryAccess.RegistryData<T> data)
{
ResourceKey<? extends Registry<T>> registryKey = data.key();
REGISTRY_ACCESS_REGISTRIES_COPY.put(registryKey, data);
if (data.networkCodec() != null)
{
SYNCED_CUSTOM_REGISTRIES.add(registryKey);
}
}
/**
* {@return unmodifiable view of the set of synced non-vanilla datapack registry IDs}
* Clients must have each of a server's synced datapack registries to be able to connect to that server;
* vanilla clients therefore cannot connect if this list is non-empty on the server.
*/
public static Set<ResourceKey<? extends Registry<?>>> getSyncedCustomRegistries()
{
return SYNCED_CUSTOM_REGISTRIES_VIEW;
}
}
@@ -7,6 +7,8 @@ package net.minecraftforge.registries;
import com.mojang.logging.LogUtils;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.data.BuiltinRegistries;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.fml.event.IModBusEvent;
import org.jetbrains.annotations.Nullable;
@@ -76,7 +78,16 @@ public class NewRegistryEvent extends Event implements IModBusEvent
builtRegistries.forEach((builder, reg) -> {
try
{
if (builder.getHasWrapper() && !Registry.REGISTRY.containsKey(reg.getRegistryName()))
RegistryAccess.RegistryData<?> dataPackRegistryData = builder.getDataPackRegistryData();
if (dataPackRegistryData != null) // if this is a datapack registry
{
if (!BuiltinRegistries.REGISTRY.containsKey(reg.getRegistryName()))
{
DataPackRegistriesHooks.addRegistryCodec(dataPackRegistryData);
RegistryManager.registerToBuiltinRegistry((ForgeRegistry<?>) reg);
}
}
else if (builder.getHasWrapper() && !Registry.REGISTRY.containsKey(reg.getRegistryName()))
RegistryManager.registerToRootRegistry((ForgeRegistry<?>) reg);
} catch (Exception e)
{
@@ -9,10 +9,17 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.Lists;
import com.mojang.serialization.Codec;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.registries.IForgeRegistry.*;
@@ -38,6 +45,7 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
private boolean allowOverrides = true;
private boolean allowModifications = false;
private boolean hasWrapper = false;
private Supplier<RegistryAccess.RegistryData<T>> dataPackRegistryData = () -> null; // If present, implies this is a datapack registry.
private DummyFactory<T> dummyFactory;
private MissingFactory<T> missingFactory;
private Set<ResourceLocation> legacyNames = new HashSet<>();
@@ -175,6 +183,11 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
return this;
}
/**
* Prevents the registry from being synced to clients. Does *not* affect datapack registries, datapack registries are unsynced by default
* unless a non-null network codec is registered via {@link #dataPackRegistry(Codec, Codec)}
* @return this
*/
public RegistryBuilder<T> disableSync()
{
this.sync = false;
@@ -230,6 +243,66 @@ public class RegistryBuilder<T extends IForgeRegistryEntry<T>>
return this;
}
/**
* <p>Register this registry as an unsynced datapack registry, which will cause data to be loaded from
* a datapack folder based on the registry's name. The mod that registers this registry does not need to exist
* on the client to connect to servers with the mod/registry.</p>
* <p>Data JSONs will be loaded from {@code data/<datapack_namespace>/modid/registryname/}, where modid is the mod that registered this registry.</p>
*
* @param codec the codec to be used for loading data from datapacks on servers
* @return this builder
*
* @see #dataPackRegistry(Codec, Codec)
*/
public RegistryBuilder<T> dataPackRegistry(Codec<T> codec)
{
return this.dataPackRegistry(codec, null);
}
/**
* <p>Register this registry as a datapack registry, which will cause data to be loaded from
* a datapack folder based on the registry's name.</p>
* <p>Data JSONs will be loaded from {@code data/<datapack_namespace>/modid/registryname/}, where modid is the mod that registered this registry.</p>
*
* @param codec the codec to be used for loading data from datapacks on servers
* @param networkCodec the codec to be used for syncing loaded data to clients.<br>
* If networkCodec is null, data will not be synced, and clients without the mod that registered this registry can
* connect to servers with the mod.<br>
* If networkCodec is not null, then data will be synced (accessible via {@link ClientPacketListener#registryAccess()}),
* and the mod must be present on a client to connect to servers with the mod.
* @return this builder
*
* @see #dataPackRegistry(Codec)
*/
public RegistryBuilder<T> dataPackRegistry(Codec<T> codec, @Nullable Codec<T> networkCodec)
{
this.hasWrapper(); // A wrapper is required for data pack registries.
this.disableSync(); // Datapack registries are synced using a different system than static registries.
// Supplier averts having to set the registry name before calling this.
this.dataPackRegistryData = Suppliers.memoize(() -> {
// Validate registry key.
if (this.registryName == null)
throw new IllegalStateException("Registry builder cannot build a datapack registry: registry name not set");
ResourceKey<Registry<T>> registryKey = ResourceKey.createRegistryKey(this.registryName);
return new RegistryAccess.RegistryData<>(registryKey, codec, networkCodec);
});
return this;
}
/**
* Retrieves datapack registry information, if any.
*
* @return RegistryData containing the registry's key and codec(s). If returned data is null, this has not been marked as a datapack registry.
*
* @throws IllegalStateException if this has been marked as a datapack registry, but registry name has not been set.
*/
@Nullable
RegistryAccess.RegistryData<T> getDataPackRegistryData()
{
return this.dataPackRegistryData.get();
}
/**
* Modders: Use {@link NewRegistryEvent#create(RegistryBuilder)} instead
*/
@@ -22,6 +22,7 @@ import com.google.common.collect.Sets.SetView;
import com.mojang.serialization.Lifecycle;
import net.minecraft.core.WritableRegistry;
import net.minecraft.data.BuiltinRegistries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
@@ -161,10 +162,20 @@ public class RegistryManager
return getRegistry(name);
}
@SuppressWarnings("unchecked")
static <V extends IForgeRegistryEntry<V>> void registerToRootRegistry(ForgeRegistry<V> forgeReg)
{
WritableRegistry<Registry<V>> registry = (WritableRegistry<Registry<V>>) Registry.REGISTRY;
injectForgeRegistry(forgeReg, Registry.REGISTRY);
}
static <V extends IForgeRegistryEntry<V>> void registerToBuiltinRegistry(ForgeRegistry<V> forgeReg)
{
injectForgeRegistry(forgeReg, BuiltinRegistries.REGISTRY);
}
@SuppressWarnings("unchecked")
private static <V extends IForgeRegistryEntry<V>> void injectForgeRegistry(ForgeRegistry<V> forgeReg, Registry<? extends Registry<?>> rootRegistry)
{
WritableRegistry<Registry<V>> registry = (WritableRegistry<Registry<V>>) rootRegistry;
Registry<V> wrapper = forgeReg.getWrapper();
if (wrapper != null)
registry.register(forgeReg.getRegistryKey(), wrapper, Lifecycle.experimental());