From b323b6318bf0a094c1b1a9c4dc4dc82d01375a56 Mon Sep 17 00:00:00 2001 From: Steven Lawson Date: Sun, 16 Sep 2012 17:18:30 -0400 Subject: [PATCH] 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 00000000..0638416b --- /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 e428ce0b..787b7192 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 cf250ca5..002f498a 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 1e7bf4b6..4a5aa088 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: /