From c968c19ee530be49e465e4552a041ef7ec2bed78 Mon Sep 17 00:00:00 2001 From: Lumine1909 <133463833+Lumine1909@users.noreply.github.com> Date: Thu, 3 Jul 2025 00:24:42 -0700 Subject: [PATCH] Protocol draft 2 --- .../leaves/protocol/core/ProtocolUtils.java | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/ProtocolUtils.java b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/ProtocolUtils.java index 361660c6..ec6771d7 100644 --- a/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/ProtocolUtils.java +++ b/leaves-server/src/main/java/org/leavesmc/leaves/protocol/core/ProtocolUtils.java @@ -11,7 +11,10 @@ import net.minecraft.network.protocol.common.custom.DiscardedPayload; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.network.ServerCommonPacketListenerImpl; +import net.minecraft.server.network.ServerGamePacketListenerImpl; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.function.Consumer; import java.util.function.Function; @@ -19,26 +22,50 @@ import java.util.function.Function; public class ProtocolUtils { private static final Function bufDecorator = buf -> buf instanceof RegistryFriendlyByteBuf registry ? registry : new RegistryFriendlyByteBuf(buf, MinecraftServer.getServer().registryAccess()); + private static final byte[] EMPTY = new byte[0]; public static String buildProtocolVersion(String protocol) { return protocol + "-leaves-" + ServerBuildInfo.buildInfo().asString(ServerBuildInfo.StringRepresentation.VERSION_SIMPLE); } public static void sendEmptyPacket(ServerPlayer player, ResourceLocation id) { - player.internalConnection.send(new ClientboundCustomPayloadPacket(new DiscardedPayload(id, null))); + player.connection.send(new ClientboundCustomPayloadPacket(new DiscardedPayload(id, EMPTY))); } public static void sendBytebufPacket(@NotNull ServerPlayer player, ResourceLocation id, Consumer consumer) { RegistryFriendlyByteBuf buf = decorate(Unpooled.buffer()); consumer.accept(buf); - player.internalConnection.send(new ClientboundCustomPayloadPacket(new DiscardedPayload(id, ByteBufUtil.getBytes(buf)))); + player.connection.send(new ClientboundCustomPayloadPacket(new DiscardedPayload(id, ByteBufUtil.getBytes(buf)))); } public static void sendPayloadPacket(ServerPlayer player, CustomPacketPayload payload) { - player.internalConnection.send(new ClientboundCustomPayloadPacket(payload)); + player.connection.send(new ClientboundCustomPayloadPacket(payload)); + } + + public static void sendEmptyPacket(Context context, ResourceLocation id) { + context.connection().send(new ClientboundCustomPayloadPacket(new DiscardedPayload(id, EMPTY))); + } + + public static void sendBytebufPacket(@NotNull Context context, ResourceLocation id, Consumer consumer) { + RegistryFriendlyByteBuf buf = decorate(Unpooled.buffer()); + consumer.accept(buf); + context.connection().send(new ClientboundCustomPayloadPacket(new DiscardedPayload(id, ByteBufUtil.getBytes(buf)))); + } + + public static void sendPayloadPacket(Context context, CustomPacketPayload payload) { + context.connection().send(new ClientboundCustomPayloadPacket(payload)); } public static RegistryFriendlyByteBuf decorate(ByteBuf buf) { return bufDecorator.apply(buf); } + + public static IdentifierSelector createSelector(ServerCommonPacketListenerImpl common) { + ServerPlayer player = common instanceof ServerGamePacketListenerImpl game ? game.getPlayer() : null; + return new IdentifierSelector(new Context(common.profile, common.connection), player); + } + + public static ByteBuf wrapNullable(byte @Nullable [] data) { + return data == null ? Unpooled.wrappedBuffer(EMPTY) : Unpooled.wrappedBuffer(data); + } }