9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-27 19:09:22 +00:00
Files
Leaf/patches/server/0036-Asteor-Bar-protocol.patch

134 lines
5.4 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: HaHaWTH <fsjk947@gmail.com>
Date: Fri, 22 Mar 2024 04:36:25 +0800
Subject: [PATCH] Asteor Bar protocol
This patch is Powered by AsteorBar (https://github.com/afoxxvi/AsteorBarMod)
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 53ae1ecb7acf8e9d8b9b1c50d5a483008b303a66..b91f0ae28f044ceed0b9db46669a4cca2d47cb2d 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
@@ -24,6 +24,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/org/leavesmc/leaves/protocol/AsteorBarProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/AsteorBarProtocol.java
new file mode 100644
index 0000000000000000000000000000000000000000..f2a9cc78796587862dd4ec57e377e1788497071d
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/protocol/AsteorBarProtocol.java
@@ -0,0 +1,106 @@
+package org.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 org.leavesmc.leaves.protocol.core.LeavesProtocol;
+import org.leavesmc.leaves.protocol.core.ProtocolHandler;
+import org.leavesmc.leaves.protocol.core.ProtocolUtils;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+
+@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<UUID, Float> previousSaturationLevels = new HashMap<>();
+ private static final Map<UUID, Float> previousExhaustionLevels = new HashMap<>();
+
+ private static final float THRESHOLD = 0.01F;
+
+ private static final Set<ServerPlayer> 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());
+ }
+}