234 lines
8.1 KiB
Java
234 lines
8.1 KiB
Java
package net.minecraft.advancements;
|
|
|
|
import com.google.gson.JsonParseException;
|
|
import net.minecraft.util.GsonHelper;
|
|
import com.google.gson.JsonDeserializationContext;
|
|
import com.google.gson.JsonObject;
|
|
import com.google.gson.JsonElement;
|
|
import com.google.gson.JsonSerializationContext;
|
|
import java.lang.reflect.Type;
|
|
import com.google.gson.JsonSerializer;
|
|
import com.google.gson.JsonDeserializer;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import com.google.common.collect.Lists;
|
|
import javax.annotation.Nullable;
|
|
import net.minecraft.network.FriendlyByteBuf;
|
|
import java.util.Arrays;
|
|
import java.util.Iterator;
|
|
import java.util.Set;
|
|
import com.google.common.collect.Maps;
|
|
import java.util.Map;
|
|
|
|
public class AdvancementProgress implements Comparable<AdvancementProgress> {
|
|
private final Map<String, CriterionProgress> criteria;
|
|
private String[][] requirements;
|
|
|
|
public AdvancementProgress() {
|
|
this.criteria = Maps.newHashMap();
|
|
this.requirements = new String[0][];
|
|
}
|
|
|
|
public void update(final Map<String, Criterion> map, final String[][] arr) {
|
|
final Set<String> set4 = map.keySet();
|
|
this.criteria.entrySet().removeIf(entry -> !set4.contains(entry.getKey()));
|
|
for (final String string6 : set4) {
|
|
if (!this.criteria.containsKey(string6)) {
|
|
this.criteria.put(string6, new CriterionProgress());
|
|
}
|
|
}
|
|
this.requirements = arr;
|
|
}
|
|
|
|
public boolean isDone() {
|
|
if (this.requirements.length == 0) {
|
|
return false;
|
|
}
|
|
for (final String[] arr5 : this.requirements) {
|
|
boolean boolean6 = false;
|
|
for (final String string10 : arr5) {
|
|
final CriterionProgress aa11 = this.getCriterion(string10);
|
|
if (aa11 != null && aa11.isDone()) {
|
|
boolean6 = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!boolean6) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public boolean hasProgress() {
|
|
for (final CriterionProgress aa3 : this.criteria.values()) {
|
|
if (aa3.isDone()) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean grantProgress(final String string) {
|
|
final CriterionProgress aa3 = this.criteria.get(string);
|
|
if (aa3 != null && !aa3.isDone()) {
|
|
aa3.grant();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean revokeProgress(final String string) {
|
|
final CriterionProgress aa3 = this.criteria.get(string);
|
|
if (aa3 != null && aa3.isDone()) {
|
|
aa3.revoke();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return "AdvancementProgress{criteria=" + this.criteria + ", requirements=" + Arrays.deepToString(this.requirements) + '}';
|
|
}
|
|
|
|
public void serializeToNetwork(final FriendlyByteBuf kv) {
|
|
kv.writeVarInt(this.criteria.size());
|
|
for (final Map.Entry<String, CriterionProgress> entry4 : this.criteria.entrySet()) {
|
|
kv.writeUtf(entry4.getKey());
|
|
entry4.getValue().serializeToNetwork(kv);
|
|
}
|
|
}
|
|
|
|
public static AdvancementProgress fromNetwork(final FriendlyByteBuf kv) {
|
|
final AdvancementProgress w2 = new AdvancementProgress();
|
|
for (int integer3 = kv.readVarInt(), integer4 = 0; integer4 < integer3; ++integer4) {
|
|
w2.criteria.put(kv.readUtf(32767), CriterionProgress.fromNetwork(kv));
|
|
}
|
|
return w2;
|
|
}
|
|
|
|
@Nullable
|
|
public CriterionProgress getCriterion(final String string) {
|
|
return this.criteria.get(string);
|
|
}
|
|
|
|
public float getPercent() {
|
|
if (this.criteria.isEmpty()) {
|
|
return 0.0f;
|
|
}
|
|
final float float2 = (float)this.requirements.length;
|
|
final float float3 = (float)this.countCompletedRequirements();
|
|
return float3 / float2;
|
|
}
|
|
|
|
@Nullable
|
|
public String getProgressText() {
|
|
if (this.criteria.isEmpty()) {
|
|
return null;
|
|
}
|
|
final int integer2 = this.requirements.length;
|
|
if (integer2 <= 1) {
|
|
return null;
|
|
}
|
|
final int integer3 = this.countCompletedRequirements();
|
|
return integer3 + "/" + integer2;
|
|
}
|
|
|
|
private int countCompletedRequirements() {
|
|
int integer2 = 0;
|
|
for (final String[] arr6 : this.requirements) {
|
|
boolean boolean7 = false;
|
|
for (final String string11 : arr6) {
|
|
final CriterionProgress aa12 = this.getCriterion(string11);
|
|
if (aa12 != null && aa12.isDone()) {
|
|
boolean7 = true;
|
|
break;
|
|
}
|
|
}
|
|
if (boolean7) {
|
|
++integer2;
|
|
}
|
|
}
|
|
return integer2;
|
|
}
|
|
|
|
public Iterable<String> getRemainingCriteria() {
|
|
final List<String> list2 = Lists.newArrayList();
|
|
for (final Map.Entry<String, CriterionProgress> entry4 : this.criteria.entrySet()) {
|
|
if (!entry4.getValue().isDone()) {
|
|
list2.add(entry4.getKey());
|
|
}
|
|
}
|
|
return list2;
|
|
}
|
|
|
|
public Iterable<String> getCompletedCriteria() {
|
|
final List<String> list2 = Lists.newArrayList();
|
|
for (final Map.Entry<String, CriterionProgress> entry4 : this.criteria.entrySet()) {
|
|
if (entry4.getValue().isDone()) {
|
|
list2.add(entry4.getKey());
|
|
}
|
|
}
|
|
return list2;
|
|
}
|
|
|
|
@Nullable
|
|
public Date getFirstProgressDate() {
|
|
Date date2 = null;
|
|
for (final CriterionProgress aa4 : this.criteria.values()) {
|
|
if (aa4.isDone() && (date2 == null || aa4.getObtained().before(date2))) {
|
|
date2 = aa4.getObtained();
|
|
}
|
|
}
|
|
return date2;
|
|
}
|
|
|
|
@Override
|
|
public int compareTo(final AdvancementProgress w) {
|
|
final Date date3 = this.getFirstProgressDate();
|
|
final Date date4 = w.getFirstProgressDate();
|
|
if (date3 == null && date4 != null) {
|
|
return 1;
|
|
}
|
|
if (date3 != null && date4 == null) {
|
|
return -1;
|
|
}
|
|
if (date3 == null && date4 == null) {
|
|
return 0;
|
|
}
|
|
return date3.compareTo(date4);
|
|
}
|
|
|
|
public static class Serializer implements JsonDeserializer<AdvancementProgress>, JsonSerializer<AdvancementProgress> {
|
|
@Override
|
|
public JsonElement serialize(final AdvancementProgress w, final Type type, final JsonSerializationContext jsonSerializationContext) {
|
|
final JsonObject jsonObject5 = new JsonObject();
|
|
final JsonObject jsonObject6 = new JsonObject();
|
|
for (final Map.Entry<String, CriterionProgress> entry8 : w.criteria.entrySet()) {
|
|
final CriterionProgress aa9 = entry8.getValue();
|
|
if (aa9.isDone()) {
|
|
jsonObject6.add(entry8.getKey(), aa9.serializeToJson());
|
|
}
|
|
}
|
|
if (!jsonObject6.entrySet().isEmpty()) {
|
|
jsonObject5.add("criteria", jsonObject6);
|
|
}
|
|
jsonObject5.addProperty("done", w.isDone());
|
|
return jsonObject5;
|
|
}
|
|
|
|
@Override
|
|
public AdvancementProgress deserialize(final JsonElement jsonElement, final Type type, final JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
|
|
final JsonObject jsonObject5 = GsonHelper.convertToJsonObject(jsonElement, "advancement");
|
|
final JsonObject jsonObject6 = GsonHelper.getAsJsonObject(jsonObject5, "criteria", new JsonObject());
|
|
final AdvancementProgress w7 = new AdvancementProgress();
|
|
for (final Map.Entry<String, JsonElement> entry9 : jsonObject6.entrySet()) {
|
|
final String string10 = entry9.getKey();
|
|
w7.criteria.put(string10, CriterionProgress.fromJson(GsonHelper.convertToString(entry9.getValue(), string10)));
|
|
}
|
|
return w7;
|
|
}
|
|
}
|
|
}
|