Skip to content

Commit

Permalink
notify when we are doing a server restart reconnect and use custom au…
Browse files Browse the repository at this point in the history
…toreconnect delay
  • Loading branch information
rfresh2 committed Oct 17, 2024
1 parent 60dc60e commit 7d2b288
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
22 changes: 10 additions & 12 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,6 @@ val shade: Configuration by configurations.creating
shade.extendsFrom(configurations.implementation.get())

dependencies {
implementation("com.zaxxer:HikariCP:6.0.0")
implementation("org.postgresql:postgresql:42.7.4")
val jdbiVersion = "3.45.4"
implementation("org.jdbi:jdbi3-core:$jdbiVersion")
implementation("org.jdbi:jdbi3-postgres:$jdbiVersion")
implementation("com.google.guava:guava:33.3.1-jre")
implementation("ch.qos.logback:logback-classic:1.5.10")
implementation("org.slf4j:slf4j-api:2.0.16")
implementation("org.slf4j:jul-to-slf4j:2.0.16")
implementation("com.mojang:brigadier:1.2.9")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.18.0")
implementation("com.github.rfresh2:SimpleEventBus:1.2")
implementation("com.github.rfresh2.discord4j:discord4j-core:3.4.3.9") {
exclude(group = "io.netty")
}
Expand All @@ -92,6 +80,7 @@ dependencies {
implementation("org.redisson:redisson:3.37.0") {
exclude(group = "io.netty")
}
implementation("com.github.rfresh2:SimpleEventBus:1.2")
val fastutilVersion = "8.5.14"
implementation("com.github.rfresh2.fastutil.maps:object-object-maps:$fastutilVersion")
implementation("com.github.rfresh2.fastutil.maps:int-object-maps:$fastutilVersion")
Expand All @@ -106,6 +95,15 @@ dependencies {
implementation("org.jline:jline:3.27.0")
implementation("org.jline:jline-terminal-jni:3.27.0")
implementation("ar.com.hjg:pngj:2.1.0")
implementation("com.zaxxer:HikariCP:6.0.0")
implementation("org.postgresql:postgresql:42.7.4")
implementation("org.jdbi:jdbi3-postgres:3.45.4")
implementation("com.google.guava:guava:33.3.1-jre")
implementation("ch.qos.logback:logback-classic:1.5.10")
implementation("org.slf4j:slf4j-api:2.0.16")
implementation("org.slf4j:jul-to-slf4j:2.0.16")
implementation("com.mojang:brigadier:1.2.9")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.18.0")
testImplementation("org.junit.jupiter:junit-jupiter:5.11.2")
val lombokVersion = "1.18.34"
compileOnly("org.projectlombok:lombok:$lombokVersion")
Expand Down
27 changes: 21 additions & 6 deletions src/main/java/com/zenith/Proxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -753,12 +753,27 @@ public void handlePlayerOnlineEvent(PlayerOnlineEvent event) {
}

public void handleServerRestartingEvent(ServerRestartingEvent event) {
if (!this.isPrio() && isNull(getCurrentPlayer().get())) {
EXECUTOR.schedule(() -> {
if (isNull(getCurrentPlayer().get()))
disconnect(SERVER_RESTARTING);
}, ((int) (Math.random() * 20)), TimeUnit.SECONDS);
}
if (!CONFIG.client.extra.serverRestartReconnect
|| !CONFIG.client.extra.autoReconnect.enabled
|| !isOn2b2t()
|| isPrio()
|| !isNull(getCurrentPlayer().get())
|| isInQueue()) return;
EXECUTOR.schedule(() -> {
if (DISCORD.isRunning()) {
DISCORD.sendEmbedMessage(
Embed.builder()
.title("Server Restart Reconnect")
.description("AutoReconnecting to end of queue to get back into server as quick as possible")
.successColor());
} else {
CLIENT_LOG.warn("AutoReconnecting to end of queue to get back into server as quick as possible");
}
if (isNull(getCurrentPlayer().get())) {
disconnect(SYSTEM_DISCONNECT);
}
MODULE.get(AutoReconnect.class).scheduleAutoReconnect(60);
}, ((int) (Math.random() * 20)), TimeUnit.SECONDS);
}

public void handlePrioStatusEvent(PrioStatusEvent event) {
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/com/zenith/module/impl/AutoReconnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,20 @@ public void handleDisconnectEvent(DisconnectEvent event) {
return;
}
if (isReconnectableDisconnect(event.reason())) {
if (autoReconnectIsInProgress()) {
debug("AutoReconnect already in progress, not starting another");
return;
}
this.autoReconnectFuture = EXECUTOR.submit(this::autoReconnectRunnable);
scheduleAutoReconnect(CONFIG.client.extra.autoReconnect.delaySeconds);
} else {
info("Cancelling AutoReconnect because disconnect reason is not reconnectable");
}
}

public void scheduleAutoReconnect(final int delaySeconds) {
if (autoReconnectIsInProgress()) {
info("AutoReconnect already in progress, not starting another");
return;
}
this.autoReconnectFuture = EXECUTOR.submit(() -> autoReconnectRunnable(delaySeconds));
}

public boolean shouldAutoDisconnectCancelAutoReconnect(DisconnectEvent event) {
return CONFIG.client.extra.utility.actions.autoDisconnect.enabled && CONFIG.client.extra.utility.actions.autoDisconnect.cancelAutoReconnect && AUTO_DISCONNECT.equals(event.reason());
}
Expand All @@ -74,9 +78,9 @@ public void handleConnectEvent(ConnectEvent event) {
cancelAutoReconnect();
}

private void autoReconnectRunnable() {
private void autoReconnectRunnable(int delaySeconds) {
try {
delayBeforeReconnect();
delayBeforeReconnect(delaySeconds);
if (Thread.currentThread().isInterrupted()) return;
EXECUTOR.execute(() -> {
try {
Expand All @@ -91,8 +95,8 @@ private void autoReconnectRunnable() {
}
}

private void delayBeforeReconnect() {
final int countdown = CONFIG.client.extra.autoReconnect.delaySeconds;
private void delayBeforeReconnect(int delaySeconds) {
final int countdown = delaySeconds;
EVENT_BUS.postAsync(new AutoReconnectEvent(countdown));
// random jitter to help prevent multiple clients from logging in at the same time
Wait.wait((((int) (Math.random() * 5))) % 10);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/zenith/util/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public static final class Extra {
public boolean prioStatusChangeMention = true;
public boolean killMessage = true;
public boolean logChatMessages = true;
public boolean serverRestartReconnect = true;
public final ActionLimiter actionLimiter = new ActionLimiter();
public final VisualRange visualRange = new VisualRange();
public final AutoArmor autoArmor = new AutoArmor();
Expand Down

0 comments on commit 7d2b288

Please sign in to comment.