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;
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);
}

View File

@ -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

View File

@ -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();

View File

@ -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 {

View File

@ -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<ByteBuf> 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<Player> 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);
}
}

View File

@ -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;
}
}

View File

@ -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();