2020-04-24 03:25:58 +02:00
|
|
|
package net.minestom.server.utils;
|
2019-08-22 14:52:32 +02:00
|
|
|
|
2020-04-17 01:16:02 +02:00
|
|
|
import io.netty.buffer.ByteBuf;
|
|
|
|
import io.netty.buffer.Unpooled;
|
2020-11-20 03:57:05 +01:00
|
|
|
import net.minestom.server.MinecraftServer;
|
2020-11-13 07:43:35 +01:00
|
|
|
import net.minestom.server.entity.Player;
|
2020-11-20 03:57:05 +01:00
|
|
|
import net.minestom.server.listener.manager.PacketListenerManager;
|
2020-04-24 03:25:58 +02:00
|
|
|
import net.minestom.server.network.packet.server.ServerPacket;
|
2020-11-20 03:57:05 +01:00
|
|
|
import net.minestom.server.network.player.NettyPlayerConnection;
|
|
|
|
import net.minestom.server.network.player.PlayerConnection;
|
2020-08-19 20:34:21 +02:00
|
|
|
import net.minestom.server.utils.binary.BinaryWriter;
|
2020-11-05 22:20:51 +01:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2019-08-22 14:52:32 +02:00
|
|
|
|
2020-11-13 07:43:35 +01:00
|
|
|
import java.util.Collection;
|
|
|
|
|
2020-08-15 04:05:15 +02:00
|
|
|
/**
|
2020-11-13 09:17:53 +01:00
|
|
|
* Utils class for packets. Including writing a {@link ServerPacket} into a {@link ByteBuf}
|
|
|
|
* for network processing.
|
2020-08-15 04:05:15 +02:00
|
|
|
*/
|
2020-08-07 08:10:10 +02:00
|
|
|
public final class PacketUtils {
|
|
|
|
|
2020-11-20 03:57:05 +01:00
|
|
|
private static final PacketListenerManager PACKET_LISTENER_MANAGER = MinecraftServer.getPacketListenerManager();
|
|
|
|
|
2020-08-07 08:10:10 +02:00
|
|
|
private PacketUtils() {
|
|
|
|
|
|
|
|
}
|
2019-08-22 14:52:32 +02:00
|
|
|
|
2020-11-13 09:17:53 +01:00
|
|
|
/**
|
2020-11-20 03:57:05 +01:00
|
|
|
* Sends a {@link ServerPacket} to multiple players.
|
2020-11-13 09:17:53 +01:00
|
|
|
* <p>
|
2020-11-20 03:57:05 +01:00
|
|
|
* Can drastically improve performance since the packet will not have to be processed as much.
|
2020-11-13 09:17:53 +01:00
|
|
|
*
|
|
|
|
* @param players the players to send the packet to
|
|
|
|
* @param packet the packet to send to the players
|
|
|
|
*/
|
|
|
|
public static void sendGroupedPacket(@NotNull Collection<Player> players, @NotNull ServerPacket packet) {
|
2020-11-20 03:57:05 +01:00
|
|
|
final boolean success = PACKET_LISTENER_MANAGER.processServerPacket(packet, players);
|
|
|
|
if (success) {
|
2020-11-20 04:48:33 +01:00
|
|
|
final ByteBuf buffer = writePacket(packet);
|
|
|
|
|
2020-11-20 03:57:05 +01:00
|
|
|
for (Player player : players) {
|
|
|
|
final PlayerConnection playerConnection = player.getPlayerConnection();
|
|
|
|
if (playerConnection instanceof NettyPlayerConnection) {
|
2020-11-20 04:48:33 +01:00
|
|
|
final NettyPlayerConnection nettyPlayerConnection = (NettyPlayerConnection) playerConnection;
|
|
|
|
nettyPlayerConnection.getChannel().write(buffer.retainedSlice());
|
2020-11-20 03:57:05 +01:00
|
|
|
} else {
|
|
|
|
playerConnection.sendPacket(packet);
|
|
|
|
}
|
|
|
|
}
|
2020-11-13 07:43:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 04:05:15 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Writes a {@link ServerPacket} into a {@link ByteBuf}.
|
2020-08-15 04:05:15 +02:00
|
|
|
*
|
|
|
|
* @param buf the recipient of {@code packet}
|
|
|
|
* @param packet the packet to write into {@code buf}
|
|
|
|
*/
|
2020-11-05 22:20:51 +01:00
|
|
|
public static void writePacket(@NotNull ByteBuf buf, @NotNull ServerPacket packet) {
|
2020-08-21 13:16:44 +02:00
|
|
|
final ByteBuf packetBuffer = getPacketBuffer(packet);
|
|
|
|
|
|
|
|
writePacket(buf, packetBuffer, packet.getId());
|
2020-06-28 22:07:48 +02:00
|
|
|
}
|
2020-04-17 01:16:02 +02:00
|
|
|
|
2020-08-15 04:05:15 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Writes a {@link ServerPacket} into a newly created {@link ByteBuf}.
|
2020-08-15 04:05:15 +02:00
|
|
|
*
|
|
|
|
* @param packet the packet to write
|
|
|
|
* @return a {@link ByteBuf} containing {@code packet}
|
|
|
|
*/
|
2020-11-05 22:20:51 +01:00
|
|
|
@NotNull
|
|
|
|
public static ByteBuf writePacket(@NotNull ServerPacket packet) {
|
2020-08-21 13:16:44 +02:00
|
|
|
final ByteBuf packetBuffer = getPacketBuffer(packet);
|
2020-04-17 01:16:02 +02:00
|
|
|
|
2020-08-21 13:16:44 +02:00
|
|
|
// Add 5 for the packet id and for the packet size
|
|
|
|
final int size = packetBuffer.writerIndex() + 5 + 5;
|
|
|
|
ByteBuf buffer = Unpooled.buffer(size);
|
|
|
|
|
|
|
|
writePacket(buffer, packetBuffer, packet.getId());
|
2020-03-20 19:50:22 +01:00
|
|
|
|
2020-05-26 15:35:48 +02:00
|
|
|
return buffer;
|
2019-08-23 15:37:38 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 13:16:44 +02:00
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Writes a packet buffer into {@code buf}.
|
2020-08-21 13:16:44 +02:00
|
|
|
*
|
|
|
|
* @param buf the buffer which will receive the packet id/data
|
|
|
|
* @param packetBuffer the buffer containing the raw packet data
|
|
|
|
* @param packetId the packet id
|
|
|
|
*/
|
2020-11-05 22:20:51 +01:00
|
|
|
private static void writePacket(@NotNull ByteBuf buf, @NotNull ByteBuf packetBuffer, int packetId) {
|
2020-08-21 13:16:44 +02:00
|
|
|
Utils.writeVarIntBuf(buf, packetId);
|
|
|
|
buf.writeBytes(packetBuffer);
|
2020-11-18 06:39:20 +01:00
|
|
|
packetBuffer.release();
|
2020-08-21 13:16:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-15 21:16:31 +02:00
|
|
|
* Gets the buffer representing the raw packet data.
|
2020-08-21 13:16:44 +02:00
|
|
|
*
|
|
|
|
* @param packet the packet to write
|
|
|
|
* @return the {@link ByteBuf} containing the raw packet data
|
|
|
|
*/
|
2020-11-05 22:20:51 +01:00
|
|
|
@NotNull
|
|
|
|
private static ByteBuf getPacketBuffer(@NotNull ServerPacket packet) {
|
2020-08-21 13:16:44 +02:00
|
|
|
BinaryWriter writer = new BinaryWriter();
|
|
|
|
packet.write(writer);
|
|
|
|
|
|
|
|
return writer.getBuffer();
|
|
|
|
}
|
|
|
|
|
2019-08-22 14:52:32 +02:00
|
|
|
}
|