Skip to content

Commit

Permalink
Drop DataResourceManager class type
Browse files Browse the repository at this point in the history
Construct data resource managers directly as `MultiPackResourceManager`
  • Loading branch information
ApexModder committed Dec 25, 2024
1 parent 0a62ad0 commit cf02b33
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 144 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -101,8 +116,8 @@ public static class DataGeneratorConfig {
private final boolean validate;
private final boolean flat;
private final List<DataGenerator> generators = new ArrayList<>();
private final DataResourceManager clientResourceManager;
private final DataResourceManager serverResourceManager;
private final ResourceManager clientResourceManager;
private final ResourceManager serverResourceManager;

public DataGeneratorConfig(final Set<String> mods, final Path path, final Collection<Path> inputs, final CompletableFuture<HolderLookup.Provider> lookupProvider,
final boolean dev, final boolean reports, final boolean validate, final boolean flat, final DataGenerator vanillaGenerator, final @Nullable String assetIndex, final @Nullable File assetsDir) {
Expand All @@ -115,8 +130,12 @@ public DataGeneratorConfig(final Set<String> 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);
Expand Down Expand Up @@ -156,6 +175,23 @@ public void runAll() {
}
});
}

private static ResourceManager createResourceManager(PackType packType, Consumer<Consumer<PackResources>> consumer) {
var packs = Lists.<PackResources>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 extends DataProvider> T addProvider(T provider) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -589,9 +589,9 @@ public void generate(HolderLookup.Provider registries, Consumer<AdvancementHolde
}

private static class ParticleDescriptions extends ParticleDescriptionProvider {
private final DataResourceManager resourceManager;
private final ResourceManager resourceManager;

public ParticleDescriptions(PackOutput output, DataResourceManager resourceManager) {
public ParticleDescriptions(PackOutput output, ResourceManager resourceManager) {
super(output);

this.resourceManager = resourceManager;
Expand Down

0 comments on commit cf02b33

Please sign in to comment.