| 1 | package net.minecraft.world.scores.criteria;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableSet;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.mojang.serialization.Codec;
|
| 6 | import com.mojang.serialization.DataResult;
|
| 7 | import java.util.EnumMap;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.Set;
|
| 11 | import java.util.function.Function;
|
| 12 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 13 | import net.minecraft.resources.Identifier;
|
| 14 | import net.minecraft.stats.StatType;
|
| 15 | import net.minecraft.util.StringRepresentable;
|
| 16 | import net.minecraft.world.scores.TeamColor;
|
| 17 |
|
| 18 | /**
|
| 19 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 20 | */
|
| 21 | public class ObjectiveCriteria {
|
| 22 | private static final Map<String, ObjectiveCriteria> CUSTOM_CRITERIA = Maps.newHashMap();
|
| 23 | private static final Map<String, ObjectiveCriteria> CRITERIA_CACHE = Maps.newHashMap();
|
| 24 | public static final Codec<ObjectiveCriteria> CODEC = Codec.STRING
|
| 25 | .comapFlatMap(
|
| 26 | name -> byName(name).map(DataResult::success).orElse(DataResult.error(() -> "No scoreboard criteria with name: " + name)), ObjectiveCriteria::getName
|
| 27 | );
|
| 28 | public static final ObjectiveCriteria DUMMY = registerCustom("dummy");
|
| 29 | public static final ObjectiveCriteria TRIGGER = registerCustom("trigger");
|
| 30 | public static final ObjectiveCriteria DEATH_COUNT = registerCustom("deathCount");
|
| 31 | public static final ObjectiveCriteria KILL_COUNT_PLAYERS = registerCustom("playerKillCount");
|
| 32 | public static final ObjectiveCriteria KILL_COUNT_ALL = registerCustom("totalKillCount");
|
| 33 | public static final ObjectiveCriteria HEALTH = registerCustom("health", true, ObjectiveCriteria.RenderType.HEARTS);
|
| 34 | public static final ObjectiveCriteria FOOD = registerCustom("food", true, ObjectiveCriteria.RenderType.INTEGER);
|
| 35 | public static final ObjectiveCriteria AIR = registerCustom("air", true, ObjectiveCriteria.RenderType.INTEGER);
|
| 36 | public static final ObjectiveCriteria ARMOR = registerCustom("armor", true, ObjectiveCriteria.RenderType.INTEGER);
|
| 37 | public static final ObjectiveCriteria EXPERIENCE = registerCustom("xp", true, ObjectiveCriteria.RenderType.INTEGER);
|
| 38 | public static final ObjectiveCriteria LEVEL = registerCustom("level", true, ObjectiveCriteria.RenderType.INTEGER);
|
| 39 | public static final Map<TeamColor, ObjectiveCriteria> TEAM_KILL = registerForEveryTeamColor(format -> "teamkill." + format.getSerializedName());
|
| 40 | public static final Map<TeamColor, ObjectiveCriteria> KILLED_BY_TEAM = registerForEveryTeamColor(format -> "killedByTeam." + format.getSerializedName());
|
| 41 | private final String name;
|
| 42 | private final boolean readOnly;
|
| 43 | private final ObjectiveCriteria.RenderType renderType;
|
| 44 |
|
| 45 | private static Map<TeamColor, ObjectiveCriteria> registerForEveryTeamColor(final Function<TeamColor, String> idFactory) {
|
| 46 | Map<TeamColor, ObjectiveCriteria> result = new EnumMap<>(TeamColor.class);
|
| 47 |
|
| 48 | for (TeamColor value : TeamColor.values()) {
|
| 49 | String id = idFactory.apply(value);
|
| 50 | result.put(value, registerCustom(id));
|
| 51 | }
|
| 52 |
|
| 53 | return result;
|
| 54 | }
|
| 55 |
|
| 56 | /**
|
| 57 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 58 | */
|
| 59 | public static ObjectiveCriteria registerCustom(final String name, final boolean readOnly, final ObjectiveCriteria.RenderType renderType) {
|
| 60 | ObjectiveCriteria result = new ObjectiveCriteria(name, readOnly, renderType);
|
| 61 | CUSTOM_CRITERIA.put(name, result);
|
| 62 | return result;
|
| 63 | }
|
| 64 |
|
| 65 | /**
|
| 66 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 67 | */
|
| 68 | public static ObjectiveCriteria registerCustom(final String name) {
|
| 69 | return registerCustom(name, false, ObjectiveCriteria.RenderType.INTEGER);
|
| 70 | }
|
| 71 |
|
| 72 | protected ObjectiveCriteria(final String name) {
|
| 73 | this(name, false, ObjectiveCriteria.RenderType.INTEGER);
|
| 74 | }
|
| 75 |
|
| 76 | protected ObjectiveCriteria(final String name, final boolean readOnly, final ObjectiveCriteria.RenderType renderType) {
|
| 77 | this.name = name;
|
| 78 | this.readOnly = readOnly;
|
| 79 | this.renderType = renderType;
|
| 80 | CRITERIA_CACHE.put(name, this);
|
| 81 | }
|
| 82 |
|
| 83 | public static Set<String> getCustomCriteriaNames() {
|
| 84 | return ImmutableSet.copyOf(CUSTOM_CRITERIA.keySet());
|
| 85 | }
|
| 86 |
|
| 87 | public static Optional<ObjectiveCriteria> byName(final String name) {
|
| 88 | ObjectiveCriteria value = CRITERIA_CACHE.get(name);
|
| 89 | if (value != null) {
|
| 90 | return Optional.of(value);
|
| 91 | }
|
| 92 |
|
| 93 | int colonPos = name.indexOf(58);
|
| 94 | return colonPos < 0
|
| 95 | ? Optional.empty()
|
| 96 | : BuiltInRegistries.STAT_TYPE
|
| 97 | .getOptional(Identifier.bySeparator(name.substring(0, colonPos), '.'))
|
| 98 | .flatMap(statType -> getStat((StatType<?>)statType, Identifier.bySeparator(name.substring(colonPos + 1), '.')));
|
| 99 | }
|
| 100 |
|
| 101 | private static <T> Optional<ObjectiveCriteria> getStat(final StatType<T> statType, final Identifier key) {
|
| 102 | return statType.getRegistry().getOptional(key).map(statType::get);
|
| 103 | }
|
| 104 |
|
| 105 | public String getName() {
|
| 106 | return this.name;
|
| 107 | }
|
| 108 |
|
| 109 | public boolean isReadOnly() {
|
| 110 | return this.readOnly;
|
| 111 | }
|
| 112 |
|
| 113 | public ObjectiveCriteria.RenderType getDefaultRenderType() {
|
| 114 | return this.renderType;
|
| 115 | }
|
| 116 |
|
| 117 | public enum RenderType implements StringRepresentable {
|
| 118 | INTEGER("integer"),
|
| 119 | HEARTS("hearts");
|
| 120 |
|
| 121 | private final String id;
|
| 122 | public static final StringRepresentable.EnumCodec<ObjectiveCriteria.RenderType> CODEC = StringRepresentable.fromEnum(ObjectiveCriteria.RenderType::values);
|
| 123 |
|
| 124 | RenderType(final String id) {
|
| 125 | this.id = id;
|
| 126 | }
|
| 127 |
|
| 128 | public String getId() {
|
| 129 | return this.id;
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public String getSerializedName() {
|
| 134 | return this.id;
|
| 135 | }
|
| 136 |
|
| 137 | public static ObjectiveCriteria.RenderType byId(final String key) {
|
| 138 | return CODEC.byName(key, INTEGER);
|
| 139 | }
|
| 140 | }
|
| 141 | }
|
| 142 |
|