9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-21 07:59:26 +00:00
Files
Leaf/patches/server/0036-Asteor-Bar-protocol.patch
2024-09-03 15:27:31 -04:00

140 lines
6.2 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 6f151c185850738a9f8a575f9c09e3c469f5f9e2..1d8d321ac3f6c8bf12afe22d8b3f37655fa8c46c 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
@@ -13,6 +13,7 @@ public class ProtocolSupport extends ConfigModules {
public static boolean jadeProtocol = false;
public static boolean appleskinProtocol = false;
+ public static boolean asteorBarProtocol = false;
public static boolean chatImageProtocol = false;
public static boolean xaeroMapProtocol = false;
public static int xaeroMapServerID = new Random().nextInt();
@@ -24,6 +25,7 @@ public class ProtocolSupport extends ConfigModules {
public void onLoaded() {
jadeProtocol = config.getBoolean(getBasePath() + ".jade-protocol", jadeProtocol);
appleskinProtocol = config.getBoolean(getBasePath() + ".appleskin-protocol", appleskinProtocol);
+ asteorBarProtocol = config.getBoolean(getBasePath() + ".asteorbar-protocol", asteorBarProtocol);
chatImageProtocol = config.getBoolean(getBasePath() + ".chatimage-protocol", chatImageProtocol);
xaeroMapProtocol = config.getBoolean(getBasePath() + ".xaero-map-protocol", xaeroMapProtocol);
xaeroMapServerID = config.getInt(getBasePath() + ".xaero-map-server-id", xaeroMapServerID);
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..ed8d9888a24d3ae6cf8fe2f8b269554102e451df
--- /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 ResourceLocation.fromNamespaceAndPath(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());
+ }
+}