From b323b6318bf0a094c1b1a9c4dc4dc82d01375a56 Mon Sep 17 00:00:00 2001 From: Steven Lawson Date: Sun, 16 Sep 2012 17:18:30 -0400 Subject: [PATCH 1/3] Added TFM based tempban. Blocked vanilla admin commands. --- .../Commands/Command_tempban.java | 69 +++++++++++ .../Listener/TFM_PlayerListener.java | 25 ++++ .../TotalFreedomMod/TFM_Util.java | 107 ++++++++++++++++++ src/plugin.yml | 4 + 4 files changed, 205 insertions(+) create mode 100644 src/me/StevenLawson/TotalFreedomMod/Commands/Command_tempban.java diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tempban.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tempban.java new file mode 100644 index 0000000..0638416 --- /dev/null +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_tempban.java @@ -0,0 +1,69 @@ +package me.StevenLawson.TotalFreedomMod.Commands; + +import java.text.SimpleDateFormat; +import java.util.Date; +import me.StevenLawson.TotalFreedomMod.TFM_Util; +import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Command_tempban extends TFM_Command +{ + private static final SimpleDateFormat date_format = new SimpleDateFormat("yyyy-MM-dd \'at\' HH:mm:ss z"); + + @Override + public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) + { + if (args.length < 1) + { + return false; + } + + if (!(senderIsConsole || TFM_Util.isUserSuperadmin(sender))) + { + sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS); + return true; + } + + Player p; + try + { + p = getPlayer(args[0]); + } + catch (CantFindPlayerException ex) + { + sender.sendMessage(ex.getMessage()); + return true; + } + + StringBuilder bcast_msg = new StringBuilder("Temporarily banned " + p.getName()); + + Date ban_duration = TFM_Util.parseDateOffset("30m"); + if (args.length >= 2) + { + Date parsed_offset = TFM_Util.parseDateOffset(args[1]); + if (parsed_offset != null) + { + ban_duration = parsed_offset; + } + } + bcast_msg.append(" until ").append(date_format.format(ban_duration)); + + String ban_reason = "Banned by " + sender.getName(); + if (args.length >= 3) + { + ban_reason = StringUtils.join(ArrayUtils.subarray(args, 2, args.length), " ") + " (" + sender.getName() + ")"; + bcast_msg.append(", Reason: \"").append(ban_reason).append("\""); + } + + TFM_Util.adminAction(sender.getName(), bcast_msg.toString(), true); + TFM_Util.banUsername(p.getName(), ban_reason, sender.getName(), ban_duration); + TFM_Util.banIP(p.getAddress().getAddress().getHostAddress().trim(), ban_reason, sender.getName(), ban_duration); + p.kickPlayer(sender.getName() + " - " + bcast_msg.toString()); + + return true; + } +} diff --git a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java index e428ce0..787b719 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java +++ b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java @@ -444,6 +444,31 @@ public class TFM_PlayerListener implements Listener p.sendMessage(ChatColor.GRAY + "This server now uses DisguiseCraft instead of MobDisguise. Type /d to disguise and /u to undisguise."); block_command = true; } + else if (Pattern.compile("^/ban").matcher(command).find()) + { + if (!Pattern.compile("^/banlist").matcher(command).find()) + { + block_command = true; + } + } + else if (Pattern.compile("^/kick").matcher(command).find()) + { + if (!TFM_Util.isUserSuperadmin(p)) + { + block_command = true; + } + } + else if (Pattern.compile("^/kill").matcher(command).find()) + { + if (!TFM_Util.isUserSuperadmin(p)) + { + block_command = true; + } + } + else if (Pattern.compile("^/pardon").matcher(command).find()) + { + block_command = true; + } } if (block_command) diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java b/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java index cf250ca..002f498 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java @@ -5,6 +5,8 @@ import java.net.InetSocketAddress; import java.net.URI; import java.util.*; import java.util.jar.JarFile; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; @@ -822,6 +824,111 @@ public class TFM_Util ipBans.removeExpired(); return ipBans.getEntries().containsKey(ip); } + + public static Date parseDateOffset(String time) + { + Pattern timePattern = Pattern.compile( + "(?:([0-9]+)\\s*y[a-z]*[,\\s]*)?" + + "(?:([0-9]+)\\s*mo[a-z]*[,\\s]*)?" + + "(?:([0-9]+)\\s*w[a-z]*[,\\s]*)?" + + "(?:([0-9]+)\\s*d[a-z]*[,\\s]*)?" + + "(?:([0-9]+)\\s*h[a-z]*[,\\s]*)?" + + "(?:([0-9]+)\\s*m[a-z]*[,\\s]*)?" + + "(?:([0-9]+)\\s*(?:s[a-z]*)?)?", Pattern.CASE_INSENSITIVE); + Matcher m = timePattern.matcher(time); + int years = 0; + int months = 0; + int weeks = 0; + int days = 0; + int hours = 0; + int minutes = 0; + int seconds = 0; + boolean found = false; + while (m.find()) + { + if (m.group() == null || m.group().isEmpty()) + { + continue; + } + for (int i = 0; i < m.groupCount(); i++) + { + if (m.group(i) != null && !m.group(i).isEmpty()) + { + found = true; + break; + } + } + if (found) + { + if (m.group(1) != null && !m.group(1).isEmpty()) + { + years = Integer.parseInt(m.group(1)); + } + if (m.group(2) != null && !m.group(2).isEmpty()) + { + months = Integer.parseInt(m.group(2)); + } + if (m.group(3) != null && !m.group(3).isEmpty()) + { + weeks = Integer.parseInt(m.group(3)); + } + if (m.group(4) != null && !m.group(4).isEmpty()) + { + days = Integer.parseInt(m.group(4)); + } + if (m.group(5) != null && !m.group(5).isEmpty()) + { + hours = Integer.parseInt(m.group(5)); + } + if (m.group(6) != null && !m.group(6).isEmpty()) + { + minutes = Integer.parseInt(m.group(6)); + } + if (m.group(7) != null && !m.group(7).isEmpty()) + { + seconds = Integer.parseInt(m.group(7)); + } + break; + } + } + if (!found) + { + return null; + } + + Calendar c = new GregorianCalendar(); + + if (years > 0) + { + c.add(Calendar.YEAR, years); + } + if (months > 0) + { + c.add(Calendar.MONTH, months); + } + if (weeks > 0) + { + c.add(Calendar.WEEK_OF_YEAR, weeks); + } + if (days > 0) + { + c.add(Calendar.DAY_OF_MONTH, days); + } + if (hours > 0) + { + c.add(Calendar.HOUR_OF_DAY, hours); + } + if (minutes > 0) + { + c.add(Calendar.MINUTE, minutes); + } + if (seconds > 0) + { + c.add(Calendar.SECOND, seconds); + } + + return c.getTime(); + } // I wrote all this before i discovered getTargetBlock >.> - might come in handy some day... // public static final double LOOKAT_VIEW_HEIGHT = 1.65; // public static final double LOOKAT_STEP_DISTANCE = 0.2; diff --git a/src/plugin.yml b/src/plugin.yml index 1e7bf4b..4a5aa08 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -163,12 +163,16 @@ commands: stfu: description: Superadmin Command - Mutes a player with brute force. usage: / [list | purge | ] + aliases: [mute] stop: description: Superadmin command - Kicks everyone and stops the server. usage: / survival: description: Quickly change your own gamemode to survival, or define someone's username to change theirs. usage: / [partialname] + tempban: + description: Superadmin Command - Temporarily ban someone. + usage: / [playername] [duration] [reason] terminal: description: Execute a system command. usage: / From b2746f2299d66832febbd849b5d187520850077a Mon Sep 17 00:00:00 2001 From: Steven Lawson Date: Sun, 16 Sep 2012 17:25:34 -0400 Subject: [PATCH 2/3] Update autoEject --- .../TotalFreedomMod/TFM_Util.java | 34 +++++++------------ 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java b/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java index 002f498..d20e030 100644 --- a/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java +++ b/src/me/StevenLawson/TotalFreedomMod/TFM_Util.java @@ -624,36 +624,28 @@ public class TFM_Util { case STRIKE_ONE: { - //Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), String.format("tempban %s 1m", p.getName())); - - Calendar calendar = Calendar.getInstance(); - calendar.setTime(new Date()); - calendar.add(Calendar.MINUTE, 1); - Date expires = calendar.getTime(); - - TFM_Util.banIP(player_ip, null, null, expires); - TFM_Util.banUsername(p.getName(), null, null, expires); + Calendar c = new GregorianCalendar(); + c.add(Calendar.MINUTE, 1); + Date expires = c.getTime(); TFM_Util.bcastMsg(ChatColor.RED + p.getName() + " has been banned for 1 minute."); + TFM_Util.banIP(player_ip, kickMessage, "AutoEject", expires); + TFM_Util.banUsername(p.getName(), kickMessage, "AutoEject", expires); p.kickPlayer(kickMessage); break; } case STRIKE_TWO: { - //Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), String.format("tempban %s 3m", p.getName())); - - Calendar calendar = Calendar.getInstance(); - calendar.setTime(new Date()); - calendar.add(Calendar.MINUTE, 3); - Date expires = calendar.getTime(); - - TFM_Util.banIP(player_ip, null, null, expires); - TFM_Util.banUsername(p.getName(), null, null, expires); + Calendar c = new GregorianCalendar(); + c.add(Calendar.MINUTE, 3); + Date expires = c.getTime(); TFM_Util.bcastMsg(ChatColor.RED + p.getName() + " has been banned for 3 minutes."); + TFM_Util.banIP(player_ip, kickMessage, "AutoEject", expires); + TFM_Util.banUsername(p.getName(), kickMessage, "AutoEject", expires); p.kickPlayer(kickMessage); break; @@ -661,13 +653,13 @@ public class TFM_Util case STRIKE_THREE: { //Bukkit.banIP(player_ip); - TFM_Util.banIP(player_ip, null, null, null); + TFM_Util.banIP(player_ip, kickMessage, "AutoEject", null); String[] ip_address_parts = player_ip.split("\\."); //Bukkit.banIP(); - TFM_Util.banIP(ip_address_parts[0] + "." + ip_address_parts[1] + ".*.*", null, null, null); + TFM_Util.banIP(ip_address_parts[0] + "." + ip_address_parts[1] + ".*.*", kickMessage, "AutoEject", null); //p.setBanned(true); - TFM_Util.banUsername(p.getName(), null, null, null); + TFM_Util.banUsername(p.getName(), kickMessage, "AutoEject", null); TFM_Util.bcastMsg(ChatColor.RED + p.getName() + " has been banned permanently."); From d23c99f3c59333b3fab43dc1fea86d1cd53c67d8 Mon Sep 17 00:00:00 2001 From: Steven Lawson Date: Sun, 16 Sep 2012 17:33:26 -0400 Subject: [PATCH 3/3] Blocked the /gamemode command. Made it so /smite actually blocks non-superadmins :D --- .../TotalFreedomMod/Commands/Command_smite.java | 11 ++++++----- .../TotalFreedomMod/Listener/TFM_PlayerListener.java | 7 ++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_smite.java b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_smite.java index 6f81db7..a740edf 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Commands/Command_smite.java +++ b/src/me/StevenLawson/TotalFreedomMod/Commands/Command_smite.java @@ -16,16 +16,17 @@ public class Command_smite extends TFM_Command @Override public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) { - if (!TFM_Util.isUserSuperadmin(sender)) - { - sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS); - } - if (args.length != 1) { return false; } + if (!TFM_Util.isUserSuperadmin(sender)) + { + sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS); + return true; + } + Player p; try { diff --git a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java index 787b719..aa17137 100644 --- a/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java +++ b/src/me/StevenLawson/TotalFreedomMod/Listener/TFM_PlayerListener.java @@ -444,6 +444,11 @@ public class TFM_PlayerListener implements Listener p.sendMessage(ChatColor.GRAY + "This server now uses DisguiseCraft instead of MobDisguise. Type /d to disguise and /u to undisguise."); block_command = true; } + else if (Pattern.compile("^/gamemode").matcher(command).find()) + { + p.sendMessage(ChatColor.GRAY + "Use /creative and /survival to set your gamemode."); + block_command = true; + } else if (Pattern.compile("^/ban").matcher(command).find()) { if (!Pattern.compile("^/banlist").matcher(command).find()) @@ -473,7 +478,7 @@ public class TFM_PlayerListener implements Listener if (block_command) { - p.sendMessage(ChatColor.RED + "That command is prohibited."); + p.sendMessage(ChatColor.GRAY + "That command is blocked."); event.setCancelled(true); return; }