| 1 | package net.minecraft.stats;
|
| 2 |
|
| 3 | import com.google.common.collect.Sets;
|
| 4 | import com.google.gson.Gson;
|
| 5 | import com.google.gson.GsonBuilder;
|
| 6 | import com.google.gson.JsonElement;
|
| 7 | import com.google.gson.JsonIOException;
|
| 8 | import com.google.gson.JsonObject;
|
| 9 | import com.google.gson.JsonParseException;
|
| 10 | import com.mojang.datafixers.DataFixer;
|
| 11 | import com.mojang.logging.LogUtils;
|
| 12 | import com.mojang.serialization.Codec;
|
| 13 | import com.mojang.serialization.DataResult;
|
| 14 | import com.mojang.serialization.Dynamic;
|
| 15 | import com.mojang.serialization.JsonOps;
|
| 16 | import it.unimi.dsi.fastutil.objects.Object2IntMap;
|
| 17 | import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
| 18 | import java.io.IOException;
|
| 19 | import java.io.Reader;
|
| 20 | import java.io.Writer;
|
| 21 | import java.nio.charset.StandardCharsets;
|
| 22 | import java.nio.file.Files;
|
| 23 | import java.nio.file.Path;
|
| 24 | import java.util.HashMap;
|
| 25 | import java.util.Map;
|
| 26 | import java.util.Set;
|
| 27 | import java.util.stream.Collectors;
|
| 28 | import net.minecraft.SharedConstants;
|
| 29 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 30 | import net.minecraft.nbt.NbtUtils;
|
| 31 | import net.minecraft.network.protocol.game.ClientboundAwardStatsPacket;
|
| 32 | import net.minecraft.server.MinecraftServer;
|
| 33 | import net.minecraft.server.level.ServerPlayer;
|
| 34 | import net.minecraft.util.FileUtil;
|
| 35 | import net.minecraft.util.StrictJsonParser;
|
| 36 | import net.minecraft.util.Util;
|
| 37 | import net.minecraft.util.datafix.DataFixTypes;
|
| 38 | import net.minecraft.world.entity.player.Player;
|
| 39 | import org.slf4j.Logger;
|
| 40 |
|
| 41 | public class ServerStatsCounter extends StatsCounter {
|
| 42 | private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
|
| 43 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 44 | private static final Codec<Map<Stat<?>, Integer>> STATS_CODEC = Codec.dispatchedMap(
|
| 45 | BuiltInRegistries.STAT_TYPE.byNameCodec(), Util.memoize(ServerStatsCounter::createTypedStatsCodec)
|
| 46 | )
|
| 47 | .xmap(groupedStats -> {
|
| 48 | Map<Stat<?>, Integer> stats = new HashMap<>();
|
| 49 | groupedStats.forEach((type, values) -> stats.putAll((Map<? extends Stat<?>, ? extends Integer>)values));
|
| 50 | return stats;
|
| 51 | }, map -> map.entrySet().stream().collect(Collectors.groupingBy(entry -> entry.getKey().getType(), Util.toMap())));
|
| 52 | private final Path file;
|
| 53 | private final Set<Stat<?>> dirty = Sets.newHashSet();
|
| 54 |
|
| 55 | private static <T> Codec<Map<Stat<?>, Integer>> createTypedStatsCodec(final StatType<T> type) {
|
| 56 | Codec<T> valueCodec = type.getRegistry().byNameCodec();
|
| 57 | Codec<Stat<?>> statCodec = valueCodec.flatComapMap(
|
| 58 | type::get,
|
| 59 | stat -> stat.getType() == type ? DataResult.success((T)stat.getValue()) : DataResult.error(() -> "Expected type " + type + ", but got " + stat.getType())
|
| 60 | );
|
| 61 | return Codec.unboundedMap(statCodec, Codec.INT);
|
| 62 | }
|
| 63 |
|
| 64 | public ServerStatsCounter(final MinecraftServer server, final Path file) {
|
| 65 | this.file = file;
|
| 66 | if (Files.isRegularFile(file)) {
|
| 67 | try (Reader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) {
|
| 68 | JsonElement element = StrictJsonParser.parse(reader);
|
| 69 | this.parse(server.getFixerUpper(), element);
|
| 70 | } catch (IOException e) {
|
| 71 | LOGGER.error("Couldn't read statistics file {}", file, e);
|
| 72 | } catch (JsonParseException e) {
|
| 73 | LOGGER.error("Couldn't parse statistics file {}", file, e);
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | public void save() {
|
| 79 | try {
|
| 80 | FileUtil.createDirectoriesSafe(this.file.getParent());
|
| 81 |
|
| 82 | try (Writer writer = Files.newBufferedWriter(this.file, StandardCharsets.UTF_8)) {
|
| 83 | GSON.toJson(this.toJson(), GSON.newJsonWriter(writer));
|
| 84 | }
|
| 85 | } catch (IOException | JsonIOException e) {
|
| 86 | LOGGER.error("Couldn't save stats to {}", this.file, e);
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public void setValue(final Player player, final Stat<?> stat, final int count) {
|
| 92 | super.setValue(player, stat, count);
|
| 93 | this.dirty.add(stat);
|
| 94 | }
|
| 95 |
|
| 96 | private Set<Stat<?>> getDirty() {
|
| 97 | Set<Stat<?>> result = Sets.newHashSet(this.dirty);
|
| 98 | this.dirty.clear();
|
| 99 | return result;
|
| 100 | }
|
| 101 |
|
| 102 | public void parse(final DataFixer fixerUpper, final JsonElement element) {
|
| 103 | Dynamic<JsonElement> data = new Dynamic<>(JsonOps.INSTANCE, element);
|
| 104 | data = DataFixTypes.STATS.updateToCurrentVersion(fixerUpper, data, NbtUtils.getDataVersion(data, 1343));
|
| 105 | this.stats
|
| 106 | .putAll(
|
| 107 | STATS_CODEC.parse(data.get("stats").orElseEmptyMap())
|
| 108 | .resultOrPartial(error -> LOGGER.error("Failed to parse statistics for {}: {}", this.file, error))
|
| 109 | .orElse(Map.of())
|
| 110 | );
|
| 111 | }
|
| 112 |
|
| 113 | protected JsonElement toJson() {
|
| 114 | JsonObject result = new JsonObject();
|
| 115 | result.add("stats", STATS_CODEC.encodeStart(JsonOps.INSTANCE, this.stats).getOrThrow());
|
| 116 | result.addProperty("DataVersion", SharedConstants.getCurrentVersion().dataVersion().version());
|
| 117 | return result;
|
| 118 | }
|
| 119 |
|
| 120 | public void markAllDirty() {
|
| 121 | this.dirty.addAll(this.stats.keySet());
|
| 122 | }
|
| 123 |
|
| 124 | public void sendStats(final ServerPlayer player) {
|
| 125 | Object2IntMap<Stat<?>> statsToSend = new Object2IntOpenHashMap<>();
|
| 126 |
|
| 127 | for (Stat<?> stat : this.getDirty()) {
|
| 128 | statsToSend.put(stat, this.getValue(stat));
|
| 129 | }
|
| 130 |
|
| 131 | player.connection.send(new ClientboundAwardStatsPacket(statsToSend));
|
| 132 | }
|
| 133 | }
|
| 134 |
|