Comments + cleanup

This commit is contained in:
Felix Cravic 2020-08-15 04:05:15 +02:00
parent 0d4689a367
commit b716d8a47a
7 changed files with 138 additions and 73 deletions

View File

@ -1,6 +1,7 @@
package net.minestom.server.instance; package net.minestom.server.instance;
import net.minestom.server.instance.block.Block; 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.network.packet.server.play.ExplosionPacket;
import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.BlockPosition;
@ -78,9 +79,8 @@ public abstract class Explosion {
postExplosion(instance, blocks, packet); postExplosion(instance, blocks, packet);
instance.getPlayers().forEach(player -> { // TODO send only to close players
player.sendPacketToViewersAndSelf(packet); PacketWriterUtils.writeAndSend(instance.getPlayers(), packet);
});
postSend(instance, blocks); postSend(instance, blocks);
} }

View File

@ -8,9 +8,9 @@ public interface ExplosionSupplier {
/** /**
* Creates a new explosion * Creates a new explosion
* *
* @param centerX center of the explosion * @param centerX center X of the explosion
* @param centerY center of the explosion * @param centerY center Y of the explosion
* @param centerZ center of the explosion * @param centerZ center Z of the explosion
* @param strength strength of the explosion * @param strength strength of the explosion
* @param additionalData data passed via {@link Instance#explode)}. Can be null * @param additionalData data passed via {@link Instance#explode)}. Can be null
* @return Explosion object representing the algorithm to use * @return Explosion object representing the algorithm to use

View File

@ -28,8 +28,9 @@ public class MinestomBasicChunkLoader implements IChunkLoader {
final int chunkZ = chunk.getChunkZ(); final int chunkZ = chunk.getChunkZ();
try { try {
final String key = getChunkKey(chunkX, chunkZ);
final byte[] data = chunk.getSerializedData(); final byte[] data = chunk.getSerializedData();
storageFolder.set(getChunkKey(chunkX, chunkZ), data); storageFolder.set(key, data);
if (callback != null) if (callback != null)
callback.run(); callback.run();

View File

@ -2,6 +2,7 @@ package net.minestom.server.instance;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Player; 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.network.packet.server.play.WorldBorderPacket;
import net.minestom.server.utils.Position; import net.minestom.server.utils.Position;
@ -59,6 +60,11 @@ public class WorldBorder {
return centerX; return centerX;
} }
/**
* Change the center X of the world border
*
* @param centerX the new center X
*/
public void setCenterX(float centerX) { public void setCenterX(float centerX) {
this.centerX = centerX; this.centerX = centerX;
refreshCenter(); refreshCenter();
@ -73,6 +79,11 @@ public class WorldBorder {
return centerZ; return centerZ;
} }
/**
* Change the center Z of the world border
*
* @param centerZ the new center Z
*/
public void setCenterZ(float centerZ) { public void setCenterZ(float centerZ) {
this.centerZ = centerZ; this.centerZ = centerZ;
refreshCenter(); 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) { protected void init(Player player) {
WorldBorderPacket worldBorderPacket = new WorldBorderPacket(); WorldBorderPacket worldBorderPacket = new WorldBorderPacket();
worldBorderPacket.action = WorldBorderPacket.Action.INITIALIZE; worldBorderPacket.action = WorldBorderPacket.Action.INITIALIZE;
@ -237,6 +253,9 @@ public class WorldBorder {
return instance; return instance;
} }
/**
* Send the new world border centers to all instance players
*/
private void refreshCenter() { private void refreshCenter() {
WorldBorderPacket worldBorderPacket = new WorldBorderPacket(); WorldBorderPacket worldBorderPacket = new WorldBorderPacket();
worldBorderPacket.action = WorldBorderPacket.Action.SET_CENTER; worldBorderPacket.action = WorldBorderPacket.Action.SET_CENTER;
@ -244,8 +263,13 @@ public class WorldBorder {
sendPacket(worldBorderPacket); sendPacket(worldBorderPacket);
} }
/**
* Send a {@link WorldBorderPacket} to all the instance players
*
* @param worldBorderPacket the packet to send
*/
private void sendPacket(WorldBorderPacket worldBorderPacket) { private void sendPacket(WorldBorderPacket worldBorderPacket) {
instance.getPlayers().forEach(player -> player.getPlayerConnection().sendPacket(worldBorderPacket)); PacketWriterUtils.writeAndSend(instance.getPlayers(), worldBorderPacket);
} }
public enum CollisionAxis { public enum CollisionAxis {

View File

@ -13,19 +13,34 @@ import java.util.Collection;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.function.Consumer; 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<ByteBuf> consumer) { public static void writeCallbackPacket(ServerPacket serverPacket, Consumer<ByteBuf> consumer) {
batchesPool.execute(() -> { PACKET_WRITER_POOL.execute(() -> {
final ByteBuf buffer = PacketUtils.writePacket(serverPacket); final ByteBuf buffer = PacketUtils.writePacket(serverPacket);
consumer.accept(buffer); 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<Player> players, ServerPacket serverPacket) { public static void writeAndSend(Collection<Player> players, ServerPacket serverPacket) {
batchesPool.execute(() -> { PACKET_WRITER_POOL.execute(() -> {
if (players.isEmpty()) if (players.isEmpty())
return; 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) { public static void writeAndSend(PlayerConnection playerConnection, ServerPacket serverPacket) {
batchesPool.execute(() -> { PACKET_WRITER_POOL.execute(() -> {
playerConnection.sendPacket(serverPacket); 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) { public static void writeAndSend(Player player, ServerPacket serverPacket) {
writeAndSend(player.getPlayerConnection(), serverPacket); final PlayerConnection playerConnection = player.getPlayerConnection();
writeAndSend(playerConnection, serverPacket);
} }
} }

View File

@ -2,11 +2,11 @@ package net.minestom.server.network.player;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel; import io.netty.channel.Channel;
import io.netty.channel.socket.SocketChannel;
import lombok.Getter; import lombok.Getter;
import net.minestom.server.extras.mojangAuth.Decrypter; import net.minestom.server.extras.mojangAuth.Decrypter;
import net.minestom.server.extras.mojangAuth.Encrypter; import net.minestom.server.extras.mojangAuth.Encrypter;
import net.minestom.server.extras.mojangAuth.MojangCrypt; 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.netty.codec.PacketCompressor;
import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.login.SetCompressionPacket; import net.minestom.server.network.packet.server.login.SetCompressionPacket;
@ -20,79 +20,76 @@ import java.net.SocketAddress;
*/ */
public class NettyPlayerConnection extends PlayerConnection { public class NettyPlayerConnection extends PlayerConnection {
private final SocketChannel channel; private final SocketChannel channel;
@Getter @Getter
private boolean encrypted = false; private boolean encrypted = false;
@Getter @Getter
private boolean compressed = false; private boolean compressed = false;
public NettyPlayerConnection(SocketChannel channel) { public NettyPlayerConnection(SocketChannel channel) {
super(); super();
this.channel = channel; this.channel = channel;
} }
public void setEncryptionKey(SecretKey secretKey) { public void setEncryptionKey(SecretKey secretKey) {
this.encrypted = true; this.encrypted = true;
getChannel().pipeline().addBefore("framer", "decrypt", new Decrypter(MojangCrypt.getCipher(2, secretKey))); getChannel().pipeline().addBefore("framer", "decrypt", new Decrypter(MojangCrypt.getCipher(2, secretKey)));
getChannel().pipeline().addBefore("framer", "encrypt", new Encrypter(MojangCrypt.getCipher(1, secretKey))); getChannel().pipeline().addBefore("framer", "encrypt", new Encrypter(MojangCrypt.getCipher(1, secretKey)));
} }
@Override @Override
public void enableCompression(int threshold) { public void enableCompression(int threshold) {
this.compressed = true; this.compressed = true;
sendPacket(new SetCompressionPacket(threshold)); sendPacket(new SetCompressionPacket(threshold));
channel.pipeline().addAfter("framer", "compressor", new PacketCompressor(threshold)); channel.pipeline().addAfter("framer", "compressor", new PacketCompressor(threshold));
} }
@Override @Override
public void sendPacket(ByteBuf buffer, boolean copy) { public void sendPacket(ByteBuf buffer, boolean copy) {
//System.out.println(getConnectionState() + " out"); if ((encrypted || compressed) && copy) {
if ((encrypted || compressed) && copy) { buffer = buffer.copy();
buffer = buffer.copy(); buffer.retain();
buffer.retain(); channel.writeAndFlush(buffer);
channel.writeAndFlush(buffer); buffer.release();
buffer.release(); } else {
} else { getChannel().writeAndFlush(buffer);
getChannel().writeAndFlush(buffer); }
} }
}
@Override @Override
public void writePacket(ByteBuf buffer, boolean copy) { public void writePacket(ByteBuf buffer, boolean copy) {
//System.out.println(getConnectionState() + " out"); if ((encrypted || compressed) && copy) {
if ((encrypted || compressed) && copy) { buffer = buffer.copy();
buffer = buffer.copy(); buffer.retain();
buffer.retain(); channel.write(buffer);
channel.write(buffer); buffer.release();
buffer.release(); } else {
} else { getChannel().write(buffer);
getChannel().write(buffer); }
} }
}
@Override @Override
public void sendPacket(ServerPacket serverPacket) { public void sendPacket(ServerPacket serverPacket) {
//System.out.println(serverPacket.getClass().getName() + " out");
channel.writeAndFlush(serverPacket); channel.writeAndFlush(serverPacket);
} }
@Override @Override
public void flush() { public void flush() {
getChannel().flush(); getChannel().flush();
} }
@Override @Override
public SocketAddress getRemoteAddress() { public SocketAddress getRemoteAddress() {
return getChannel().remoteAddress(); return getChannel().remoteAddress();
} }
@Override @Override
public void disconnect() { public void disconnect() {
getChannel().close(); getChannel().close();
} }
public Channel getChannel() { public Channel getChannel() {
return channel; return channel;
} }
} }

View File

@ -5,12 +5,21 @@ import io.netty.buffer.Unpooled;
import net.minestom.server.network.packet.PacketWriter; import net.minestom.server.network.packet.PacketWriter;
import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacket;
/**
* Class used to write packets
*/
public final class PacketUtils { public final class PacketUtils {
private 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) { public static void writePacket(ByteBuf buf, ServerPacket packet) {
PacketWriter writer = new PacketWriter(); PacketWriter writer = new PacketWriter();
@ -19,6 +28,12 @@ public final class PacketUtils {
buf.writeBytes(writer.toByteArray()); 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) { public static ByteBuf writePacket(ServerPacket packet) {
ByteBuf buffer = Unpooled.buffer(); ByteBuf buffer = Unpooled.buffer();