fix some reconnecting bugs, todo for position (1 coordinate buggy)

This commit is contained in:
Bixilon 2020-06-23 22:19:18 +02:00
parent ff679a36d1
commit 6a4e83bc28
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 28 additions and 8 deletions

View File

@ -48,6 +48,7 @@ public class Minosoft {
Log.info(String.format("Loaded config file (version=%s)", config.getInteger(GameConfiguration.CONFIG_VERSION)));
// set log level from config
Log.setLevel(LogLevel.byName(config.getString(GameConfiguration.GENERAL_LOG_LEVEL)));
Log.info(String.format("Logging info with level: %s", Log.getLevel().name()));
checkClientToken();

View File

@ -35,8 +35,8 @@ public class Connection {
private final int port;
private final Network network;
private final PacketHandler handler;
private final PluginChannelHandler pluginChannelHandler;
private final ArrayList<ClientboundPacket> handlingQueue;
private PluginChannelHandler pluginChannelHandler;
Thread handleThread;
ProtocolVersion version = ProtocolVersion.VERSION_1_7_10; // default
private Player player;
@ -49,14 +49,13 @@ public class Connection {
network = new Network(this);
handlingQueue = new ArrayList<>();
handler = new PacketHandler(this);
pluginChannelHandler = new PluginChannelHandler(this);
registerDefaultChannels();
}
/**
* Sends an server ping to the server (player count, motd, ...)
*/
public void ping() {
Log.info(String.format("Pinging server: %s:%d", host, port));
reason = ConnectionReason.PING;
network.connect();
}
@ -65,6 +64,7 @@ public class Connection {
* Tries to connect to the server and login
*/
public void connect() {
Log.info(String.format("Connecting to server: %s:%d", host, port));
if (reason == null) {
// first get version, then login
reason = ConnectionReason.GET_VERSION;
@ -97,7 +97,7 @@ public class Connection {
network.startPacketThread();
startHandlingThread();
ConnectionState next = ((reason == ConnectionReason.CONNECT) ? ConnectionState.LOGIN : ConnectionState.STATUS);
network.sendPacket(new PacketHandshake(getHost(), getPort(), next, next == ConnectionState.STATUS ? -1 : getVersion().getVersion()));
network.sendPacket(new PacketHandshake(getHost(), getPort(), next, (next == ConnectionState.STATUS) ? -1 : getVersion().getVersion()));
// after sending it, switch to next state
setConnectionState(next);
break;
@ -108,7 +108,14 @@ public class Connection {
break;
case LOGIN:
network.sendPacket(new PacketLoginStart(player));
pluginChannelHandler = new PluginChannelHandler(this);
registerDefaultChannels();
break;
case DISCONNECTED:
if (reason == ConnectionReason.GET_VERSION) {
setReason(ConnectionReason.CONNECT);
connect();
}
}
}

View File

@ -43,6 +43,7 @@ public class Network {
private Cipher cipherEncrypt;
private Cipher cipherDecrypt;
private Thread packetThread;
Thread socketThread;
private boolean connected;
public Network(Connection c) {
@ -60,7 +61,7 @@ public class Network {
// everything sent for now, waiting for data
// add to queue
// Could not connect
Thread socketThread = new Thread(() -> {
socketThread = new Thread(() -> {
try {
socket = new Socket(connection.getHost(), connection.getPort());
connected = true;

View File

@ -176,8 +176,21 @@ public class InByteBuffer {
}
public BlockPosition readPosition() {
//ToDo: here is something wrong with z :(
Long raw = readLong();
return new BlockPosition(Long.valueOf(raw >>> 38).intValue(), Long.valueOf(raw & 0xFFF).shortValue(), Long.valueOf(raw << 26 >>> 38).intValue());
int x = (int) (raw >> 38);
short y = (short) (raw & 0xFFF);
int z = (int) (raw >> 12) & 0x3FFFFFF;
if (x >= (2 ^ 25)) {
x -= 2 ^ 26;
}
if (y >= (2 ^ 11)) {
y -= 2 ^ 12;
}
if (z >= (2 ^ 25)) {
z -= 2 ^ 26;
}
return new BlockPosition(x, y, z);
}
@Override

View File

@ -67,8 +67,6 @@ public class PacketHandler {
// reconnect...
connection.disconnect();
Log.info(String.format("Server is running on version %s, reconnecting...", connection.getVersion().getName()));
connection.setReason(ConnectionReason.CONNECT);
connection.connect();
break;
case CONNECT:
// do nothing