Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: API for AFK and excluded players #88

Merged
merged 22 commits into from
Jul 10, 2021
Merged
Changes from 1 commit
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
33 changes: 22 additions & 11 deletions src/main/java/xyz/nkomarn/harbor/listener/AfkListener.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package xyz.nkomarn.harbor.listener;

import io.papermc.lib.PaperLib;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.HandlerList;
Expand All @@ -17,14 +15,14 @@
import xyz.nkomarn.harbor.Harbor;
import xyz.nkomarn.harbor.provider.DefaultAFKProvider;

import java.util.HashSet;
import java.util.Set;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.function.Function;
import java.util.stream.Collectors;

public final class AfkListener implements Listener {
private final DefaultAFKProvider afkProvider;
private Set<AfkPlayer> players;
private Queue<AfkPlayer> players;
private PlayerMovementChecker movementChecker;

public AfkListener(DefaultAFKProvider afkProvider) {
Expand All @@ -37,16 +35,18 @@ public AfkListener(DefaultAFKProvider afkProvider) {
*/
public void start() {
JavaPlugin plugin = JavaPlugin.getPlugin(Harbor.class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please pass the Harbor instance trough the constructor, to maintain consistency with the rest of the code.

players = new HashSet<>();
players = new ArrayDeque<>();
movementChecker = new PlayerMovementChecker();

// Populate the queue with any existing players
players.addAll(Bukkit.getOnlinePlayers().stream().map((Function<Player, AfkPlayer>) AfkPlayer::new).collect(Collectors.toSet()));

// Register listeners after populating the queue
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);

// We want every player to get a check every 20 ticks.
movementChecker.runTaskTimer(plugin, 0, 20);
// We want every player to get a check every 20 ticks. The runnable smooths out checking a certain
// percentage of players over all 20 ticks. Thusly, the runnable must run on every tick
movementChecker.runTaskTimer(plugin, 0, 1);

JavaPlugin.getPlugin(Harbor.class).getLogger().info("Fallback AFK detection system is enabled");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass Harbor instance through the constructor

(ps you already had the harbor instance assigned to a variable in at the top of the method 🙃 )

}
Expand Down Expand Up @@ -92,11 +92,22 @@ public void onLeave(PlayerQuitEvent event) {
* Internal class for handling the task of checking player movement; Is a separate task so that we can cancel and restart it easily
*/
private final class PlayerMovementChecker extends BukkitRunnable {
private int checksToMake = 0;
@Override
public void run() {
// This might cause lag if there are a TON of players on the server, but by keeping things minimalistic,
// we can try hard to reduce it. This also short-circuits if there are no players to check.
players.stream().filter(AfkPlayer::changed).forEach(afkPlayer -> afkProvider.updateActivity(afkPlayer.player));
if(players.isEmpty()){
checksToMake = 0;
return;
}

// We want every player to get a check every 20 ticks. Therefore we check 1/20th of the players
for (checksToMake += Math.ceil(players.size() / 20.0); checksToMake > 0 && !players.isEmpty(); checksToMake--) {
AfkPlayer afkPlayer = players.poll();
if (afkPlayer.changed()) {
afkProvider.updateActivity(afkPlayer.player);
}
players.add(afkPlayer);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use a double :D This wont kill anyone and will make the logic simpler :D
Currently you will check a player in an unstable state. If one player is on the server the player will be checked every tick.
4 Players -> Every 5 Ticks
10 Players -> Every 10 Ticks
Only after 20 players online the splitting will have the effect I want.
That I used a double had the thought that I might want to check only a friction of the players and I will wait until I have a full player 1 > checksToMake. this will allow empty runs when less then 20 players are online and when more then 20 players are online it will check in some runs more than one,
Doubles arent bad for loops :D

Otherwise we could just merge it and I will apply the optimizations by myself in a new PR. Your choice.

Or still the choice of the maintainers of this project.

        if (players.isEmpty()) return;

        checksToMake += players.size() / 20.0;

        while (checksToMake > 1) {
            AfkPlayer afkPlayer = players.poll();
            if (afkPlayer.changed()) {
                playerManager.updateActivity(afkPlayer.player);
            }
            players.add(afkPlayer);
            checksToMake--;
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted to double, using for loop syntax:

 private final class PlayerMovementChecker extends BukkitRunnable {
        private double checksToMake = 0;
        @Override
        public void run() {
            if(players.isEmpty()){
                checksToMake = 0;
                return;
            }

            // We want every player to get a check every 20 ticks. Therefore we check 1/20th of the players
            for (checksToMake += players.size() / 20.0D; checksToMake > 0 && !players.isEmpty(); checksToMake--) {
                AfkPlayer afkPlayer = players.poll();
                if (afkPlayer.changed()) {
                    afkProvider.updateActivity(afkPlayer.player);
                }
                players.add(afkPlayer);
            }
        }
    }

}
}

Expand Down