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 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 9324ca1f1abd2343b2f1eaec019e84428f01c626..45c422b993e254fd892bfd6f47a074f1e9688714 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 @@ -21,6 +21,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 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()); + } +}