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 e30b9fec7a9e405433889eb4439056a9f4847791..4f9b13c8c3add6ce08deed2bc6788ec00b0112ea 100644 --- a/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java +++ b/src/main/java/net/minecraft/server/network/ServerGamePacketListenerImpl.java @@ -3583,6 +3583,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 6c19d9b64830efe4b9d6f17d8ca92f88ad3475d5..fb6810289ac855e622d3970101f27b2133bd67d2 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 5db4a3687f05bdc1ab3da0e74536dc8f72c7c1b5..41ecc020ee1277dc6af126a3531901567ce8217b 100644 --- a/src/main/java/top/leavesmc/leaves/LeavesConfig.java +++ b/src/main/java/top/leavesmc/leaves/LeavesConfig.java @@ -15,6 +15,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..afcbb7b8c9a6e933665500b068fe7c9111658df7 --- /dev/null +++ b/src/main/java/top/leavesmc/leaves/protocol/CarpetServerProtocol.java @@ -0,0 +1,85 @@ +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 { + + public static 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 record CarpetRule(String identifier, String name, String 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); + } + } +}