mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-25 18:09:17 +00:00
Update AsteorBarProtocol to adapt Leaves's protocol manager update
Fixes https://github.com/Winds-Studio/Leaf/issues/430
This commit is contained in:
@@ -3,6 +3,7 @@ package org.leavesmc.leaves.protocol;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.players.PlayerList;
|
||||
import net.minecraft.world.food.FoodData;
|
||||
import net.minecraft.world.level.GameRules;
|
||||
import org.dreeam.leaf.config.modules.network.ProtocolSupport;
|
||||
@@ -59,8 +60,9 @@ public class AppleSkinProtocol implements LeavesProtocol {
|
||||
|
||||
@ProtocolHandler.Ticker
|
||||
public static void tick() {
|
||||
final PlayerList playerList = MinecraftServer.getServer().getPlayerList(); // Leaf - optimize leaves protocol manager
|
||||
for (Map.Entry<UUID, Set<String>> entry : subscribedChannels.entrySet()) {
|
||||
ServerPlayer player = MinecraftServer.getServer().getPlayerList().getPlayer(entry.getKey());
|
||||
ServerPlayer player = playerList.getPlayer(entry.getKey()); // Leaf - optimize leaves protocol manager
|
||||
if (player == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -3,10 +3,12 @@ package org.leavesmc.leaves.protocol;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.players.PlayerList;
|
||||
import net.minecraft.world.food.FoodData;
|
||||
import org.dreeam.leaf.config.modules.network.ProtocolSupport;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.leavesmc.leaves.protocol.core.Context;
|
||||
import org.leavesmc.leaves.protocol.core.LeavesProtocol;
|
||||
import org.leavesmc.leaves.protocol.core.ProtocolHandler;
|
||||
import org.leavesmc.leaves.protocol.core.ProtocolUtils;
|
||||
@@ -29,7 +31,7 @@ public class AsteorBarProtocol implements LeavesProtocol {
|
||||
|
||||
private static final float THRESHOLD = 0.01F;
|
||||
|
||||
private static final Set<ServerPlayer> players = new HashSet<>();
|
||||
private static final Set<UUID> players = new HashSet<>();
|
||||
|
||||
@Contract("_ -> new")
|
||||
public static @NotNull ResourceLocation id(String path) {
|
||||
@@ -43,38 +45,44 @@ public class AsteorBarProtocol implements LeavesProtocol {
|
||||
|
||||
@ProtocolHandler.PlayerLeave
|
||||
public static void onPlayerLoggedOut(@NotNull ServerPlayer player) {
|
||||
players.remove(player);
|
||||
players.remove(player.getUUID());
|
||||
resetPlayerData(player);
|
||||
}
|
||||
|
||||
@ProtocolHandler.MinecraftRegister(onlyNamespace = true)
|
||||
public static void onPlayerSubscribed(@NotNull ServerPlayer player, ResourceLocation id) {
|
||||
players.add(player);
|
||||
public static void onPlayerSubscribed(@NotNull Context context, ResourceLocation id) {
|
||||
players.add(context.profile().getId());
|
||||
}
|
||||
|
||||
@ProtocolHandler.Ticker
|
||||
public static void tick() {
|
||||
for (ServerPlayer player : players) {
|
||||
final PlayerList playerList = MinecraftServer.getServer().getPlayerList();
|
||||
for (UUID uuid : players) {
|
||||
ServerPlayer player = playerList.getPlayer(uuid);
|
||||
if (player == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FoodData data = player.getFoodData();
|
||||
|
||||
float saturation = data.getSaturationLevel();
|
||||
Float previousSaturation = previousSaturationLevels.get(player.getUUID());
|
||||
Float previousSaturation = previousSaturationLevels.get(uuid);
|
||||
if (previousSaturation == null || saturation != previousSaturation) {
|
||||
ProtocolUtils.sendBytebufPacket(player, NETWORK_KEY, buf -> {
|
||||
buf.writeByte(1);
|
||||
buf.writeFloat(saturation);
|
||||
});
|
||||
previousSaturationLevels.put(player.getUUID(), saturation);
|
||||
previousSaturationLevels.put(uuid, saturation);
|
||||
}
|
||||
|
||||
float exhaustion = data.exhaustionLevel;
|
||||
Float previousExhaustion = previousExhaustionLevels.get(player.getUUID());
|
||||
Float previousExhaustion = previousExhaustionLevels.get(uuid);
|
||||
if (previousExhaustion == null || Math.abs(exhaustion - previousExhaustion) >= THRESHOLD) {
|
||||
ProtocolUtils.sendBytebufPacket(player, NETWORK_KEY, buf -> {
|
||||
buf.writeByte(0);
|
||||
buf.writeFloat(exhaustion);
|
||||
});
|
||||
previousExhaustionLevels.put(player.getUUID(), exhaustion);
|
||||
previousExhaustionLevels.put(uuid, exhaustion);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user