PackRepository.java

net.minecraft.server.packs.repository.PackRepository

信息

  • 全限定名:net.minecraft.server.packs.repository.PackRepository
  • 类型:public class
  • 包:net.minecraft.server.packs.repository
  • 源码路径:src/main/java/net/minecraft/server/packs/repository/PackRepository.java
  • 起始行号:L20
  • 职责:

    TODO

字段/常量

  • sources

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

      TODO

  • available

    • 类型: Map<String,Pack>
    • 修饰符: private
    • 源码定位: L22
    • 说明:

      TODO

  • selected

    • 类型: List<Pack>
    • 修饰符: private
    • 源码定位: L23
    • 说明:

      TODO

内部类/嵌套类型

构造器

public PackRepository(RepositorySource... sources) @ L25

  • 构造器名:PackRepository
  • 源码定位:L25
  • 修饰符:public

参数:

  • sources: RepositorySource…

说明:

TODO

方法

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

public static String displayPackList(Collection<Pack> packs) @ L29

  • 方法名:displayPackList
  • 源码定位:L29
  • 返回类型:String
  • 修饰符:public static

参数:

  • packs: Collection

说明:

TODO

public void reload() @ L33

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

参数:

说明:

TODO

private Map<String,Pack> discoverAvailable() @ L39

  • 方法名:discoverAvailable
  • 源码定位:L39
  • 返回类型:Map<String,Pack>
  • 修饰符:private

参数:

说明:

TODO

public boolean isAbleToClearAnyPack() @ L49

  • 方法名:isAbleToClearAnyPack
  • 源码定位:L49
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public void setSelected(Collection<String> packs) @ L54

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

参数:

  • packs: Collection

说明:

TODO

public boolean addPack(String packId) @ L58

  • 方法名:addPack
  • 源码定位:L58
  • 返回类型:boolean
  • 修饰符:public

参数:

  • packId: String

说明:

TODO

public boolean removePack(String packId) @ L70

  • 方法名:removePack
  • 源码定位:L70
  • 返回类型:boolean
  • 修饰符:public

参数:

  • packId: String

说明:

TODO

private List<Pack> rebuildSelected(Collection<String> selectedNames) @ L82

  • 方法名:rebuildSelected
  • 源码定位:L82
  • 返回类型:List
  • 修饰符:private

参数:

  • selectedNames: Collection

说明:

TODO

private Stream<Pack> getAvailablePacks(Collection<String> ids) @ L94

  • 方法名:getAvailablePacks
  • 源码定位:L94
  • 返回类型:Stream
  • 修饰符:private

参数:

  • ids: Collection

说明:

TODO

public Collection<String> getAvailableIds() @ L98

  • 方法名:getAvailableIds
  • 源码定位:L98
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

public Collection<Pack> getAvailablePacks() @ L102

  • 方法名:getAvailablePacks
  • 源码定位:L102
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

public Collection<String> getSelectedIds() @ L106

  • 方法名:getSelectedIds
  • 源码定位:L106
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

public FeatureFlagSet getRequestedFeatureFlags() @ L110

  • 方法名:getRequestedFeatureFlags
  • 源码定位:L110
  • 返回类型:FeatureFlagSet
  • 修饰符:public

参数:

说明:

TODO

public Collection<Pack> getSelectedPacks() @ L114

  • 方法名:getSelectedPacks
  • 源码定位:L114
  • 返回类型:Collection
  • 修饰符:public

参数:

说明:

TODO

public Pack getPack(String id) @ L118

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

参数:

  • id: String

说明:

TODO

public boolean isAvailable(String id) @ L122

  • 方法名:isAvailable
  • 源码定位:L122
  • 返回类型:boolean
  • 修饰符:public

参数:

  • id: String

说明:

TODO

public List<PackResources> openAllSelected() @ L126

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

参数:

说明:

TODO

代码

public class PackRepository {
    private final Set<RepositorySource> sources;
    private Map<String, Pack> available = ImmutableMap.of();
    private List<Pack> selected = ImmutableList.of();
 
    public PackRepository(RepositorySource... sources) {
        this.sources = ImmutableSet.copyOf(sources);
    }
 
    public static String displayPackList(Collection<Pack> packs) {
        return packs.stream().map(pack -> pack.getId() + (pack.getCompatibility().isCompatible() ? "" : " (incompatible)")).collect(Collectors.joining(", "));
    }
 
    public void reload() {
        List<String> currentlySelectedNames = this.selected.stream().map(Pack::getId).collect(ImmutableList.toImmutableList());
        this.available = this.discoverAvailable();
        this.selected = this.rebuildSelected(currentlySelectedNames);
    }
 
    private Map<String, Pack> discoverAvailable() {
        Map<String, Pack> discovered = Maps.newTreeMap();
 
        for (RepositorySource source : this.sources) {
            source.loadPacks(pack -> discovered.put(pack.getId(), pack));
        }
 
        return ImmutableMap.copyOf(discovered);
    }
 
    public boolean isAbleToClearAnyPack() {
        List<Pack> newSelected = this.rebuildSelected(List.of());
        return !this.selected.equals(newSelected);
    }
 
    public void setSelected(Collection<String> packs) {
        this.selected = this.rebuildSelected(packs);
    }
 
    public boolean addPack(String packId) {
        Pack pack = this.available.get(packId);
        if (pack != null && !this.selected.contains(pack)) {
            List<Pack> selectedCopy = Lists.newArrayList(this.selected);
            selectedCopy.add(pack);
            this.selected = selectedCopy;
            return true;
        } else {
            return false;
        }
    }
 
    public boolean removePack(String packId) {
        Pack pack = this.available.get(packId);
        if (pack != null && this.selected.contains(pack)) {
            List<Pack> selectedCopy = Lists.newArrayList(this.selected);
            selectedCopy.remove(pack);
            this.selected = selectedCopy;
            return true;
        } else {
            return false;
        }
    }
 
    private List<Pack> rebuildSelected(Collection<String> selectedNames) {
        List<Pack> selectedAndPresent = this.getAvailablePacks(selectedNames).collect(Util.toMutableList());
 
        for (Pack pack : this.available.values()) {
            if (pack.isRequired() && !selectedAndPresent.contains(pack)) {
                pack.getDefaultPosition().insert(selectedAndPresent, pack, Pack::selectionConfig, false);
            }
        }
 
        return ImmutableList.copyOf(selectedAndPresent);
    }
 
    private Stream<Pack> getAvailablePacks(Collection<String> ids) {
        return ids.stream().map(this.available::get).filter(Objects::nonNull);
    }
 
    public Collection<String> getAvailableIds() {
        return this.available.keySet();
    }
 
    public Collection<Pack> getAvailablePacks() {
        return this.available.values();
    }
 
    public Collection<String> getSelectedIds() {
        return this.selected.stream().map(Pack::getId).collect(ImmutableSet.toImmutableSet());
    }
 
    public FeatureFlagSet getRequestedFeatureFlags() {
        return this.getSelectedPacks().stream().map(Pack::getRequestedFeatures).reduce(FeatureFlagSet::join).orElse(FeatureFlagSet.of());
    }
 
    public Collection<Pack> getSelectedPacks() {
        return this.selected;
    }
 
    public @Nullable Pack getPack(String id) {
        return this.available.get(id);
    }
 
    public boolean isAvailable(String id) {
        return this.available.containsKey(id);
    }
 
    public List<PackResources> openAllSelected() {
        return this.selected.stream().map(Pack::open).collect(ImmutableList.toImmutableList());
    }
}

引用的其他类

  • PackResources

    • 引用位置: 返回值
  • Pack

    • 引用位置: 参数/字段/返回值
  • RepositorySource

    • 引用位置: 参数/字段
  • Util

    • 引用位置: 方法调用
    • 关联成员: Util.toMutableList()
  • FeatureFlagSet

    • 引用位置: 方法调用/返回值
    • 关联成员: FeatureFlagSet.of()