| 1 | package net.minecraft.client.telemetry;
|
| 2 |
|
| 3 | import com.mojang.serialization.DataResult;
|
| 4 | import com.mojang.serialization.DynamicOps;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.MapLike;
|
| 7 | import com.mojang.serialization.RecordBuilder;
|
| 8 | import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.stream.Stream;
|
| 13 | import net.fabricmc.api.EnvType;
|
| 14 | import net.fabricmc.api.Environment;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class TelemetryPropertyMap {
|
| 19 | private final Map<TelemetryProperty<?>, Object> entries;
|
| 20 |
|
| 21 | private TelemetryPropertyMap(final Map<TelemetryProperty<?>, Object> entries) {
|
| 22 | this.entries = entries;
|
| 23 | }
|
| 24 |
|
| 25 | public static TelemetryPropertyMap.Builder builder() {
|
| 26 | return new TelemetryPropertyMap.Builder();
|
| 27 | }
|
| 28 |
|
| 29 | public static MapCodec<TelemetryPropertyMap> createCodec(final List<TelemetryProperty<?>> properties) {
|
| 30 | return new MapCodec<TelemetryPropertyMap>() {
|
| 31 | public <T> RecordBuilder<T> encode(final TelemetryPropertyMap input, final DynamicOps<T> ops, final RecordBuilder<T> prefix) {
|
| 32 | RecordBuilder<T> result = prefix;
|
| 33 |
|
| 34 | for (TelemetryProperty<?> property : properties) {
|
| 35 | result = this.encodeProperty(input, result, property);
|
| 36 | }
|
| 37 |
|
| 38 | return result;
|
| 39 | }
|
| 40 |
|
| 41 | private <T, V> RecordBuilder<T> encodeProperty(final TelemetryPropertyMap input, final RecordBuilder<T> result, final TelemetryProperty<V> property) {
|
| 42 | V value = input.get(property);
|
| 43 | return value != null ? result.add(property.id(), value, property.codec()) : result;
|
| 44 | }
|
| 45 |
|
| 46 | @Override
|
| 47 | public <T> DataResult<TelemetryPropertyMap> decode(final DynamicOps<T> ops, final MapLike<T> input) {
|
| 48 | DataResult<TelemetryPropertyMap.Builder> result = DataResult.success(new TelemetryPropertyMap.Builder());
|
| 49 |
|
| 50 | for (TelemetryProperty<?> property : properties) {
|
| 51 | result = this.decodeProperty(result, ops, input, property);
|
| 52 | }
|
| 53 |
|
| 54 | return result.map(TelemetryPropertyMap.Builder::build);
|
| 55 | }
|
| 56 |
|
| 57 | private <T, V> DataResult<TelemetryPropertyMap.Builder> decodeProperty(
|
| 58 | final DataResult<TelemetryPropertyMap.Builder> result, final DynamicOps<T> ops, final MapLike<T> input, final TelemetryProperty<V> property
|
| 59 | ) {
|
| 60 | T value = input.get(property.id());
|
| 61 | if (value != null) {
|
| 62 | DataResult<V> parse = property.codec().parse(ops, value);
|
| 63 | return result.apply2stable((b, v) -> b.put(property, (V)v), parse);
|
| 64 | } else {
|
| 65 | return result;
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public <T> Stream<T> keys(final DynamicOps<T> ops) {
|
| 71 | return properties.stream().map(TelemetryProperty::id).map(ops::createString);
|
| 72 | }
|
| 73 | };
|
| 74 | }
|
| 75 |
|
| 76 | public <T> @Nullable T get(final TelemetryProperty<T> property) {
|
| 77 | return (T)this.entries.get(property);
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public String toString() {
|
| 82 | return this.entries.toString();
|
| 83 | }
|
| 84 |
|
| 85 | public Set<TelemetryProperty<?>> propertySet() {
|
| 86 | return this.entries.keySet();
|
| 87 | }
|
| 88 |
|
| 89 | @Environment(EnvType.CLIENT)
|
| 90 | public static class Builder {
|
| 91 | private final Map<TelemetryProperty<?>, Object> entries = new Reference2ObjectOpenHashMap<>();
|
| 92 |
|
| 93 | private Builder() {
|
| 94 | }
|
| 95 |
|
| 96 | public <T> TelemetryPropertyMap.Builder put(final TelemetryProperty<T> property, final T value) {
|
| 97 | this.entries.put(property, value);
|
| 98 | return this;
|
| 99 | }
|
| 100 |
|
| 101 | public <T> TelemetryPropertyMap.Builder putIfNotNull(final TelemetryProperty<T> property, final @Nullable T value) {
|
| 102 | if (value != null) {
|
| 103 | this.entries.put(property, value);
|
| 104 | }
|
| 105 |
|
| 106 | return this;
|
| 107 | }
|
| 108 |
|
| 109 | public TelemetryPropertyMap.Builder putAll(final TelemetryPropertyMap properties) {
|
| 110 | this.entries.putAll(properties.entries);
|
| 111 | return this;
|
| 112 | }
|
| 113 |
|
| 114 | public TelemetryPropertyMap build() {
|
| 115 | return new TelemetryPropertyMap(this.entries);
|
| 116 | }
|
| 117 | }
|
| 118 | }
|
| 119 |
|