From d7577951349061b8b7c59944030be163345eefed Mon Sep 17 00:00:00 2001 From: granny Date: Sat, 27 Apr 2024 00:53:09 -0700 Subject: [PATCH] parse payload through codec --- .../net/pl3x/map/bukkit/BukkitNetwork.java | 53 ++++++++++--------- .../bukkit/command/BukkitCommandManager.java | 2 +- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/bukkit/src/main/java/net/pl3x/map/bukkit/BukkitNetwork.java b/bukkit/src/main/java/net/pl3x/map/bukkit/BukkitNetwork.java index c4ef09b05..2867f6631 100644 --- a/bukkit/src/main/java/net/pl3x/map/bukkit/BukkitNetwork.java +++ b/bukkit/src/main/java/net/pl3x/map/bukkit/BukkitNetwork.java @@ -23,11 +23,10 @@ */ package net.pl3x.map.bukkit; -import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteArrayDataOutput; -import net.minecraft.network.protocol.common.ClientboundCustomPayloadPacket; +import io.netty.buffer.Unpooled; +import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; -import net.minecraft.server.level.ServerPlayer; import net.pl3x.map.bukkit.network.ClientboundMapPayload; import net.pl3x.map.bukkit.network.ClientboundServerPayload; import net.pl3x.map.bukkit.network.ServerboundMapPayload; @@ -37,10 +36,7 @@ import net.pl3x.map.core.network.Network; import org.bukkit.Bukkit; import org.bukkit.World; -import org.bukkit.craftbukkit.entity.CraftPlayer; -import org.bukkit.craftbukkit.map.CraftMapRenderer; import org.bukkit.entity.Player; -import org.bukkit.map.MapRenderer; import org.bukkit.map.MapView; import org.jetbrains.annotations.NotNull; @@ -56,46 +52,51 @@ public void register() { Bukkit.getMessenger().registerOutgoingPluginChannel(this.plugin, ClientboundMapPayload.TYPE.id().toString()); Bukkit.getMessenger().registerIncomingPluginChannel(this.plugin, ServerboundServerPayload.TYPE.id().toString(), (channel, player, bytes) -> { - // sendServerData - // TODO: parse bytes - - ServerPlayer serverPlayer = ((CraftPlayer) player).getHandle(); - sendCustomPayloadPacket(serverPlayer, new ClientboundServerPayload(Constants.PROTOCOL, Constants.RESPONSE_SUCCESS, Config.WEB_ADDRESS)); + ClientboundServerPayload payload = new ClientboundServerPayload(Constants.PROTOCOL, Constants.RESPONSE_SUCCESS, Config.WEB_ADDRESS); + FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.buffer()); + ClientboundServerPayload.STREAM_CODEC.encode(friendlyByteBuf, payload); + sendCustomPayloadPacket(player, payload, friendlyByteBuf); } ); Bukkit.getMessenger().registerIncomingPluginChannel(this.plugin, ServerboundMapPayload.TYPE.id().toString(), (channel, player, bytes) -> { - // sendMapData - // TODO: parse bytes - - // TODO: replace with id from incoming channel - int mapId = 0; - - ServerPlayer serverPlayer = ((CraftPlayer) player).getHandle(); + FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.copiedBuffer(bytes)); + ServerboundMapPayload payload = ServerboundMapPayload.STREAM_CODEC.decode(byteBuf); - MapView map = Bukkit.getMap(mapId); + MapView map = Bukkit.getMap(payload.mapId()); if (map == null) { - sendCustomPayloadPacket(serverPlayer, new ClientboundMapPayload(Constants.PROTOCOL, Constants.ERROR_NO_SUCH_MAP, mapId)); + ClientboundMapPayload customPacketPayload = new ClientboundMapPayload(Constants.PROTOCOL, Constants.ERROR_NO_SUCH_MAP, payload.mapId()); + FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.buffer(bytes.length)); + ClientboundMapPayload.STREAM_CODEC.encode(friendlyByteBuf, customPacketPayload); + sendCustomPayloadPacket(player, customPacketPayload, friendlyByteBuf); return; } World world = map.getWorld(); if (world == null) { - sendCustomPayloadPacket(serverPlayer, new ClientboundMapPayload(Constants.PROTOCOL, Constants.ERROR_NO_SUCH_WORLD, mapId)); + ClientboundMapPayload customPacketPayload = new ClientboundMapPayload(Constants.PROTOCOL, Constants.ERROR_NO_SUCH_WORLD, payload.mapId()); + FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.buffer()); + ClientboundMapPayload.STREAM_CODEC.encode(friendlyByteBuf, customPacketPayload); + sendCustomPayloadPacket(player, customPacketPayload, friendlyByteBuf); return; } - sendCustomPayloadPacket(serverPlayer, new ClientboundMapPayload( - Constants.PROTOCOL, Constants.RESPONSE_SUCCESS, mapId, + ClientboundMapPayload customPacketPayload = new ClientboundMapPayload( + Constants.PROTOCOL, Constants.RESPONSE_SUCCESS, payload.mapId(), getScale(map), map.getCenterX(), map.getCenterZ(), world.getName() - )); + ); + FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.buffer()); + ClientboundMapPayload.STREAM_CODEC.encode(friendlyByteBuf, customPacketPayload); + sendCustomPayloadPacket(player, customPacketPayload, friendlyByteBuf); } ); } @NotNull - private static void sendCustomPayloadPacket(ServerPlayer player, CustomPacketPayload customPacketPayload) { - player.connection.send(new ClientboundCustomPayloadPacket(customPacketPayload)); + private void sendCustomPayloadPacket(Player player, CustomPacketPayload customPacketPayload, FriendlyByteBuf friendlyByteBuf) { + byte[] byteArray = new byte[friendlyByteBuf.readableBytes()]; + friendlyByteBuf.readBytes(byteArray); + player.sendPluginMessage(this.plugin, customPacketPayload.type().id().toString(), byteArray); } public void unregister() { diff --git a/bukkit/src/main/java/net/pl3x/map/bukkit/command/BukkitCommandManager.java b/bukkit/src/main/java/net/pl3x/map/bukkit/command/BukkitCommandManager.java index 890d2da60..d69582688 100644 --- a/bukkit/src/main/java/net/pl3x/map/bukkit/command/BukkitCommandManager.java +++ b/bukkit/src/main/java/net/pl3x/map/bukkit/command/BukkitCommandManager.java @@ -40,7 +40,7 @@ public class BukkitCommandManager implements CommandHandler { private final Command.Builder<@NotNull Sender> root; public BukkitCommandManager(@NotNull Plugin plugin) throws Exception { - this.manager = new PaperCommandManager<>(plugin, + this.manager = new PaperCommandManager(plugin, ExecutionCoordinator.simpleCoordinator(), SenderMapper.create(BukkitSender::create, Sender::getSender));