From c12cc58ac00e881018128736d71025f009922c67 Mon Sep 17 00:00:00 2001 From: luna Date: Tue, 31 Jan 2023 17:33:41 +0100 Subject: [PATCH] fixes a no internet crash --- .../voidxwalker/serversiderng/RNGHandler.java | 14 +++++++++----- .../voidxwalker/serversiderng/RNGSession.java | 18 +++++++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/main/java/me/voidxwalker/serversiderng/RNGHandler.java b/src/main/java/me/voidxwalker/serversiderng/RNGHandler.java index 5c8e546..7d93682 100644 --- a/src/main/java/me/voidxwalker/serversiderng/RNGHandler.java +++ b/src/main/java/me/voidxwalker/serversiderng/RNGHandler.java @@ -9,6 +9,8 @@ import org.jetbrains.annotations.Nullable; import java.io.IOException; +import java.net.ConnectException; +import java.net.UnknownHostException; import java.util.LinkedHashMap; import java.util.Map; import java.util.Random; @@ -50,7 +52,12 @@ public void log(Level level,String message){ public static RNGHandler createRNGHandlerOrNull(long runId) { try { return new RNGHandler(new Random(ServerSideRNG.getGetRandomToken(runId).get("random").getAsLong()).nextLong()); - } catch (IOException | NullPointerException e) { + } + catch (UnknownHostException | ConnectException e){ + ServerSideRNG.log(Level.WARN,"Failed to create new RNGHandler: Could not connect to the Server."); + return null; + } + catch (IOException | NullPointerException e) { ServerSideRNG.log(Level.WARN,"Failed to create new RNGHandler: "); e.printStackTrace(); return null; @@ -207,7 +214,4 @@ public RandomMapEntry(Random random){ useTimes=0; } } -} - - - +} \ No newline at end of file diff --git a/src/main/java/me/voidxwalker/serversiderng/RNGSession.java b/src/main/java/me/voidxwalker/serversiderng/RNGSession.java index 48ead74..6596ee9 100644 --- a/src/main/java/me/voidxwalker/serversiderng/RNGSession.java +++ b/src/main/java/me/voidxwalker/serversiderng/RNGSession.java @@ -5,6 +5,8 @@ import org.jetbrains.annotations.Nullable; import java.io.IOException; +import java.net.ConnectException; +import java.net.UnknownHostException; import java.util.Optional; import java.util.Random; import java.util.concurrent.CompletableFuture; @@ -129,7 +131,12 @@ public static boolean inSession() { public static RNGSession createRNGSessionOrNull() { try { return new RNGSession(ServerSideRNG.getStartRunToken()); - } catch (IOException e) { + } + catch (UnknownHostException | ConnectException e){ + ServerSideRNG.log(Level.WARN,"Failed to create new RNGSession: Could not connect to the Server."); + return null; + } + catch (IOException e) { ServerSideRNG.log(Level.WARN,"Failed to create new RNGSession: "); e.printStackTrace(); return null; @@ -208,8 +215,6 @@ boolean updateSessionState(){ public void getRngHandlerFromFuture() { try { currentRNGHandler = rngHandlerCompletableFuture.get(1L, TimeUnit.SECONDS); - currentRNGHandler.activate(handlerIndex++); - currentRNGHandler.log(Level.INFO,"Successfully updated the current RNGHandler!"); } catch (InterruptedException | ExecutionException | TimeoutException e) { e.printStackTrace(); } @@ -222,8 +227,15 @@ public void getRngHandlerFromFuture() { } else { this.log(Level.WARN,"Failed to update the current RNGHandler!"); + return; } } + else { + currentRNGHandler.activate(handlerIndex++); + currentRNGHandler.log(Level.INFO,"Successfully updated the current RNGHandler!"); + } + + rngHandlerCompletableFuture = CompletableFuture.supplyAsync(()-> RNGHandler.createRNGHandlerOrNull(runId)); } /**