package net.minecraft.server.packs; import com.mojang.logging.LogUtils; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.LinkOption; import java.nio.file.Path; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.function.Consumer; import net.minecraft.FileUtil; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.packs.metadata.MetadataSectionType; import net.minecraft.server.packs.resources.IoSupplier; import net.minecraft.server.packs.resources.Resource; import net.minecraft.server.packs.resources.ResourceProvider; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; public class VanillaPackResources implements PackResources { private static final Logger LOGGER = LogUtils.getLogger(); private final PackLocationInfo location; private final BuiltInMetadata metadata; private final Set namespaces; private final List rootPaths; private final Map> pathsForType; VanillaPackResources(PackLocationInfo location, BuiltInMetadata metadata, Set namespaces, List rootPaths, Map> pathsForType) { this.location = location; this.metadata = metadata; this.namespaces = namespaces; this.rootPaths = rootPaths; this.pathsForType = pathsForType; } @Nullable @Override public IoSupplier getRootResource(String... elements) { FileUtil.validatePath(elements); List list = List.of(elements); for (Path path : this.rootPaths) { Path path2 = FileUtil.resolvePath(path, list); if (Files.exists(path2, new LinkOption[0]) && PathPackResources.validatePath(path2)) { return IoSupplier.create(path2); } } return null; } public void listRawPaths(PackType packType, ResourceLocation packLocation, Consumer output) { FileUtil.decomposePath(packLocation.getPath()).ifSuccess(list -> { String string = packLocation.getNamespace(); for (Path path : (List)this.pathsForType.get(packType)) { Path path2 = path.resolve(string); output.accept(FileUtil.resolvePath(path2, list)); } }).ifError(error -> LOGGER.error("Invalid path {}: {}", packLocation, error.message())); } @Override public void listResources(PackType packType, String namespace, String path, PackResources.ResourceOutput resourceOutput) { FileUtil.decomposePath(path).ifSuccess(list -> { List list2 = (List)this.pathsForType.get(packType); int i = list2.size(); if (i == 1) { getResources(resourceOutput, namespace, (Path)list2.get(0), list); } else if (i > 1) { Map> map = new HashMap(); for (int j = 0; j < i - 1; j++) { getResources(map::putIfAbsent, namespace, (Path)list2.get(j), list); } Path pathx = (Path)list2.get(i - 1); if (map.isEmpty()) { getResources(resourceOutput, namespace, pathx, list); } else { getResources(map::putIfAbsent, namespace, pathx, list); map.forEach(resourceOutput); } } }).ifError(error -> LOGGER.error("Invalid path {}: {}", path, error.message())); } private static void getResources(PackResources.ResourceOutput resourceOutput, String namespace, Path root, List paths) { Path path = root.resolve(namespace); PathPackResources.listPath(namespace, path, paths, resourceOutput); } @Nullable @Override public IoSupplier getResource(PackType packType, ResourceLocation location) { return FileUtil.decomposePath(location.getPath()).mapOrElse(list -> { String string = location.getNamespace(); for (Path path : (List)this.pathsForType.get(packType)) { Path path2 = FileUtil.resolvePath(path.resolve(string), list); if (Files.exists(path2, new LinkOption[0]) && PathPackResources.validatePath(path2)) { return IoSupplier.create(path2); } } return null; }, error -> { LOGGER.error("Invalid path {}: {}", location, error.message()); return null; }); } @Override public Set getNamespaces(PackType type) { return this.namespaces; } @Nullable @Override public T getMetadataSection(MetadataSectionType type) { IoSupplier ioSupplier = this.getRootResource("pack.mcmeta"); if (ioSupplier != null) { try { InputStream inputStream = ioSupplier.get(); Object var5; label53: { try { T object = AbstractPackResources.getMetadataFromStream(type, inputStream); if (object != null) { var5 = object; break label53; } } catch (Throwable var7) { if (inputStream != null) { try { inputStream.close(); } catch (Throwable var6) { var7.addSuppressed(var6); } } throw var7; } if (inputStream != null) { inputStream.close(); } return this.metadata.get(type); } if (inputStream != null) { inputStream.close(); } return (T)var5; } catch (IOException var8) { } } return this.metadata.get(type); } @Override public PackLocationInfo location() { return this.location; } @Override public void close() { } public ResourceProvider asProvider() { return resourceLocation -> Optional.ofNullable(this.getResource(PackType.CLIENT_RESOURCES, resourceLocation)) .map(ioSupplier -> new Resource(this, ioSupplier)); } }