9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-28 03:19:21 +00:00
Files
Leaf/patches/server/0030-Leaves-Appleskin-Protocol.patch
2024-12-13 21:19:57 -05:00

161 lines
7.4 KiB
Diff

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
This patch is Powered by AppleSkin (https://github.com/squeek502/AppleSkin)
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 4e91895387b214cadc658b150ed6b88efb58d6cd..90ab5ec9906207cbe835e56eb4560cd89af9fcfe 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
@@ -10,9 +10,13 @@ public class ProtocolSupport extends ConfigModules {
}
public static boolean jadeProtocol = false;
+ public static boolean appleskinProtocol = false;
+ public static int appleskinSyncTickInterval = 20;
@Override
public void onLoaded() {
jadeProtocol = config.getBoolean(getBasePath() + ".jade-protocol", jadeProtocol);
+ appleskinProtocol = config.getBoolean(getBasePath() + ".appleskin-protocol", appleskinProtocol);
+ appleskinSyncTickInterval = config.getInt(getBasePath() + ".appleskin-protocol-sync-tick-interval", appleskinSyncTickInterval);
}
}
diff --git a/src/main/java/org/leavesmc/leaves/protocol/AppleSkinProtocol.java b/src/main/java/org/leavesmc/leaves/protocol/AppleSkinProtocol.java
new file mode 100644
index 0000000000000000000000000000000000000000..eb51acf0351769af0910ebb2d4a5abf542cbfb90
--- /dev/null
+++ b/src/main/java/org/leavesmc/leaves/protocol/AppleSkinProtocol.java
@@ -0,0 +1,126 @@
+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 net.minecraft.world.level.GameRules;
+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;
+
+@LeavesProtocol(namespace = "appleskin")
+public class AppleSkinProtocol {
+
+ public static final String PROTOCOL_ID = "appleskin";
+
+ private static final ResourceLocation SATURATION_KEY = id("saturation");
+ private static final ResourceLocation EXHAUSTION_KEY = id("exhaustion");
+ private static final ResourceLocation NATURAL_REGENERATION_KEY = id("natural_regeneration");
+
+ private static final float MINIMUM_EXHAUSTION_CHANGE_THRESHOLD = 0.01F;
+
+ private static final Map<ServerPlayer, Float> previousSaturationLevels = new HashMap<>();
+ private static final Map<ServerPlayer, Float> previousExhaustionLevels = new HashMap<>();
+ private static final Map<ServerPlayer, Boolean> previousNaturalRegeneration = new HashMap<>();
+
+ private static final Map<ServerPlayer, Set<String>> subscribedChannels = new HashMap<>();
+
+ @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) {
+ subscribedChannels.remove(player);
+ resetPlayerData(player);
+ }
+ }
+
+ @ProtocolHandler.MinecraftRegister(ignoreId = true)
+ public static void onPlayerSubscribed(@NotNull ServerPlayer player, String channel) {
+ if (org.dreeam.leaf.config.modules.network.ProtocolSupport.appleskinProtocol) {
+ subscribedChannels.computeIfAbsent(player, k -> new HashSet<>()).add(channel);
+ }
+ }
+
+ @ProtocolHandler.Ticker
+ public static void tick() {
+ if (org.dreeam.leaf.config.modules.network.ProtocolSupport.appleskinProtocol) {
+ if (MinecraftServer.getServer().getTickCount() % org.dreeam.leaf.config.modules.network.ProtocolSupport.appleskinSyncTickInterval != 0) {
+ return;
+ }
+
+ for (Map.Entry<ServerPlayer, Set<String>> entry : subscribedChannels.entrySet()) {
+ ServerPlayer player = entry.getKey();
+ FoodData data = player.getFoodData();
+
+ for (String channel : entry.getValue()) {
+ switch (channel) {
+ case "saturation" -> {
+ float saturation = data.getSaturationLevel();
+ Float previousSaturation = previousSaturationLevels.get(player);
+ if (previousSaturation == null || saturation != previousSaturation) {
+ ProtocolUtils.sendPayloadPacket(player, SATURATION_KEY, buf -> buf.writeFloat(saturation));
+ previousSaturationLevels.put(player, saturation);
+ }
+ }
+
+ case "exhaustion" -> {
+ float exhaustion = data.exhaustionLevel;
+ Float previousExhaustion = previousExhaustionLevels.get(player);
+ if (previousExhaustion == null || Math.abs(exhaustion - previousExhaustion) >= MINIMUM_EXHAUSTION_CHANGE_THRESHOLD) {
+ ProtocolUtils.sendPayloadPacket(player, EXHAUSTION_KEY, buf -> buf.writeFloat(exhaustion));
+ previousExhaustionLevels.put(player, exhaustion);
+ }
+ }
+
+ case "natural_regeneration" -> {
+ boolean regeneration = player.serverLevel().getGameRules().getBoolean(GameRules.RULE_NATURAL_REGENERATION);
+ Boolean previousRegeneration = previousNaturalRegeneration.get(player);
+ if (previousRegeneration == null || regeneration != previousRegeneration) {
+ ProtocolUtils.sendPayloadPacket(player, NATURAL_REGENERATION_KEY, buf -> buf.writeBoolean(regeneration));
+ previousNaturalRegeneration.put(player, regeneration);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ @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);
+ previousSaturationLevels.remove(player);
+ previousNaturalRegeneration.remove(player);
+ }
+}