Skip to content

Commit

Permalink
Use UUID based map adapter and tweak logic (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pugzy authored Apr 19, 2023
1 parent b896671 commit 73be2e0
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 37 deletions.
6 changes: 0 additions & 6 deletions src/main/java/tc/oc/occ/idly/IdlyConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public class IdlyConfig {
private int warningDuration;
private int warningFrequency;
private boolean bypassEnabled;
private boolean requireMatchRunning;
private boolean preciseMovement;
private boolean movementCheck;
private boolean chatCheck;
Expand Down Expand Up @@ -61,10 +60,6 @@ public boolean isBypassEnabled() {
return bypassEnabled;
}

public boolean isRequireMatchRunning() {
return requireMatchRunning;
}

public boolean isPreciseMovement() {
return preciseMovement;
}
Expand Down Expand Up @@ -110,7 +105,6 @@ public void reload(Configuration config) {
this.warningDuration = config.getInt("warning-duration");
this.warningFrequency = config.getInt("warning-frequency");
this.bypassEnabled = config.getBoolean("bypass-enabled", true);
this.requireMatchRunning = config.getBoolean("require-match-running");
this.preciseMovement = config.getBoolean("precise-movement");
this.movementCheck = config.getBoolean("checks.movement");
this.chatCheck = config.getBoolean("checks.chat");
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/tc/oc/occ/idly/IdlyListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import tc.oc.pgm.api.match.event.MatchStartEvent;
import tc.oc.pgm.api.match.MatchPhase;
import tc.oc.pgm.api.match.event.MatchPhaseChangeEvent;
import tc.oc.pgm.api.party.Competitor;
import tc.oc.pgm.events.PlayerPartyChangeEvent;

Expand All @@ -34,8 +35,11 @@ public void onPlayerPartyChange(PlayerPartyChangeEvent event) {
}

@EventHandler(priority = EventPriority.MONITOR)
public void onMatchStart(MatchStartEvent event) {
event.getMatch().getParticipants().forEach(p -> this.manager.logMovement(p.getBukkit()));
public void onMatchStart(MatchPhaseChangeEvent event) {
// Log participant activity when match starts (to avoid instant kicking)
if (event.getOldPhase() == MatchPhase.IDLE && event.getNewPhase() != MatchPhase.IDLE) {
event.getMatch().getParticipants().forEach(p -> this.manager.logMovement(p.getBukkit()));
}
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
Expand Down
39 changes: 23 additions & 16 deletions src/main/java/tc/oc/occ/idly/IdlyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import static net.kyori.adventure.text.Component.text;

import com.google.common.base.Objects;
import java.util.UUID;
import javax.annotation.Nullable;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import tc.oc.occ.idly.utils.OnlinePlayerUUIDMapAdapter;
import tc.oc.pgm.api.match.Match;
import tc.oc.pgm.api.player.MatchPlayer;
import tc.oc.pgm.util.bukkit.OnlinePlayerMapAdapter;

public class IdlyManager {

Expand All @@ -22,12 +24,12 @@ public class IdlyManager {

private final Idly plugin;
private final IdlyConfig config;
private final OnlinePlayerMapAdapter<Integer> playerInactivityTicks;
private final OnlinePlayerUUIDMapAdapter<Integer> playerInactivityTicks;

public IdlyManager(Idly plugin) {
this.plugin = plugin;
this.config = plugin.getIdlyConfig();
this.playerInactivityTicks = new OnlinePlayerMapAdapter<Integer>(plugin);
this.playerInactivityTicks = new OnlinePlayerUUIDMapAdapter<>(plugin);

plugin
.getServer()
Expand All @@ -36,11 +38,11 @@ public IdlyManager(Idly plugin) {
}

public void logMovement(Player player) {
playerInactivityTicks.put(player, 0);
playerInactivityTicks.put(player.getUniqueId(), 0);
}

public boolean isIdle(Player player, int timeoutSeconds) {
Integer inactivityTicks = playerInactivityTicks.get(player);
Integer inactivityTicks = playerInactivityTicks.get(player.getUniqueId());
if (inactivityTicks == null) {
return false;
}
Expand All @@ -52,25 +54,28 @@ public boolean isIdle(Player player, int timeoutSeconds) {
private void checkPlayers() {
if (!config.isEnabled()) return;

for (Player player : this.playerInactivityTicks.keySet()) {
if (config.isRequireMatchRunning() && !plugin.getAPI().isMatchRunning(player)) continue;

checkPlayer(player, plugin.getAPI().isPlaying(player));
for (UUID uuid : this.playerInactivityTicks.keySet()) {
Player player = Bukkit.getPlayer(uuid);
if (player == null || !player.isOnline()) continue;
checkPlayer(player);
}
}

private void checkPlayer(Player player, boolean isPlaying) {
int inactivity =
playerInactivityTicks.compute(
player, (p, t) -> Objects.firstNonNull(t, 0) + TICK_FREQUENCY);
private void checkPlayer(Player player) {
// Ignore those with the bypass permission
if (config.isBypassEnabled() && player.hasPermission(IdlyPermissions.BYPASS)) return;

boolean isPlaying = plugin.getAPI().isPlaying(player);

// Don't track observers when kick mode is disabled
if (!config.isKickMode() && !isPlaying) return;

// Ignore those with the bypass permission
if (config.isBypassEnabled() && player.hasPermission(IdlyPermissions.BYPASS)) return;

int duration = (isPlaying ? config.getParticipantDelay() : config.getObserverDelay());

int inactivity =
playerInactivityTicks.compute(
player.getUniqueId(), (p, t) -> Objects.firstNonNull(t, 0) + TICK_FREQUENCY);

float remaining = duration - inactivity;
if (remaining <= 0) {
kick(player);
Expand All @@ -94,6 +99,8 @@ private void kickFromServer(Player player) {

private void kickFromMatch(Player player) {
MatchPlayer mp = IdlyUtils.getMatchPlayer(player);
if (mp == null) return;

Match match = mp.getMatch();
Audience viewer = plugin.getViewer(player);

Expand Down
11 changes: 4 additions & 7 deletions src/main/java/tc/oc/occ/idly/api/BaseIdlyAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@

import org.bukkit.entity.Player;
import tc.oc.occ.idly.IdlyUtils;
import tc.oc.pgm.api.match.MatchPhase;
import tc.oc.pgm.api.party.Competitor;
import tc.oc.pgm.api.player.MatchPlayer;

public class BaseIdlyAPI implements IdlyAPI {

@Override
public boolean isMatchRunning(Player player) {
MatchPlayer mp = IdlyUtils.getMatchPlayer(player);
return mp != null && mp.getMatch().isRunning();
}

@Override
public boolean isPlaying(Player player) {
MatchPlayer mp = IdlyUtils.getMatchPlayer(player);
return mp != null && mp.getParty() instanceof Competitor && !mp.getMatch().isFinished();
return mp != null
&& mp.getParty() instanceof Competitor
&& (mp.getMatch().getPhase().equals(MatchPhase.STARTING) || mp.getMatch().isRunning());
}
}
2 changes: 0 additions & 2 deletions src/main/java/tc/oc/occ/idly/api/IdlyAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@

public interface IdlyAPI {

boolean isMatchRunning(Player player);

boolean isPlaying(Player player);
}
33 changes: 33 additions & 0 deletions src/main/java/tc/oc/occ/idly/utils/OnlinePlayerUUIDMapAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tc.oc.occ.idly.utils;

import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin;
import tc.oc.pgm.util.bukkit.ListeningMapAdapter;

public class OnlinePlayerUUIDMapAdapter<V> extends ListeningMapAdapter<UUID, V>
implements Listener {
public OnlinePlayerUUIDMapAdapter(Plugin plugin) {
super(plugin);
}

public OnlinePlayerUUIDMapAdapter(Map<UUID, V> map, Plugin plugin) {
super(map, plugin);
}

public boolean isValid(UUID key) {
Player player = Bukkit.getPlayer(key);
return player != null && player.isOnline();
}

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerQuit(PlayerQuitEvent event) {
this.remove(event.getPlayer().getUniqueId());
}
}
3 changes: 0 additions & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ warning-frequency: 5
# Allow player bypass with 'idly.bypass' permission
bypass-enabled: true

# Only check for inactivity when match is running
require-match-running: true

# When true movement is tracked on pitch/yaw movement only
precise-movement: true

Expand Down

0 comments on commit 73be2e0

Please sign in to comment.