From ea3eb82c8f9769f96eb8d5f38cf602745edf247d Mon Sep 17 00:00:00 2001 From: Steven Lawson Date: Sat, 1 Oct 2011 13:59:46 -0400 Subject: [PATCH] Forgot this. --- .../TotalFreedomMod/TFUserInfo.java | 47 ++++++ .../TotalFreedomMod/TotalFreedomMod.java | 140 +++++++++++++++--- .../TotalFreedomModBlockListener.java | 58 ++++++-- .../TotalFreedomModPlayerListener.java | 75 ++++++++-- src/plugin.yml | 2 +- 5 files changed, 276 insertions(+), 46 deletions(-) create mode 100644 src/me/StevenLawson/TotalFreedomMod/TFUserInfo.java diff --git a/src/me/StevenLawson/TotalFreedomMod/TFUserInfo.java b/src/me/StevenLawson/TotalFreedomMod/TFUserInfo.java new file mode 100644 index 00000000..1913a3ba --- /dev/null +++ b/src/me/StevenLawson/TotalFreedomMod/TFUserInfo.java @@ -0,0 +1,47 @@ +package me.StevenLawson.TotalFreedomMod; + +public class TFUserInfo +{ + private boolean user_frozen = false; + private int msg_count = 0; + private int bd_count = 0; + + public TFUserInfo() + { + } + + public boolean isFrozen() + { + return this.user_frozen; + } + public void setFrozen(boolean fr) + { + this.user_frozen = fr; + } + + public void resetMsgCount() + { + this.msg_count = 0; + } + public void incrementMsgCount() + { + this.msg_count++; + } + public int getMsgCount() + { + return this.msg_count; + } + + public void incrementBlockDestroyCount() + { + this.bd_count++; + } + public int getBlockDestroyCount() + { + return this.bd_count; + } + public void resetBlockDestroyCount() + { + this.bd_count = 0; + } +} diff --git a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java index 16ff12f9..65110f57 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java +++ b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomMod.java @@ -5,6 +5,7 @@ import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -22,21 +23,29 @@ import org.bukkit.util.config.Configuration; public class TotalFreedomMod extends JavaPlugin { + public TotalFreedomMod tfm = this; + private final TotalFreedomModEntityListener entityListener = new TotalFreedomModEntityListener(this); private final TotalFreedomModBlockListener blockListener = new TotalFreedomModBlockListener(this); private final TotalFreedomModPlayerListener playerListener = new TotalFreedomModPlayerListener(this); + private static final Logger log = Logger.getLogger("Minecraft"); + protected static Configuration CONFIG; - private List superadmins = new ArrayList(); - private List superadmin_ips = new ArrayList(); + public List superadmins = new ArrayList(); + public List superadmin_ips = new ArrayList(); public Boolean allowExplosions = false; public Boolean allowLavaDamage = false; public Boolean allowFire = false; public double explosiveRadius = 4.0; public Boolean preprocessLogEnabled = false; - public boolean nukeMonitor = false; + public boolean nukeMonitor = true; public double nukeMonitorRange = 10.0; - public boolean playersFrozen = false; + public int nukeMonitorCount = 40; + public boolean allPlayersFrozen = false; + + public HashMap userinfo = new HashMap(); + public final static String MSG_NO_PERMS = ChatColor.YELLOW + "You do not have permission to use this command."; public final static String YOU_ARE_OP = ChatColor.YELLOW + "You are now op!"; public final static String YOU_ARE_NOT_OP = ChatColor.YELLOW + "You are no longer op!"; @@ -62,6 +71,9 @@ public class TotalFreedomMod extends JavaPlugin CONFIG.setProperty("allow_fire", false); CONFIG.setProperty("explosiveRadius", 4.0); CONFIG.setProperty("preprocess_log", false); + CONFIG.setProperty("nuke_monitor", true); + CONFIG.setProperty("nuke_monitor_range", 10.0); + CONFIG.setProperty("nuke_monitor_count", 40); CONFIG.save(); } CONFIG.load(); @@ -72,6 +84,8 @@ public class TotalFreedomMod extends JavaPlugin allowFire = CONFIG.getBoolean("allow_fire", false); explosiveRadius = CONFIG.getDouble("explosiveRadius", 4.0); preprocessLogEnabled = CONFIG.getBoolean("preprocess_log", false); + nukeMonitorRange = CONFIG.getDouble("nuke_monitor_range", 10.0); + nukeMonitorCount = CONFIG.getInt("nuke_monitor_count", 40); PluginManager pm = this.getServer().getPluginManager(); @@ -88,9 +102,19 @@ public class TotalFreedomMod extends JavaPlugin pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Event.Priority.High, this); pm.registerEvent(Event.Type.PLAYER_INTERACT, playerListener, Event.Priority.High, this); pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Event.Priority.Normal, this); + pm.registerEvent(Event.Type.PLAYER_CHAT, playerListener, Event.Priority.Normal, this); log.log(Level.INFO, "[Total Freedom Mod] - Enabled! - Version: " + this.getDescription().getVersion() + " by Madgeek1450"); log.log(Level.INFO, "[Total Freedom Mod] - Loaded superadmins: " + implodeStringList(", ", superadmins)); + + Bukkit.getServer().getScheduler().scheduleAsyncRepeatingTask(this, new Runnable() + { + @Override + public void run() + { + tfm.resetCounts(); + } + }, 100L, 100L); } @Override @@ -773,7 +797,7 @@ public class TotalFreedomMod extends JavaPlugin { sender.sendMessage(ChatColor.GOLD + "[Real Name]:[Display Name] - Hash:"); } - + for (Player p : Bukkit.getOnlinePlayers()) { String hash = p.getUniqueId().toString().substring(0, 4); @@ -827,11 +851,27 @@ public class TotalFreedomMod extends JavaPlugin { p.getInventory().clear(); } - + else if (mode.equals("fr")) + { + TFUserInfo playerdata = (TFUserInfo) this.userinfo.get(p); + if (playerdata != null) + { + playerdata.setFrozen(!playerdata.isFrozen()); + } + else + { + playerdata = new TFUserInfo(); + playerdata.setFrozen(true); + this.userinfo.put(p, playerdata); + } + sender.sendMessage(ChatColor.AQUA + p.getName() + " has been " + (playerdata.isFrozen() ? "frozen" : "unfrozen") + "."); + p.sendMessage(ChatColor.AQUA + "You have been " + (playerdata.isFrozen() ? "frozen" : "unfrozen") + "."); + } + return true; } } - + if (!mode.equals("list")) { sender.sendMessage(ChatColor.RED + "Invalid hash."); @@ -854,19 +894,51 @@ public class TotalFreedomMod extends JavaPlugin { if (player == null || isUserSuperadmin(sender)) { - this.playersFrozen = !this.playersFrozen; - - if (this.playersFrozen) + if (args.length == 0) { - this.playersFrozen = true; - sender.sendMessage("Players are now frozen."); - tfBroadcastMessage(sender.getName() + " has temporarily frozen everyone on the server.", ChatColor.AQUA); + this.allPlayersFrozen = !this.allPlayersFrozen; + + if (this.allPlayersFrozen) + { + this.allPlayersFrozen = true; + sender.sendMessage("Players are now frozen."); + tfBroadcastMessage(sender.getName() + " has temporarily frozen everyone on the server.", ChatColor.AQUA); + } + else + { + this.allPlayersFrozen = false; + sender.sendMessage("Players are now free to move."); + tfBroadcastMessage(sender.getName() + " has unfrozen everyone.", ChatColor.AQUA); + } } else { - this.playersFrozen = false; - sender.sendMessage("Players are now free to move."); - tfBroadcastMessage(sender.getName() + " has unfrozen everyone.", ChatColor.AQUA); + Player p; + List matches = Bukkit.matchPlayer(args[0]); + if (matches.isEmpty()) + { + sender.sendMessage("Can't find user " + args[0]); + return true; + } + else + { + p = matches.get(0); + } + + TFUserInfo playerdata = (TFUserInfo) this.userinfo.get(p); + if (playerdata != null) + { + playerdata.setFrozen(!playerdata.isFrozen()); + } + else + { + playerdata = new TFUserInfo(); + playerdata.setFrozen(true); + this.userinfo.put(p, playerdata); + } + + sender.sendMessage(ChatColor.AQUA + p.getName() + " has been " + (playerdata.isFrozen() ? "frozen" : "unfrozen") + "."); + p.sendMessage(ChatColor.AQUA + "You have been " + (playerdata.isFrozen() ? "frozen" : "unfrozen") + "."); } } else @@ -882,23 +954,36 @@ public class TotalFreedomMod extends JavaPlugin { return false; } - - if (args.length == 2) + + if (args.length >= 2) { - this.nukeMonitorRange = Integer.parseInt(args[1]); + this.nukeMonitorRange = Double.parseDouble(args[1]); + } + + if (args.length >= 3) + { + this.nukeMonitorCount = Integer.parseInt(args[2]); } if (args[0].equalsIgnoreCase("on")) { this.nukeMonitor = true; - sender.sendMessage("Nuke monitor is enabled, range is set to " + this.nukeMonitorRange + " blocks."); + sender.sendMessage(ChatColor.GRAY + "Nuke monitor is enabled."); + sender.sendMessage(ChatColor.GRAY + "Anti-freecam range is set to " + this.nukeMonitorRange + " blocks."); + sender.sendMessage(ChatColor.GRAY + "Block throttle rate is set to " + this.nukeMonitorCount + " blocks destroyed per 5 seconds."); } else { this.nukeMonitor = false; sender.sendMessage("Nuke monitor is disabled."); } - + + CONFIG.load(); + CONFIG.setProperty("nuke_monitor", this.nukeMonitor); + CONFIG.setProperty("nuke_monitor_range", this.nukeMonitorRange); + CONFIG.setProperty("nuke_monitor_count", this.nukeMonitorCount); + CONFIG.save(); + return true; } } @@ -984,4 +1069,17 @@ public class TotalFreedomMod extends JavaPlugin return false; } + + private void resetCounts() + { + for (Player p : Bukkit.getOnlinePlayers()) + { + TFUserInfo playerdata = (TFUserInfo) this.userinfo.get(p); + if (playerdata != null) + { + playerdata.resetMsgCount(); + playerdata.resetBlockDestroyCount(); + } + } + } } diff --git a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomModBlockListener.java b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomModBlockListener.java index 10d11068..93355d16 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomModBlockListener.java +++ b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomModBlockListener.java @@ -46,24 +46,56 @@ public class TotalFreedomModBlockListener extends BlockListener @Override public void onBlockBreak(BlockBreakEvent event) { - if (plugin.nukeMonitor) + try { - Player p = event.getPlayer(); - - Location player_pos = p.getLocation(); - Location block_pos = event.getBlock().getLocation(); - - if (player_pos.distance(block_pos) > plugin.nukeMonitorRange) + if (plugin.nukeMonitor) { - p.setOp(false); - p.setGameMode(GameMode.SURVIVAL); - p.getInventory().clear(); - - plugin.tfBroadcastMessage(p.getName() + " has been flagged for possible freecam nuking.", ChatColor.RED); + Player p = event.getPlayer(); - event.setCancelled(true); + Location player_pos = p.getLocation(); + Location block_pos = event.getBlock().getLocation(); + + if (player_pos.distance(block_pos) > plugin.nukeMonitorRange) + { + p.setOp(false); + p.setGameMode(GameMode.SURVIVAL); + p.getInventory().clear(); + + plugin.tfBroadcastMessage(p.getName() + " has been flagged for possible freecam nuking.", ChatColor.RED); + + event.setCancelled(true); + return; + } + + TFUserInfo playerdata = (TFUserInfo) plugin.userinfo.get(p); + if (playerdata != null) + { + playerdata.incrementBlockDestroyCount(); + + if (playerdata.getBlockDestroyCount() > plugin.nukeMonitorCount) + { + plugin.tfBroadcastMessage(p.getName() + " is breaking blocks too fast!", ChatColor.RED); + + p.setOp(false); + p.setGameMode(GameMode.SURVIVAL); + p.getInventory().clear(); + + event.setCancelled(true); + return; + } + } + else + { + playerdata = new TFUserInfo(); + playerdata.incrementBlockDestroyCount(); + plugin.userinfo.put(p, playerdata); + } } } + catch (Exception ex) + { + log.info("Exception in TFM Block Listener onBlockBreak: " + ex.getMessage()); + } } @Override diff --git a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomModPlayerListener.java b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomModPlayerListener.java index 7763455c..e66a11f0 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TotalFreedomModPlayerListener.java +++ b/src/me/StevenLawson/TotalFreedomMod/TotalFreedomModPlayerListener.java @@ -6,6 +6,7 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerChatEvent; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerListener; @@ -59,21 +60,73 @@ class TotalFreedomModPlayerListener extends PlayerListener @Override public void onPlayerMove(PlayerMoveEvent event) { - if (plugin.playersFrozen) + try { - if (plugin.isUserSuperadmin(event.getPlayer())) + Player p = event.getPlayer(); + + boolean do_freeze = false; + if (plugin.allPlayersFrozen) { + if (!plugin.isUserSuperadmin(p)) + { + do_freeze = true; + } + } + else + { + TFUserInfo playerdata = (TFUserInfo) plugin.userinfo.get(p); + if (playerdata != null) + { + if (playerdata.isFrozen()) + { + do_freeze = true; + } + } + } + + if (do_freeze) + { + Location from = event.getFrom(); + Location to = event.getTo().clone(); + + to.setX(from.getX()); + to.setY(from.getY()); + to.setZ(from.getZ()); + + event.setTo(to); + } + } + catch (Exception ex) + { + log.severe("Exception in TFM Player Listener onMove: " + ex.getMessage()); + } + } + + @Override + public void onPlayerChat(PlayerChatEvent event) + { + Player p = event.getPlayer(); + + TFUserInfo playerdata = (TFUserInfo) plugin.userinfo.get(p); + if (playerdata != null) + { + playerdata.incrementMsgCount(); + + if (playerdata.getMsgCount() > 10) + { + p.setOp(false); + p.kickPlayer("No Spamming"); + plugin.tfBroadcastMessage(p.getName() + " was automatically kicked for spamming chat.", ChatColor.RED); + + event.setCancelled(true); return; } - - Location from = event.getFrom(); - Location to = event.getTo().clone(); - - to.setX(from.getX()); - to.setY(from.getY()); - to.setZ(from.getZ()); - - event.setTo(to); + } + else + { + playerdata = new TFUserInfo(); + playerdata.incrementMsgCount(); + plugin.userinfo.put(p, playerdata); } } diff --git a/src/plugin.yml b/src/plugin.yml index 3ba71e2e..76754d01 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -39,7 +39,7 @@ commands: usage: / nonuke: description: Attempt to detect "invisible griefers" and "nukers". - usage: / [range] + usage: / [range] [blockrate] opall: description: Superadmin command - Op everyone on the server, optionally change everyone's gamemode at the same time. usage: / [-c|-s]