| 1 | package net.minecraft.tags;
|
| 2 |
|
| 3 | import com.google.gson.JsonElement;
|
| 4 | import com.mojang.datafixers.util.Either;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.Dynamic;
|
| 7 | import com.mojang.serialization.JsonOps;
|
| 8 | import java.io.Reader;
|
| 9 | import java.util.ArrayList;
|
| 10 | import java.util.Collection;
|
| 11 | import java.util.HashMap;
|
| 12 | import java.util.LinkedHashSet;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Map;
|
| 15 | import java.util.Objects;
|
| 16 | import java.util.Optional;
|
| 17 | import java.util.SequencedSet;
|
| 18 | import java.util.Map.Entry;
|
| 19 | import java.util.function.Consumer;
|
| 20 | import java.util.stream.Collectors;
|
| 21 | import net.minecraft.core.Holder;
|
| 22 | import net.minecraft.core.HolderGetter;
|
| 23 | import net.minecraft.core.HolderLookup;
|
| 24 | import net.minecraft.core.Registry;
|
| 25 | import net.minecraft.core.RegistryAccess;
|
| 26 | import net.minecraft.core.WritableRegistry;
|
| 27 | import net.minecraft.core.registries.Registries;
|
| 28 | import net.minecraft.resources.FileToIdConverter;
|
| 29 | import net.minecraft.resources.Identifier;
|
| 30 | import net.minecraft.resources.ResourceKey;
|
| 31 | import net.minecraft.server.packs.resources.Resource;
|
| 32 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 33 | import net.minecraft.util.DependencySorter;
|
| 34 | import net.minecraft.util.StrictJsonParser;
|
| 35 | import org.jspecify.annotations.Nullable;
|
| 36 | import org.slf4j.Logger;
|
| 37 |
|
| 38 | public class TagLoader<T> {
|
| 39 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 40 | private final TagLoader.ElementLookup<T> elementLookup;
|
| 41 | private final String directory;
|
| 42 |
|
| 43 | public TagLoader(final TagLoader.ElementLookup<T> elementLookup, final String directory) {
|
| 44 | this.elementLookup = elementLookup;
|
| 45 | this.directory = directory;
|
| 46 | }
|
| 47 |
|
| 48 | public Map<Identifier, List<TagLoader.EntryWithSource>> load(final ResourceManager resourceManager) {
|
| 49 | Map<Identifier, List<TagLoader.EntryWithSource>> builders = new HashMap<>();
|
| 50 | FileToIdConverter lister = FileToIdConverter.json(this.directory);
|
| 51 |
|
| 52 | for (Entry<Identifier, List<Resource>> entry : lister.listMatchingResourceStacks(resourceManager).entrySet()) {
|
| 53 | Identifier location = entry.getKey();
|
| 54 | Identifier id = lister.fileToId(location);
|
| 55 |
|
| 56 | for (Resource resource : entry.getValue()) {
|
| 57 | try (Reader reader = resource.openAsReader()) {
|
| 58 | JsonElement element = StrictJsonParser.parse(reader);
|
| 59 | List<TagLoader.EntryWithSource> tagContents = builders.computeIfAbsent(id, key -> new ArrayList<>());
|
| 60 | TagFile parsedContents = TagFile.CODEC.parse(new Dynamic<>(JsonOps.INSTANCE, element)).getOrThrow();
|
| 61 | if (parsedContents.replace()) {
|
| 62 | tagContents.clear();
|
| 63 | }
|
| 64 |
|
| 65 | String sourceId = resource.sourcePackId();
|
| 66 | parsedContents.entries().forEach(ex -> tagContents.add(new TagLoader.EntryWithSource(ex, sourceId)));
|
| 67 | } catch (Exception e) {
|
| 68 | LOGGER.error("Couldn't read tag list {} from {} in data pack {}", id, location, resource.sourcePackId(), e);
|
| 69 | }
|
| 70 | }
|
| 71 | }
|
| 72 |
|
| 73 | return builders;
|
| 74 | }
|
| 75 |
|
| 76 | private Either<List<TagLoader.EntryWithSource>, List<T>> tryBuildTag(final TagEntry.Lookup<T> lookup, final List<TagLoader.EntryWithSource> entries) {
|
| 77 | SequencedSet<T> values = new LinkedHashSet<>();
|
| 78 | List<TagLoader.EntryWithSource> missingElements = new ArrayList<>();
|
| 79 |
|
| 80 | for (TagLoader.EntryWithSource entry : entries) {
|
| 81 | if (!entry.entry().build(lookup, values::add)) {
|
| 82 | missingElements.add(entry);
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | return missingElements.isEmpty() ? Either.right(List.copyOf(values)) : Either.left(missingElements);
|
| 87 | }
|
| 88 |
|
| 89 | public Map<Identifier, List<T>> build(final Map<Identifier, List<TagLoader.EntryWithSource>> builders) {
|
| 90 | final Map<Identifier, List<T>> newTags = new HashMap<>();
|
| 91 | TagEntry.Lookup<T> lookup = new TagEntry.Lookup<T>() {
|
| 92 | @Override
|
| 93 | public @Nullable T element(final Identifier key, final boolean required) {
|
| 94 | return (T)TagLoader.this.elementLookup.get(key, required).orElse(null);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public @Nullable Collection<T> tag(final Identifier key) {
|
| 99 | return newTags.get(key);
|
| 100 | }
|
| 101 | };
|
| 102 | DependencySorter<Identifier, TagLoader.SortingEntry> sorter = new DependencySorter<>();
|
| 103 | builders.forEach((id, entry) -> sorter.addEntry(id, new TagLoader.SortingEntry((List<TagLoader.EntryWithSource>)entry)));
|
| 104 | sorter.orderByDependencies(
|
| 105 | (id, contents) -> this.tryBuildTag(lookup, contents.entries)
|
| 106 | .ifLeft(
|
| 107 | missing -> LOGGER.error(
|
| 108 | "Couldn't load tag {} as it is missing following references: {}", id, missing.stream().map(Objects::toString).collect(Collectors.joining(", "))
|
| 109 | )
|
| 110 | )
|
| 111 | .ifRight(tag -> newTags.put(id, (List<T>)tag))
|
| 112 | );
|
| 113 | return newTags;
|
| 114 | }
|
| 115 |
|
| 116 | public static <T> Map<TagKey<T>, List<Holder<T>>> loadTagsFromNetwork(final TagNetworkSerialization.NetworkPayload tags, final Registry<T> registry) {
|
| 117 | return tags.resolve(registry).tags;
|
| 118 | }
|
| 119 |
|
| 120 | public static List<Registry.PendingTags<?>> loadTagsForExistingRegistries(final ResourceManager manager, final RegistryAccess layer) {
|
| 121 | return layer.registries().map(entry -> loadPendingTags(manager, entry.value())).flatMap(Optional::stream).collect(Collectors.toUnmodifiableList());
|
| 122 | }
|
| 123 |
|
| 124 | public static <T> void loadTagsForRegistry(final ResourceManager manager, final WritableRegistry<T> registry) {
|
| 125 | loadTagsForRegistry(manager, registry.key(), TagLoader.ElementLookup.fromWritableRegistry(registry));
|
| 126 | }
|
| 127 |
|
| 128 | public static <T> Map<TagKey<T>, List<Holder<T>>> loadTagsForRegistry(
|
| 129 | final ResourceManager manager, final ResourceKey<? extends Registry<T>> registryKey, final TagLoader.ElementLookup<Holder<T>> lookup
|
| 130 | ) {
|
| 131 | TagLoader<Holder<T>> loader = new TagLoader<>(lookup, Registries.tagsDirPath(registryKey));
|
| 132 | return wrapTags(registryKey, loader.build(loader.load(manager)));
|
| 133 | }
|
| 134 |
|
| 135 | private static <T> Map<TagKey<T>, List<Holder<T>>> wrapTags(final ResourceKey<? extends Registry<T>> registryKey, final Map<Identifier, List<Holder<T>>> tags) {
|
| 136 | return tags.entrySet().stream().collect(Collectors.toUnmodifiableMap(e -> TagKey.create(registryKey, e.getKey()), Entry::getValue));
|
| 137 | }
|
| 138 |
|
| 139 | private static <T> Optional<Registry.PendingTags<T>> loadPendingTags(final ResourceManager manager, final Registry<T> registry) {
|
| 140 | ResourceKey<? extends Registry<T>> key = registry.key();
|
| 141 | TagLoader<Holder<T>> loader = new TagLoader<>(
|
| 142 | (TagLoader.ElementLookup<Holder<T>>)TagLoader.ElementLookup.fromFrozenRegistry(registry), Registries.tagsDirPath(key)
|
| 143 | );
|
| 144 | TagLoader.LoadResult<T> tags = new TagLoader.LoadResult<>(key, wrapTags(registry.key(), loader.build(loader.load(manager))));
|
| 145 | return tags.tags().isEmpty() ? Optional.empty() : Optional.of(registry.prepareTagReload(tags));
|
| 146 | }
|
| 147 |
|
| 148 | public static List<HolderLookup.RegistryLookup<?>> buildUpdatedLookups(final RegistryAccess.Frozen registries, final List<Registry.PendingTags<?>> tags) {
|
| 149 | List<HolderLookup.RegistryLookup<?>> result = new ArrayList<>();
|
| 150 | registries.registries().forEach(lookup -> {
|
| 151 | Registry.PendingTags<?> foundTags = findTagsForRegistry(tags, lookup.key());
|
| 152 | result.add(foundTags != null ? foundTags.lookup() : lookup.value());
|
| 153 | });
|
| 154 | return result;
|
| 155 | }
|
| 156 |
|
| 157 | private static Registry.@Nullable PendingTags<?> findTagsForRegistry(
|
| 158 | final List<Registry.PendingTags<?>> tags, final ResourceKey<? extends Registry<?>> registryKey
|
| 159 | ) {
|
| 160 | for (Registry.PendingTags<?> tag : tags) {
|
| 161 | if (tag.key() == registryKey) {
|
| 162 | return tag;
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | return null;
|
| 167 | }
|
| 168 |
|
| 169 | public interface ElementLookup<T> {
|
| 170 | Optional<? extends T> get(Identifier id, boolean required);
|
| 171 |
|
| 172 | static <T> TagLoader.ElementLookup<? extends Holder<T>> fromFrozenRegistry(final Registry<T> registry) {
|
| 173 | return (id, required) -> registry.get(id);
|
| 174 | }
|
| 175 |
|
| 176 | static <T> TagLoader.ElementLookup<Holder<T>> fromWritableRegistry(final WritableRegistry<T> registry) {
|
| 177 | return fromGetters(registry.key(), registry.createRegistrationLookup(), registry);
|
| 178 | }
|
| 179 |
|
| 180 | static <T> TagLoader.ElementLookup<Holder<T>> fromGetters(
|
| 181 | final ResourceKey<? extends Registry<T>> registryKey, final HolderGetter<T> writable, final HolderGetter<T> immutable
|
| 182 | ) {
|
| 183 | return (id, required) -> (required ? writable : immutable).get(ResourceKey.create(registryKey, id));
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | public record EntryWithSource(TagEntry entry, String source) {
|
| 188 | @Override
|
| 189 | public String toString() {
|
| 190 | return this.entry + " (from " + this.source + ")";
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | public record LoadResult<T>(ResourceKey<? extends Registry<T>> key, Map<TagKey<T>, List<Holder<T>>> tags) {
|
| 195 | }
|
| 196 |
|
| 197 | private record SortingEntry(List<TagLoader.EntryWithSource> entries) implements DependencySorter.Entry<Identifier> {
|
| 198 | @Override
|
| 199 | public void visitRequiredDependencies(final Consumer<Identifier> output) {
|
| 200 | this.entries.forEach(e -> e.entry.visitRequiredDependencies(output));
|
| 201 | }
|
| 202 |
|
| 203 | @Override
|
| 204 | public void visitOptionalDependencies(final Consumer<Identifier> output) {
|
| 205 | this.entries.forEach(e -> e.entry.visitOptionalDependencies(output));
|
| 206 | }
|
| 207 | }
|
| 208 | }
|
| 209 |
|