From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Wed, 25 Jan 2023 11:03:53 +0800 Subject: [PATCH] Leaves: Appleskin Protocol Original license: GPLv3 Original project: https://github.com/LeavesMC/Leaves 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 b63d87d0593d116b0c5ee835dc4372f1b5542453..ef23577faaf7948d7a9b1def4d56506eab19cc80 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 @@ -18,4 +18,7 @@ public class ProtocolSupport implements IConfigModule { @ConfigInfo(baseName = "jade-protocol") public static boolean jadeProtocol = false; + + @ConfigInfo(baseName = "appleskin-protocol") + public static boolean appleskinProtocol = false; } diff --git a/src/main/java/top/leavesmc/leaves/protocol/AppleSkinProtocol.java b/src/main/java/top/leavesmc/leaves/protocol/AppleSkinProtocol.java new file mode 100644 index 0000000000000000000000000000000000000000..2e7ad52155b4308a0361c3cdc0ecec97f912ef7f --- /dev/null +++ b/src/main/java/top/leavesmc/leaves/protocol/AppleSkinProtocol.java @@ -0,0 +1,105 @@ +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.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.UUID; + +@LeavesProtocol(namespace = "appleskin") +public class AppleSkinProtocol { + + public static final String PROTOCOL_ID = "appleskin"; + + private static final ResourceLocation SATURATION_KEY = id("saturation_sync"); + private static final ResourceLocation EXHAUSTION_KEY = id("exhaustion_sync"); + + private static final Map previousSaturationLevels = new HashMap<>(); + private static final Map previousExhaustionLevels = new HashMap<>(); + + private static final float MINIMUM_EXHAUSTION_CHANGE_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.appleskinProtocol) { + resetPlayerData(player); + } + } + + @ProtocolHandler.PlayerLeave + public static void onPlayerLoggedOut(@NotNull ServerPlayer player) { + if (org.dreeam.leaf.config.modules.network.ProtocolSupport.appleskinProtocol) { + players.remove(player); + resetPlayerData(player); + } + } + + @ProtocolHandler.MinecraftRegister(ignoreId = true) + public static void onPlayerSubscribed(@NotNull ServerPlayer player) { + if (org.dreeam.leaf.config.modules.network.ProtocolSupport.appleskinProtocol) { + players.add(player); + } + } + + @ProtocolHandler.Ticker + public static void tick() { + if (org.dreeam.leaf.config.modules.network.ProtocolSupport.appleskinProtocol) { + 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, SATURATION_KEY, buf -> { + 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) >= MINIMUM_EXHAUSTION_CHANGE_THRESHOLD) { + ProtocolUtils.sendPayloadPacket(player, EXHAUSTION_KEY, buf -> { + buf.writeFloat(exhaustion); + }); + previousExhaustionLevels.put(player.getUUID(), exhaustion); + } + } + } + } + + @ProtocolHandler.ReloadServer + public static void onServerReload() { + if (!org.dreeam.leaf.config.modules.network.ProtocolSupport.appleskinProtocol) { + 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()); + } +}