Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
@EqualsAndHashCode(callSuper = true)
public class AutoReconnectDecisionEvent extends CancellableEvent {
private final ClientDisconnectEvent disconnectEvent;
private int delaySeconds;
}
25 changes: 21 additions & 4 deletions src/main/java/com/zenith/module/impl/AutoReconnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.zenith.event.module.AutoReconnectDecisionEvent;
import com.zenith.event.module.AutoReconnectEvent;
import com.zenith.module.api.Module;
import com.zenith.util.DisconnectReasonInfo;
import com.zenith.util.Wait;
import org.jspecify.annotations.Nullable;

Expand All @@ -27,7 +28,8 @@ public List<EventConsumer<?>> registerEvents() {
return List.of(
of(ClientDisconnectEvent.class, this::handleDisconnectEvent),
of(ClientConnectEvent.class, this::handleConnectEvent),
of(AutoReconnectDecisionEvent.class, this::defaultAutoReconnectCancelHandler)
of(AutoReconnectDecisionEvent.class, this::defaultAutoReconnectCancelHandler),
of(AutoReconnectDecisionEvent.class, this::defaultAlreadyConnectedDelayHandler)
);
}

Expand Down Expand Up @@ -57,12 +59,14 @@ public void onDisable() {

public void handleDisconnectEvent(ClientDisconnectEvent event) {
var autoReconnectEvent = new AutoReconnectDecisionEvent(event);
autoReconnectEvent.setDelaySeconds(CONFIG.client.extra.autoReconnect.delaySeconds);
EVENT_BUS.post(autoReconnectEvent);
if (!autoReconnectEvent.isCancelled()) {
scheduleAutoReconnect(CONFIG.client.extra.autoReconnect.delaySeconds);
} else {
if (autoReconnectEvent.isCancelled()) {
info("Cancelling AutoReconnect because disconnect reason is not reconnectable");
return;
}

scheduleAutoReconnect(autoReconnectEvent.getDelaySeconds());
}

public void scheduleAutoReconnect(final int delaySeconds) {
Expand Down Expand Up @@ -118,4 +122,17 @@ public void defaultAutoReconnectCancelHandler(AutoReconnectDecisionEvent event)
event.setCancelled(true);
}
}

public void defaultAlreadyConnectedDelayHandler(AutoReconnectDecisionEvent event) {
if (event.isCancelled()) return;
if (DisconnectReasonInfo.getDisconnectCategory(event.getDisconnectEvent().reason())
!= DisconnectReasonInfo.DisconnectCategory.ALREADY_CONNECTED) {
return;
}
if (event.getDelaySeconds() != CONFIG.client.extra.autoReconnect.delaySeconds) return;
int configured = CONFIG.client.extra.autoReconnect.alreadyConnectedDelaySecond;
if (configured <= 0) return;
event.setDelaySeconds(configured);
info("Account is already connected; delaying AutoReconnect by {}s", configured);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/zenith/util/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ public static final class AutoReconnect {
public boolean enabled = true;
public int delaySeconds = 5;
public int maxAttempts = 200;
public int alreadyConnectedDelaySecond = 300;
}

public static final class AutoRespawn {
Expand Down