| 1 | package net.minecraft.core;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.google.common.collect.ImmutableMap.Builder;
|
| 5 | import com.mojang.serialization.Lifecycle;
|
| 6 | import java.util.ArrayList;
|
| 7 | import java.util.HashMap;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Map;
|
| 10 | import java.util.Objects;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.stream.Collectors;
|
| 13 | import java.util.stream.Stream;
|
| 14 | import net.minecraft.core.registries.BootstrapRegistry;
|
| 15 | import net.minecraft.core.registries.EmptyTagLookupWrapper;
|
| 16 | import net.minecraft.core.registries.MultiRegistryBootstrap;
|
| 17 | import net.minecraft.core.registries.PatchedRegistry;
|
| 18 | import net.minecraft.core.registries.SingleRegistryBootstrap;
|
| 19 | import net.minecraft.data.worldgen.BootstrapContext;
|
| 20 | import net.minecraft.resources.ResourceKey;
|
| 21 |
|
| 22 | public class RegistrySetBuilder {
|
| 23 | private final List<RegistrySetBuilder.RegistryStub> entries = new ArrayList<>();
|
| 24 |
|
| 25 | private static RegistrySetBuilder.RegistryStub placeholderStub(final ResourceKey<? extends Registry<?>> key) {
|
| 26 | return new RegistrySetBuilder.RegistryStub() {
|
| 27 | @Override
|
| 28 | public Stream<ResourceKey<? extends Registry<?>>> requiredRegistries() {
|
| 29 | return Stream.of(key);
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public void apply(final RegistrySetBuilder.BuildState state) {
|
| 34 | }
|
| 35 | };
|
| 36 | }
|
| 37 |
|
| 38 | public <T> RegistrySetBuilder add(final ResourceKey<? extends Registry<T>> key, final SingleRegistryBootstrap<T> bootstrap) {
|
| 39 | this.entries.add(new RegistrySetBuilder.RegistryStub() {
|
| 40 | @Override
|
| 41 | public Stream<ResourceKey<? extends Registry<?>>> requiredRegistries() {
|
| 42 | return Stream.of(key);
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public void apply(final RegistrySetBuilder.BuildState state) {
|
| 47 | bootstrap.run(state.createBootstrap(key));
|
| 48 | }
|
| 49 | });
|
| 50 | return this;
|
| 51 | }
|
| 52 |
|
| 53 | public RegistrySetBuilder add(final MultiRegistryBootstrap bootstrap) {
|
| 54 | this.entries.add(new RegistrySetBuilder.RegistryStub() {
|
| 55 | @Override
|
| 56 | public Stream<ResourceKey<? extends Registry<?>>> requiredRegistries() {
|
| 57 | return bootstrap.requestedRegistries().stream();
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public void apply(final RegistrySetBuilder.BuildState state) {
|
| 62 | bootstrap.run(state::createBootstrap);
|
| 63 | }
|
| 64 | });
|
| 65 | return this;
|
| 66 | }
|
| 67 |
|
| 68 | private static HolderLookup.Provider buildProviderWithContext(
|
| 69 | final HolderLookup.Provider context, final Stream<? extends HolderLookup.RegistryLookup<?>> newRegistries
|
| 70 | ) {
|
| 71 | Map<ResourceKey<? extends Registry<?>>, HolderLookup.RegistryLookup<?>> lookups = new HashMap<>();
|
| 72 | context.listRegistries()
|
| 73 | .forEach(contextRegistry -> lookups.put(contextRegistry.key(), EmptyTagLookupWrapper.wrap((HolderLookup.RegistryLookup<?>)contextRegistry)));
|
| 74 | newRegistries.forEach(newRegistry -> lookups.put(newRegistry.key(), EmptyTagLookupWrapper.wrap((HolderLookup.RegistryLookup<?>)newRegistry)));
|
| 75 | return HolderLookup.Provider.create(lookups.values().stream());
|
| 76 | }
|
| 77 |
|
| 78 | public HolderLookup.Provider build(final HolderLookup.Provider context) {
|
| 79 | RegistrySetBuilder.BuildState state = RegistrySetBuilder.BuildState.createAndApply(context, this.entries);
|
| 80 | List<HolderLookup.RegistryLookup<?>> bootstrappedRegistries = new ArrayList<>(state.bootstrappedRegistries.size());
|
| 81 |
|
| 82 | for (RegistrySetBuilder.BootstrappedRegistryState<?> newRegistry : state.bootstrappedRegistries.values()) {
|
| 83 | newRegistry.bindHolders();
|
| 84 | newRegistry.freeze();
|
| 85 | newRegistry.errorOnMissingHolders(state);
|
| 86 | bootstrappedRegistries.add(newRegistry.registry());
|
| 87 | }
|
| 88 |
|
| 89 | state.throwOnError();
|
| 90 | return buildProviderWithContext(context, bootstrappedRegistries.stream());
|
| 91 | }
|
| 92 |
|
| 93 | private static Set<ResourceKey<? extends Registry<?>>> findRegistriesMissingFromPatch(
|
| 94 | final HolderLookup.Provider contextRegistries, final HolderLookup.Provider baseRegistries, final List<RegistrySetBuilder.RegistryStub> entries
|
| 95 | ) {
|
| 96 | Set<? extends ResourceKey<? extends Registry<?>>> existingKeys = Stream.concat(newRegistryKeys(entries.stream()), contextRegistries.listRegistryKeys())
|
| 97 | .collect(Collectors.toSet());
|
| 98 | return baseRegistries.listRegistryKeys().filter(e -> !existingKeys.contains(e)).collect(Collectors.toSet());
|
| 99 | }
|
| 100 |
|
| 101 | public RegistrySetBuilder.PatchedRegistries buildPatch(
|
| 102 | final HolderLookup.Provider context, final HolderLookup.Provider fallbackProvider, final Cloner.Factory clonerFactory
|
| 103 | ) {
|
| 104 | Set<ResourceKey<? extends Registry<?>>> missingFromPatch = findRegistriesMissingFromPatch(context, fallbackProvider, this.entries);
|
| 105 | List<RegistrySetBuilder.RegistryStub> expandedEntries = Stream.concat(
|
| 106 | this.entries.stream(), missingFromPatch.stream().map(RegistrySetBuilder::placeholderStub)
|
| 107 | )
|
| 108 | .toList();
|
| 109 | RegistrySetBuilder.BuildState state = RegistrySetBuilder.BuildState.createAndApply(context, expandedEntries);
|
| 110 | List<HolderLookup.RegistryLookup<?>> bootstrappedRegistries = new ArrayList<>(state.bootstrappedRegistries.size());
|
| 111 |
|
| 112 | for (RegistrySetBuilder.BootstrappedRegistryState<?> newRegistry : state.bootstrappedRegistries.values()) {
|
| 113 | newRegistry.bindHolders();
|
| 114 | newRegistry.validatePatchHolders(state, fallbackProvider);
|
| 115 | newRegistry.freeze();
|
| 116 | bootstrappedRegistries.add(newRegistry.registry());
|
| 117 | }
|
| 118 |
|
| 119 | HolderLookup.Provider patchOnlyRegistries = buildProviderWithContext(context, bootstrappedRegistries.stream());
|
| 120 | state.throwOnError();
|
| 121 | HolderLookup.Provider fullPatchedRegistries = EmptyTagLookupWrapper.wrap(
|
| 122 | PatchedRegistry.applyPatches(context, fallbackProvider, patchOnlyRegistries, clonerFactory, state.bootstrappedRegistries.keySet())
|
| 123 | );
|
| 124 | return new RegistrySetBuilder.PatchedRegistries(fullPatchedRegistries, patchOnlyRegistries);
|
| 125 | }
|
| 126 |
|
| 127 | private static <T> ResourceKey<? extends Registry<T>> eyerollCast(final ResourceKey<? extends Registry<? extends T>> registryKey) {
|
| 128 | return (ResourceKey<? extends Registry<T>>)registryKey;
|
| 129 | }
|
| 130 |
|
| 131 | private static Stream<ResourceKey<? extends Registry<?>>> newRegistryKeys(final Stream<RegistrySetBuilder.RegistryStub> entries) {
|
| 132 | return entries.flatMap(RegistrySetBuilder.RegistryStub::requiredRegistries).distinct();
|
| 133 | }
|
| 134 |
|
| 135 | private record BootstrappedRegistryState<T>(BootstrapRegistry<T> registry, Map<ResourceKey<T>, T> registeredValues) {
|
| 136 | public static <T> RegistrySetBuilder.BootstrappedRegistryState<T> create(final ResourceKey<? extends Registry<T>> key, final Lifecycle lifecycle) {
|
| 137 | BootstrapRegistry<T> newRegistry = new BootstrapRegistry<>(key, lifecycle);
|
| 138 | return new RegistrySetBuilder.BootstrappedRegistryState<>(newRegistry, new HashMap<>());
|
| 139 | }
|
| 140 |
|
| 141 | public BootstrapContext<T> createBootstrapContext(final RegistrySetBuilder.BuildState state) {
|
| 142 | return new BootstrapContext<T>() {
|
| 143 | @Override
|
| 144 | public Holder.Reference<T> register(final ResourceKey<T> key, final T value) {
|
| 145 | T previousValue = BootstrappedRegistryState.this.registeredValues.put(key, value);
|
| 146 | if (previousValue != null) {
|
| 147 | state.errors.add(new IllegalStateException("Duplicate registration for " + key + ", new=" + value + ", old=" + previousValue));
|
| 148 | }
|
| 149 |
|
| 150 | return BootstrappedRegistryState.this.registry.getOrThrow(key);
|
| 151 | }
|
| 152 |
|
| 153 | @Override
|
| 154 | public <S> HolderGetter<S> lookup(final ResourceKey<? extends Registry<? extends S>> key) {
|
| 155 | return state.allRegistries.lookupOrThrow(key);
|
| 156 | }
|
| 157 |
|
| 158 | @Override
|
| 159 | public <S> Stream<Holder.Reference<S>> listContextElements(final ResourceKey<? extends Registry<? extends S>> key) {
|
| 160 | return state.contextRegistries.lookupOrThrow(key).listElements();
|
| 161 | }
|
| 162 | };
|
| 163 | }
|
| 164 |
|
| 165 | public void bindHolders() {
|
| 166 | this.registeredValues.forEach((key, value) -> this.registry.getOrThrow((ResourceKey<T>)key).bindValue((T)value));
|
| 167 | }
|
| 168 |
|
| 169 | public void freeze() {
|
| 170 | this.registry.freeze();
|
| 171 | }
|
| 172 |
|
| 173 | public void errorOnMissingHolders(final RegistrySetBuilder.BuildState state) {
|
| 174 | this.registry.listElements().forEach(element -> {
|
| 175 | if (!element.isBound()) {
|
| 176 | state.errors().add(new IllegalStateException("No value registered for key " + element.key().identifier()));
|
| 177 | }
|
| 178 | });
|
| 179 | }
|
| 180 |
|
| 181 | public void validatePatchHolders(final RegistrySetBuilder.BuildState state, final HolderLookup.Provider fallback) {
|
| 182 | HolderLookup<T> baseRegistry = fallback.lookupOrThrow(this.registry.key());
|
| 183 | this.registry.removeIf(element -> {
|
| 184 | if (element.isBound()) {
|
| 185 | return false;
|
| 186 | }
|
| 187 |
|
| 188 | if (baseRegistry.get(element.key()).isEmpty()) {
|
| 189 | state.errors().add(new IllegalStateException("Value " + element.key().identifier() + " referenced by patched element is not present in base"));
|
| 190 | }
|
| 191 |
|
| 192 | return true;
|
| 193 | });
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | private record BuildState(
|
| 198 | HolderLookup.Provider contextRegistries,
|
| 199 | HolderLookup.Provider allRegistries,
|
| 200 | Map<ResourceKey<? extends Registry<?>>, RegistrySetBuilder.BootstrappedRegistryState<?>> bootstrappedRegistries,
|
| 201 | List<RuntimeException> errors
|
| 202 | ) {
|
| 203 | public static RegistrySetBuilder.BuildState createAndApply(final HolderLookup.Provider context, final List<RegistrySetBuilder.RegistryStub> entries) {
|
| 204 | RegistrySetBuilder.BuildState state = create(context, entries);
|
| 205 | entries.forEach(e -> e.apply(state));
|
| 206 | return state;
|
| 207 | }
|
| 208 |
|
| 209 | private static RegistrySetBuilder.BuildState create(final HolderLookup.Provider context, final List<RegistrySetBuilder.RegistryStub> entries) {
|
| 210 | List<RuntimeException> errors = new ArrayList<>();
|
| 211 | Builder<ResourceKey<? extends Registry<?>>, HolderLookup.RegistryLookup<?>> allRegistries = ImmutableMap.builder();
|
| 212 | Builder<ResourceKey<? extends Registry<?>>, RegistrySetBuilder.BootstrappedRegistryState<?>> bootstrappedRegistries = ImmutableMap.builder();
|
| 213 | context.listRegistries()
|
| 214 | .forEach(contextRegistry -> allRegistries.put(contextRegistry.key(), EmptyTagLookupWrapper.wrap((HolderLookup.RegistryLookup<?>)contextRegistry)));
|
| 215 | RegistrySetBuilder.newRegistryKeys(entries.stream())
|
| 216 | .forEach(
|
| 217 | newRegistryKey -> {
|
| 218 | RegistrySetBuilder.BootstrappedRegistryState<?> newRegistryEntry = RegistrySetBuilder.BootstrappedRegistryState.create(
|
| 219 | RegistrySetBuilder.eyerollCast((ResourceKey<? extends Registry<?>>)newRegistryKey), Lifecycle.stable()
|
| 220 | );
|
| 221 | BootstrapRegistry<?> newRegistry = newRegistryEntry.registry();
|
| 222 | bootstrappedRegistries.put(newRegistry.key(), newRegistryEntry);
|
| 223 | allRegistries.put(newRegistry.key(), newRegistry);
|
| 224 | }
|
| 225 | );
|
| 226 | return new RegistrySetBuilder.BuildState(
|
| 227 | context, HolderLookup.Provider.create(allRegistries.build().values().stream()), bootstrappedRegistries.build(), errors
|
| 228 | );
|
| 229 | }
|
| 230 |
|
| 231 | public void throwOnError() {
|
| 232 | if (!this.errors.isEmpty()) {
|
| 233 | IllegalStateException result = new IllegalStateException("Errors during registry creation");
|
| 234 |
|
| 235 | for (RuntimeException error : this.errors) {
|
| 236 | result.addSuppressed(error);
|
| 237 | }
|
| 238 |
|
| 239 | throw result;
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | public <T> BootstrapContext<T> createBootstrap(final ResourceKey<? extends Registry<T>> key) {
|
| 244 | RegistrySetBuilder.BootstrappedRegistryState<T> targetRegistry = Objects.requireNonNull(
|
| 245 | (RegistrySetBuilder.BootstrappedRegistryState<T>)this.bootstrappedRegistries.get(key), () -> "No registry named " + key.identifier()
|
| 246 | );
|
| 247 | return targetRegistry.createBootstrapContext(this);
|
| 248 | }
|
| 249 | }
|
| 250 |
|
| 251 | public record PatchedRegistries(HolderLookup.Provider full, HolderLookup.Provider patches) {
|
| 252 | }
|
| 253 |
|
| 254 | private interface RegistryStub {
|
| 255 | Stream<ResourceKey<? extends Registry<?>>> requiredRegistries();
|
| 256 |
|
| 257 | void apply(RegistrySetBuilder.BuildState state);
|
| 258 | }
|
| 259 | }
|
| 260 |
|