| 1 | package net.minecraft.client.telemetry;
|
| 2 |
|
| 3 | import com.mojang.authlib.minecraft.TelemetryEvent;
|
| 4 | import com.mojang.authlib.minecraft.TelemetrySession;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.DataResult;
|
| 8 | import com.mojang.serialization.MapCodec;
|
| 9 | import it.unimi.dsi.fastutil.objects.Object2ObjectLinkedOpenHashMap;
|
| 10 | import java.util.ArrayList;
|
| 11 | import java.util.HashSet;
|
| 12 | import java.util.List;
|
| 13 | import java.util.Map;
|
| 14 | import java.util.Set;
|
| 15 | import java.util.Map.Entry;
|
| 16 | import java.util.stream.Stream;
|
| 17 | import net.fabricmc.api.EnvType;
|
| 18 | import net.fabricmc.api.Environment;
|
| 19 | import net.minecraft.network.chat.Component;
|
| 20 | import net.minecraft.network.chat.ComponentUtils;
|
| 21 | import net.minecraft.network.chat.MutableComponent;
|
| 22 | import org.slf4j.Logger;
|
| 23 |
|
| 24 | @Environment(EnvType.CLIENT)
|
| 25 | public class TelemetryEventType {
|
| 26 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 27 | private static final Map<String, TelemetryEventType> REGISTRY = new Object2ObjectLinkedOpenHashMap<>();
|
| 28 | public static final Codec<TelemetryEventType> CODEC = Codec.STRING.comapFlatMap(key -> {
|
| 29 | TelemetryEventType type = REGISTRY.get(key);
|
| 30 | return type != null ? DataResult.success(type) : DataResult.error(() -> "No TelemetryEventType with key: '" + key + "'");
|
| 31 | }, TelemetryEventType::id);
|
| 32 | private static final List<TelemetryProperty<?>> GLOBAL_PROPERTIES = List.of(
|
| 33 | TelemetryProperty.USER_ID,
|
| 34 | TelemetryProperty.CLIENT_ID,
|
| 35 | TelemetryProperty.MINECRAFT_SESSION_ID,
|
| 36 | TelemetryProperty.GAME_VERSION,
|
| 37 | TelemetryProperty.OPERATING_SYSTEM,
|
| 38 | TelemetryProperty.PLATFORM,
|
| 39 | TelemetryProperty.CLIENT_MODDED,
|
| 40 | TelemetryProperty.LAUNCHER_NAME,
|
| 41 | TelemetryProperty.EVENT_TIMESTAMP_UTC,
|
| 42 | TelemetryProperty.OPT_IN
|
| 43 | );
|
| 44 | private static final List<TelemetryProperty<?>> WORLD_SESSION_PROPERTIES = Stream.concat(
|
| 45 | GLOBAL_PROPERTIES.stream(),
|
| 46 | Stream.of(TelemetryProperty.WORLD_SESSION_ID, TelemetryProperty.SERVER_MODDED, TelemetryProperty.SERVER_TYPE, TelemetryProperty.SERVER_SESSION_ID)
|
| 47 | )
|
| 48 | .toList();
|
| 49 | public static final TelemetryEventType GRAPHICS_CAPABILITIES = builder("graphics_capabilities", "GraphicsCapabilities")
|
| 50 | .defineAll(GLOBAL_PROPERTIES)
|
| 51 | .define(TelemetryProperty.BACKEND_NAME)
|
| 52 | .define(TelemetryProperty.BACKEND_FAILURE_MESSAGE)
|
| 53 | .define(TelemetryProperty.BACKEND_FAILURE_REASON)
|
| 54 | .define(TelemetryProperty.BACKEND_FAILURE_MISSING_CAPABILITIES)
|
| 55 | .register();
|
| 56 | public static final TelemetryEventType WORLD_LOADED = builder("world_loaded", "WorldLoaded")
|
| 57 | .defineAll(WORLD_SESSION_PROPERTIES)
|
| 58 | .define(TelemetryProperty.GAME_MODE)
|
| 59 | .define(TelemetryProperty.REALMS_MAP_CONTENT)
|
| 60 | .register();
|
| 61 | public static final TelemetryEventType PERFORMANCE_METRICS = builder("performance_metrics", "PerformanceMetrics")
|
| 62 | .defineAll(WORLD_SESSION_PROPERTIES)
|
| 63 | .define(TelemetryProperty.FRAME_RATE_SAMPLES)
|
| 64 | .define(TelemetryProperty.RENDER_TIME_SAMPLES)
|
| 65 | .define(TelemetryProperty.USED_MEMORY_SAMPLES)
|
| 66 | .define(TelemetryProperty.NUMBER_OF_SAMPLES)
|
| 67 | .define(TelemetryProperty.RENDER_DISTANCE)
|
| 68 | .define(TelemetryProperty.DEDICATED_MEMORY_KB)
|
| 69 | .optIn()
|
| 70 | .register();
|
| 71 | public static final TelemetryEventType WORLD_LOAD_TIMES = builder("world_load_times", "WorldLoadTimes")
|
| 72 | .defineAll(WORLD_SESSION_PROPERTIES)
|
| 73 | .define(TelemetryProperty.WORLD_LOAD_TIME_MS)
|
| 74 | .define(TelemetryProperty.NEW_WORLD)
|
| 75 | .optIn()
|
| 76 | .register();
|
| 77 | public static final TelemetryEventType WORLD_UNLOADED = builder("world_unloaded", "WorldUnloaded")
|
| 78 | .defineAll(WORLD_SESSION_PROPERTIES)
|
| 79 | .define(TelemetryProperty.SECONDS_SINCE_LOAD)
|
| 80 | .define(TelemetryProperty.TICKS_SINCE_LOAD)
|
| 81 | .register();
|
| 82 | public static final TelemetryEventType ADVANCEMENT_MADE = builder("advancement_made", "AdvancementMade")
|
| 83 | .defineAll(WORLD_SESSION_PROPERTIES)
|
| 84 | .define(TelemetryProperty.ADVANCEMENT_ID)
|
| 85 | .define(TelemetryProperty.ADVANCEMENT_GAME_TIME)
|
| 86 | .optIn()
|
| 87 | .register();
|
| 88 | public static final TelemetryEventType GAME_LOAD_TIMES = builder("game_load_times", "GameLoadTimes")
|
| 89 | .defineAll(GLOBAL_PROPERTIES)
|
| 90 | .define(TelemetryProperty.LOAD_TIME_TOTAL_TIME_MS)
|
| 91 | .define(TelemetryProperty.LOAD_TIME_PRE_WINDOW_MS)
|
| 92 | .define(TelemetryProperty.LOAD_TIME_BOOTSTRAP_MS)
|
| 93 | .define(TelemetryProperty.LOAD_TIME_LOADING_OVERLAY_MS)
|
| 94 | .optIn()
|
| 95 | .register();
|
| 96 | private final String id;
|
| 97 | private final String exportKey;
|
| 98 | private final List<TelemetryProperty<?>> properties;
|
| 99 | private final boolean isOptIn;
|
| 100 | private final MapCodec<TelemetryEventInstance> codec;
|
| 101 |
|
| 102 | private TelemetryEventType(final String id, final String exportKey, final List<TelemetryProperty<?>> properties, final boolean isOptIn) {
|
| 103 | this.id = id;
|
| 104 | this.exportKey = exportKey;
|
| 105 | this.properties = properties;
|
| 106 | this.isOptIn = isOptIn;
|
| 107 | this.codec = TelemetryPropertyMap.createCodec(properties).xmap(map -> new TelemetryEventInstance(this, map), TelemetryEventInstance::properties);
|
| 108 | }
|
| 109 |
|
| 110 | public static TelemetryEventType.Builder builder(final String id, final String exportKey) {
|
| 111 | return new TelemetryEventType.Builder(id, exportKey);
|
| 112 | }
|
| 113 |
|
| 114 | public String id() {
|
| 115 | return this.id;
|
| 116 | }
|
| 117 |
|
| 118 | public List<TelemetryProperty<?>> properties() {
|
| 119 | return this.properties;
|
| 120 | }
|
| 121 |
|
| 122 | public MapCodec<TelemetryEventInstance> codec() {
|
| 123 | return this.codec;
|
| 124 | }
|
| 125 |
|
| 126 | public boolean isOptIn() {
|
| 127 | return this.isOptIn;
|
| 128 | }
|
| 129 |
|
| 130 | public TelemetryEvent export(final TelemetrySession session, final TelemetryPropertyMap input) {
|
| 131 | TelemetryEvent output = session.createNewEvent(this.exportKey);
|
| 132 |
|
| 133 | for (TelemetryProperty<?> property : this.properties) {
|
| 134 | property.export(input, output);
|
| 135 | }
|
| 136 |
|
| 137 | return output;
|
| 138 | }
|
| 139 |
|
| 140 | public <T> boolean contains(final TelemetryProperty<T> property) {
|
| 141 | return this.properties.contains(property);
|
| 142 | }
|
| 143 |
|
| 144 | @Override
|
| 145 | public String toString() {
|
| 146 | return "TelemetryEventType[" + this.id + "]";
|
| 147 | }
|
| 148 |
|
| 149 | public MutableComponent title() {
|
| 150 | return this.makeTranslation("title");
|
| 151 | }
|
| 152 |
|
| 153 | public MutableComponent description() {
|
| 154 | return this.makeTranslation("description");
|
| 155 | }
|
| 156 |
|
| 157 | private MutableComponent makeTranslation(final String suffix) {
|
| 158 | return Component.translatable("telemetry.event." + this.id + "." + suffix);
|
| 159 | }
|
| 160 |
|
| 161 | public static List<TelemetryEventType> values() {
|
| 162 | return List.copyOf(REGISTRY.values());
|
| 163 | }
|
| 164 |
|
| 165 | public static boolean selfTest() {
|
| 166 | boolean hasErrors = false;
|
| 167 | Set<TelemetryProperty<?>> allProperties = new HashSet<>();
|
| 168 |
|
| 169 | for (Entry<String, TelemetryEventType> entry : REGISTRY.entrySet()) {
|
| 170 | TelemetryEventType type = entry.getValue();
|
| 171 | if (!ComponentUtils.isTranslationResolvable(type.description()) || !ComponentUtils.isTranslationResolvable(type.title())) {
|
| 172 | LOGGER.warn("Missing translations for telemetry event {}", entry.getKey());
|
| 173 | hasErrors = true;
|
| 174 | }
|
| 175 |
|
| 176 | allProperties.addAll(type.properties);
|
| 177 | }
|
| 178 |
|
| 179 | for (TelemetryProperty<?> property : allProperties) {
|
| 180 | if (!ComponentUtils.isTranslationResolvable(property.title())) {
|
| 181 | LOGGER.warn("Missing translation for telemetry property {}", property.id());
|
| 182 | hasErrors = true;
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | return hasErrors;
|
| 187 | }
|
| 188 |
|
| 189 | @Environment(EnvType.CLIENT)
|
| 190 | public static class Builder {
|
| 191 | private final String id;
|
| 192 | private final String exportKey;
|
| 193 | private final List<TelemetryProperty<?>> properties = new ArrayList<>();
|
| 194 | private boolean isOptIn;
|
| 195 |
|
| 196 | private Builder(final String id, final String exportKey) {
|
| 197 | this.id = id;
|
| 198 | this.exportKey = exportKey;
|
| 199 | }
|
| 200 |
|
| 201 | public TelemetryEventType.Builder defineAll(final List<TelemetryProperty<?>> properties) {
|
| 202 | this.properties.addAll(properties);
|
| 203 | return this;
|
| 204 | }
|
| 205 |
|
| 206 | public <T> TelemetryEventType.Builder define(final TelemetryProperty<T> property) {
|
| 207 | this.properties.add(property);
|
| 208 | return this;
|
| 209 | }
|
| 210 |
|
| 211 | public TelemetryEventType.Builder optIn() {
|
| 212 | this.isOptIn = true;
|
| 213 | return this;
|
| 214 | }
|
| 215 |
|
| 216 | public TelemetryEventType register() {
|
| 217 | TelemetryEventType type = new TelemetryEventType(this.id, this.exportKey, List.copyOf(this.properties), this.isOptIn);
|
| 218 | if (TelemetryEventType.REGISTRY.putIfAbsent(this.id, type) != null) {
|
| 219 | throw new IllegalStateException("Duplicate TelemetryEventType with key: '" + this.id + "'");
|
| 220 | } else {
|
| 221 | return type;
|
| 222 | }
|
| 223 | }
|
| 224 | }
|
| 225 | }
|
| 226 |
|