2022-06-05 10:27:30 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) Forge Development LLC and contributors
|
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1-only
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.minecraftforge.registries;
|
|
|
|
|
|
2022-12-15 21:53:53 +02:00
|
|
|
import com.google.common.collect.ImmutableMap;
|
2022-11-22 13:32:47 -06:00
|
|
|
import net.minecraft.core.Registry;
|
2022-12-15 21:53:53 +02:00
|
|
|
import net.minecraft.core.RegistrySynchronization;
|
2022-11-22 13:32:47 -06:00
|
|
|
import net.minecraft.resources.RegistryDataLoader;
|
|
|
|
|
import net.minecraft.resources.ResourceKey;
|
|
|
|
|
import org.jetbrains.annotations.ApiStatus;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2022-06-05 10:27:30 -04:00
|
|
|
import java.util.Collections;
|
2022-12-15 21:53:53 +02:00
|
|
|
import java.util.HashMap;
|
2022-06-05 10:27:30 -04:00
|
|
|
import java.util.HashSet;
|
2022-12-15 21:53:53 +02:00
|
|
|
import java.util.LinkedHashMap;
|
2022-11-22 13:32:47 -06:00
|
|
|
import java.util.List;
|
2022-12-15 21:53:53 +02:00
|
|
|
import java.util.Map;
|
2022-06-05 10:27:30 -04:00
|
|
|
import java.util.Set;
|
2022-12-26 13:19:36 -08:00
|
|
|
import java.util.stream.Stream;
|
2022-06-05 10:27:30 -04:00
|
|
|
|
|
|
|
|
@ApiStatus.Internal
|
|
|
|
|
public final class DataPackRegistriesHooks
|
|
|
|
|
{
|
|
|
|
|
private DataPackRegistriesHooks() {} // utility class
|
2022-11-22 13:32:47 -06:00
|
|
|
|
2022-12-15 21:53:53 +02:00
|
|
|
private static final Map<ResourceKey<? extends Registry<?>>, RegistrySynchronization.NetworkedRegistryData<?>> NETWORKABLE_REGISTRIES = new LinkedHashMap<>();
|
2022-11-22 13:32:47 -06:00
|
|
|
private static final List<RegistryDataLoader.RegistryData<?>> DATA_PACK_REGISTRIES = new ArrayList<>(RegistryDataLoader.WORLDGEN_REGISTRIES);
|
|
|
|
|
private static final List<RegistryDataLoader.RegistryData<?>> DATA_PACK_REGISTRIES_VIEW = Collections.unmodifiableList(DATA_PACK_REGISTRIES);
|
2022-06-05 10:27:30 -04:00
|
|
|
private static final Set<ResourceKey<? extends Registry<?>>> SYNCED_CUSTOM_REGISTRIES = new HashSet<>();
|
2022-11-22 13:32:47 -06:00
|
|
|
private static final Set<ResourceKey<? extends Registry<?>>> SYNCED_CUSTOM_REGISTRIES_VIEW = Collections.unmodifiableSet(SYNCED_CUSTOM_REGISTRIES);
|
2022-06-05 10:27:30 -04:00
|
|
|
|
2022-12-15 21:53:53 +02:00
|
|
|
/* Internal forge hook for retaining mutable access to RegistryAccess's codec registry when it bootstraps. */
|
|
|
|
|
public static Map<ResourceKey<? extends Registry<?>>, RegistrySynchronization.NetworkedRegistryData<?>> grabNetworkableRegistries(ImmutableMap.Builder<ResourceKey<? extends Registry<?>>, RegistrySynchronization.NetworkedRegistryData<?>> builder)
|
|
|
|
|
{
|
|
|
|
|
if (!StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).getCallerClass().equals(RegistrySynchronization.class))
|
|
|
|
|
throw new IllegalCallerException("Attempted to call DataPackRegistriesHooks#grabNetworkableRegistries!");
|
|
|
|
|
NETWORKABLE_REGISTRIES.forEach(builder::put);
|
|
|
|
|
NETWORKABLE_REGISTRIES.clear();
|
|
|
|
|
NETWORKABLE_REGISTRIES.putAll(builder.build());
|
|
|
|
|
return Collections.unmodifiableMap(NETWORKABLE_REGISTRIES);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-22 13:32:47 -06:00
|
|
|
/* Internal forge method, registers a datapack registry codec and folder. */
|
|
|
|
|
static <T> void addRegistryCodec(DataPackRegistryEvent.DataPackRegistryData<T> data)
|
2022-06-05 10:27:30 -04:00
|
|
|
{
|
2022-11-22 13:32:47 -06:00
|
|
|
RegistryDataLoader.RegistryData<T> loaderData = data.loaderData();
|
|
|
|
|
DATA_PACK_REGISTRIES.add(loaderData);
|
|
|
|
|
if (data.networkCodec() != null)
|
2022-12-15 21:53:53 +02:00
|
|
|
{
|
2022-11-22 13:32:47 -06:00
|
|
|
SYNCED_CUSTOM_REGISTRIES.add(loaderData.key());
|
2022-12-15 21:53:53 +02:00
|
|
|
NETWORKABLE_REGISTRIES.put(loaderData.key(), new RegistrySynchronization.NetworkedRegistryData<>(loaderData.key(), data.networkCodec()));
|
|
|
|
|
}
|
2022-06-05 10:27:30 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-22 13:32:47 -06:00
|
|
|
/**
|
|
|
|
|
* {@return An unmodifiable view of the list of datapack registries}.
|
|
|
|
|
* These registries are loaded from per-world datapacks on server startup.
|
|
|
|
|
*/
|
|
|
|
|
public static List<RegistryDataLoader.RegistryData<?>> getDataPackRegistries()
|
2022-06-05 10:27:30 -04:00
|
|
|
{
|
2022-11-22 13:32:47 -06:00
|
|
|
return DATA_PACK_REGISTRIES_VIEW;
|
2022-06-05 10:27:30 -04:00
|
|
|
}
|
2022-11-22 13:32:47 -06:00
|
|
|
|
2022-12-26 13:19:36 -08:00
|
|
|
public static Stream<RegistryDataLoader.RegistryData<?>> getDataPackRegistriesWithDimensions() {
|
|
|
|
|
return Stream.concat(DATA_PACK_REGISTRIES_VIEW.stream(), RegistryDataLoader.DIMENSION_REGISTRIES.stream());
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-05 10:27:30 -04:00
|
|
|
/**
|
2022-11-22 13:32:47 -06:00
|
|
|
* {@return An unmodifiable view of the set of synced non-vanilla datapack registry IDs}
|
2022-06-05 10:27:30 -04:00
|
|
|
* 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;
|
|
|
|
|
}
|
2022-12-15 21:53:53 +02:00
|
|
|
}
|