Skip to content

Instantly share code, notes, and snippets.

@lucko
Created June 8, 2017 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucko/68c476c1a7b991fe79960ee96d8a57d6 to your computer and use it in GitHub Desktop.
Save lucko/68c476c1a7b991fe79960ee96d8a57d6 to your computer and use it in GitHub Desktop.
MetadataKey<AtomicInteger> AFK_TIME_KEY = MetadataKey.create("afk", AtomicInteger.class);
MetadataKey<Direction> AFK_DIRECTION_KEY = MetadataKey.create("afk-direction", Direction.class);
Scheduler.runTaskRepeatingSync(() -> {
for (Player player : Bukkit.getOnlinePlayers()) {
MetadataMap metadata = Metadata.provideForPlayer(player);
Direction prev = metadata.getOrNull(AFK_DIRECTION_KEY);
Direction now = Direction.from(player.getLocation());
metadata.put(AFK_DIRECTION_KEY, now);
// no previous yaw/pitch data for the player
if (prev == null) {
continue;
}
AtomicInteger counter = metadata.getOrPut(AFK_TIME_KEY, AtomicInteger::new);
// check if they have moved since last time
if (!prev.equals(now)) {
counter.set(0);
continue;
}
if (counter.incrementAndGet() >= 300) {
player.kickPlayer("You have been idle for 5 minutes.");
}
}
}, 20L, 20L);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment