From cb27476401d1c01a258cb092431717463c6ed1c2 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Sat, 13 Feb 2021 14:31:56 +0100 Subject: [PATCH] rendering: use more features of modding api, fix some bugs in ServerListCell --- .../minosoft/data/entities/Location.kt | 4 +++ .../minosoft/gui/main/ServerListCell.java | 29 +++++++++++------ .../minosoft/gui/rendering/RenderWindow.kt | 32 ++++++++++++++++++- .../minosoft/gui/rendering/Rendering.kt | 21 ------------ .../minosoft/protocol/network/Connection.java | 2 +- .../network/socket/BlockingSocketNetwork.java | 2 +- .../play/PacketPlayerPositionAndRotation.java | 2 -- 7 files changed, 56 insertions(+), 36 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/data/entities/Location.kt b/src/main/java/de/bixilon/minosoft/data/entities/Location.kt index db660939d..498e48e61 100644 --- a/src/main/java/de/bixilon/minosoft/data/entities/Location.kt +++ b/src/main/java/de/bixilon/minosoft/data/entities/Location.kt @@ -24,6 +24,10 @@ data class Location(val x: Double, val y: Double, val z: Double) { return "($x $y $z)" } + fun toVec3(): Vec3 { + return Vec3(x, y, z) + } + companion object { @JvmStatic fun fromPosition(position: BlockPosition): Location { diff --git a/src/main/java/de/bixilon/minosoft/gui/main/ServerListCell.java b/src/main/java/de/bixilon/minosoft/gui/main/ServerListCell.java index 103954e48..ce590d7c3 100644 --- a/src/main/java/de/bixilon/minosoft/gui/main/ServerListCell.java +++ b/src/main/java/de/bixilon/minosoft/gui/main/ServerListCell.java @@ -76,7 +76,7 @@ public class ServerListCell extends ListCell implements Initializable { public MenuButton optionsMenu; public Label pingField; - boolean canConnect; + private boolean canConnect; private Server server; public static ServerListCell newInstance() { @@ -286,21 +286,26 @@ public class ServerListCell extends ListCell implements Initializable { if (!this.canConnect || this.server.getLastPing() == null) { return; } + if (this.server.isConnected()) { + return; + } this.root.getStyleClass().add("list-cell-connecting"); new Thread(() -> { Connection connection = new Connection(Connection.lastConnectionId++, this.server.getAddress(), new Player(Minosoft.getConfig().getSelectedAccount())); + this.server.addConnection(connection); + Platform.runLater(() -> { + this.optionsConnect.setDisable(true); + }); Version version; if (this.server.getDesiredVersionId() == ProtocolDefinition.QUERY_PROTOCOL_VERSION_ID) { version = this.server.getLastPing().getVersion(); } else { version = Versions.getVersionById(this.server.getDesiredVersionId()); } - this.optionsConnect.setDisable(true); // ToDo: show progress dialog connection.connect(this.server.getLastPing().getAddress(), version, new CountUpAndDownLatch(1)); connection.registerEvent(new EventInvokerCallback<>(this::handleConnectionCallback)); - this.server.addConnection(connection); }, "ConnectThread").start(); } @@ -328,15 +333,19 @@ public class ServerListCell extends ListCell implements Initializable { default -> ""; }); - if (!connection.isConnected()) { - // maybe we got disconnected - if (!this.server.isConnected()) { - this.optionsSessions.setDisable(true); - return; - } + if (connection.isConnected()) { + this.optionsConnect.setDisable(Minosoft.getConfig().getSelectedAccount() == connection.getPlayer().getAccount()); + this.optionsSessions.setDisable(false); + return; } - this.optionsConnect.setDisable(Minosoft.getConfig().getSelectedAccount() == connection.getPlayer().getAccount()); + if (this.server.isConnected()) { + this.optionsSessions.setDisable(false); + this.optionsConnect.setDisable(false); + return; + } + + this.optionsConnect.setDisable(false); this.optionsSessions.setDisable(false); }); } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt index 6526b37eb..2466b8629 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt @@ -5,7 +5,11 @@ import de.bixilon.minosoft.data.entities.Location import de.bixilon.minosoft.gui.rendering.chunk.ChunkRenderer import de.bixilon.minosoft.gui.rendering.hud.HUDRenderer import de.bixilon.minosoft.gui.rendering.hud.elements.RenderStats +import de.bixilon.minosoft.modding.event.EventInvokerCallback +import de.bixilon.minosoft.modding.event.events.ConnectionStateChangeEvent +import de.bixilon.minosoft.modding.event.events.PacketReceiveEvent import de.bixilon.minosoft.protocol.network.Connection +import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketPlayerPositionAndRotation import de.bixilon.minosoft.protocol.packets.serverbound.play.PacketPlayerPositionAndRotationSending import de.bixilon.minosoft.util.CountUpAndDownLatch import de.bixilon.minosoft.util.logging.Log @@ -30,7 +34,7 @@ class RenderWindow(private val connection: Connection, val rendering: Rendering) private var lastFrame = 0.0 lateinit var camera: Camera - var latch = CountUpAndDownLatch(1) + private val latch = CountUpAndDownLatch(1) // all renderers val chunkRenderer: ChunkRenderer = ChunkRenderer(connection, connection.player.world, this) @@ -38,6 +42,29 @@ class RenderWindow(private val connection: Connection, val rendering: Rendering) val renderQueue = ConcurrentLinkedQueue() + init { + connection.registerEvent(EventInvokerCallback { + if (it.connection.isDisconnected) { + renderQueue.add { + glfwSetWindowShouldClose(windowId, true) + } + } + }) + connection.registerEvent(EventInvokerCallback { + val packet = it.packet + if (packet !is PacketPlayerPositionAndRotation) { + return@EventInvokerCallback + } + if (latch.count > 0) { + latch.countDown() + } + renderQueue.add { + camera.cameraPosition = packet.location.toVec3() + camera.setRotation(packet.rotation.yaw.toDouble(), packet.rotation.pitch.toDouble()) + } + }) + } + fun init(latch: CountUpAndDownLatch) { // Setup an error callback. The default implementation @@ -191,6 +218,9 @@ class RenderWindow(private val connection: Connection, val rendering: Rendering) // Terminate GLFW and free the error callback glfwTerminate() glfwSetErrorCallback(null)!!.free() + + // disconnect + connection.disconnect() } private fun switchPolygonMode() { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/Rendering.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/Rendering.kt index 3df30b3f5..8d2e9fd85 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/Rendering.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/Rendering.kt @@ -1,12 +1,9 @@ package de.bixilon.minosoft.gui.rendering -import de.bixilon.minosoft.data.entities.EntityRotation -import de.bixilon.minosoft.data.entities.Location import de.bixilon.minosoft.protocol.network.Connection import de.bixilon.minosoft.util.CountUpAndDownLatch import de.bixilon.minosoft.util.Util import de.bixilon.minosoft.util.logging.Log -import glm_.vec3.Vec3 import org.lwjgl.Version import java.util.concurrent.ExecutorService import java.util.concurrent.Executors @@ -24,22 +21,4 @@ class Rendering(private val connection: Connection) { renderWindow.exit() }, "Rendering").start() } - - - fun teleport(position: Location) { - // tell the window we are ready (received position) - if (renderWindow.latch.count > 0) { - renderWindow.latch.countDown() - } - renderWindow.renderQueue.add { - renderWindow.camera.cameraPosition = Vec3(position.x, position.y, position.z) - } - } - - fun rotate(rotation: EntityRotation) { - renderWindow.renderQueue.add { - renderWindow.camera.setRotation(rotation.yaw.toDouble(), rotation.pitch.toDouble()) - } - } - } diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java b/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java index 90c5cdd63..357d4cd83 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java +++ b/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java @@ -450,7 +450,7 @@ public class Connection { this.pong = pong; } - public boolean shouldDisconnect() { + public boolean isDisconnected() { return getConnectionState() == ConnectionStates.DISCONNECTING || getConnectionState() == ConnectionStates.DISCONNECTED || getConnectionState() == ConnectionStates.FAILED || getConnectionState() == ConnectionStates.FAILED_NO_RETRY; } diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/socket/BlockingSocketNetwork.java b/src/main/java/de/bixilon/minosoft/protocol/network/socket/BlockingSocketNetwork.java index 9e6adf266..887682805 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/socket/BlockingSocketNetwork.java +++ b/src/main/java/de/bixilon/minosoft/protocol/network/socket/BlockingSocketNetwork.java @@ -133,7 +133,7 @@ public class BlockingSocketNetwork extends Network { @Override public void disconnect() { - if (this.connection.shouldDisconnect()) { + if (this.connection.isDisconnected()) { // already trying return; } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketPlayerPositionAndRotation.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketPlayerPositionAndRotation.java index 0d71d65d0..c7199933f 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketPlayerPositionAndRotation.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketPlayerPositionAndRotation.java @@ -58,8 +58,6 @@ public class PacketPlayerPositionAndRotation extends ClientboundPacket { } else { connection.sendPacket(new PacketPlayerPositionAndRotationSending(getLocation(), getRotation(), isOnGround())); } - connection.getRenderer().teleport(this.location); - connection.getRenderer().rotate(this.rotation); } @Override