From 83449aecc92e7a6b781ccfe965fee3d432021992 Mon Sep 17 00:00:00 2001 From: Felix Cravic Date: Mon, 25 May 2020 00:29:37 +0200 Subject: [PATCH] Making PlayerConnection an abstract class --- .../net/minestom/server/entity/Player.java | 2 +- .../server/network/PacketProcessor.java | 4 +- .../packet/client/status/PingPacket.java | 2 +- .../network/player/IPlayerConnection.java | 61 +++++++++++++++++++ .../network/player/PlayerConnection.java | 45 +++++--------- 5 files changed, 82 insertions(+), 32 deletions(-) create mode 100644 src/main/java/net/minestom/server/network/player/IPlayerConnection.java diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 095898b03..915b8c3c3 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -867,7 +867,7 @@ public class Player extends LivingEntity { DisconnectPacket disconnectPacket = new DisconnectPacket(); disconnectPacket.message = Chat.toJsonString(message); playerConnection.sendPacket(disconnectPacket); - playerConnection.getChannel().close(); + playerConnection.disconnect(); } public void kick(String message) { diff --git a/src/main/java/net/minestom/server/network/PacketProcessor.java b/src/main/java/net/minestom/server/network/PacketProcessor.java index 51c9df835..77a37a757 100644 --- a/src/main/java/net/minestom/server/network/PacketProcessor.java +++ b/src/main/java/net/minestom/server/network/PacketProcessor.java @@ -11,6 +11,7 @@ import net.minestom.server.network.packet.client.handler.ClientLoginPacketsHandl import net.minestom.server.network.packet.client.handler.ClientPlayPacketsHandler; import net.minestom.server.network.packet.client.handler.ClientStatusPacketsHandler; import net.minestom.server.network.packet.client.handshake.HandshakePacket; +import net.minestom.server.network.player.IPlayerConnection; import net.minestom.server.network.player.PlayerConnection; import java.util.Arrays; @@ -40,7 +41,8 @@ public class PacketProcessor { private List printBlackList = Arrays.asList(17, 18, 19); public void process(ChannelHandlerContext channel, ByteBuf buffer, int id, int offset) { - PlayerConnection playerConnection = connectionPlayerConnectionMap.computeIfAbsent(channel, c -> new PlayerConnection(channel)); + PlayerConnection playerConnection = + connectionPlayerConnectionMap.computeIfAbsent(channel, c -> new IPlayerConnection(channel)); ConnectionState connectionState = playerConnection.getConnectionState(); //if (!printBlackList.contains(id)) { //System.out.println("RECEIVED ID: 0x" + Integer.toHexString(id) + " State: " + connectionState); diff --git a/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java b/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java index 6ae85441e..2709f79e8 100644 --- a/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java @@ -14,7 +14,7 @@ public class PingPacket implements ClientPreplayPacket { public void process(PlayerConnection connection, ConnectionManager connectionManager) { PongPacket pongPacket = new PongPacket(number); connection.sendPacket(pongPacket); - connection.getChannel().close(); + connection.disconnect(); } @Override diff --git a/src/main/java/net/minestom/server/network/player/IPlayerConnection.java b/src/main/java/net/minestom/server/network/player/IPlayerConnection.java new file mode 100644 index 000000000..78187c2e9 --- /dev/null +++ b/src/main/java/net/minestom/server/network/player/IPlayerConnection.java @@ -0,0 +1,61 @@ +package net.minestom.server.network.player; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import net.minestom.server.network.packet.server.ServerPacket; +import net.minestom.server.utils.PacketUtils; + +import java.net.SocketAddress; + +/** + * Represent a networking connection with Netty + * It is the implementation used for all server connection client + */ +public class IPlayerConnection extends PlayerConnection { + + private ChannelHandlerContext channel; + + public IPlayerConnection(ChannelHandlerContext channel) { + super(); + this.channel = channel; + } + + @Override + public void sendPacket(ByteBuf buffer) { + buffer.retain(); + channel.writeAndFlush(buffer); + } + + @Override + public void writePacket(ByteBuf buffer) { + buffer.retain(); + channel.write(buffer); + } + + @Override + public void sendPacket(ServerPacket serverPacket) { + ByteBuf buffer = PacketUtils.writePacket(serverPacket); + sendPacket(buffer); + buffer.release(); + } + + @Override + public void flush() { + channel.flush(); + } + + @Override + public SocketAddress getRemoteAddress() { + return channel.channel().remoteAddress(); + } + + @Override + public void disconnect() { + getChannel().close(); + } + + public ChannelHandlerContext getChannel() { + return channel; + } + +} diff --git a/src/main/java/net/minestom/server/network/player/PlayerConnection.java b/src/main/java/net/minestom/server/network/player/PlayerConnection.java index ce347ec2b..2f33ce6af 100644 --- a/src/main/java/net/minestom/server/network/player/PlayerConnection.java +++ b/src/main/java/net/minestom/server/network/player/PlayerConnection.java @@ -1,52 +1,39 @@ package net.minestom.server.network.player; import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; import net.minestom.server.network.ConnectionState; import net.minestom.server.network.packet.server.ServerPacket; -import net.minestom.server.utils.PacketUtils; import java.net.SocketAddress; -public class PlayerConnection { +/** + * A PlayerConnection is an object needed for all created player + * It can be extended to create a new kind of player (NPC for instance) + */ +public abstract class PlayerConnection { - private ChannelHandlerContext channel; private ConnectionState connectionState; private boolean online; - public PlayerConnection(ChannelHandlerContext channel) { - this.channel = channel; - this.connectionState = ConnectionState.UNKNOWN; + public PlayerConnection() { this.online = true; + this.connectionState = ConnectionState.UNKNOWN; } - public void sendPacket(ByteBuf buffer) { - buffer.retain(); - channel.writeAndFlush(buffer); - } + public abstract void sendPacket(ByteBuf buffer); - public void writePacket(ByteBuf buffer) { - buffer.retain(); - channel.write(buffer); - } + public abstract void writePacket(ByteBuf buffer); - public void sendPacket(ServerPacket serverPacket) { - ByteBuf buffer = PacketUtils.writePacket(serverPacket); - sendPacket(buffer); - buffer.release(); - } + public abstract void sendPacket(ServerPacket serverPacket); - public void flush() { - channel.flush(); - } + public abstract void flush(); - public SocketAddress getRemoteAddress() { - return channel.channel().remoteAddress(); - } + public abstract SocketAddress getRemoteAddress(); - public ChannelHandlerContext getChannel() { - return channel; - } + /** + * Forcing the player to disconnect + */ + public abstract void disconnect(); public boolean isOnline() { return online;