two stage auth timer
This commit is contained in:
@@ -128,6 +128,8 @@ public final class AuthManager {
|
||||
}
|
||||
if (!s.authenticated) {
|
||||
s.stage = isRegistered(player.getName().getString()) ? Stage.LOGIN : Stage.REGISTER;
|
||||
s.joinedAt = System.currentTimeMillis();
|
||||
s.lastCountdownSecond = -1L;
|
||||
showCurrentDialog(player, s);
|
||||
}
|
||||
}
|
||||
@@ -225,9 +227,13 @@ public final class AuthManager {
|
||||
for (ServerPlayer player : List.copyOf(server.getPlayerList().getPlayers())) {
|
||||
Session s = sessions.get(player.getUUID());
|
||||
if (s == null || s.authenticated) continue;
|
||||
long remainingMillis = config.authenticationTimeoutSeconds * 1000L - (now - s.joinedAt);
|
||||
int timeoutSeconds = s.stage == Stage.RULES ? Math.max(1, rules.timeoutSeconds) : Math.max(1, config.authenticationTimeoutSeconds);
|
||||
long remainingMillis = timeoutSeconds * 1000L - (now - s.joinedAt);
|
||||
if (remainingMillis <= 0L) {
|
||||
player.connection.disconnect(Component.literal("Authentication timed out. Please reconnect and try again."));
|
||||
String timeoutMessage = s.stage == Stage.RULES
|
||||
? "You did not accept the server rules in time. Please reconnect and try again."
|
||||
: "Authentication timed out. Please reconnect and try again.";
|
||||
player.connection.disconnect(Component.literal(timeoutMessage));
|
||||
continue;
|
||||
}
|
||||
// Keep the player at the safe Lobby authentication position. World safety is
|
||||
@@ -248,8 +254,16 @@ public final class AuthManager {
|
||||
long remainingSeconds = Math.max(1L, (remainingMillis + 999L) / 1000L);
|
||||
if (remainingSeconds != s.lastCountdownSecond) {
|
||||
s.lastCountdownSecond = remainingSeconds;
|
||||
String countdownLabel = switch (s.stage) {
|
||||
case RULES -> "Accept the rules within: ";
|
||||
case REGISTER -> "Register within: ";
|
||||
case LOGIN -> "Login within: ";
|
||||
default -> "Time remaining: ";
|
||||
};
|
||||
long minutes = remainingSeconds / 60L;
|
||||
long seconds = remainingSeconds % 60L;
|
||||
player.connection.send(new ClientboundSetActionBarTextPacket(
|
||||
Component.literal("Authentication time remaining: " + remainingSeconds + "s")
|
||||
Component.literal(countdownLabel + String.format(Locale.ROOT, "%02d:%02d", minutes, seconds))
|
||||
.withStyle(net.minecraft.ChatFormatting.YELLOW)
|
||||
));
|
||||
}
|
||||
@@ -275,6 +289,11 @@ public final class AuthManager {
|
||||
a.rulesAcceptedAt = Instant.now().toString();
|
||||
saveAccounts();
|
||||
}
|
||||
// Rules and password authentication have independent timers. Once the rules
|
||||
// are accepted, start a fresh timer for REGISTER/LOGIN instead of carrying over
|
||||
// time spent reading the rules.
|
||||
s.joinedAt = System.currentTimeMillis();
|
||||
s.lastCountdownSecond = -1L;
|
||||
continueAfterRules(player);
|
||||
return 1;
|
||||
}
|
||||
@@ -422,7 +441,7 @@ public final class AuthManager {
|
||||
}
|
||||
}
|
||||
body.add(plainMessage(lines.toString(), "white"));
|
||||
body.add(plainMessage(rules.footer + "\n\nRules version: " + rules.version, "gray"));
|
||||
body.add(plainMessage(rules.footer, "gray"));
|
||||
dialog.add("body", body);
|
||||
|
||||
com.google.gson.JsonArray inputs = new com.google.gson.JsonArray();
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.List;
|
||||
public final class RulesConfig {
|
||||
public boolean enabled = true;
|
||||
public int version = 1;
|
||||
public int timeoutSeconds = 60;
|
||||
public String title = "Server Rules";
|
||||
public String introduction = "Please read and accept our rules:";
|
||||
public List<String> rules = new ArrayList<>(List.of(
|
||||
|
||||
Reference in New Issue
Block a user