-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Changes from 1 commit
d695fc0
a33042f
548de9e
5d10802
18fd171
4b9bc53
15a05a4
75bd636
434dc1a
1d627ba
fb8f988
dea5f99
59393cf
62aa52b
8f7e11c
b2cc7b8
b071d21
db165e6
58218ea
b0820c9
270ccd2
e361db8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
|
@@ -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) { | ||
|
@@ -37,16 +35,18 @@ public AfkListener(DefaultAFKProvider afkProvider) { | |
*/ | ||
public void start() { | ||
JavaPlugin plugin = JavaPlugin.getPlugin(Harbor.class); | ||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 🙃 ) |
||
} | ||
|
@@ -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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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--;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe 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);
}
}
} |
||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
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.