Making PlayerConnection an abstract class

This commit is contained in:
Felix Cravic 2020-05-25 00:29:37 +02:00
parent ea3189023a
commit 83449aecc9
5 changed files with 82 additions and 32 deletions

View File

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

View File

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

View File

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

View File

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

View File

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