From 2746aaf362898ceab81636ade9ec6e4d9f6192a1 Mon Sep 17 00:00:00 2001 From: DGun Otto Date: Fri, 22 Mar 2024 21:37:36 +0800 Subject: [PATCH] AsteorBar Protocol (#39) * AsteorBar Protocol --- patches/server/0078-asteor-bar-protocol.patch | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 patches/server/0078-asteor-bar-protocol.patch diff --git a/patches/server/0078-asteor-bar-protocol.patch b/patches/server/0078-asteor-bar-protocol.patch new file mode 100644 index 000000000..c9158dee7 --- /dev/null +++ b/patches/server/0078-asteor-bar-protocol.patch @@ -0,0 +1,128 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: HaHaWTH +Date: Fri, 22 Mar 2024 04:36:25 +0800 +Subject: [PATCH] asteor-bar-protocol + + +diff --git a/src/main/java/org/dreeam/leaf/config/modules/network/ProtocolSupport.java b/src/main/java/org/dreeam/leaf/config/modules/network/ProtocolSupport.java +index 1c1abbb3a74a519520d2c1229721887c19d9df2e..92bac2a84fef7b91f033479f2362ce7a2009a51a 100644 +--- a/src/main/java/org/dreeam/leaf/config/modules/network/ProtocolSupport.java ++++ b/src/main/java/org/dreeam/leaf/config/modules/network/ProtocolSupport.java +@@ -25,6 +25,9 @@ public class ProtocolSupport implements IConfigModule { + @ConfigInfo(baseName = "appleskin-protocol") + public static boolean appleskinProtocol = false; + ++ @ConfigInfo(baseName = "asteorbar-protocol") ++ public static boolean asteorBarProtocol = false; ++ + @ConfigInfo(baseName = "chatImage-protocol") + public static boolean chatImageProtocol = false; + +diff --git a/src/main/java/top/leavesmc/leaves/protocol/AsteorBarProtocol.java b/src/main/java/top/leavesmc/leaves/protocol/AsteorBarProtocol.java +new file mode 100644 +index 0000000000000000000000000000000000000000..9651c6c83d17b42edd81070890bd81dcb887f898 +--- /dev/null ++++ b/src/main/java/top/leavesmc/leaves/protocol/AsteorBarProtocol.java +@@ -0,0 +1,102 @@ ++package top.leavesmc.leaves.protocol; ++ ++import net.minecraft.resources.ResourceLocation; ++import net.minecraft.server.MinecraftServer; ++import net.minecraft.server.level.ServerPlayer; ++import net.minecraft.world.food.FoodData; ++import org.jetbrains.annotations.Contract; ++import org.jetbrains.annotations.NotNull; ++import top.leavesmc.leaves.protocol.core.LeavesProtocol; ++import top.leavesmc.leaves.protocol.core.ProtocolHandler; ++import top.leavesmc.leaves.protocol.core.ProtocolUtils; ++ ++import java.util.*; ++ ++@LeavesProtocol(namespace = "asteorbar") ++public class AsteorBarProtocol { ++ ++ public static final String PROTOCOL_ID = "asteorbar"; ++ ++ private static final ResourceLocation NETWORK_KEY = id("network"); ++ ++ private static final Map previousSaturationLevels = new HashMap<>(); ++ private static final Map previousExhaustionLevels = new HashMap<>(); ++ ++ private static final float 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); ++ } ++ ++ @ProtocolHandler.PlayerJoin ++ public static void onPlayerLoggedIn(@NotNull ServerPlayer player) { ++ if (org.dreeam.leaf.config.modules.network.ProtocolSupport.asteorBarProtocol) { ++ resetPlayerData(player); ++ } ++ } ++ ++ @ProtocolHandler.PlayerLeave ++ public static void onPlayerLoggedOut(@NotNull ServerPlayer player) { ++ if (org.dreeam.leaf.config.modules.network.ProtocolSupport.asteorBarProtocol) { ++ players.remove(player); ++ resetPlayerData(player); ++ } ++ } ++ ++ @ProtocolHandler.MinecraftRegister(ignoreId = true) ++ public static void onPlayerSubscribed(@NotNull ServerPlayer player) { ++ if (org.dreeam.leaf.config.modules.network.ProtocolSupport.asteorBarProtocol) { ++ players.add(player); ++ } ++ } ++ ++ @ProtocolHandler.Ticker ++ public static void tick() { ++ if (org.dreeam.leaf.config.modules.network.ProtocolSupport.asteorBarProtocol) { ++ for (ServerPlayer player : players) { ++ FoodData data = player.getFoodData(); ++ ++ float saturation = data.getSaturationLevel(); ++ Float previousSaturation = previousSaturationLevels.get(player.getUUID()); ++ if (previousSaturation == null || saturation != previousSaturation) { ++ ProtocolUtils.sendPayloadPacket(player, NETWORK_KEY, buf -> { ++ buf.writeByte(1); ++ buf.writeFloat(saturation); ++ }); ++ previousSaturationLevels.put(player.getUUID(), saturation); ++ } ++ ++ float exhaustion = data.getExhaustionLevel(); ++ Float previousExhaustion = previousExhaustionLevels.get(player.getUUID()); ++ if (previousExhaustion == null || Math.abs(exhaustion - previousExhaustion) >= THRESHOLD) { ++ ProtocolUtils.sendPayloadPacket(player, NETWORK_KEY, buf -> { ++ buf.writeByte(0); ++ buf.writeFloat(exhaustion); ++ }); ++ previousExhaustionLevels.put(player.getUUID(), exhaustion); ++ } ++ } ++ } ++ } ++ ++ @ProtocolHandler.ReloadServer ++ public static void onServerReload() { ++ if (!org.dreeam.leaf.config.modules.network.ProtocolSupport.asteorBarProtocol) { ++ disableAllPlayer(); ++ } ++ } ++ ++ public static void disableAllPlayer() { ++ for (ServerPlayer player : MinecraftServer.getServer().getPlayerList().getPlayers()) { ++ onPlayerLoggedOut(player); ++ } ++ } ++ ++ private static void resetPlayerData(@NotNull ServerPlayer player) { ++ previousExhaustionLevels.remove(player.getUUID()); ++ previousSaturationLevels.remove(player.getUUID()); ++ } ++}