From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Wed, 25 Jan 2023 11:03:53 +0800 Subject: [PATCH] Appleskin Protocol diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index 5a36cebb698ff296a057abb2745c60b41c243ad7..4119b5248c7fca37a59ee81d0ce4e7a184cc0f0a 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -1595,6 +1595,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop previousSaturationLevels = new HashMap<>(); + private static final Map previousExhaustionLevels = new HashMap<>(); + + private static final float MINIMUM_EXHAUSTION_CHANGE_THRESHOLD = 0.01F; + + private static final Set players = new HashSet<>(); + + @Contract("_ -> new") + public static @NotNull ResourceLocation id(String path) { + return new ResourceLocation(PROTOCOL_ID, path); + } + + public static void enableAllPlayer() { + for (ServerPlayer player : MinecraftServer.getServer().getPlayerList().getPlayers()) { + onPlayerLoggedIn(player); + } + } + + public static void disableAllPlayer() { + for (ServerPlayer player : MinecraftServer.getServer().getPlayerList().getPlayers()) { + onPlayerLoggedOut(player); + } + } + + public static void onPlayerLoggedIn(@NotNull ServerPlayer player) { + if (LeavesConfig.appleskinProtocol) { + players.add(player); + resetPlayerData(player); + } + } + + public static void onPlayerLoggedOut(@NotNull ServerPlayer player) { + if (LeavesConfig.appleskinProtocol) { + players.remove(player); + resetPlayerData(player); + } + } + + private static void resetPlayerData(@NotNull ServerPlayer player) { + previousExhaustionLevels.remove(player.getUUID()); + previousSaturationLevels.remove(player.getUUID()); + } + + public static void tick() { + if (LeavesConfig.appleskinProtocol) { + for (ServerPlayer player : players) { + FoodData data = player.getFoodData(); + + float saturation = data.getSaturationLevel(); + Float previousSaturation = previousSaturationLevels.get(player.getUUID()); + if (previousSaturation == null || saturation != previousSaturation) { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + buf.writeFloat(saturation); + ProtocolUtils.sendPayloadPacket(player, SATURATION_KEY, buf); + previousSaturationLevels.put(player.getUUID(), saturation); + } + + float exhaustion = data.getExhaustionLevel(); + Float previousExhaustion = previousExhaustionLevels.get(player.getUUID()); + if (previousExhaustion == null || Math.abs(exhaustion - previousExhaustion) >= MINIMUM_EXHAUSTION_CHANGE_THRESHOLD) { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + buf.writeFloat(exhaustion); + ProtocolUtils.sendPayloadPacket(player, EXHAUSTION_KEY, buf); + previousExhaustionLevels.put(player.getUUID(), exhaustion); + } + } + } + } +}