From 20bcc896bb624afa5bc3f07ea4937423e673d218 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Wed, 25 Jan 2023 11:06:19 +0800 Subject: [PATCH] Appleskin Protocol (#12) --- patches/server/0058-Appleskin-Protocol.patch | 173 +++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 patches/server/0058-Appleskin-Protocol.patch diff --git a/patches/server/0058-Appleskin-Protocol.patch b/patches/server/0058-Appleskin-Protocol.patch new file mode 100644 index 00000000..f3b8ef66 --- /dev/null +++ b/patches/server/0058-Appleskin-Protocol.patch @@ -0,0 +1,173 @@ +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 f6bc15c331f22aba5cbb41261dbc10a4dc1e2a8d..a611b34058c79383db1be22e9f755e36edfa6ff1 100644 +--- a/src/main/java/net/minecraft/server/MinecraftServer.java ++++ b/src/main/java/net/minecraft/server/MinecraftServer.java +@@ -1582,6 +1582,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); ++ } ++ } ++ } ++ } ++}