Search in sources :

Example 1 with ResourceType

use of net.minecraft.resource.ResourceType in project fabric by FabricMC.

the class ModNioResourcePack method findResources.

@Override
public Collection<Identifier> findResources(ResourceType type, String path, int depth, Predicate<String> predicate) {
    List<Identifier> ids = new ArrayList<>();
    String nioPath = path.replace("/", separator);
    for (String namespace : getNamespaces(type)) {
        Path namespacePath = getPath(type.getName() + "/" + namespace);
        if (namespacePath != null) {
            Path searchPath = namespacePath.resolve(nioPath).toAbsolutePath().normalize();
            if (Files.exists(searchPath)) {
                try {
                    Files.walk(searchPath, depth).filter(Files::isRegularFile).filter((p) -> {
                        String filename = p.getFileName().toString();
                        return !filename.endsWith(".mcmeta") && predicate.test(filename);
                    }).map(namespacePath::relativize).map((p) -> p.toString().replace(separator, "/")).forEach((s) -> {
                        try {
                            ids.add(new Identifier(namespace, s));
                        } catch (InvalidIdentifierException e) {
                            LOGGER.error(e.getMessage());
                        }
                    });
                } catch (IOException e) {
                    LOGGER.warn("findResources at " + path + " in namespace " + namespace + ", mod " + modInfo.getId() + " failed!", e);
                }
            }
        }
    }
    return ids;
}
Also used : Path(java.nio.file.Path) java.util(java.util) Files(java.nio.file.Files) ModResourcePack(net.fabricmc.fabric.api.resource.ModResourcePack) Predicate(java.util.function.Predicate) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) DirectoryStream(java.nio.file.DirectoryStream) ModMetadata(net.fabricmc.loader.api.metadata.ModMetadata) InvalidIdentifierException(net.minecraft.util.InvalidIdentifierException) Logger(org.apache.logging.log4j.Logger) ResourceType(net.minecraft.resource.ResourceType) Identifier(net.minecraft.util.Identifier) Pattern(java.util.regex.Pattern) LogManager(org.apache.logging.log4j.LogManager) Path(java.nio.file.Path) AbstractFileResourcePack(net.minecraft.resource.AbstractFileResourcePack) InputStream(java.io.InputStream) Identifier(net.minecraft.util.Identifier) InvalidIdentifierException(net.minecraft.util.InvalidIdentifierException) IOException(java.io.IOException) Files(java.nio.file.Files)

Example 2 with ResourceType

use of net.minecraft.resource.ResourceType in project quilt-standard-libraries by QuiltMC.

the class ModNioResourcePack method findResources.

@Override
public Collection<Identifier> findResources(ResourceType type, String namespace, String path, int depth, Predicate<String> pathFilter) {
    var ids = new ArrayList<Identifier>();
    String nioPath = path.replace("/", separator);
    Path namespacePath = this.getPath(type.getDirectory() + "/" + namespace);
    if (namespacePath != null) {
        Path searchPath = namespacePath.resolve(nioPath).toAbsolutePath().normalize();
        if (Files.exists(searchPath)) {
            try {
                Files.walk(searchPath, depth).filter(Files::isRegularFile).filter((p) -> {
                    String filename = p.getFileName().toString();
                    return !filename.endsWith(".mcmeta") && pathFilter.test(filename);
                }).map(namespacePath::relativize).map((p) -> p.toString().replace(separator, "/")).forEach((s) -> {
                    try {
                        ids.add(new Identifier(namespace, s));
                    } catch (InvalidIdentifierException e) {
                        LOGGER.error(e.getMessage());
                    }
                });
            } catch (IOException e) {
                LOGGER.warn("findResources at " + path + " in namespace " + namespace + ", mod " + this.modInfo.getId() + " failed!", e);
            }
        }
    }
    return ids;
}
Also used : Path(java.nio.file.Path) LogUtils(com.mojang.logging.LogUtils) FabricLoader(net.fabricmc.loader.api.FabricLoader) LiteralText(net.minecraft.text.LiteralText) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) DirectoryStream(java.nio.file.DirectoryStream) ResourceType(net.minecraft.resource.ResourceType) Map(java.util.Map) Path(java.nio.file.Path) ApiStatus(org.jetbrains.annotations.ApiStatus) Logger(org.slf4j.Logger) Files(java.nio.file.Files) EnumMap(java.util.EnumMap) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) AbstractFileResourcePack(net.minecraft.resource.pack.AbstractFileResourcePack) FileNotFoundException(java.io.FileNotFoundException) Nullable(org.jetbrains.annotations.Nullable) ModMetadata(net.fabricmc.loader.api.metadata.ModMetadata) InvalidIdentifierException(net.minecraft.util.InvalidIdentifierException) Identifier(net.minecraft.util.Identifier) Text(net.minecraft.text.Text) Pattern(java.util.regex.Pattern) ResourcePackActivationType(org.quiltmc.qsl.resource.loader.api.ResourcePackActivationType) Collections(java.util.Collections) InputStream(java.io.InputStream) Identifier(net.minecraft.util.Identifier) ArrayList(java.util.ArrayList) InvalidIdentifierException(net.minecraft.util.InvalidIdentifierException) IOException(java.io.IOException) Files(java.nio.file.Files)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 DirectoryStream (java.nio.file.DirectoryStream)2 Files (java.nio.file.Files)2 Path (java.nio.file.Path)2 Predicate (java.util.function.Predicate)2 Pattern (java.util.regex.Pattern)2 ModMetadata (net.fabricmc.loader.api.metadata.ModMetadata)2 ResourceType (net.minecraft.resource.ResourceType)2 Identifier (net.minecraft.util.Identifier)2 InvalidIdentifierException (net.minecraft.util.InvalidIdentifierException)2 LogUtils (com.mojang.logging.LogUtils)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 EnumMap (java.util.EnumMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1