MultiPackResourceManager.java

net.minecraft.server.packs.resources.MultiPackResourceManager

信息

  • 全限定名:net.minecraft.server.packs.resources.MultiPackResourceManager
  • 类型:public class
  • 包:net.minecraft.server.packs.resources
  • 源码路径:src/main/java/net/minecraft/server/packs/resources/MultiPackResourceManager.java
  • 起始行号:L18
  • 实现:CloseableResourceManager
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L19
    • 说明:

      TODO

  • namespacedManagers

    • 类型: Map<String,FallbackResourceManager>
    • 修饰符: private final
    • 源码定位: L20
    • 说明:

      TODO

  • packs

    • 类型: List<PackResources>
    • 修饰符: private final
    • 源码定位: L21
    • 说明:

      TODO

内部类/嵌套类型

构造器

public MultiPackResourceManager(PackType type, List<PackResources> packs) @ L23

  • 构造器名:MultiPackResourceManager
  • 源码定位:L23
  • 修饰符:public

参数:

  • type: PackType
  • packs: List

说明:

TODO

方法

下面的方法块按源码顺序生成。

private ResourceFilterSection getPackFilterSection(PackResources pack) @ L57

  • 方法名:getPackFilterSection
  • 源码定位:L57
  • 返回类型:ResourceFilterSection
  • 修饰符:private

参数:

  • pack: PackResources

说明:

TODO

public Set<String> getNamespaces() @ L66

  • 方法名:getNamespaces
  • 源码定位:L66
  • 返回类型:Set
  • 修饰符:public

参数:

说明:

TODO

public Optional<Resource> getResource(Identifier location) @ L71

  • 方法名:getResource
  • 源码定位:L71
  • 返回类型:Optional
  • 修饰符:public

参数:

  • location: Identifier

说明:

TODO

public List<Resource> getResourceStack(Identifier location) @ L77

  • 方法名:getResourceStack
  • 源码定位:L77
  • 返回类型:List
  • 修饰符:public

参数:

  • location: Identifier

说明:

TODO

public Map<Identifier,Resource> listResources(String directory, Predicate<Identifier> filter) @ L83

  • 方法名:listResources
  • 源码定位:L83
  • 返回类型:Map<Identifier,Resource>
  • 修饰符:public

参数:

  • directory: String
  • filter: Predicate

说明:

TODO

public Map<Identifier,List<Resource>> listResourceStacks(String directory, Predicate<Identifier> filter) @ L95

  • 方法名:listResourceStacks
  • 源码定位:L95
  • 返回类型:Map<Identifier,List>
  • 修饰符:public

参数:

  • directory: String
  • filter: Predicate

说明:

TODO

private static void checkTrailingDirectoryPath(String directory) @ L107

  • 方法名:checkTrailingDirectoryPath
  • 源码定位:L107
  • 返回类型:void
  • 修饰符:private static

参数:

  • directory: String

说明:

TODO

public Stream<PackResources> listPacks() @ L113

  • 方法名:listPacks
  • 源码定位:L113
  • 返回类型:Stream
  • 修饰符:public

参数:

说明:

TODO

public void close() @ L118

  • 方法名:close
  • 源码定位:L118
  • 返回类型:void
  • 修饰符:public

参数:

说明:

TODO

代码

public class MultiPackResourceManager implements CloseableResourceManager {
    private static final Logger LOGGER = LogUtils.getLogger();
    private final Map<String, FallbackResourceManager> namespacedManagers;
    private final List<PackResources> packs;
 
    public MultiPackResourceManager(PackType type, List<PackResources> packs) {
        this.packs = List.copyOf(packs);
        Map<String, FallbackResourceManager> namespacedManagers = new HashMap<>();
        List<String> namespaces = packs.stream().flatMap(p -> p.getNamespaces(type).stream()).distinct().toList();
 
        for (PackResources pack : packs) {
            ResourceFilterSection filterSection = this.getPackFilterSection(pack);
            Set<String> providedNamespaces = pack.getNamespaces(type);
            Predicate<Identifier> pathFilter = filterSection != null ? location -> filterSection.isPathFiltered(location.getPath()) : null;
 
            for (String namespace : namespaces) {
                boolean packContainsNamespace = providedNamespaces.contains(namespace);
                boolean filterMatchesNamespace = filterSection != null && filterSection.isNamespaceFiltered(namespace);
                if (packContainsNamespace || filterMatchesNamespace) {
                    FallbackResourceManager fallbackResourceManager = namespacedManagers.get(namespace);
                    if (fallbackResourceManager == null) {
                        fallbackResourceManager = new FallbackResourceManager(type, namespace);
                        namespacedManagers.put(namespace, fallbackResourceManager);
                    }
 
                    if (packContainsNamespace && filterMatchesNamespace) {
                        fallbackResourceManager.push(pack, pathFilter);
                    } else if (packContainsNamespace) {
                        fallbackResourceManager.push(pack);
                    } else {
                        fallbackResourceManager.pushFilterOnly(pack.packId(), pathFilter);
                    }
                }
            }
        }
 
        this.namespacedManagers = namespacedManagers;
    }
 
    private @Nullable ResourceFilterSection getPackFilterSection(PackResources pack) {
        try {
            return pack.getMetadataSection(ResourceFilterSection.TYPE);
        } catch (Exception var3) {
            LOGGER.error("Failed to get filter section from pack {}", pack.packId());
            return null;
        }
    }
 
    @Override
    public Set<String> getNamespaces() {
        return this.namespacedManagers.keySet();
    }
 
    @Override
    public Optional<Resource> getResource(Identifier location) {
        ResourceManager pack = this.namespacedManagers.get(location.getNamespace());
        return pack != null ? pack.getResource(location) : Optional.empty();
    }
 
    @Override
    public List<Resource> getResourceStack(Identifier location) {
        ResourceManager pack = this.namespacedManagers.get(location.getNamespace());
        return pack != null ? pack.getResourceStack(location) : List.of();
    }
 
    @Override
    public Map<Identifier, Resource> listResources(String directory, Predicate<Identifier> filter) {
        checkTrailingDirectoryPath(directory);
        Map<Identifier, Resource> result = new TreeMap<>();
 
        for (FallbackResourceManager manager : this.namespacedManagers.values()) {
            result.putAll(manager.listResources(directory, filter));
        }
 
        return result;
    }
 
    @Override
    public Map<Identifier, List<Resource>> listResourceStacks(String directory, Predicate<Identifier> filter) {
        checkTrailingDirectoryPath(directory);
        Map<Identifier, List<Resource>> result = new TreeMap<>();
 
        for (FallbackResourceManager manager : this.namespacedManagers.values()) {
            result.putAll(manager.listResourceStacks(directory, filter));
        }
 
        return result;
    }
 
    private static void checkTrailingDirectoryPath(String directory) {
        if (directory.endsWith("/")) {
            throw new IllegalArgumentException("Trailing slash in path " + directory);
        }
    }
 
    @Override
    public Stream<PackResources> listPacks() {
        return this.packs.stream();
    }
 
    @Override
    public void close() {
        this.packs.forEach(PackResources::close);
    }
}

引用的其他类