Skip to content

Commit

Permalink
Merge pull request #7 from Minecraft-Java-Edition-Speedrunning/fix_no…
Browse files Browse the repository at this point in the history
…_internet_crash

fixes a no internet crash
  • Loading branch information
VoidXWalker authored Feb 1, 2023
2 parents d43a42b + c12cc58 commit 328714e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
14 changes: 9 additions & 5 deletions src/main/java/me/voidxwalker/serversiderng/RNGHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -207,7 +214,4 @@ public RandomMapEntry(Random random){
useTimes=0;
}
}
}



}
18 changes: 15 additions & 3 deletions src/main/java/me/voidxwalker/serversiderng/RNGSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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));
}
/**
Expand Down

0 comments on commit 328714e

Please sign in to comment.