-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* AsteorBar Protocol
- Loading branch information
Showing
1 changed file
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
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 | ||
|
||
|
||
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<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()); | ||
+ } | ||
+} |