From b716d8a47a3397397683202c43a10a6ee5859670 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Sat, 15 Aug 2020 04:05:15 +0200 Subject: [PATCH] Comments + cleanup --- .../minestom/server/instance/Explosion.java | 6 +- .../server/instance/ExplosionSupplier.java | 6 +- .../instance/MinestomBasicChunkLoader.java | 3 +- .../minestom/server/instance/WorldBorder.java | 26 +++- .../server/network/PacketWriterUtils.java | 40 +++++- .../network/player/NettyPlayerConnection.java | 115 +++++++++--------- .../minestom/server/utils/PacketUtils.java | 15 +++ 7 files changed, 138 insertions(+), 73 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/Explosion.java b/src/main/java/net/minestom/server/instance/Explosion.java index 0753c6d3c..b10da5581 100644 --- a/src/main/java/net/minestom/server/instance/Explosion.java +++ b/src/main/java/net/minestom/server/instance/Explosion.java @@ -1,6 +1,7 @@ package net.minestom.server.instance; import net.minestom.server.instance.block.Block; +import net.minestom.server.network.PacketWriterUtils; import net.minestom.server.network.packet.server.play.ExplosionPacket; import net.minestom.server.utils.BlockPosition; @@ -78,9 +79,8 @@ public abstract class Explosion { postExplosion(instance, blocks, packet); - instance.getPlayers().forEach(player -> { - player.sendPacketToViewersAndSelf(packet); - }); + // TODO send only to close players + PacketWriterUtils.writeAndSend(instance.getPlayers(), packet); postSend(instance, blocks); } diff --git a/src/main/java/net/minestom/server/instance/ExplosionSupplier.java b/src/main/java/net/minestom/server/instance/ExplosionSupplier.java index 1c295d6a2..a3d5b3ae3 100644 --- a/src/main/java/net/minestom/server/instance/ExplosionSupplier.java +++ b/src/main/java/net/minestom/server/instance/ExplosionSupplier.java @@ -8,9 +8,9 @@ public interface ExplosionSupplier { /** * Creates a new explosion * - * @param centerX center of the explosion - * @param centerY center of the explosion - * @param centerZ center of the explosion + * @param centerX center X of the explosion + * @param centerY center Y of the explosion + * @param centerZ center Z of the explosion * @param strength strength of the explosion * @param additionalData data passed via {@link Instance#explode)}. Can be null * @return Explosion object representing the algorithm to use diff --git a/src/main/java/net/minestom/server/instance/MinestomBasicChunkLoader.java b/src/main/java/net/minestom/server/instance/MinestomBasicChunkLoader.java index a7135cdd9..28254babc 100644 --- a/src/main/java/net/minestom/server/instance/MinestomBasicChunkLoader.java +++ b/src/main/java/net/minestom/server/instance/MinestomBasicChunkLoader.java @@ -28,8 +28,9 @@ public class MinestomBasicChunkLoader implements IChunkLoader { final int chunkZ = chunk.getChunkZ(); try { + final String key = getChunkKey(chunkX, chunkZ); final byte[] data = chunk.getSerializedData(); - storageFolder.set(getChunkKey(chunkX, chunkZ), data); + storageFolder.set(key, data); if (callback != null) callback.run(); diff --git a/src/main/java/net/minestom/server/instance/WorldBorder.java b/src/main/java/net/minestom/server/instance/WorldBorder.java index a099d13ca..a1bc0a505 100644 --- a/src/main/java/net/minestom/server/instance/WorldBorder.java +++ b/src/main/java/net/minestom/server/instance/WorldBorder.java @@ -2,6 +2,7 @@ package net.minestom.server.instance; import net.minestom.server.entity.Entity; import net.minestom.server.entity.Player; +import net.minestom.server.network.PacketWriterUtils; import net.minestom.server.network.packet.server.play.WorldBorderPacket; import net.minestom.server.utils.Position; @@ -59,6 +60,11 @@ public class WorldBorder { return centerX; } + /** + * Change the center X of the world border + * + * @param centerX the new center X + */ public void setCenterX(float centerX) { this.centerX = centerX; refreshCenter(); @@ -73,6 +79,11 @@ public class WorldBorder { return centerZ; } + /** + * Change the center Z of the world border + * + * @param centerZ the new center Z + */ public void setCenterZ(float centerZ) { this.centerZ = centerZ; refreshCenter(); @@ -220,6 +231,11 @@ public class WorldBorder { } } + /** + * Send the world border init packet to a player + * + * @param player the player to send the packet to + */ protected void init(Player player) { WorldBorderPacket worldBorderPacket = new WorldBorderPacket(); worldBorderPacket.action = WorldBorderPacket.Action.INITIALIZE; @@ -237,6 +253,9 @@ public class WorldBorder { return instance; } + /** + * Send the new world border centers to all instance players + */ private void refreshCenter() { WorldBorderPacket worldBorderPacket = new WorldBorderPacket(); worldBorderPacket.action = WorldBorderPacket.Action.SET_CENTER; @@ -244,8 +263,13 @@ public class WorldBorder { sendPacket(worldBorderPacket); } + /** + * Send a {@link WorldBorderPacket} to all the instance players + * + * @param worldBorderPacket the packet to send + */ private void sendPacket(WorldBorderPacket worldBorderPacket) { - instance.getPlayers().forEach(player -> player.getPlayerConnection().sendPacket(worldBorderPacket)); + PacketWriterUtils.writeAndSend(instance.getPlayers(), worldBorderPacket); } public enum CollisionAxis { diff --git a/src/main/java/net/minestom/server/network/PacketWriterUtils.java b/src/main/java/net/minestom/server/network/PacketWriterUtils.java index 27eb51615..2adcab42b 100644 --- a/src/main/java/net/minestom/server/network/PacketWriterUtils.java +++ b/src/main/java/net/minestom/server/network/PacketWriterUtils.java @@ -13,19 +13,34 @@ import java.util.Collection; import java.util.concurrent.ExecutorService; import java.util.function.Consumer; -public class PacketWriterUtils { +/** + * Utils class used to write packets in the appropriate thread pool + */ +public final class PacketWriterUtils { - private static ExecutorService batchesPool = new MinestomThread(MinecraftServer.THREAD_COUNT_PACKET_WRITER, MinecraftServer.THREAD_NAME_PACKET_WRITER); + private static final ExecutorService PACKET_WRITER_POOL = new MinestomThread(MinecraftServer.THREAD_COUNT_PACKET_WRITER, MinecraftServer.THREAD_NAME_PACKET_WRITER); + /** + * Write the packet in the writer thread pool + * + * @param serverPacket the packet to write + * @param consumer the consumer called once the packet has been written + */ public static void writeCallbackPacket(ServerPacket serverPacket, Consumer consumer) { - batchesPool.execute(() -> { + PACKET_WRITER_POOL.execute(() -> { final ByteBuf buffer = PacketUtils.writePacket(serverPacket); consumer.accept(buffer); }); } + /** + * Write a packet in the writer thread pool and send it to every players in {@code players} + * + * @param players the players list to send the packet to + * @param serverPacket the packet to write and send + */ public static void writeAndSend(Collection players, ServerPacket serverPacket) { - batchesPool.execute(() -> { + PACKET_WRITER_POOL.execute(() -> { if (players.isEmpty()) return; @@ -42,14 +57,27 @@ public class PacketWriterUtils { }); } + /** + * Write a packet and send it to a player connection + * + * @param playerConnection the connection to send the packet to + * @param serverPacket the packet to write and send + */ public static void writeAndSend(PlayerConnection playerConnection, ServerPacket serverPacket) { - batchesPool.execute(() -> { + PACKET_WRITER_POOL.execute(() -> { playerConnection.sendPacket(serverPacket); }); } + /** + * Write a packet and send it to a player + * + * @param player the player to send the packet to + * @param serverPacket the packet to write and send + */ public static void writeAndSend(Player player, ServerPacket serverPacket) { - writeAndSend(player.getPlayerConnection(), serverPacket); + final PlayerConnection playerConnection = player.getPlayerConnection(); + writeAndSend(playerConnection, serverPacket); } } diff --git a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java index 9d4fbe78c..9055ba074 100644 --- a/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/NettyPlayerConnection.java @@ -2,11 +2,11 @@ package net.minestom.server.network.player; import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; +import io.netty.channel.socket.SocketChannel; import lombok.Getter; import net.minestom.server.extras.mojangAuth.Decrypter; import net.minestom.server.extras.mojangAuth.Encrypter; import net.minestom.server.extras.mojangAuth.MojangCrypt; -import io.netty.channel.socket.SocketChannel; import net.minestom.server.network.netty.codec.PacketCompressor; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.login.SetCompressionPacket; @@ -20,79 +20,76 @@ import java.net.SocketAddress; */ public class NettyPlayerConnection extends PlayerConnection { - private final SocketChannel channel; - @Getter - private boolean encrypted = false; - @Getter - private boolean compressed = false; + private final SocketChannel channel; + @Getter + private boolean encrypted = false; + @Getter + private boolean compressed = false; - public NettyPlayerConnection(SocketChannel channel) { - super(); - this.channel = channel; - } + public NettyPlayerConnection(SocketChannel channel) { + super(); + this.channel = channel; + } - public void setEncryptionKey(SecretKey secretKey) { - this.encrypted = true; - getChannel().pipeline().addBefore("framer", "decrypt", new Decrypter(MojangCrypt.getCipher(2, secretKey))); - getChannel().pipeline().addBefore("framer", "encrypt", new Encrypter(MojangCrypt.getCipher(1, secretKey))); - } + public void setEncryptionKey(SecretKey secretKey) { + this.encrypted = true; + getChannel().pipeline().addBefore("framer", "decrypt", new Decrypter(MojangCrypt.getCipher(2, secretKey))); + getChannel().pipeline().addBefore("framer", "encrypt", new Encrypter(MojangCrypt.getCipher(1, secretKey))); + } - @Override + @Override public void enableCompression(int threshold) { - this.compressed = true; + this.compressed = true; sendPacket(new SetCompressionPacket(threshold)); channel.pipeline().addAfter("framer", "compressor", new PacketCompressor(threshold)); } @Override - public void sendPacket(ByteBuf buffer, boolean copy) { - //System.out.println(getConnectionState() + " out"); - if ((encrypted || compressed) && copy) { - buffer = buffer.copy(); - buffer.retain(); - channel.writeAndFlush(buffer); - buffer.release(); - } else { - getChannel().writeAndFlush(buffer); - } - } + public void sendPacket(ByteBuf buffer, boolean copy) { + if ((encrypted || compressed) && copy) { + buffer = buffer.copy(); + buffer.retain(); + channel.writeAndFlush(buffer); + buffer.release(); + } else { + getChannel().writeAndFlush(buffer); + } + } - @Override - public void writePacket(ByteBuf buffer, boolean copy) { - //System.out.println(getConnectionState() + " out"); - if ((encrypted || compressed) && copy) { - buffer = buffer.copy(); - buffer.retain(); - channel.write(buffer); - buffer.release(); - } else { - getChannel().write(buffer); - } - } + @Override + public void writePacket(ByteBuf buffer, boolean copy) { + if ((encrypted || compressed) && copy) { + buffer = buffer.copy(); + buffer.retain(); + channel.write(buffer); + buffer.release(); + } else { + getChannel().write(buffer); + } + } - @Override - public void sendPacket(ServerPacket serverPacket) { - //System.out.println(serverPacket.getClass().getName() + " out"); + @Override + public void sendPacket(ServerPacket serverPacket) { channel.writeAndFlush(serverPacket); - } + } - @Override - public void flush() { - getChannel().flush(); - } + @Override + public void flush() { + getChannel().flush(); + } - @Override - public SocketAddress getRemoteAddress() { - return getChannel().remoteAddress(); - } + @Override + public SocketAddress getRemoteAddress() { + return getChannel().remoteAddress(); + } - @Override - public void disconnect() { - getChannel().close(); - } + @Override + public void disconnect() { + getChannel().close(); + } - public Channel getChannel() { - return channel; - } + public Channel getChannel() { + return channel; + } } diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index 5c82581b5..e7f3bd242 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -5,12 +5,21 @@ import io.netty.buffer.Unpooled; import net.minestom.server.network.packet.PacketWriter; import net.minestom.server.network.packet.server.ServerPacket; +/** + * Class used to write packets + */ public final class PacketUtils { private PacketUtils() { } + /** + * Write a {@link ServerPacket} into a {@link ByteBuf} + * + * @param buf the recipient of {@code packet} + * @param packet the packet to write into {@code buf} + */ public static void writePacket(ByteBuf buf, ServerPacket packet) { PacketWriter writer = new PacketWriter(); @@ -19,6 +28,12 @@ public final class PacketUtils { buf.writeBytes(writer.toByteArray()); } + /** + * Write a {@link ServerPacket} into a newly created {@link ByteBuf} + * + * @param packet the packet to write + * @return a {@link ByteBuf} containing {@code packet} + */ public static ByteBuf writePacket(ServerPacket packet) { ByteBuf buffer = Unpooled.buffer();