Skip to content

Commit

Permalink
parse payload through codec
Browse files Browse the repository at this point in the history
  • Loading branch information
granny committed Apr 27, 2024
1 parent 2994f3a commit d757795
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
53 changes: 27 additions & 26 deletions bukkit/src/main/java/net/pl3x/map/bukkit/BukkitNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sender>(plugin,
ExecutionCoordinator.simpleCoordinator(),
SenderMapper.create(BukkitSender::create, Sender::getSender));

Expand Down

0 comments on commit d757795

Please sign in to comment.