AdvancementProgress.java

net.minecraft.advancements.AdvancementProgress

信息

  • 全限定名:net.minecraft.advancements.AdvancementProgress
  • 类型:public class
  • 包:net.minecraft.advancements
  • 源码路径:src/main/java/net/minecraft/advancements/AdvancementProgress.java
  • 起始行号:L25
  • 实现:Comparable
  • 职责:

    TODO

字段/常量

  • OBTAINED_TIME_FORMAT

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

      TODO

  • OBTAINED_TIME_CODEC

    • 类型: Codec<Instant>
    • 修饰符: private static final
    • 源码定位: L27
    • 说明:

      TODO

  • CRITERIA_CODEC

    • 类型: Codec<Map<String,CriterionProgress>>
    • 修饰符: private static final
    • 源码定位: L29
    • 说明:

      TODO

  • CODEC

    • 类型: Codec<AdvancementProgress>
    • 修饰符: public static final
    • 源码定位: L37
    • 说明:

      TODO

  • criteria

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

      TODO

  • requirements

    • 类型: AdvancementRequirements
    • 修饰符: private
    • 源码定位: L45
    • 说明:

      TODO

内部类/嵌套类型

构造器

private AdvancementProgress(Map<String,CriterionProgress> criteria) @ L47

  • 构造器名:AdvancementProgress
  • 源码定位:L47
  • 修饰符:private

参数:

  • criteria: Map<String,CriterionProgress>

说明:

TODO

public AdvancementProgress() @ L51

  • 构造器名:AdvancementProgress
  • 源码定位:L51
  • 修饰符:public

参数:

说明:

TODO

方法

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

public void update(AdvancementRequirements requirements) @ L55

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

参数:

  • requirements: AdvancementRequirements

说明:

TODO

public boolean isDone() @ L66

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

参数:

说明:

TODO

public boolean hasProgress() @ L70

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

参数:

说明:

TODO

public boolean grantProgress(String name) @ L80

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

参数:

  • name: String

说明:

TODO

public boolean revokeProgress(String name) @ L90

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

参数:

  • name: String

说明:

TODO

public String toString() @ L100

  • 方法名:toString
  • 源码定位:L100
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

public void serializeToNetwork(FriendlyByteBuf output) @ L105

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

参数:

  • output: FriendlyByteBuf

说明:

TODO

public static AdvancementProgress fromNetwork(FriendlyByteBuf input) @ L109

  • 方法名:fromNetwork
  • 源码定位:L109
  • 返回类型:AdvancementProgress
  • 修饰符:public static

参数:

  • input: FriendlyByteBuf

说明:

TODO

public CriterionProgress getCriterion(String id) @ L114

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

参数:

  • id: String

说明:

TODO

private boolean isCriterionDone(String criterion) @ L118

  • 方法名:isCriterionDone
  • 源码定位:L118
  • 返回类型:boolean
  • 修饰符:private

参数:

  • criterion: String

说明:

TODO

public float getPercent() @ L123

  • 方法名:getPercent
  • 源码定位:L123
  • 返回类型:float
  • 修饰符:public

参数:

说明:

TODO

public Component getProgressText() @ L133

  • 方法名:getProgressText
  • 源码定位:L133
  • 返回类型:Component
  • 修饰符:public

参数:

说明:

TODO

private int countCompletedRequirements() @ L147

  • 方法名:countCompletedRequirements
  • 源码定位:L147
  • 返回类型:int
  • 修饰符:private

参数:

说明:

TODO

public Iterable<String> getRemainingCriteria() @ L151

  • 方法名:getRemainingCriteria
  • 源码定位:L151
  • 返回类型:Iterable
  • 修饰符:public

参数:

说明:

TODO

public Iterable<String> getCompletedCriteria() @ L163

  • 方法名:getCompletedCriteria
  • 源码定位:L163
  • 返回类型:Iterable
  • 修饰符:public

参数:

说明:

TODO

public Instant getFirstProgressDate() @ L175

  • 方法名:getFirstProgressDate
  • 源码定位:L175
  • 返回类型:Instant
  • 修饰符:public

参数:

说明:

TODO

public int compareTo(AdvancementProgress o) @ L179

  • 方法名:compareTo
  • 源码定位:L179
  • 返回类型:int
  • 修饰符:public

参数:

  • o: AdvancementProgress

说明:

TODO

代码

public class AdvancementProgress implements Comparable<AdvancementProgress> {
    private static final DateTimeFormatter OBTAINED_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z", Locale.ROOT);
    private static final Codec<Instant> OBTAINED_TIME_CODEC = ExtraCodecs.temporalCodec(OBTAINED_TIME_FORMAT)
        .xmap(Instant::from, instant -> instant.atZone(ZoneId.systemDefault()));
    private static final Codec<Map<String, CriterionProgress>> CRITERIA_CODEC = Codec.unboundedMap(Codec.STRING, OBTAINED_TIME_CODEC)
        .xmap(
            map -> Util.mapValues((Map<String, Instant>)map, CriterionProgress::new),
            map -> map.entrySet()
                .stream()
                .filter(e -> e.getValue().isDone())
                .collect(Collectors.toMap(Entry::getKey, e -> Objects.requireNonNull(e.getValue().getObtained())))
        );
    public static final Codec<AdvancementProgress> CODEC = RecordCodecBuilder.create(
        i -> i.group(
                CRITERIA_CODEC.optionalFieldOf("criteria", Map.of()).forGetter(a -> a.criteria),
                Codec.BOOL.fieldOf("done").orElse(true).forGetter(AdvancementProgress::isDone)
            )
            .apply(i, (criteria, done) -> new AdvancementProgress(new HashMap<>(criteria)))
    );
    private final Map<String, CriterionProgress> criteria;
    private AdvancementRequirements requirements = AdvancementRequirements.EMPTY;
 
    private AdvancementProgress(Map<String, CriterionProgress> criteria) {
        this.criteria = criteria;
    }
 
    public AdvancementProgress() {
        this.criteria = Maps.newHashMap();
    }
 
    public void update(AdvancementRequirements requirements) {
        Set<String> names = requirements.names();
        this.criteria.entrySet().removeIf(entry -> !names.contains(entry.getKey()));
 
        for (String name : names) {
            this.criteria.putIfAbsent(name, new CriterionProgress());
        }
 
        this.requirements = requirements;
    }
 
    public boolean isDone() {
        return this.requirements.test(this::isCriterionDone);
    }
 
    public boolean hasProgress() {
        for (CriterionProgress progress : this.criteria.values()) {
            if (progress.isDone()) {
                return true;
            }
        }
 
        return false;
    }
 
    public boolean grantProgress(String name) {
        CriterionProgress progress = this.criteria.get(name);
        if (progress != null && !progress.isDone()) {
            progress.grant();
            return true;
        } else {
            return false;
        }
    }
 
    public boolean revokeProgress(String name) {
        CriterionProgress progress = this.criteria.get(name);
        if (progress != null && progress.isDone()) {
            progress.revoke();
            return true;
        } else {
            return false;
        }
    }
 
    @Override
    public String toString() {
        return "AdvancementProgress{criteria=" + this.criteria + ", requirements=" + this.requirements + "}";
    }
 
    public void serializeToNetwork(FriendlyByteBuf output) {
        output.writeMap(this.criteria, FriendlyByteBuf::writeUtf, (b, v) -> v.serializeToNetwork(b));
    }
 
    public static AdvancementProgress fromNetwork(FriendlyByteBuf input) {
        Map<String, CriterionProgress> criteria = input.readMap(FriendlyByteBuf::readUtf, CriterionProgress::fromNetwork);
        return new AdvancementProgress(criteria);
    }
 
    public @Nullable CriterionProgress getCriterion(String id) {
        return this.criteria.get(id);
    }
 
    private boolean isCriterionDone(String criterion) {
        CriterionProgress progress = this.getCriterion(criterion);
        return progress != null && progress.isDone();
    }
 
    public float getPercent() {
        if (this.criteria.isEmpty()) {
            return 0.0F;
        } else {
            float total = this.requirements.size();
            float complete = this.countCompletedRequirements();
            return complete / total;
        }
    }
 
    public @Nullable Component getProgressText() {
        if (this.criteria.isEmpty()) {
            return null;
        } else {
            int total = this.requirements.size();
            if (total <= 1) {
                return null;
            } else {
                int complete = this.countCompletedRequirements();
                return Component.translatable("advancements.progress", complete, total);
            }
        }
    }
 
    private int countCompletedRequirements() {
        return this.requirements.count(this::isCriterionDone);
    }
 
    public Iterable<String> getRemainingCriteria() {
        List<String> remaining = Lists.newArrayList();
 
        for (Entry<String, CriterionProgress> entry : this.criteria.entrySet()) {
            if (!entry.getValue().isDone()) {
                remaining.add(entry.getKey());
            }
        }
 
        return remaining;
    }
 
    public Iterable<String> getCompletedCriteria() {
        List<String> completed = Lists.newArrayList();
 
        for (Entry<String, CriterionProgress> entry : this.criteria.entrySet()) {
            if (entry.getValue().isDone()) {
                completed.add(entry.getKey());
            }
        }
 
        return completed;
    }
 
    public @Nullable Instant getFirstProgressDate() {
        return this.criteria.values().stream().map(CriterionProgress::getObtained).filter(Objects::nonNull).min(Comparator.naturalOrder()).orElse(null);
    }
 
    public int compareTo(AdvancementProgress o) {
        Instant ourSmallestDate = this.getFirstProgressDate();
        Instant theirSmallestDate = o.getFirstProgressDate();
        if (ourSmallestDate == null && theirSmallestDate != null) {
            return 1;
        } else if (ourSmallestDate != null && theirSmallestDate == null) {
            return -1;
        } else {
            return ourSmallestDate == null && theirSmallestDate == null ? 0 : ourSmallestDate.compareTo(theirSmallestDate);
        }
    }
}

引用的其他类

  • AdvancementRequirements

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

    • 引用位置: 参数/字段/构造调用/返回值
    • 关联成员: CriterionProgress()
  • FriendlyByteBuf

    • 引用位置: 参数
  • Component

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

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

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