| 1 | package net.minecraft.advancements;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import io.netty.buffer.ByteBuf;
|
| 8 | import java.time.Instant;
|
| 9 | import java.time.ZoneId;
|
| 10 | import java.time.format.DateTimeFormatter;
|
| 11 | import java.util.Comparator;
|
| 12 | import java.util.HashMap;
|
| 13 | import java.util.List;
|
| 14 | import java.util.Locale;
|
| 15 | import java.util.Map;
|
| 16 | import java.util.Objects;
|
| 17 | import java.util.Set;
|
| 18 | import java.util.Map.Entry;
|
| 19 | import java.util.stream.Collectors;
|
| 20 | import net.minecraft.network.chat.Component;
|
| 21 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 22 | import net.minecraft.network.codec.StreamCodec;
|
| 23 | import net.minecraft.util.ExtraCodecs;
|
| 24 | import net.minecraft.util.Util;
|
| 25 | import org.jspecify.annotations.Nullable;
|
| 26 |
|
| 27 | public class AdvancementProgress implements Comparable<AdvancementProgress> {
|
| 28 | private static final DateTimeFormatter OBTAINED_TIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss Z", Locale.ROOT);
|
| 29 | private static final Codec<Instant> OBTAINED_TIME_CODEC = ExtraCodecs.temporalCodec(OBTAINED_TIME_FORMAT)
|
| 30 | .xmap(Instant::from, instant -> instant.atZone(ZoneId.systemDefault()));
|
| 31 | private static final Codec<Map<String, CriterionProgress>> CRITERIA_CODEC = Codec.unboundedMap(Codec.STRING, OBTAINED_TIME_CODEC)
|
| 32 | .xmap(
|
| 33 | map -> Util.mapValues((Map<String, Instant>)map, CriterionProgress::new),
|
| 34 | map -> map.entrySet()
|
| 35 | .stream()
|
| 36 | .filter(e -> e.getValue().isDone())
|
| 37 | .collect(Collectors.toMap(Entry::getKey, e -> Objects.requireNonNull(e.getValue().getObtained())))
|
| 38 | );
|
| 39 | public static final Codec<AdvancementProgress> CODEC = RecordCodecBuilder.create(
|
| 40 | i -> i.group(
|
| 41 | CRITERIA_CODEC.optionalFieldOf("criteria", Map.of()).forGetter(a -> a.criteria),
|
| 42 | ExtraCodecs.optionalAlwaysPresentFieldOf(Codec.BOOL, "done", true).forGetter(AdvancementProgress::isDone)
|
| 43 | )
|
| 44 | .apply(i, (criteria, done) -> new AdvancementProgress(new HashMap<>(criteria)))
|
| 45 | );
|
| 46 | public static final StreamCodec<ByteBuf, AdvancementProgress> STREAM_CODEC = StreamCodec.composite(
|
| 47 | ByteBufCodecs.map(HashMap::new, ByteBufCodecs.STRING_UTF8, CriterionProgress.STREAM_CODEC), a -> a.criteria, AdvancementProgress::new
|
| 48 | );
|
| 49 | private final Map<String, CriterionProgress> criteria;
|
| 50 | private AdvancementRequirements requirements = AdvancementRequirements.EMPTY;
|
| 51 |
|
| 52 | private AdvancementProgress(final Map<String, CriterionProgress> criteria) {
|
| 53 | this.criteria = criteria;
|
| 54 | }
|
| 55 |
|
| 56 | public AdvancementProgress() {
|
| 57 | this.criteria = Maps.newHashMap();
|
| 58 | }
|
| 59 |
|
| 60 | public void update(final AdvancementRequirements requirements) {
|
| 61 | Set<String> names = requirements.names();
|
| 62 | this.criteria.entrySet().removeIf(entry -> !names.contains(entry.getKey()));
|
| 63 |
|
| 64 | for (String name : names) {
|
| 65 | this.criteria.putIfAbsent(name, new CriterionProgress());
|
| 66 | }
|
| 67 |
|
| 68 | this.requirements = requirements;
|
| 69 | }
|
| 70 |
|
| 71 | public boolean isDone() {
|
| 72 | return this.requirements.test(this::isCriterionDone);
|
| 73 | }
|
| 74 |
|
| 75 | public boolean hasProgress() {
|
| 76 | for (CriterionProgress progress : this.criteria.values()) {
|
| 77 | if (progress.isDone()) {
|
| 78 | return true;
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | return false;
|
| 83 | }
|
| 84 |
|
| 85 | public boolean grantProgress(final String name) {
|
| 86 | CriterionProgress progress = this.criteria.get(name);
|
| 87 | if (progress != null && !progress.isDone()) {
|
| 88 | progress.grant();
|
| 89 | return true;
|
| 90 | } else {
|
| 91 | return false;
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | public boolean revokeProgress(final String name) {
|
| 96 | CriterionProgress progress = this.criteria.get(name);
|
| 97 | if (progress != null && progress.isDone()) {
|
| 98 | progress.revoke();
|
| 99 | return true;
|
| 100 | } else {
|
| 101 | return false;
|
| 102 | }
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public String toString() {
|
| 107 | return "AdvancementProgress{criteria=" + this.criteria + ", requirements=" + this.requirements + "}";
|
| 108 | }
|
| 109 |
|
| 110 | public @Nullable CriterionProgress getCriterion(final String id) {
|
| 111 | return this.criteria.get(id);
|
| 112 | }
|
| 113 |
|
| 114 | private boolean isCriterionDone(final String criterion) {
|
| 115 | CriterionProgress progress = this.getCriterion(criterion);
|
| 116 | return progress != null && progress.isDone();
|
| 117 | }
|
| 118 |
|
| 119 | public float getPercent() {
|
| 120 | if (this.criteria.isEmpty()) {
|
| 121 | return 0.0F;
|
| 122 | }
|
| 123 |
|
| 124 | float total = this.requirements.size();
|
| 125 | float complete = this.countCompletedRequirements();
|
| 126 | return complete / total;
|
| 127 | }
|
| 128 |
|
| 129 | public @Nullable Component getProgressText() {
|
| 130 | if (this.criteria.isEmpty()) {
|
| 131 | return null;
|
| 132 | }
|
| 133 |
|
| 134 | int total = this.requirements.size();
|
| 135 | if (total <= 1) {
|
| 136 | return null;
|
| 137 | }
|
| 138 |
|
| 139 | int complete = this.countCompletedRequirements();
|
| 140 | return Component.translatable("advancements.progress", complete, total);
|
| 141 | }
|
| 142 |
|
| 143 | private int countCompletedRequirements() {
|
| 144 | return this.requirements.count(this::isCriterionDone);
|
| 145 | }
|
| 146 |
|
| 147 | public Iterable<String> getRemainingCriteria() {
|
| 148 | List<String> remaining = Lists.newArrayList();
|
| 149 |
|
| 150 | for (Entry<String, CriterionProgress> entry : this.criteria.entrySet()) {
|
| 151 | if (!entry.getValue().isDone()) {
|
| 152 | remaining.add(entry.getKey());
|
| 153 | }
|
| 154 | }
|
| 155 |
|
| 156 | return remaining;
|
| 157 | }
|
| 158 |
|
| 159 | public Iterable<String> getCompletedCriteria() {
|
| 160 | List<String> completed = Lists.newArrayList();
|
| 161 |
|
| 162 | for (Entry<String, CriterionProgress> entry : this.criteria.entrySet()) {
|
| 163 | if (entry.getValue().isDone()) {
|
| 164 | completed.add(entry.getKey());
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | return completed;
|
| 169 | }
|
| 170 |
|
| 171 | public @Nullable Instant getFirstProgressDate() {
|
| 172 | return this.criteria.values().stream().map(CriterionProgress::getObtained).filter(Objects::nonNull).min(Comparator.naturalOrder()).orElse(null);
|
| 173 | }
|
| 174 |
|
| 175 | public int compareTo(final AdvancementProgress o) {
|
| 176 | Instant ourSmallestDate = this.getFirstProgressDate();
|
| 177 | Instant theirSmallestDate = o.getFirstProgressDate();
|
| 178 | if (ourSmallestDate == null && theirSmallestDate != null) {
|
| 179 | return 1;
|
| 180 | } else if (ourSmallestDate != null && theirSmallestDate == null) {
|
| 181 | return -1;
|
| 182 | } else {
|
| 183 | return ourSmallestDate == null && theirSmallestDate == null ? 0 : ourSmallestDate.compareTo(theirSmallestDate);
|
| 184 | }
|
| 185 | }
|
| 186 | }
|
| 187 |
|