From fd771a6ff6307ed88f80e4a98497a4af786dbbdf Mon Sep 17 00:00:00 2001 From: j502647092 Date: Sun, 26 Jul 2015 17:27:25 +0800 Subject: [PATCH] add Spam Check... --- .../SimpleProtect/SimpleProtect.java | 9 +- .../citycraft/SimpleProtect/listen/Spam.java | 195 ++++++++++++++++++ src/config.yml | 13 +- 3 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 src/cn/citycraft/SimpleProtect/listen/Spam.java diff --git a/src/cn/citycraft/SimpleProtect/SimpleProtect.java b/src/cn/citycraft/SimpleProtect/SimpleProtect.java index ebddf11..9f00f1d 100644 --- a/src/cn/citycraft/SimpleProtect/SimpleProtect.java +++ b/src/cn/citycraft/SimpleProtect/SimpleProtect.java @@ -10,6 +10,7 @@ import cn.citycraft.SimpleProtect.listen.Explosion; import cn.citycraft.SimpleProtect.listen.HighRedstone; import cn.citycraft.SimpleProtect.listen.NetherDoor; import cn.citycraft.SimpleProtect.listen.Nightvision; +import cn.citycraft.SimpleProtect.listen.Spam; import cn.citycraft.SimpleProtect.listen.Tip; public class SimpleProtect extends JavaPlugin { @@ -58,7 +59,13 @@ public class SimpleProtect extends JavaPlugin { Bukkit.getScheduler().runTaskTimer(this, redstone, 20, 20); getLogger().info("防止玩家高频红石已加载!"); } - + if (Config.getInstance().getBoolean("Spam.Enable", true)) { + Spam spam = new Spam(this); + Bukkit.getPluginManager().registerEvents(spam, this); + Bukkit.getScheduler().runTaskTimer(this, spam, 20, 20); + getCommand("spam").setExecutor(spam); + getLogger().info("防止玩家聊天刷屏已加载!"); + } getLogger().info("==========" + servername + pluginname + "=========="); } diff --git a/src/cn/citycraft/SimpleProtect/listen/Spam.java b/src/cn/citycraft/SimpleProtect/listen/Spam.java new file mode 100644 index 0000000..b59c483 --- /dev/null +++ b/src/cn/citycraft/SimpleProtect/listen/Spam.java @@ -0,0 +1,195 @@ +/* + * To change this template, choose Tools | Templates and open the template in the editor. + */ +package cn.citycraft.SimpleProtect.listen; + +import java.util.HashMap; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.AsyncPlayerChatEvent; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; + +import cn.citycraft.SimpleProtect.SimpleProtect; + +/** + * + * @author 蒋天蓓 + */ +public class Spam implements Runnable, Listener, CommandExecutor { + + private final HashMap lT; + private final HashMap lC; + HashMap tc = new HashMap(); + HashMap cc = new HashMap(); + String AntiMsg; + boolean adminchat; + SimpleProtect plugin; + + public Spam(SimpleProtect main) { + plugin = main; + this.lT = new HashMap(); + this.lC = new HashMap(); + } + + public boolean AntiChat(String n, String s) { + String lm = lC.get(n); + Long lt = lT.get(n); + long nt = System.currentTimeMillis() / 1000; + if (lm == null || lt == null) { + AntiMsg = ""; + SaveMsg(n, s); + return false; + } + if (lt + plugin.getConfig().getLong("Spam.ChatWait", 1) > nt) { + AntiMsg = "请不要短时间内发送大量消息!"; + return true; + } + if (lm.equals(s)) { + if (lt + plugin.getConfig().getLong("Spam.SameChatWait", 5) > nt) { + AntiMsg = "请不要短时间内发送重复的消息!"; + return true; + } + } + AntiMsg = ""; + SaveMsg(n, s); + return false; + } + + public boolean CommandWait(String n, String s) { + String lm = lC.get(n); + Long lt = lT.get(n); + long nt = System.currentTimeMillis() / 1000; + if (lm == null || lt == null) { + AntiMsg = ""; + SaveMsg(n, s); + return false; + } + if (lt + plugin.getConfig().getLong("Spam.CommandWait", 1) > nt) { + AntiMsg = "请不要短时间内发送大量命令"; + return true; + } + if (lm.equals(s)) { + if (lt + plugin.getConfig().getLong("Spam.SameCommandWait", 3) > nt) { + AntiMsg = "请不要短时间内发送相同的命令"; + return true; + } + } + SaveMsg(n, s); + return false; + } + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (command.getName().equalsIgnoreCase("spam")) { + if (!sender.hasPermission("spam.admin")) { + sender.sendMessage(plugin.servername + " " + plugin.getmessage("no-permission")); + return true; + } + if (args.length == 2) { + Player p = Bukkit.getPlayer(args[1]); + if (args[0].equalsIgnoreCase("add")) { + if (p != null) { + cc.put(p, p); + sender.sendMessage(plugin.servername + " §c已禁止玩家 " + p.getDisplayName() + + " §c聊天!"); + } else { + sender.sendMessage(plugin.servername + " §c玩家不存在或不在线!"); + } + } + if (args[0].equalsIgnoreCase("del")) { + if (p != null && cc.containsKey(p)) { + cc.remove(p); + sender.sendMessage(plugin.servername + " §a已允许玩家 " + p.getDisplayName() + + " §a聊天!"); + } else { + sender.sendMessage(plugin.servername + " §c玩家未被禁言或不存在!"); + } + } + if (args[0].equalsIgnoreCase("admin")) { + if (args[1].equalsIgnoreCase("on")) { + adminchat = true; + sender.sendMessage(plugin.servername + " §a服务器已开启管理员聊天!"); + return true; + } + if (args[1].equalsIgnoreCase("off")) { + adminchat = false; + sender.sendMessage(plugin.servername + " §c服务器已关闭管理员聊天!"); + return true; + } + } + return true; + } + return false; + } + return false; + } + + @EventHandler(ignoreCancelled = true) + public void PlayerChat(AsyncPlayerChatEvent e) { + Player p = e.getPlayer(); + String n = p.getName(); + String s = e.getMessage(); + if (p.hasPermission("fcp.ignore.spam")) + return; + if (adminchat) { + p.sendMessage(plugin.servername + " §c服务器已开启管理员聊天!"); + e.setCancelled(true); + return; + } + if (cc.containsKey(p)) { + p.sendMessage(plugin.servername + " §c您已被禁言,请联系管理员解禁!"); + e.setCancelled(true); + return; + } + if (AntiChat(n, s)) { + p.sendMessage(plugin.servername + "§6[§4刷屏检测§6] §c" + AntiMsg); + e.setCancelled(true); + put(p); + } + } + + @EventHandler(ignoreCancelled = true) + public void PlayerCommand(PlayerCommandPreprocessEvent e) { + Player p = e.getPlayer(); + String n = p.getName(); + String s = e.getMessage(); + if (p.hasPermission("fcp.ignore.spam")) + return; + if (CommandWait(n, s)) { + put(p); + p.sendMessage(plugin.servername + "§6[§4命令检测§6] §c" + AntiMsg); + e.setCancelled(true); + } + } + + public void put(Player p) { + if (tc.containsKey(p)) { + int t = tc.get(p); + if (t > plugin.getConfig().getLong("Spam.KickCheck", 3)) { + p.sendMessage(plugin.servername + " §c您由于多次刷屏已被禁言,请联系管理解禁!"); + cc.put(p, p); + } else { + tc.put(p, t + 1); + } + } else { + tc.put(p, 1); + } + } + + @Override + public void run() { + tc.clear(); + } + + public void SaveMsg(String n, String s) { + lC.put(n, s); + lT.put(n, System.currentTimeMillis() / 1000); + } + +} diff --git a/src/config.yml b/src/config.yml index b0faf48..081c139 100644 --- a/src/config.yml +++ b/src/config.yml @@ -48,4 +48,15 @@ SafeNetherDoor: World: world X: -51 Y: 73 - Z: 38 \ No newline at end of file + Z: 38 + + #刷屏保护 +Spam: + #是否开启 + Enable: true + ChatWait: 3 + SameChatWait: 8 + CommandWait: 2 + SameCommandWait: 3 + KickCheck: 3 + KickReset: 10