From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: violetc <58360096+s-yh-china@users.noreply.github.com> Date: Tue, 27 Jun 2023 01:54:43 +0800 Subject: [PATCH] Leaves carpet support diff --git a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java index 6d527cdb75e767182dce5b338322a9c27b21d5b8..7634fc05f7e1bd12c9c6aa9204af613364ec5e7f 100644 --- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java +++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java @@ -3663,6 +3663,9 @@ public class ServerGamePacketListenerImpl implements ServerPlayerConnection, Tic if (top.leavesmc.leaves.LeavesConfig.syncmaticaProtocol && ProtocolUtils.isNamespacePacket(packet, top.leavesmc.leaves.protocol.syncmatica.SyncmaticaProtocol.PROTOCOL_ID)) { top.leavesmc.leaves.protocol.syncmatica.SyncmaticaProtocol.getCommunicationManager().onPacketGet(packet, this); } + if (top.leavesmc.leaves.LeavesConfig.leavesCarpetSupport && ProtocolUtils.isNamespacePacket(packet, top.leavesmc.leaves.protocol.CarpetServerProtocol.PROTOCOL_ID)) { + top.leavesmc.leaves.protocol.CarpetServerProtocol.handlePacket(player, packet); + } } catch (Exception ex) { ServerGamePacketListenerImpl.LOGGER.error("Couldn\'t dispatch custom payload", ex); this.disconnect("Invalid custom payload!", org.bukkit.event.player.PlayerKickEvent.Cause.INVALID_PAYLOAD); diff --git a/src/main/java/net/minecraft/server/players/PlayerList.java b/src/main/java/net/minecraft/server/players/PlayerList.java index fed72c6fef684758be756602f57393d088bac50d..13356860e13374227ac5559daf77fc4dc60e378d 100644 --- a/src/main/java/net/minecraft/server/players/PlayerList.java +++ b/src/main/java/net/minecraft/server/players/PlayerList.java @@ -364,6 +364,7 @@ public abstract class PlayerList { top.leavesmc.leaves.protocol.BBORProtocol.onPlayerLoggedIn(player); // Leaves - bbor top.leavesmc.leaves.protocol.JadeProtocol.onPlayerJoin(player); // Leaves - Jade top.leavesmc.leaves.protocol.AppleSkinProtocol.onPlayerLoggedIn(player); // Leaves - appleskin + top.leavesmc.leaves.protocol.CarpetServerProtocol.onPlayerJoin(player); // Leaves - carpet final net.kyori.adventure.text.Component jm = playerJoinEvent.joinMessage(); diff --git a/src/main/java/top/leavesmc/leaves/LeavesConfig.java b/src/main/java/top/leavesmc/leaves/LeavesConfig.java index 8049827ffba47ef1a617e17f5d7efc1a08869d3d..50b1574c090a6762a6c94aeb40abebd184b210b8 100644 --- a/src/main/java/top/leavesmc/leaves/LeavesConfig.java +++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java @@ -14,6 +14,8 @@ import top.leavesmc.leaves.bot.agent.Actions; import top.leavesmc.leaves.profile.LeavesMinecraftSessionService; import top.leavesmc.leaves.protocol.syncmatica.SyncmaticaProtocol; import top.leavesmc.leaves.util.MathUtils; +import top.leavesmc.leaves.protocol.CarpetServerProtocol.CarpetRule; +import top.leavesmc.leaves.protocol.CarpetServerProtocol.CarpetRules; import java.io.File; import java.lang.reflect.InvocationTargetException; diff --git a/src/main/java/top/leavesmc/leaves/protocol/CarpetServerProtocol.java b/src/main/java/top/leavesmc/leaves/protocol/CarpetServerProtocol.java new file mode 100644 index 0000000000000000000000000000000000000000..9c13a3b7d2321dc59beec6e220790d1df0728c16 --- /dev/null +++ b/src/main/java/top/leavesmc/leaves/protocol/CarpetServerProtocol.java @@ -0,0 +1,96 @@ +package top.leavesmc.leaves.protocol; + +import io.netty.buffer.Unpooled; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerPlayer; +import org.jetbrains.annotations.Contract; +import org.jetbrains.annotations.NotNull; +import top.leavesmc.leaves.LeavesConfig; +import top.leavesmc.leaves.LeavesLogger; +import top.leavesmc.leaves.util.ProtocolUtils; + +import java.util.HashMap; +import java.util.Map; + +public class CarpetServerProtocol { + + public static final String PROTOCOL_ID = "carpet"; + + private static final ResourceLocation HELLO_ID = id("hello"); // why????????????? + + private static final int HI = 69; + private static final int HELLO = 420; + private static final int DATA = 1; + + @Contract("_ -> new") + public static @NotNull ResourceLocation id(String path) { + return new ResourceLocation(PROTOCOL_ID, path); + } + + public static void handlePacket(ServerPlayer player, @NotNull ServerboundCustomPayloadPacket packet) { + if (packet.identifier.equals(HELLO_ID)) { + FriendlyByteBuf data = packet.data; + if (data.readVarInt() == HELLO) { + handleHello(player, data); + } + } + } + + private static void handleHello(@NotNull ServerPlayer player, @NotNull FriendlyByteBuf data) { + LeavesLogger.LOGGER.info("Player " + player.getScoreboardName() + " joined with carpet " + data.readUtf(64)); + ProtocolUtils.sendPayloadPacket(player, HELLO_ID, CarpetRules.buildBuf()); + } + + public static void onPlayerJoin(ServerPlayer player) { + if (LeavesConfig.leavesCarpetSupport) { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + buf.writeVarInt(HI).writeUtf("leaves-carpet-1.0.0"); + ProtocolUtils.sendPayloadPacket(player, HELLO_ID, buf); + } + } + + public static class CarpetRules { + + private static final Map rules = new HashMap<>(); + + @NotNull + public static FriendlyByteBuf buildBuf() { + FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer()); + buf.writeVarInt(1); + + CompoundTag rulesNbt = new CompoundTag(); + rules.values().forEach(rule -> rule.writeNBT(rulesNbt)); + + CompoundTag tag = new CompoundTag(); + tag.put("Rules", rulesNbt); + buf.writeNbt(tag); + + return buf; + } + + public static void register(CarpetRule rule) { + rules.put(rule.name, rule); + } + } + + public record CarpetRule(String identifier, String name, String value) { + + @NotNull + @Contract("_, _, _ -> new") + public static CarpetRule of(String identifier, String name, boolean value) { + return new CarpetRule(identifier, name, Boolean.toString(value)); + } + + public void writeNBT(@NotNull CompoundTag rules) { + CompoundTag rule = new CompoundTag(); + rule.putString("Value", value); + rule.putString("Manager", identifier); + rule.putString("Rule", name); + rules.put(name, rule); + } + } +}