| 1 | package net.minecraft.resources;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.mojang.datafixers.util.Either;
|
| 5 | import com.mojang.serialization.DataResult;
|
| 6 | import com.mojang.serialization.Decoder;
|
| 7 | import com.mojang.serialization.Lifecycle;
|
| 8 | import java.io.Reader;
|
| 9 | import java.util.HashMap;
|
| 10 | import java.util.List;
|
| 11 | import java.util.Locale;
|
| 12 | import java.util.Map;
|
| 13 | import java.util.Optional;
|
| 14 | import java.util.concurrent.CompletableFuture;
|
| 15 | import java.util.concurrent.Executor;
|
| 16 | import java.util.stream.Stream;
|
| 17 | import net.minecraft.core.Holder;
|
| 18 | import net.minecraft.core.MappedRegistry;
|
| 19 | import net.minecraft.core.RegistrationInfo;
|
| 20 | import net.minecraft.core.Registry;
|
| 21 | import net.minecraft.core.WritableRegistry;
|
| 22 | import net.minecraft.core.registries.ConcurrentHolderGetter;
|
| 23 | import net.minecraft.nbt.Tag;
|
| 24 | import net.minecraft.server.packs.resources.Resource;
|
| 25 | import net.minecraft.server.packs.resources.ResourceProvider;
|
| 26 | import net.minecraft.tags.TagKey;
|
| 27 | import net.minecraft.util.StrictJsonParser;
|
| 28 |
|
| 29 | public abstract class RegistryLoadTask<T> {
|
| 30 | private final Object registryWriteLock = new Object();
|
| 31 | protected final RegistryDataLoader.RegistryData<T> data;
|
| 32 | private final WritableRegistry<T> registry;
|
| 33 | protected final ConcurrentHolderGetter<T> concurrentRegistrationGetter;
|
| 34 | protected final Map<ResourceKey<?>, Exception> loadingErrors;
|
| 35 | private volatile boolean elementsRegistered;
|
| 36 |
|
| 37 | protected RegistryLoadTask(final RegistryDataLoader.RegistryData<T> data, final Lifecycle lifecycle, final Map<ResourceKey<?>, Exception> loadingErrors) {
|
| 38 | this.data = data;
|
| 39 | this.registry = new MappedRegistry<>(data.key(), lifecycle);
|
| 40 | this.loadingErrors = loadingErrors;
|
| 41 | this.concurrentRegistrationGetter = new ConcurrentHolderGetter<>(this.registryWriteLock, this.registry.createRegistrationLookup());
|
| 42 | }
|
| 43 |
|
| 44 | protected ResourceKey<? extends Registry<T>> registryKey() {
|
| 45 | return this.registry.key();
|
| 46 | }
|
| 47 |
|
| 48 | protected Registry<T> readOnlyRegistry() {
|
| 49 | if (!this.elementsRegistered) {
|
| 50 | throw new IllegalStateException("Elements not registered");
|
| 51 | } else {
|
| 52 | return this.registry;
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | public abstract CompletableFuture<?> load(RegistryOps.RegistryInfoLookup context, Executor executor);
|
| 57 |
|
| 58 | protected void registerElements(final Stream<RegistryLoadTask.PendingRegistration<T>> elements) {
|
| 59 | synchronized (this.registryWriteLock) {
|
| 60 | elements.forEach(
|
| 61 | element -> element.value
|
| 62 | .ifLeft(value -> this.registry.register(element.key, (T)value, element.registrationInfo))
|
| 63 | .ifRight(error -> this.loadingErrors.put(element.key, error))
|
| 64 | );
|
| 65 | this.elementsRegistered = true;
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | protected void registerTags(final Map<TagKey<T>, List<Holder<T>>> pendingTags) {
|
| 70 | synchronized (this.registryWriteLock) {
|
| 71 | this.registry.bindTags(pendingTags);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | public boolean freezeRegistry(final Map<ResourceKey<?>, Exception> loadingErrors) {
|
| 76 | try {
|
| 77 | this.registry.freeze();
|
| 78 | return true;
|
| 79 | } catch (Exception e) {
|
| 80 | loadingErrors.put(this.registry.key(), e);
|
| 81 | return false;
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | public Optional<Registry<T>> validateRegistry(final Map<ResourceKey<?>, Exception> loadingErrors) {
|
| 86 | Map<ResourceKey<?>, Exception> registryErrors = new HashMap<>();
|
| 87 | this.data.validator().validate(this.registry, registryErrors);
|
| 88 | if (registryErrors.isEmpty()) {
|
| 89 | return Optional.of(this.registry);
|
| 90 | }
|
| 91 |
|
| 92 | loadingErrors.putAll(registryErrors);
|
| 93 | return Optional.empty();
|
| 94 | }
|
| 95 |
|
| 96 | protected record PendingRegistration<T>(ResourceKey<T> key, Either<T, Exception> value, RegistrationInfo registrationInfo) {
|
| 97 | public static <T> Either<T, Exception> loadFromResource(
|
| 98 | final Decoder<T> elementDecoder, final RegistryOps<JsonElement> ops, final ResourceKey<T> elementKey, final Resource thunk
|
| 99 | ) {
|
| 100 | try (Reader reader = thunk.openAsReader()) {
|
| 101 | JsonElement json = StrictJsonParser.parse(reader);
|
| 102 | return Either.left(elementDecoder.parse(ops, json).getOrThrow());
|
| 103 | } catch (Exception e) {
|
| 104 | return Either.right(
|
| 105 | new IllegalStateException(String.format(Locale.ROOT, "Failed to parse %s from pack %s", elementKey.identifier(), thunk.sourcePackId()), e)
|
| 106 | );
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | public static <T> Either<T, Exception> findAndLoadFromResource(
|
| 111 | final Decoder<T> elementDecoder,
|
| 112 | final RegistryOps<JsonElement> ops,
|
| 113 | final ResourceKey<T> elementKey,
|
| 114 | final FileToIdConverter converter,
|
| 115 | final ResourceProvider resourceProvider
|
| 116 | ) {
|
| 117 | Identifier resourceId = converter.idToFile(elementKey.identifier());
|
| 118 | return resourceProvider.getResource(resourceId)
|
| 119 | .map(resource -> loadFromResource(elementDecoder, ops, elementKey, resource))
|
| 120 | .orElseGet(
|
| 121 | () -> Either.right(new IllegalStateException(String.format(Locale.ROOT, "Failed to find resource %s for element %s", resourceId, elementKey.identifier())))
|
| 122 | );
|
| 123 | }
|
| 124 |
|
| 125 | public static <T> Either<T, Exception> loadFromNetwork(
|
| 126 | final Decoder<T> elementDecoder, final RegistryOps<Tag> ops, final ResourceKey<T> elementKey, final Tag contents
|
| 127 | ) {
|
| 128 | try {
|
| 129 | DataResult<T> parseResult = elementDecoder.parse(ops, contents);
|
| 130 | return Either.left(parseResult.getOrThrow());
|
| 131 | } catch (Exception e) {
|
| 132 | return Either.right(
|
| 133 | new IllegalStateException(String.format(Locale.ROOT, "Failed to parse value %s for key %s from server", contents, elementKey.identifier()), e)
|
| 134 | );
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 | }
|
| 139 |
|