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 ac236dd4c288d21d4689d1e37eca5a5922cf95c2..9bd10bf6877852b684cb3dbfa1cc426aa4ab3df6 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -1610,6 +1610,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 onPlayerJoin(@NotNull ServerPlayer player) { + if (LeavesConfig.msptSyncProtocol) { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + ProtocolUtils.sendPayloadPacket(player, MSPT_SYNC_ENABLE, buf); + } + } + + public static void handlePacket(ServerPlayer player, @NotNull ServerboundCustomPayloadPacket packet) { + if (packet.identifier.equals(MSPT_SYNC_ENABLE)) { + 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 (server.getTickCount() % LeavesConfig.msptSyncTickInterval == 0 && !players.isEmpty()) { + 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)); + } + } + } +}