From 813f155a2e9a46207082ea324a11e44ba87a3e98 Mon Sep 17 00:00:00 2001 From: Aizistral Date: Wed, 5 Oct 2022 16:28:52 +0200 Subject: Implement reconnect delay, fix unsafe persistence Fixes #190 --- .../nochatreports/NoChatReportsClient.java | 31 +++++++- .../nochatreports/config/NCRConfigClient.java | 20 +++++- .../nochatreports/core/ServerSafetyState.java | 44 +++++++++--- .../nochatreports/gui/AwaitConnectionScreen.java | 82 ++++++++++++++++++++++ .../nochatreports/gui/UnsafeServerScreen.java | 8 ++- 5 files changed, 172 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/aizistral/nochatreports/gui/AwaitConnectionScreen.java (limited to 'src/main/java') diff --git a/src/main/java/com/aizistral/nochatreports/NoChatReportsClient.java b/src/main/java/com/aizistral/nochatreports/NoChatReportsClient.java index 55a31f5..53ee07e 100644 --- a/src/main/java/com/aizistral/nochatreports/NoChatReportsClient.java +++ b/src/main/java/com/aizistral/nochatreports/NoChatReportsClient.java @@ -7,6 +7,7 @@ import com.aizistral.nochatreports.config.NCRConfigClient; import com.aizistral.nochatreports.core.ServerDataExtension; import com.aizistral.nochatreports.core.ServerSafetyLevel; import com.aizistral.nochatreports.core.ServerSafetyState; +import com.aizistral.nochatreports.gui.AwaitConnectionScreen; import com.aizistral.nochatreports.gui.UnsafeServerScreen; import com.aizistral.nochatreports.mixins.client.AccessorDisconnectedScreen; import com.aizistral.nochatreports.network.ClientChannelHandler; @@ -19,6 +20,7 @@ import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; import net.fabricmc.fabric.api.networking.v1.PacketSender; +import net.minecraft.Util; import net.minecraft.client.GuiMessageTag; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.ConnectScreen; @@ -78,6 +80,11 @@ public final class NoChatReportsClient implements ClientModInitializer { var disconnectReason = dsc.getReason(); if (disconnectReason != null) { + if (NCRConfig.getCommon().enableDebugLog()) { + NoChatReports.LOGGER.info("Disconnected with reason {}, reconnect count: {}", + Component.Serializer.toStableJson(disconnectReason), ServerSafetyState.getReconnectCount()); + } + if (ServerSafetyState.allowsUnsafeServer()) { screen = new DisconnectedScreen(new JoinMultiplayerScreen(new TitleScreen()), CONNECT_FAILED_NCR, dsc.getReason()); @@ -89,11 +96,16 @@ public final class NoChatReportsClient implements ClientModInitializer { KEY_DISCONNECT_REASONS.contains(translatable.getKey()))) { if (ServerSafetyState.getLastServerAddress() != null) { if (!NCRConfig.getServerWhitelist().isWhitelisted(ServerSafetyState.getLastServerAddress()) && !NCRConfig.getClient().whitelistAllServers()) { + if (NCRConfig.getCommon().enableDebugLog()) { + NoChatReports.LOGGER.info("Server {} evaluated as unsafe and is not whitelisted, displaying warning screen.", + ServerSafetyState.getLastServerAddress().getHost() + ":" + ServerSafetyState.getLastServerAddress().getPort()); + } + client.setScreen(new UnsafeServerScreen()); } else { if (ServerSafetyState.getReconnectCount() <= 0) { ServerSafetyState.setAllowsUnsafeServer(true); - reconnectLastServer(); + client.setScreen(new AwaitConnectionScreen(new JoinMultiplayerScreen(new TitleScreen()))); screenOverride = false; return; } else { @@ -106,6 +118,12 @@ public final class NoChatReportsClient implements ClientModInitializer { } } screenOverride = false; + } else if (screen instanceof JoinMultiplayerScreen) { + if (NCRConfig.getCommon().enableDebugLog()) { + NoChatReports.LOGGER.info("Initialized JoinMultiplayerScreen screen, resetting safety state!"); + } + + ServerSafetyState.reset(); } } @@ -113,7 +131,12 @@ public final class NoChatReportsClient implements ClientModInitializer { if (!NCRConfig.getClient().enableMod()) return; - client.execute(ServerSafetyState::reset); + if (NCRConfig.getCommon().enableDebugLog()) { + NoChatReports.LOGGER.info("Disconnected from server, resetting safety state!"); + } + + ServerSafetyState.reset(); + ServerSafetyState.setDisconnectMillis(Util.getMillis()); } private void onPlayReady(ClientPacketListener handler, PacketSender sender, Minecraft client) { @@ -140,6 +163,10 @@ public final class NoChatReportsClient implements ClientModInitializer { } } + if (NCRConfig.getCommon().enableDebugLog()) { + NoChatReports.LOGGER.info("Sucessfully connected to server, safety state: {}, reconnect count reset.", ServerSafetyState.getCurrent()); + } + if (NCRConfig.getClient().demandOnServer() && !ClientPlayNetworking.canSend(NoChatReports.CHANNEL)) { handler.getConnection().disconnect(Component.translatable("disconnect.nochatreports.client")); } diff --git a/src/main/java/com/aizistral/nochatreports/config/NCRConfigClient.java b/src/main/java/com/aizistral/nochatreports/config/NCRConfigClient.java index caae64d..3ae3deb 100644 --- a/src/main/java/com/aizistral/nochatreports/config/NCRConfigClient.java +++ b/src/main/java/com/aizistral/nochatreports/config/NCRConfigClient.java @@ -23,7 +23,7 @@ public final class NCRConfigClient extends JSONConfig { alwaysHideReportButton = false, disableTelemetry = true, showReloadButton = true, whitelistAllServers = false, verifiedIconEnabled = true, showNCRButton = true, enableMod = true; - protected int verifiedIconOffsetX = 0, verifiedIconOffsetY = 0; + protected int verifiedIconOffsetX = 0, verifiedIconOffsetY = 0, reconnectAwaitSeconds = 4, postDisconnectAwaitSeconds = 10; protected NCRConfigClient() { super(FILE_NAME); @@ -200,4 +200,22 @@ public final class NCRConfigClient extends JSONConfig { return this.enableMod; } + /** + * @return How many seconds No Chat Reports should wait before attemting automatic reconnects. + * @see Issue #190 + */ + + public int getReconnectAwaitSeconds() { + return this.reconnectAwaitSeconds; + } + + /** + * @return Same as reconnectAwaitSeconds, but countdown starts when user disconnects + * from server. If this delay is greater than reconnectAwaitSeconds then it will apply. + */ + + public int getPostDisconnectAwaitSeconds() { + return this.postDisconnectAwaitSeconds; + } + } diff --git a/src/main/java/com/aizistral/nochatreports/core/ServerSafetyState.java b/src/main/java/com/aizistral/nochatreports/core/ServerSafetyState.java index 96e91eb..50c3661 100644 --- a/src/main/java/com/aizistral/nochatreports/core/ServerSafetyState.java +++ b/src/main/java/com/aizistral/nochatreports/core/ServerSafetyState.java @@ -1,7 +1,14 @@ package com.aizistral.nochatreports.core; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; + import javax.annotation.Nullable; +import com.aizistral.nochatreports.NoChatReports; +import com.aizistral.nochatreports.config.NCRConfig; + import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.multiplayer.ServerData; @@ -17,8 +24,9 @@ public final class ServerSafetyState { private static ServerSafetyLevel current = ServerSafetyLevel.UNKNOWN; private static ServerAddress lastServerAddress = null; private static ServerData lastServerData = null; - private static boolean allowUnsafeServer = false, sessionRequestedKey = false; - private static int reconnectCount = 0; + private static AtomicBoolean allowUnsafeServer = new AtomicBoolean(false), sessionRequestedKey = new AtomicBoolean(false); + private static AtomicInteger reconnectCount = new AtomicInteger(0); + private static AtomicLong disconnectMillis = new AtomicLong(0); public static void updateCurrent(ServerSafetyLevel level) { current = level; @@ -29,19 +37,24 @@ public final class ServerSafetyState { } public static boolean allowsUnsafeServer() { - return current != ServerSafetyLevel.SECURE ? allowUnsafeServer : false; + return current != ServerSafetyLevel.SECURE ? allowUnsafeServer.get() : false; } public static void setAllowsUnsafeServer(boolean allows) { - allowUnsafeServer = allows; + if (NCRConfig.getCommon().enableDebugLog()) { + NoChatReports.LOGGER.info("Set allowUnsafeServer to: " + allows + ", value set in stacktrace:"); + NoChatReports.LOGGER.catching(new RuntimeException().fillInStackTrace()); + } + + allowUnsafeServer.set(allows); } public static void setSessionRequestedKey(boolean requested) { - sessionRequestedKey = requested; + sessionRequestedKey.set(requested); } public static boolean sessionRequestedKey() { - return current != ServerSafetyLevel.SECURE ? sessionRequestedKey : false; + return current != ServerSafetyLevel.SECURE ? sessionRequestedKey.get() : false; } public static boolean forceSignedMessages() { @@ -64,16 +77,29 @@ public final class ServerSafetyState { } public static int getReconnectCount() { - return reconnectCount; + return reconnectCount.get(); } public static void setReconnectCount(int count) { - reconnectCount = count; + reconnectCount.set(count); + } + + public static long getDisconnectMillis() { + return disconnectMillis.get(); + } + + public static void setDisconnectMillis(long millis) { + disconnectMillis.set(millis); } public static void reset() { current = ServerSafetyLevel.UNKNOWN; - allowUnsafeServer = sessionRequestedKey = false; + allowUnsafeServer.set(false); + sessionRequestedKey.set(false); + + if (NCRConfig.getCommon().enableDebugLog()) { + NoChatReports.LOGGER.info("allowUnsafeServer: {}", allowUnsafeServer.get()); + } } } diff --git a/src/main/java/com/aizistral/nochatreports/gui/AwaitConnectionScreen.java b/src/main/java/com/aizistral/nochatreports/gui/AwaitConnectionScreen.java new file mode 100644 index 0000000..cd24a10 --- /dev/null +++ b/src/main/java/com/aizistral/nochatreports/gui/AwaitConnectionScreen.java @@ -0,0 +1,82 @@ +package com.aizistral.nochatreports.gui; + +import com.aizistral.nochatreports.NoChatReports; +import com.aizistral.nochatreports.NoChatReportsClient; +import com.aizistral.nochatreports.config.NCRConfig; +import com.aizistral.nochatreports.core.ServerSafetyState; +import com.mojang.blaze3d.vertex.PoseStack; + +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.Util; +import net.minecraft.client.GameNarrator; +import net.minecraft.client.gui.components.Button; +import net.minecraft.client.gui.screens.ConnectScreen; +import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.CommonComponents; +import net.minecraft.network.chat.Component; + +@Environment(value=EnvType.CLIENT) +public class AwaitConnectionScreen extends Screen { + private final Screen parent; + private boolean aborted = false; + private long millisAwaiting = 0; + + public AwaitConnectionScreen(Screen parent) { + super(GameNarrator.NO_TITLE); + this.parent = parent; + } + + @Override + protected void init() { + this.clearWidgets(); + this.millisAwaiting = Util.getMillis(); + + this.addRenderableWidget(new Button(this.width / 2 - 100, this.height / 4 + 120 + 12, 200, 20, CommonComponents.GUI_CANCEL, button -> { + if (NCRConfig.getCommon().enableDebugLog()) { + NoChatReports.LOGGER.info("Aborted reconnect await!"); + } + + this.aborted = true; + this.minecraft.setScreen(this.parent); + ServerSafetyState.reset(); + })); + } + + @Override + public void tick() { + if (!this.aborted) + if (this.getRemainingTime() <= 0) { + this.aborted = true; + NoChatReportsClient.reconnectLastServer(); + } + } + + @Override + public void render(PoseStack poseStack, int i, int j, float f) { + this.renderBackground(poseStack); + Component status = Component.translatable("nochatreports.connect.await", (this.getRemainingTime()) / 1000L); + ConnectScreen.drawCenteredString(poseStack, this.font, Component.translatable("nochatreports.connect.nokey_rejected"), this.width / 2, this.height / 2 - 50, 0xFFFFFF); + ConnectScreen.drawCenteredString(poseStack, this.font, status, this.width / 2, this.height / 2 - 36, 0xFFFFFF); + super.render(poseStack, i, j, f); + } + + private long getRemainingTime() { + long current = Util.getMillis(); + long disconnect = ServerSafetyState.getDisconnectMillis(); + + if (current - disconnect <= this.getDisconnectAwaitTime() + 100L) + return this.getDisconnectAwaitTime() - (current - disconnect); + else + return this.getAwaitTime() - (current - this.millisAwaiting); + } + + private long getDisconnectAwaitTime() { + return NCRConfig.getClient().getPostDisconnectAwaitSeconds() * 1000L; + } + + private long getAwaitTime() { + return NCRConfig.getClient().getReconnectAwaitSeconds() * 1000L; + } + +} diff --git a/src/main/java/com/aizistral/nochatreports/gui/UnsafeServerScreen.java b/src/main/java/com/aizistral/nochatreports/gui/UnsafeServerScreen.java index 2547c77..30583f7 100644 --- a/src/main/java/com/aizistral/nochatreports/gui/UnsafeServerScreen.java +++ b/src/main/java/com/aizistral/nochatreports/gui/UnsafeServerScreen.java @@ -1,5 +1,6 @@ package com.aizistral.nochatreports.gui; +import com.aizistral.nochatreports.NoChatReports; import com.aizistral.nochatreports.NoChatReportsClient; import com.aizistral.nochatreports.config.NCRConfig; import com.aizistral.nochatreports.core.ServerSafetyState; @@ -52,8 +53,13 @@ public final class UnsafeServerScreen extends WarningScreen { NCRConfig.getServerWhitelist().saveFile(); } + if (NCRConfig.getCommon().enableDebugLog()) { + NoChatReports.LOGGER.info("Proceeding with unsafe connection to {}, added to whitelist: {}", + address.getHost() + ":" + address.getPort(), this.stopShowing.selected()); + } + ServerSafetyState.setAllowsUnsafeServer(true); - NoChatReportsClient.reconnectLastServer(); + this.minecraft.setScreen(new AwaitConnectionScreen(this.joinMultiplayer)); } })); this.addRenderableWidget(new Button(this.width / 2 - 155 + 160, 100 + i, 150, 20, CommonComponents.GUI_BACK, button -> { -- cgit v1.2.3