ProtectedPlayerManager: State changes.

- Updated updateState method to fit in all current config options as well as the new grace states.
- Added Javadoc to startAfkTimer method.
- Removed old todo comments.
- Removed unnecessary state check in the afk timer task.
- Renamed startGracePeriod method to startGraceTimer. Added delay parameter. Updated Javadoc. Removed old grace period time initialization in the protected player. Rewrote the timer task to now just update the player state after the delay.
- Removed unnecessary check at the end of applyPostEffects to remove player if afk is disabled. This is now done in the updateState method.
This commit is contained in:
2022-06-07 14:35:12 +01:00
parent 1ad35ed9d9
commit 091e148dcd

View File

@ -89,18 +89,56 @@ public class ProtectedPlayerManager {
addPlayer(playerUUID);
} else {
ProtectedPlayer.State currentState = protectedPlayer.getState();
if (currentState.equals(ProtectedPlayer.State.JOINING) || currentState.equals(ProtectedPlayer.State.AFK)) {
// JOINING, AFK -> ACTIVE
protectedPlayer.setState(ProtectedPlayer.State.ACTIVE);
if (Config.Server.POST_GRACE_ENABLED.get()) {
startGracePeriod(playerUUID);
if (currentState.equals(ProtectedPlayer.State.JOINING)) {
if (Config.Server.LOGIN_GRACE_ENABLED.get()) {
// JOINING -> LOGIN_GRACE
protectedPlayer.setState(ProtectedPlayer.State.LOGIN_GRACE);
startGraceTimer(playerUUID, Config.Server.LOGIN_GRACE_DURATION.get() * 1000);
} else {
// JOINING -> ACTIVE
protectedPlayer.setState(ProtectedPlayer.State.ACTIVE);
if (Config.Server.LOGIN_APPLY_POST_EFFECTS.get()) {
applyPostEffects(playerUUID);
}
if (Config.Server.AFK_PROTECTION_ENABLED.get()) {
startAfkTimer(playerUUID, Config.Server.AFK_TIME_THRESHOLD.get() * 1000);
} else {
removePlayer(playerUUID);
}
}
} else if (currentState.equals(ProtectedPlayer.State.LOGIN_GRACE) ||
currentState.equals(ProtectedPlayer.State.AFK_GRACE)) {
// LOGIN_GRACE, AFK_GRACE -> ACTIVE
protectedPlayer.setState(ProtectedPlayer.State.ACTIVE);
if ((currentState.equals(ProtectedPlayer.State.LOGIN_GRACE) && Config.Server.LOGIN_APPLY_POST_EFFECTS.get()) ||
(currentState.equals(ProtectedPlayer.State.AFK_GRACE) && Config.Server.AFK_APPLY_POST_EFFECTS.get())) {
applyPostEffects(playerUUID);
}
startAfkTimer(playerUUID, Config.Server.AFK_TIME_THRESHOLD.get() * 1000);
} else {
if (Config.Server.AFK_PROTECTION_ENABLED.get()) {
startAfkTimer(playerUUID, Config.Server.AFK_TIME_THRESHOLD.get() * 1000);
} else {
removePlayer(playerUUID);
}
} else if (currentState.equals(ProtectedPlayer.State.ACTIVE)) {
// ACTIVE -> AFK
protectedPlayer.setState(ProtectedPlayer.State.AFK);
} else if (currentState.equals(ProtectedPlayer.State.AFK)) {
if (Config.Server.AFK_GRACE_ENABLED.get()) {
// AFK -> AFK_GRACE
protectedPlayer.setState(ProtectedPlayer.State.AFK_GRACE);
startGraceTimer(playerUUID, Config.Server.AFK_GRACE_DURATION.get() * 1000);
} else {
// AFK -> ACTIVE
protectedPlayer.setState(ProtectedPlayer.State.ACTIVE);
if (Config.Server.AFK_APPLY_POST_EFFECTS.get()) {
applyPostEffects(playerUUID);
}
if (Config.Server.AFK_PROTECTION_ENABLED.get()) {
startAfkTimer(playerUUID, Config.Server.AFK_TIME_THRESHOLD.get() * 1000);
} else {
removePlayer(playerUUID);
}
}
}
// Send state packet to player
@ -110,6 +148,11 @@ public class ProtectedPlayerManager {
}
}
/**
* Starts the afk timer for a given player if they are a protected player.
* @param playerUUID The UUID of the player.
* @param delay After how much time should the task run? (in milliseconds)
*/
public static void startAfkTimer(UUID playerUUID, long delay) {
ProtectedPlayer player = getPlayer(playerUUID);
if (player == null) {
@ -117,15 +160,9 @@ public class ProtectedPlayerManager {
}
// Create scheduled task
// Todo: Make task period change according to the last input tick of a player
player.setAfkTimerTask(new TimerTask() {
@Override
public void run() {
if (!player.getState().equals(ProtectedPlayer.State.ACTIVE)) {
this.cancel();
return;
}
try {
// Send request for list input tick packet to player
NetworkManager.getChannel().sendTo(new RequestLastInputTickPacket(),
@ -139,32 +176,24 @@ public class ProtectedPlayerManager {
}
/**
* Starts the grace period counter for a player.
* @param playerUUID UUID of player to start the grace period for.
* Starts the grace period timer for a given player if they are a protected player.
* @param playerUUID UUID of the player.
* @param delay How long should the grace period last? (in milliseconds)
*/
private static void startGracePeriod(UUID playerUUID) {
private static void startGraceTimer(UUID playerUUID, long delay) {
ProtectedPlayer player = getPlayer(playerUUID);
if (player == null) {
return;
}
// Set grace period length
player.setGracePeriod(Config.Server.POST_GRACE_DURATION.get());
// Create scheduled task
player.setGracePeriodTimerTask(new TimerTask() {
@Override
public void run() {
player.setGracePeriod(player.getGracePeriodTimeRemaining() - 1);
if (player.getGracePeriodTimeRemaining() == 0) {
applyPostEffects(playerUUID);
this.cancel();
if (!Config.Server.AFK_PROTECTION_ENABLED.get()) {
ProtectedPlayerManager.removePlayer(playerUUID);
}
}
// Update player state
updateState(playerUUID);
}
});
}, delay);
}
/**
@ -201,10 +230,5 @@ public class ProtectedPlayerManager {
Config.Server.POST_FIRE_DURATION.get()*20, 0));
}
}
// Remove player if afk protection is disabled
if (!Config.Server.AFK_PROTECTION_ENABLED.get()) {
ProtectedPlayerManager.removePlayer(playerUUID);
}
}
}