| 1 | package net.minecraft.stats;
|
| 2 |
|
| 3 | import java.util.Objects;
|
| 4 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 5 | import net.minecraft.core.registries.Registries;
|
| 6 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 7 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 8 | import net.minecraft.network.codec.StreamCodec;
|
| 9 | import net.minecraft.resources.Identifier;
|
| 10 | import net.minecraft.world.scores.criteria.ObjectiveCriteria;
|
| 11 | import org.jspecify.annotations.Nullable;
|
| 12 |
|
| 13 | public class Stat<T> extends ObjectiveCriteria {
|
| 14 | public static final StreamCodec<RegistryFriendlyByteBuf, Stat<?>> STREAM_CODEC = ByteBufCodecs.registry(Registries.STAT_TYPE)
|
| 15 | .dispatch(Stat::getType, StatType::streamCodec);
|
| 16 | private final StatFormatter formatter;
|
| 17 | private final T value;
|
| 18 | private final StatType<T> type;
|
| 19 |
|
| 20 | protected Stat(final StatType<T> type, final T value, final StatFormatter formatter) {
|
| 21 | super(buildName(type, value));
|
| 22 | this.type = type;
|
| 23 | this.formatter = formatter;
|
| 24 | this.value = value;
|
| 25 | }
|
| 26 |
|
| 27 | public static <T> String buildName(final StatType<T> type, final T value) {
|
| 28 | return locationToKey(BuiltInRegistries.STAT_TYPE.getKey(type)) + ":" + locationToKey(type.getRegistry().getKey(value));
|
| 29 | }
|
| 30 |
|
| 31 | private static String locationToKey(final @Nullable Identifier location) {
|
| 32 | return location.toString().replace(':', '.');
|
| 33 | }
|
| 34 |
|
| 35 | public StatType<T> getType() {
|
| 36 | return this.type;
|
| 37 | }
|
| 38 |
|
| 39 | public T getValue() {
|
| 40 | return this.value;
|
| 41 | }
|
| 42 |
|
| 43 | public String format(final int value) {
|
| 44 | return this.formatter.format(value);
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public boolean equals(final Object o) {
|
| 49 | return this == o || o instanceof Stat && Objects.equals(this.getName(), ((Stat)o).getName());
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public int hashCode() {
|
| 54 | return this.getName().hashCode();
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public String toString() {
|
| 59 | return "Stat{name=" + this.getName() + ", formatter=" + this.formatter + "}";
|
| 60 | }
|
| 61 | }
|
| 62 |
|