Skip to content

Instantly share code, notes, and snippets.

@dumptruckman
Last active May 1, 2020 05:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dumptruckman/6fb06c89ae5f40fe2fe6175b316ab7ed to your computer and use it in GitHub Desktop.
Save dumptruckman/6fb06c89ae5f40fe2fe6175b316ab7ed to your computer and use it in GitHub Desktop.
Punishes players who stand in liquid for too long.
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.HashMap;
import java.util.Map;
public class AFKPoolListener implements Listener {
// How often to check each player
private static final long AFK_POOL_TASK_TICKS = 20L;
// How long until punish in milliseconds
private static final long TIME_TO_PUNISH = 300000L;
// Do what you want to the player here.
private static void punish(Player player) {
}
// You can add any other liquids here...
private static boolean isLiquid(Material material) {
return material == Material.WATER
|| material == Material.LAVA
|| material == Material.STATIONARY_WATER
|| material == Material.STATIONARY_LAVA;
}
// Call this in plugin onEnable to register the listener
public static void enableAFKPoolListener(Plugin plugin) {
AFKPoolListener listener = new AFKPoolListener(plugin);
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
}
private final Plugin plugin;
private final Map<Player, AFKPoolTask> playerTasks = new HashMap<>();
public AFKPoolListener(Plugin plugin) {
this.plugin = plugin;
}
@EventHandler
public void onJoin(PlayerJoinEvent event) {
AFKPoolTask task = new AFKPoolTask(event.getPlayer());
playerTasks.put(event.getPlayer(), task);
task.runTaskTimer(plugin, AFK_POOL_TASK_TICKS, AFK_POOL_TASK_TICKS);
}
@EventHandler
public void onQuit(PlayerQuitEvent event) {
AFKPoolTask task = playerTasks.get(event.getPlayer());
task.cancel();
playerTasks.remove(event.getPlayer());
}
public static class AFKPoolTask extends BukkitRunnable {
private final Player player;
private Location lastLocation;
private Long timeStartedInLocation;
public AFKPoolTask(Player player) {
this.player = player;
}
@Override
public void run() {
if (isInLocationTooLong()) {
punish(player);
timeStartedInLocation = null;
} else {
updatePlayerStatus();
}
}
private boolean isInLocationTooLong() {
if (timeStartedInLocation != null) {
long now = System.currentTimeMillis();
if (now - timeStartedInLocation >= TIME_TO_PUNISH) {
return true;
}
}
return false;
}
private void updatePlayerStatus() {
Location loc = player.getLocation();
if (loc.equals(lastLocation)) {
if (timeStartedInLocation == null && isLiquid(loc.getBlock().getType())) {
timeStartedInLocation = System.currentTimeMillis();
}
} else {
timeStartedInLocation = null;
}
lastLocation = loc;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment