-
-
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 17 commits
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package xyz.nkomarn.harbor.api; | ||
|
||
import org.bukkit.entity.Player; | ||
|
||
/** | ||
* The {@link AFKProvider} interface provides a way for an implementing | ||
* class in an external plugin to provide a way for external plugins to tell | ||
* Harbor if a Player is AFK, in case of a custom AFK implementation | ||
* | ||
* @see xyz.nkomarn.harbor.Harbor#addExclusionProvider(ExclusionProvider) | ||
*/ | ||
public interface AFKProvider { | ||
/** | ||
* Tests if a {@link Player} is AFK | ||
* | ||
* @param player The {@link Player} that is being checked | ||
* | ||
* @return If the player is afk (true) or not (false) | ||
*/ | ||
boolean isAFK(Player player); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package xyz.nkomarn.harbor.api; | ||
|
||
import org.bukkit.entity.Player; | ||
|
||
/** | ||
* The {@link ExclusionProvider} interface provides a way for an implementing | ||
* class in an external plugin to provide a way for external plugins to control | ||
* programmatically if a player should be excluded from the cap | ||
* | ||
* @see xyz.nkomarn.harbor.Harbor#addExclusionProvider(ExclusionProvider) | ||
*/ | ||
public interface ExclusionProvider { | ||
/** | ||
* Tests if a {@link Player} is excluded from the sleep checks for Harbor | ||
* | ||
* @param player The {@link Player} that is being checked | ||
* | ||
* @return If the player is excluded (true) or not (false) | ||
*/ | ||
boolean isExcluded(Player player); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package xyz.nkomarn.harbor.api; | ||
|
||
import org.bukkit.configuration.Configuration; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* An enum to represent the type of logic to be used when combining multiple Providers | ||
*/ | ||
public enum LogicType { | ||
AND, OR; | ||
|
||
public static LogicType fromConfig(@NotNull Configuration configuration, String path, LogicType defaultType) { | ||
return valueOf(configuration.getString(path, defaultType.toString()).toUpperCase().trim()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package xyz.nkomarn.harbor.listener; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.HandlerList; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.inventory.InventoryClickEvent; | ||
import org.bukkit.event.player.AsyncPlayerChatEvent; | ||
import org.bukkit.event.player.PlayerCommandPreprocessEvent; | ||
import org.bukkit.event.player.PlayerJoinEvent; | ||
import org.bukkit.event.player.PlayerQuitEvent; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
import org.bukkit.scheduler.BukkitRunnable; | ||
import xyz.nkomarn.harbor.Harbor; | ||
import xyz.nkomarn.harbor.provider.DefaultAFKProvider; | ||
|
||
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 Queue<AfkPlayer> players; | ||
private PlayerMovementChecker movementChecker; | ||
|
||
public AfkListener(DefaultAFKProvider afkProvider) { | ||
this.afkProvider = afkProvider; | ||
JavaPlugin.getPlugin(Harbor.class).getLogger().info("Initializing fallback AFK detection system. Fallback AFK system is not enabled at this time"); | ||
} | ||
|
||
/** | ||
* Provides a way to start the listener | ||
*/ | ||
public void start() { | ||
JavaPlugin plugin = JavaPlugin.getPlugin(Harbor.class); | ||
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. 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 🙃 ) |
||
} | ||
|
||
/** | ||
* Provides a way to halt the listener | ||
*/ | ||
public void stop() { | ||
movementChecker.cancel(); | ||
HandlerList.unregisterAll(this); | ||
players = null; | ||
JavaPlugin.getPlugin(Harbor.class).getLogger().info("Fallback AFK detection system is disabled"); | ||
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 |
||
} | ||
|
||
@EventHandler(ignoreCancelled = true) | ||
public void onChat(AsyncPlayerChatEvent event) { | ||
afkProvider.updateActivity(event.getPlayer()); | ||
} | ||
|
||
@EventHandler(ignoreCancelled = true) | ||
public void onCommand(PlayerCommandPreprocessEvent event) { | ||
afkProvider.updateActivity(event.getPlayer()); | ||
} | ||
|
||
@EventHandler(ignoreCancelled = true) | ||
public void onInventoryClick(InventoryClickEvent event) { | ||
afkProvider.updateActivity((Player) event.getWhoClicked()); | ||
} | ||
|
||
@EventHandler | ||
public void onJoin(PlayerJoinEvent event) { | ||
players.add(new AfkPlayer(event.getPlayer())); | ||
afkProvider.updateActivity(event.getPlayer()); | ||
} | ||
|
||
@EventHandler | ||
public void onLeave(PlayerQuitEvent event) { | ||
players.remove(new AfkPlayer(event.getPlayer())); | ||
afkProvider.removePlayer(event.getPlayer().getUniqueId()); | ||
} | ||
|
||
/** | ||
* 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() { | ||
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);
}
}
} |
||
} | ||
} | ||
|
||
|
||
|
||
private static final class AfkPlayer { | ||
private final Player player; | ||
private int hash; | ||
|
||
public AfkPlayer(Player player) { | ||
this.player = player; | ||
} | ||
|
||
/** | ||
* Check if the player changed its position since the last check | ||
* | ||
* @return true if the position changed | ||
*/ | ||
boolean changed() { | ||
int hash = player.getLocation().hashCode(); | ||
boolean changed = hash != this.hash; | ||
this.hash = hash; | ||
return changed; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
AfkPlayer afkPlayer = (AfkPlayer) o; | ||
return player.getUniqueId().equals(afkPlayer.player.getUniqueId()); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return player.getUniqueId().hashCode(); | ||
} | ||
} | ||
} |
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.