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 5f501b2af..103954e48 100644 --- a/src/main/java/de/bixilon/minosoft/gui/main/ServerListCell.java +++ b/src/main/java/de/bixilon/minosoft/gui/main/ServerListCell.java @@ -111,16 +111,15 @@ public class ServerListCell extends ListCell implements Initializable { this.root.setVisible(server != null || !empty); this.hBox.setVisible(server != null || !empty); if (empty) { - resetCell(); return; } if (server == null) { - resetCell(); return; } - - resetCell(); + if (this.server != server) { + resetCell(); + } server.setCell(this); this.server = server; @@ -140,7 +139,8 @@ public class ServerListCell extends ListCell implements Initializable { if (server.getLastPing() == null) { server.ping(); } - server.getLastPing().registerEvent(new EventInvokerCallback(ServerListStatusArriveEvent.class, event -> Platform.runLater(() -> { + + server.getLastPing().registerEvent(new EventInvokerCallback(event -> Platform.runLater(() -> { ServerListPing ping = event.getServerListPing(); if (server != this.server) { // cell does not contains us anymore @@ -201,7 +201,7 @@ public class ServerListCell extends ListCell implements Initializable { setErrorMotd(String.format("%s: %s", server.getLastPing().getLastConnectionException().getClass().getCanonicalName(), server.getLastPing().getLastConnectionException().getMessage())); } }))); - server.getLastPing().registerEvent(new EventInvokerCallback(ServerListPongEvent.class, event -> Platform.runLater(() -> { + server.getLastPing().registerEvent(new EventInvokerCallback(event -> Platform.runLater(() -> { this.pingField.setText(String.format("%dms", event.getLatency())); switch (PingBars.byPing(event.getLatency())) { case BARS_5 -> this.pingField.getStyleClass().add("ping-5-bars"); @@ -299,7 +299,7 @@ public class ServerListCell extends ListCell implements Initializable { // ToDo: show progress dialog connection.connect(this.server.getLastPing().getAddress(), version, new CountUpAndDownLatch(1)); - connection.registerEvent(new EventInvokerCallback<>(ConnectionStateChangeEvent.class, this::handleConnectionCallback)); + connection.registerEvent(new EventInvokerCallback<>(this::handleConnectionCallback)); this.server.addConnection(connection); }, "ConnectThread").start(); diff --git a/src/main/java/de/bixilon/minosoft/gui/main/SessionListCell.java b/src/main/java/de/bixilon/minosoft/gui/main/SessionListCell.java index 9c5712e56..ffd3b6797 100644 --- a/src/main/java/de/bixilon/minosoft/gui/main/SessionListCell.java +++ b/src/main/java/de/bixilon/minosoft/gui/main/SessionListCell.java @@ -83,7 +83,7 @@ public class SessionListCell extends ListCell implements Initializab } setStyle(null); this.connection = connection; - connection.registerEvent(new EventInvokerCallback<>(ConnectionStateChangeEvent.class, this::handleConnectionCallback)); + connection.registerEvent(new EventInvokerCallback<>(this::handleConnectionCallback)); this.connectionId.setText(String.format("#%d", connection.getConnectionId())); this.account.setText(connection.getPlayer().getAccount().getUsername()); } diff --git a/src/main/java/de/bixilon/minosoft/modding/event/EventInvokerCallback.java b/src/main/java/de/bixilon/minosoft/modding/event/EventInvokerCallback.java index 9fe19d72c..f959c5d33 100644 --- a/src/main/java/de/bixilon/minosoft/modding/event/EventInvokerCallback.java +++ b/src/main/java/de/bixilon/minosoft/modding/event/EventInvokerCallback.java @@ -25,7 +25,7 @@ public class EventInvokerCallback extends EventInvoke } // if you need instant fireing support - public EventInvokerCallback(Class eventType, InvokerCallback callback) { + public EventInvokerCallback(InvokerCallback callback) { this(false, callback); } 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 bb386e0e4..d9b24a4be 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java +++ b/src/main/java/de/bixilon/minosoft/protocol/network/Connection.java @@ -390,23 +390,26 @@ public class Connection { this.desiredVersionNumber = desiredVersionNumber; } + public void unregisterEvent(EventInvoker method) { + this.eventListeners.remove(method); + + } + public void registerEvent(EventInvoker method) { - this.eventListeners.add(method); - if (method.getEventType() == ServerListStatusArriveEvent.class) { - if (getConnectionState() == ConnectionStates.FAILED || getConnectionState() == ConnectionStates.FAILED_NO_RETRY || this.lastPing != null) { - // ping done - method.invoke(new ServerListStatusArriveEvent(this, this.lastPing)); - } - } else if (method.getEventType() == ServerListPongEvent.class) { - if (getConnectionState() == ConnectionStates.FAILED || getConnectionState() == ConnectionStates.FAILED_NO_RETRY || this.lastPing != null) { - // ping done - if (this.pong != null) { - method.invoke(this.pong); - } - } + if (method.getEventType().isAssignableFrom(ServerListStatusArriveEvent.class) && wasPingDone()) { + // ping done + method.invoke(new ServerListStatusArriveEvent(this, this.lastPing)); + } else if (method.getEventType().isAssignableFrom(ServerListPongEvent.class) && wasPingDone() && this.pong != null) { + method.invoke(this.pong); + } else { + this.eventListeners.add(method); } } + private boolean wasPingDone() { + return getConnectionState() == ConnectionStates.FAILED || getConnectionState() == ConnectionStates.FAILED_NO_RETRY || this.lastPing != null; + } + public Throwable getLastConnectionException() { return (this.lastException != null) ? this.lastException : this.network.getLastException(); }