package net.minecraft.client.telemetry; import com.mojang.authlib.minecraft.TelemetryPropertyContainer; import com.mojang.serialization.Codec; import it.unimi.dsi.fastutil.longs.LongArrayList; import it.unimi.dsi.fastutil.longs.LongList; import java.time.Instant; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.UUID; import java.util.function.Function; import java.util.stream.Collectors; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.telemetry.events.GameLoadTimesEvent.Measurement; import net.minecraft.core.UUIDUtil; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.util.ExtraCodecs; import net.minecraft.util.StringRepresentable; @Environment(EnvType.CLIENT) public record TelemetryProperty(String id, String exportKey, Codec codec, TelemetryProperty.Exporter exporter) { private static final DateTimeFormatter TIMESTAMP_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME.withZone(ZoneId.from(ZoneOffset.UTC)); public static final TelemetryProperty USER_ID = string("user_id", "userId"); public static final TelemetryProperty CLIENT_ID = string("client_id", "clientId"); public static final TelemetryProperty MINECRAFT_SESSION_ID = uuid("minecraft_session_id", "deviceSessionId"); public static final TelemetryProperty GAME_VERSION = string("game_version", "buildDisplayName"); public static final TelemetryProperty OPERATING_SYSTEM = string("operating_system", "buildPlatform"); public static final TelemetryProperty PLATFORM = string("platform", "platform"); public static final TelemetryProperty CLIENT_MODDED = bool("client_modded", "clientModded"); public static final TelemetryProperty LAUNCHER_NAME = string("launcher_name", "launcherName"); public static final TelemetryProperty WORLD_SESSION_ID = uuid("world_session_id", "worldSessionId"); public static final TelemetryProperty SERVER_MODDED = bool("server_modded", "serverModded"); public static final TelemetryProperty SERVER_TYPE = create( "server_type", "serverType", TelemetryProperty.ServerType.CODEC, (telemetryPropertyContainer, string, serverType) -> telemetryPropertyContainer.addProperty(string, serverType.getSerializedName()) ); public static final TelemetryProperty OPT_IN = bool("opt_in", "isOptional"); public static final TelemetryProperty EVENT_TIMESTAMP_UTC = create( "event_timestamp_utc", "eventTimestampUtc", ExtraCodecs.INSTANT_ISO8601, (telemetryPropertyContainer, string, instant) -> telemetryPropertyContainer.addProperty(string, TIMESTAMP_FORMATTER.format(instant)) ); public static final TelemetryProperty GAME_MODE = create( "game_mode", "playerGameMode", TelemetryProperty.GameMode.CODEC, (telemetryPropertyContainer, string, gameMode) -> telemetryPropertyContainer.addProperty(string, gameMode.id()) ); public static final TelemetryProperty REALMS_MAP_CONTENT = string("realms_map_content", "realmsMapContent"); public static final TelemetryProperty SECONDS_SINCE_LOAD = integer("seconds_since_load", "secondsSinceLoad"); public static final TelemetryProperty TICKS_SINCE_LOAD = integer("ticks_since_load", "ticksSinceLoad"); public static final TelemetryProperty FRAME_RATE_SAMPLES = longSamples("frame_rate_samples", "serializedFpsSamples"); public static final TelemetryProperty RENDER_TIME_SAMPLES = longSamples("render_time_samples", "serializedRenderTimeSamples"); public static final TelemetryProperty USED_MEMORY_SAMPLES = longSamples("used_memory_samples", "serializedUsedMemoryKbSamples"); public static final TelemetryProperty NUMBER_OF_SAMPLES = integer("number_of_samples", "numSamples"); public static final TelemetryProperty RENDER_DISTANCE = integer("render_distance", "renderDistance"); public static final TelemetryProperty DEDICATED_MEMORY_KB = integer("dedicated_memory_kb", "dedicatedMemoryKb"); public static final TelemetryProperty WORLD_LOAD_TIME_MS = integer("world_load_time_ms", "worldLoadTimeMs"); public static final TelemetryProperty NEW_WORLD = bool("new_world", "newWorld"); public static final TelemetryProperty LOAD_TIME_TOTAL_TIME_MS = gameLoadMeasurement("load_time_total_time_ms", "loadTimeTotalTimeMs"); public static final TelemetryProperty LOAD_TIME_PRE_WINDOW_MS = gameLoadMeasurement("load_time_pre_window_ms", "loadTimePreWindowMs"); public static final TelemetryProperty LOAD_TIME_BOOTSTRAP_MS = gameLoadMeasurement("load_time_bootstrap_ms", "loadTimeBootstrapMs"); public static final TelemetryProperty LOAD_TIME_LOADING_OVERLAY_MS = gameLoadMeasurement( "load_time_loading_overlay_ms", "loadTimeLoadingOverlayMs" ); public static final TelemetryProperty ADVANCEMENT_ID = string("advancement_id", "advancementId"); public static final TelemetryProperty ADVANCEMENT_GAME_TIME = makeLong("advancement_game_time", "advancementGameTime"); public static TelemetryProperty create(String id, String exportKey, Codec codec, TelemetryProperty.Exporter exporter) { return new TelemetryProperty<>(id, exportKey, codec, exporter); } public static TelemetryProperty bool(String id, String exportKey) { return create(id, exportKey, Codec.BOOL, TelemetryPropertyContainer::addProperty); } public static TelemetryProperty string(String id, String exportKey) { return create(id, exportKey, Codec.STRING, TelemetryPropertyContainer::addProperty); } public static TelemetryProperty integer(String id, String exportKey) { return create(id, exportKey, Codec.INT, TelemetryPropertyContainer::addProperty); } public static TelemetryProperty makeLong(String id, String exportKey) { return create(id, exportKey, Codec.LONG, TelemetryPropertyContainer::addProperty); } public static TelemetryProperty uuid(String id, String exportKey) { return create( id, exportKey, UUIDUtil.STRING_CODEC, (telemetryPropertyContainer, string, uUID) -> telemetryPropertyContainer.addProperty(string, uUID.toString()) ); } public static TelemetryProperty gameLoadMeasurement(String id, String exportKey) { return create( id, exportKey, Measurement.CODEC, (telemetryPropertyContainer, string, measurement) -> telemetryPropertyContainer.addProperty(string, measurement.millis()) ); } public static TelemetryProperty longSamples(String id, String exportKey) { return create( id, exportKey, Codec.LONG.listOf().xmap(LongArrayList::new, Function.identity()), (telemetryPropertyContainer, string, longList) -> telemetryPropertyContainer.addProperty( string, (String)longList.longStream().mapToObj(String::valueOf).collect(Collectors.joining(";")) ) ); } public void export(TelemetryPropertyMap propertyMap, TelemetryPropertyContainer container) { T object = propertyMap.get(this); if (object != null) { this.exporter.apply(container, this.exportKey, object); } else { container.addNullProperty(this.exportKey); } } public MutableComponent title() { return Component.translatable("telemetry.property." + this.id + ".title"); } public String toString() { return "TelemetryProperty[" + this.id + "]"; } @Environment(EnvType.CLIENT) public interface Exporter { void apply(TelemetryPropertyContainer telemetryPropertyContainer, String string, T object); } @Environment(EnvType.CLIENT) public static enum GameMode implements StringRepresentable { SURVIVAL("survival", 0), CREATIVE("creative", 1), ADVENTURE("adventure", 2), SPECTATOR("spectator", 6), HARDCORE("hardcore", 99); public static final Codec CODEC = StringRepresentable.fromEnum(TelemetryProperty.GameMode::values); private final String key; private final int id; private GameMode(final String key, final int id) { this.key = key; this.id = id; } public int id() { return this.id; } @Override public String getSerializedName() { return this.key; } } @Environment(EnvType.CLIENT) public static enum ServerType implements StringRepresentable { REALM("realm"), LOCAL("local"), OTHER("server"); public static final Codec CODEC = StringRepresentable.fromEnum(TelemetryProperty.ServerType::values); private final String key; private ServerType(final String key) { this.key = key; } @Override public String getSerializedName() { return this.key; } } }