| 1 | package net.minecraft.core.registries;
|
| 2 |
|
| 3 | import com.mojang.serialization.Lifecycle;
|
| 4 | import java.util.HashMap;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import java.util.Optional;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.function.Supplier;
|
| 10 | import java.util.stream.Collectors;
|
| 11 | import java.util.stream.Stream;
|
| 12 | import net.minecraft.core.Cloner;
|
| 13 | import net.minecraft.core.Holder;
|
| 14 | import net.minecraft.core.HolderLookup;
|
| 15 | import net.minecraft.core.HolderOwner;
|
| 16 | import net.minecraft.core.HolderSet;
|
| 17 | import net.minecraft.core.Registry;
|
| 18 | import net.minecraft.resources.ResourceKey;
|
| 19 | import net.minecraft.tags.TagKey;
|
| 20 | import org.apache.commons.lang3.mutable.MutableObject;
|
| 21 | import org.jspecify.annotations.Nullable;
|
| 22 |
|
| 23 | public class PatchedRegistry<T> implements HolderLookup.RegistryLookup<T> {
|
| 24 | private final ResourceKey<? extends Registry<? extends T>> key;
|
| 25 | private final Lifecycle lifecycle;
|
| 26 | private final Map<ResourceKey<T>, Holder.Reference<T>> entries = new HashMap<>();
|
| 27 |
|
| 28 | private PatchedRegistry(final ResourceKey<? extends Registry<? extends T>> key, final Lifecycle lifecycle) {
|
| 29 | this.key = key;
|
| 30 | this.lifecycle = lifecycle;
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public ResourceKey<? extends Registry<? extends T>> key() {
|
| 35 | return this.key;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public Lifecycle registryLifecycle() {
|
| 40 | return this.lifecycle;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public Stream<Holder.Reference<T>> listElements() {
|
| 45 | return this.entries.values().stream();
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public Optional<Holder.Reference<T>> get(final ResourceKey<T> id) {
|
| 50 | return Optional.ofNullable(this.entries.get(id));
|
| 51 | }
|
| 52 |
|
| 53 | @Override
|
| 54 | public Stream<HolderSet.Named<T>> listTags() {
|
| 55 | throw new UnsupportedOperationException("Tags cloning is not supported");
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public Optional<HolderSet.Named<T>> get(final TagKey<T> id) {
|
| 60 | return Optional.of(HolderSet.emptyNamed(this, id));
|
| 61 | }
|
| 62 |
|
| 63 | private static <T> HolderLookup.RegistryLookup<T> createLazyFullPatchedRegistries(
|
| 64 | final Cloner.Factory clonerFactory,
|
| 65 | final ResourceKey<? extends Registry<? extends T>> registryKey,
|
| 66 | final HolderLookup.Provider baseProvider,
|
| 67 | final HolderLookup.Provider patchProvider,
|
| 68 | final MutableObject<HolderLookup.Provider> clonedRegistriesProvider
|
| 69 | ) {
|
| 70 | Cloner<T> cloner = clonerFactory.cloner(registryKey);
|
| 71 | if (cloner == null) {
|
| 72 | throw new NullPointerException("No cloner for " + registryKey.identifier());
|
| 73 | }
|
| 74 |
|
| 75 | HolderLookup.RegistryLookup<T> patchContents = patchProvider.lookupOrThrow(registryKey);
|
| 76 | HolderLookup.RegistryLookup<T> baseContents = baseProvider.lookupOrThrow(registryKey);
|
| 77 | Lifecycle lifecycle = patchContents.registryLifecycle().add(baseContents.registryLifecycle());
|
| 78 | PatchedRegistry<T> result = new PatchedRegistry<>(registryKey, lifecycle);
|
| 79 | patchContents.listElements().forEach(elementHolder -> {
|
| 80 | ResourceKey<T> elementKey = elementHolder.key();
|
| 81 | PatchedRegistry.LazyHolder<T> holder = new PatchedRegistry.LazyHolder<>(result, elementKey);
|
| 82 | holder.supplier = () -> cloner.clone((T)elementHolder.value(), patchProvider, clonedRegistriesProvider.get());
|
| 83 | result.entries.put(elementKey, holder);
|
| 84 | });
|
| 85 | baseContents.listElements().forEach(elementHolder -> {
|
| 86 | ResourceKey<T> elementKey = elementHolder.key();
|
| 87 | result.entries.computeIfAbsent(elementKey, key -> {
|
| 88 | PatchedRegistry.LazyHolder<T> holder = new PatchedRegistry.LazyHolder<>(result, elementKey);
|
| 89 | holder.supplier = () -> cloner.clone((T)elementHolder.value(), baseProvider, clonedRegistriesProvider.get());
|
| 90 | return holder;
|
| 91 | });
|
| 92 | });
|
| 93 | return result;
|
| 94 | }
|
| 95 |
|
| 96 | public static HolderLookup.Provider applyPatches(
|
| 97 | final HolderLookup.Provider context,
|
| 98 | final HolderLookup.Provider baseRegistries,
|
| 99 | final HolderLookup.Provider patchRegistries,
|
| 100 | final Cloner.Factory clonerFactory,
|
| 101 | final Set<ResourceKey<? extends Registry<?>>> registriesToClone
|
| 102 | ) {
|
| 103 | MutableObject<HolderLookup.Provider> resultHolder = new MutableObject<>();
|
| 104 | List<HolderLookup.RegistryLookup<?>> lazyFullRegistries = registriesToClone.stream()
|
| 105 | .map(
|
| 106 | registryKey -> createLazyFullPatchedRegistries(
|
| 107 | clonerFactory, (ResourceKey<? extends Registry<?>>)registryKey, baseRegistries, patchRegistries, resultHolder
|
| 108 | )
|
| 109 | )
|
| 110 | .collect(Collectors.toUnmodifiableList());
|
| 111 | HolderLookup.Provider result = HolderLookup.Provider.create(Stream.concat(context.listRegistries(), lazyFullRegistries.stream()));
|
| 112 | resultHolder.setValue(result);
|
| 113 | return result;
|
| 114 | }
|
| 115 |
|
| 116 | private static class LazyHolder<T> extends Holder.Reference<T> {
|
| 117 | private @Nullable Supplier<T> supplier;
|
| 118 |
|
| 119 | protected LazyHolder(final HolderOwner<T> owner, final @Nullable ResourceKey<T> key) {
|
| 120 | super(Holder.Reference.Type.STAND_ALONE, owner, key, null);
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | protected void bindValue(final T value) {
|
| 125 | super.bindValue(value);
|
| 126 | this.supplier = null;
|
| 127 | }
|
| 128 |
|
| 129 | @Override
|
| 130 | public T value() {
|
| 131 | if (this.supplier != null) {
|
| 132 | this.bindValue(this.supplier.get());
|
| 133 | }
|
| 134 |
|
| 135 | return super.value();
|
| 136 | }
|
| 137 | }
|
| 138 | }
|
| 139 |
|