| 1 | package net.minecraft.client.quickplay;
|
| 2 |
|
| 3 | import com.google.gson.Gson;
|
| 4 | import com.google.gson.GsonBuilder;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import com.mojang.serialization.Codec;
|
| 7 | import com.mojang.serialization.JsonOps;
|
| 8 | import com.mojang.serialization.MapCodec;
|
| 9 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 10 | import java.io.IOException;
|
| 11 | import java.nio.file.Files;
|
| 12 | import java.nio.file.Path;
|
| 13 | import java.time.Instant;
|
| 14 | import java.util.List;
|
| 15 | import net.fabricmc.api.EnvType;
|
| 16 | import net.fabricmc.api.Environment;
|
| 17 | import net.minecraft.client.Minecraft;
|
| 18 | import net.minecraft.util.ExtraCodecs;
|
| 19 | import net.minecraft.util.StringRepresentable;
|
| 20 | import net.minecraft.util.Util;
|
| 21 | import net.minecraft.world.level.GameType;
|
| 22 | import org.jspecify.annotations.Nullable;
|
| 23 | import org.slf4j.Logger;
|
| 24 |
|
| 25 | @Environment(EnvType.CLIENT)
|
| 26 | public class QuickPlayLog {
|
| 27 | private static final QuickPlayLog INACTIVE = new QuickPlayLog("") {
|
| 28 | @Override
|
| 29 | public void log(final Minecraft minecraft) {
|
| 30 | }
|
| 31 |
|
| 32 | @Override
|
| 33 | public void setWorldData(final QuickPlayLog.Type type, final String id, final String name) {
|
| 34 | }
|
| 35 | };
|
| 36 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 37 | private static final Gson GSON = new GsonBuilder().create();
|
| 38 | private final Path path;
|
| 39 | private QuickPlayLog.@Nullable QuickPlayWorld worldData;
|
| 40 |
|
| 41 | private QuickPlayLog(final String quickPlayPath) {
|
| 42 | this.path = Minecraft.getInstance().gameDirectory.toPath().resolve(quickPlayPath);
|
| 43 | }
|
| 44 |
|
| 45 | public static QuickPlayLog of(final @Nullable String path) {
|
| 46 | return path == null ? INACTIVE : new QuickPlayLog(path);
|
| 47 | }
|
| 48 |
|
| 49 | public void setWorldData(final QuickPlayLog.Type type, final String id, final String name) {
|
| 50 | this.worldData = new QuickPlayLog.QuickPlayWorld(type, id, name);
|
| 51 | }
|
| 52 |
|
| 53 | public void log(final Minecraft minecraft) {
|
| 54 | if (minecraft.gameMode != null && this.worldData != null) {
|
| 55 | Util.ioPool()
|
| 56 | .execute(
|
| 57 | () -> {
|
| 58 | try {
|
| 59 | Files.deleteIfExists(this.path);
|
| 60 | } catch (IOException e) {
|
| 61 | LOGGER.error("Failed to delete quickplay log file {}", this.path, e);
|
| 62 | }
|
| 63 |
|
| 64 | QuickPlayLog.QuickPlayEntry quickPlayEntry = new QuickPlayLog.QuickPlayEntry(this.worldData, Instant.now(), minecraft.gameMode.getPlayerMode());
|
| 65 | Codec.list(QuickPlayLog.QuickPlayEntry.CODEC)
|
| 66 | .encodeStart(JsonOps.INSTANCE, List.of(quickPlayEntry))
|
| 67 | .resultOrPartial(Util.prefix("Quick Play: ", LOGGER::error))
|
| 68 | .ifPresent(json -> {
|
| 69 | try {
|
| 70 | Files.createDirectories(this.path.getParent());
|
| 71 | Files.writeString(this.path, GSON.toJson(json));
|
| 72 | } catch (IOException e) {
|
| 73 | LOGGER.error("Failed to write to quickplay log file {}", this.path, e);
|
| 74 | }
|
| 75 | });
|
| 76 | }
|
| 77 | );
|
| 78 | } else {
|
| 79 | LOGGER.error("Failed to log session for quickplay. Missing world data or gamemode");
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | @Environment(EnvType.CLIENT)
|
| 84 | private record QuickPlayEntry(QuickPlayLog.QuickPlayWorld quickPlayWorld, Instant lastPlayedTime, GameType gamemode) {
|
| 85 | public static final Codec<QuickPlayLog.QuickPlayEntry> CODEC = RecordCodecBuilder.create(
|
| 86 | i -> i.group(
|
| 87 | QuickPlayLog.QuickPlayWorld.MAP_CODEC.forGetter(QuickPlayLog.QuickPlayEntry::quickPlayWorld),
|
| 88 | ExtraCodecs.INSTANT_ISO8601.fieldOf("lastPlayedTime").forGetter(QuickPlayLog.QuickPlayEntry::lastPlayedTime),
|
| 89 | GameType.CODEC.fieldOf("gamemode").forGetter(QuickPlayLog.QuickPlayEntry::gamemode)
|
| 90 | )
|
| 91 | .apply(i, QuickPlayLog.QuickPlayEntry::new)
|
| 92 | );
|
| 93 | }
|
| 94 |
|
| 95 | @Environment(EnvType.CLIENT)
|
| 96 | private record QuickPlayWorld(QuickPlayLog.Type type, String id, String name) {
|
| 97 | public static final MapCodec<QuickPlayLog.QuickPlayWorld> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 98 | i -> i.group(
|
| 99 | QuickPlayLog.Type.CODEC.fieldOf("type").forGetter(QuickPlayLog.QuickPlayWorld::type),
|
| 100 | ExtraCodecs.ESCAPED_STRING.fieldOf("id").forGetter(QuickPlayLog.QuickPlayWorld::id),
|
| 101 | Codec.STRING.fieldOf("name").forGetter(QuickPlayLog.QuickPlayWorld::name)
|
| 102 | )
|
| 103 | .apply(i, QuickPlayLog.QuickPlayWorld::new)
|
| 104 | );
|
| 105 | }
|
| 106 |
|
| 107 | @Environment(EnvType.CLIENT)
|
| 108 | public enum Type implements StringRepresentable {
|
| 109 | SINGLEPLAYER("singleplayer"),
|
| 110 | MULTIPLAYER("multiplayer"),
|
| 111 | REALMS("realms");
|
| 112 |
|
| 113 | private static final Codec<QuickPlayLog.Type> CODEC = StringRepresentable.fromEnum(QuickPlayLog.Type::values);
|
| 114 | private final String name;
|
| 115 |
|
| 116 | Type(final String name) {
|
| 117 | this.name = name;
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public String getSerializedName() {
|
| 122 | return this.name;
|
| 123 | }
|
| 124 | }
|
| 125 | }
|
| 126 |
|