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] Leaves: Appleskin Protocol Original license: GPLv3 Original project: https://github.com/LeavesMC/Leaves diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java index bae93e3bbe3067250f08b4c8fad160f9bdf6f6d3..b007d9fb7beb350424c14d5b1ac05050e9d92cef 100644 --- a/src/main/java/net/minecraft/server/MinecraftServer.java +++ b/src/main/java/net/minecraft/server/MinecraftServer.java @@ -1567,6 +1567,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 (org.dreeam.leaf.LeafConfig.appleskinProtocol) { + players.add(player); + resetPlayerData(player); + } + } + + public static void onPlayerLoggedOut(@NotNull ServerPlayer player) { + if (org.dreeam.leaf.LeafConfig.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 (org.dreeam.leaf.LeafConfig.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); + } + } + } + } +}