From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Mon, 3 Jul 2023 22:12:16 +0800 Subject: [PATCH] Bladeren mspt sync protocol diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index 5c09e2164a045346e0e2d4ce64408ac9209cf501..d47e29850462356e843591deba7e8a83f59faba0 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -1611,6 +1611,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop players = new ArrayList<>(); + + @Contract("_ -> new") + public static @NotNull ResourceLocation id(String path) { + return new ResourceLocation(PROTOCOL_ID, path); + } + + public static void init() { + LeavesProtocol.registerFeature("mspt_sync", (player, compoundTag) -> { + if (compoundTag.getString("Value").equals("true")) { + onPlayerSubmit(player); + } else { + onPlayerLoggedOut(player); + } + }); + } + + public static void onPlayerSubmit(@NotNull ServerPlayer player) { + if (LeavesConfig.msptSyncProtocol) { + players.add(player); + } + } + + public static void onPlayerLoggedOut(@NotNull ServerPlayer player) { + if (LeavesConfig.msptSyncProtocol) { + players.remove(player); + } + } + + public static void tick(MinecraftServer server) { + if (LeavesConfig.msptSyncProtocol) { + if (players.isEmpty()) { + return; + } + + if (server.getTickCount() % LeavesConfig.msptSyncTickInterval == 0) { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + double mspt = Arrays.stream(server.tickTimes).average().getAsDouble() * 1.0E-6D; + double tps = 1000.0D / Math.max(mspt, 50); + buf.writeDouble(mspt); + buf.writeDouble(tps); + players.forEach(player -> ProtocolUtils.sendPayloadPacket(player, MSPT_SYNC, buf)); + } + } + } +}