From cf02b335976cc12349db18da1e754788ef92983d Mon Sep 17 00:00:00 2001 From: Apex Date: Wed, 25 Dec 2024 23:18:37 +0000 Subject: [PATCH] Drop `DataResourceManager` class type Construct data resource managers directly as `MultiPackResourceManager` --- .../common/data/DataResourceManager.java | 133 ------------------ .../neoforge/data/event/GatherDataEvent.java | 48 ++++++- .../neoforge/oldtest/DataGeneratorTest.java | 10 +- 3 files changed, 47 insertions(+), 144 deletions(-) delete mode 100644 src/main/java/net/neoforged/neoforge/common/data/DataResourceManager.java diff --git a/src/main/java/net/neoforged/neoforge/common/data/DataResourceManager.java b/src/main/java/net/neoforged/neoforge/common/data/DataResourceManager.java deleted file mode 100644 index 9d884f7e67..0000000000 --- a/src/main/java/net/neoforged/neoforge/common/data/DataResourceManager.java +++ /dev/null @@ -1,133 +0,0 @@ -/* - * Copyright (c) NeoForged and contributors - * SPDX-License-Identifier: LGPL-2.1-only - */ - -package net.neoforged.neoforge.common.data; - -import com.google.common.collect.Lists; -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.io.InputStream; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.function.Consumer; -import java.util.function.Predicate; -import java.util.stream.Stream; -import net.minecraft.client.resources.ClientPackSource; -import net.minecraft.client.resources.IndexedAssetSource; -import net.minecraft.network.chat.Component; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.packs.PackLocationInfo; -import net.minecraft.server.packs.PackResources; -import net.minecraft.server.packs.PackType; -import net.minecraft.server.packs.repository.PackSource; -import net.minecraft.server.packs.repository.ServerPacksSource; -import net.minecraft.server.packs.resources.MultiPackResourceManager; -import net.minecraft.server.packs.resources.Resource; -import net.minecraft.server.packs.resources.ResourceManager; -import net.neoforged.fml.ModList; -import net.neoforged.fml.loading.FMLEnvironment; -import net.neoforged.neoforge.resource.ResourcePackLoader; -import org.apache.commons.lang3.function.Consumers; -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Nullable; - -/** - * Simple {@link ResourceManager} implementation for looking up resources at runtime during data generation. - *

- * This resource manager can be used to lookup any asset/data file at runtime, assuming that the respective mod is loaded. - *

- * Do take note that under server data-gen runs, most of Minecraft's assets will be missing due to {@link ClientPackSource} being client only. - * - * @see ResourceManager - */ -public final class DataResourceManager implements ResourceManager { - private final ResourceManager delegate; - private final PackType packType; - - private DataResourceManager(PackType packType, Consumer> consumer) { - this.packType = packType; - - var packs = Lists.newArrayList(); - consumer.accept(packs::add); - packs.add(ServerPacksSource.createVanillaPackSource()); - - ModList.get().forEachModInOrder(mod -> { - var packInfo = new PackLocationInfo("mod/" + mod.getModId(), Component.empty(), PackSource.BUILT_IN, Optional.empty()); - var modPack = ResourcePackLoader.createPackForMod(mod.getModInfo().getOwningFile()).openPrimary(packInfo); - packs.add(modPack); - }); - - delegate = new MultiPackResourceManager(packType, packs); - } - - @ApiStatus.Internal - public DataResourceManager(PackType packType) { - this(packType, Consumers.nop()); - } - - public PackType packType() { - return packType; - } - - public boolean exists(ResourceLocation path) { - return getResource(path).isPresent(); - } - - @Override - public Optional getResource(ResourceLocation path) { - return delegate.getResource(path); - } - - @Override - public Resource getResourceOrThrow(ResourceLocation path) throws FileNotFoundException { - return delegate.getResourceOrThrow(path); - } - - @Override - public InputStream open(ResourceLocation path) throws IOException { - return delegate.open(path); - } - - @Override - public BufferedReader openAsReader(ResourceLocation path) throws IOException { - return delegate.openAsReader(path); - } - - @Override - public List getResourceStack(ResourceLocation path) { - return delegate.getResourceStack(path); - } - - @Override - public Map listResources(String path, Predicate filter) { - return delegate.listResources(path, filter); - } - - @Override - public Map> listResourceStacks(String path, Predicate filter) { - return delegate.listResourceStacks(path, filter); - } - - @Override - public Set getNamespaces() { - return delegate.getNamespaces(); - } - - @Override - public Stream listPacks() { - return delegate.listPacks(); - } - - public static DataResourceManager forClient(@Nullable String assetIndex, @Nullable File assetsDir) { - return new DataResourceManager(PackType.CLIENT_RESOURCES, consumer -> { - if (FMLEnvironment.dist.isClient() && assetIndex != null && assetsDir != null) - consumer.accept(ClientPackSource.createVanillaPackSource(IndexedAssetSource.createIndexFs(assetsDir.toPath(), assetIndex))); - }); - } -} diff --git a/src/main/java/net/neoforged/neoforge/data/event/GatherDataEvent.java b/src/main/java/net/neoforged/neoforge/data/event/GatherDataEvent.java index b28d1917b0..64906a2999 100644 --- a/src/main/java/net/neoforged/neoforge/data/event/GatherDataEvent.java +++ b/src/main/java/net/neoforged/neoforge/data/event/GatherDataEvent.java @@ -5,6 +5,7 @@ package net.neoforged.neoforge.data.event; +import com.google.common.collect.Lists; import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; @@ -14,23 +15,37 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.concurrent.CompletableFuture; +import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Predicate; import java.util.stream.Collectors; import net.minecraft.DetectedVersion; +import net.minecraft.client.resources.ClientPackSource; +import net.minecraft.client.resources.IndexedAssetSource; import net.minecraft.core.HolderLookup; import net.minecraft.data.DataGenerator; import net.minecraft.data.DataProvider; import net.minecraft.data.PackOutput; import net.minecraft.data.tags.TagsProvider; +import net.minecraft.network.chat.Component; +import net.minecraft.server.packs.PackLocationInfo; +import net.minecraft.server.packs.PackResources; import net.minecraft.server.packs.PackType; +import net.minecraft.server.packs.repository.PackSource; +import net.minecraft.server.packs.repository.ServerPacksSource; +import net.minecraft.server.packs.resources.MultiPackResourceManager; +import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; import net.neoforged.bus.api.Event; import net.neoforged.fml.ModContainer; +import net.neoforged.fml.ModList; import net.neoforged.fml.event.IModBusEvent; -import net.neoforged.neoforge.common.data.DataResourceManager; +import net.neoforged.fml.loading.FMLEnvironment; +import net.neoforged.neoforge.resource.ResourcePackLoader; import org.jetbrains.annotations.Nullable; public abstract class GatherDataEvent extends Event implements IModBusEvent { @@ -48,7 +63,7 @@ public ModContainer getModContainer() { return this.modContainer; } - public DataResourceManager getResourceManager(PackType packType) { + public ResourceManager getResourceManager(PackType packType) { return switch (packType) { case CLIENT_RESOURCES -> config.clientResourceManager; case SERVER_DATA -> config.serverResourceManager; @@ -101,8 +116,8 @@ public static class DataGeneratorConfig { private final boolean validate; private final boolean flat; private final List generators = new ArrayList<>(); - private final DataResourceManager clientResourceManager; - private final DataResourceManager serverResourceManager; + private final ResourceManager clientResourceManager; + private final ResourceManager serverResourceManager; public DataGeneratorConfig(final Set mods, final Path path, final Collection inputs, final CompletableFuture lookupProvider, final boolean dev, final boolean reports, final boolean validate, final boolean flat, final DataGenerator vanillaGenerator, final @Nullable String assetIndex, final @Nullable File assetsDir) { @@ -115,8 +130,12 @@ public DataGeneratorConfig(final Set mods, final Path path, final Collec this.validate = validate; this.flat = flat; - clientResourceManager = DataResourceManager.forClient(assetIndex, assetsDir); - serverResourceManager = new DataResourceManager(PackType.SERVER_DATA); + clientResourceManager = createResourceManager(PackType.CLIENT_RESOURCES, consumer -> { + if (FMLEnvironment.dist.isClient() && assetIndex != null && assetsDir != null) + consumer.accept(ClientPackSource.createVanillaPackSource(IndexedAssetSource.createIndexFs(assetsDir.toPath(), assetIndex))); + }); + + serverResourceManager = createResourceManager(PackType.SERVER_DATA, consumer -> consumer.accept(ServerPacksSource.createVanillaPackSource())); if (mods.contains("minecraft") || mods.isEmpty()) { this.generators.add(vanillaGenerator); @@ -156,6 +175,23 @@ public void runAll() { } }); } + + private static ResourceManager createResourceManager(PackType packType, Consumer> consumer) { + var packs = Lists.newArrayList(); + consumer.accept(packs::add); + + ModList.get().getSortedMods().stream() + // ignore 'minecraft' mod, this is added via `[Server|Client]PackSource` + .filter(Predicate.not(mod -> mod.getModId().equals("minecraft"))) + .map(mod -> { + var owningFile = mod.getModInfo().getOwningFile(); + var packInfo = new PackLocationInfo("mod/" + mod.getModId(), Component.empty(), PackSource.BUILT_IN, Optional.empty()); + return ResourcePackLoader.createPackForMod(owningFile).openPrimary(packInfo); + }) + .forEach(packs::add); + + return new MultiPackResourceManager(packType, packs); + } } public T addProvider(T provider) { diff --git a/tests/src/main/java/net/neoforged/neoforge/oldtest/DataGeneratorTest.java b/tests/src/main/java/net/neoforged/neoforge/oldtest/DataGeneratorTest.java index a147159b01..2f79733ec9 100644 --- a/tests/src/main/java/net/neoforged/neoforge/oldtest/DataGeneratorTest.java +++ b/tests/src/main/java/net/neoforged/neoforge/oldtest/DataGeneratorTest.java @@ -60,6 +60,7 @@ import net.minecraft.server.packs.PackType; import net.minecraft.server.packs.metadata.pack.PackMetadataSection; import net.minecraft.server.packs.resources.Resource; +import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.sounds.SoundEvents; import net.minecraft.tags.BlockTags; import net.minecraft.tags.ItemTags; @@ -89,7 +90,6 @@ import net.neoforged.neoforge.common.crafting.IntersectionIngredient; import net.neoforged.neoforge.common.data.AdvancementProvider; import net.neoforged.neoforge.common.data.BlockTagsProvider; -import net.neoforged.neoforge.common.data.DataResourceManager; import net.neoforged.neoforge.common.data.DatapackBuiltinEntriesProvider; import net.neoforged.neoforge.common.data.GeneratingOverlayMetadataSection; import net.neoforged.neoforge.common.data.LanguageProvider; @@ -284,9 +284,9 @@ public String getName() { public static class SoundDefinitions extends SoundDefinitionsProvider { private static final Logger LOGGER = LogManager.getLogger(); - private final DataResourceManager resourceManager; + private final ResourceManager resourceManager; - public SoundDefinitions(final PackOutput output, final DataResourceManager resourceManager) { + public SoundDefinitions(final PackOutput output, final ResourceManager resourceManager) { super(output, MODID); this.resourceManager = resourceManager; @@ -589,9 +589,9 @@ public void generate(HolderLookup.Provider registries, Consumer