Created
June 8, 2017 18:13
-
-
Save lucko/68c476c1a7b991fe79960ee96d8a57d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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