Skip to content

Instantly share code, notes, and snippets.

@heinrichquirit
Created January 4, 2015 13:38
Show Gist options
  • Save heinrichquirit/6fed852953bec656451d to your computer and use it in GitHub Desktop.
Save heinrichquirit/6fed852953bec656451d to your computer and use it in GitHub Desktop.
NoSpambots Bukkit plugin
package main.java.net.bigbadcraft.nospambots;
import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class NoSpambots extends JavaPlugin implements Listener {
private final String bypass_permission = "nospambots.bypass";
private HashMap<String, Location> locations = new HashMap<String, Location>();
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
}
@EventHandler
public void onJoin(PlayerJoinEvent e) {
Player player = e.getPlayer();
Location loc = player.getLocation();
if (!player.hasPermission(bypass_permission)) {
locations.put(player.getName(), loc);
}
}
@EventHandler
public void onMove(PlayerMoveEvent e) {
Player player = e.getPlayer();
if (locations.containsKey(player.getName())) {
Location loc = locations.get(player.getName());
if (player.getLocation().distance(loc) >= 1) {
locations.remove(player.getName());
player.sendMessage(ChatColor.GREEN + "You are now free to chat.");
}
}
}
@EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true)
public void onChat(AsyncPlayerChatEvent e) {
Player player = e.getPlayer();
if (locations.containsKey(player.getName())) {
e.setCancelled(true);
player.sendMessage(ChatColor.RED + "You must move a block to chat.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment