Skip to content

Commit

Permalink
Polished the Bans Component
Browse files Browse the repository at this point in the history
* Bans now better represent ban reasons
* Banned players are now informed of the expiration date of their ban
* /isbanned and /baninfo have been made the same command with differing permissions levels
* The ban command can now ban a player's IP as well as their name if the -i flag is used
* Output messages now better reflect the actions which took place
  • Loading branch information
DarkArc committed May 11, 2014
1 parent 0953160 commit e9aa903
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 74 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/sk89q/commandbook/bans/BanDatabase.java
Expand Up @@ -73,6 +73,7 @@ public interface BanDatabase extends Iterable<Ban> {
* @param ID The ID to check
* @return The banned message for the given ID
*/
@Deprecated
public String getBannedMessage(UUID ID);

/**
Expand All @@ -81,6 +82,7 @@ public interface BanDatabase extends Iterable<Ban> {
* @param address The address to check
* @return The banned message for the given address
*/
@Deprecated
public String getBannedMessage(String address);

/**
Expand Down
154 changes: 80 additions & 74 deletions src/main/java/com/sk89q/commandbook/bans/BansComponent.java
Expand Up @@ -36,8 +36,7 @@
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerLoginEvent;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.net.InetAddress;
import java.util.UUID;

@ComponentInformation(friendlyName = "Bans", desc = "A system for kicks and bans.")
Expand Down Expand Up @@ -92,12 +91,21 @@ public BanDatabase getBanDatabase() {
public void playerLogin(PlayerLoginEvent event) {
Player player = event.getPlayer();

Ban ban = null;

if (getBanDatabase().isBanned(player.getUniqueId())) {
event.disallow(PlayerLoginEvent.Result.KICK_BANNED,
getBanDatabase().getBannedMessage(player.getUniqueId()));
ban = getBanDatabase().getBanned(player.getUniqueId());
} else if (getBanDatabase().isBanned(event.getAddress())) {
event.disallow(PlayerLoginEvent.Result.KICK_BANNED,
getBanDatabase().getBannedMessage(event.getAddress().getHostAddress()));
ban = getBanDatabase().getBanned(event.getAddress().getHostAddress());
}

if (ban != null) {
String reason = ban.getReason();
boolean hasReason = reason != null;
String how = "You are " + (ban.getAddress() != null ? "IP " : "") + "banned" + (hasReason ? " for:" : ".");
String end = "Expires: " + (ban.getEnd() == 0L ? ChatColor.DARK_RED + "Never" : ChatUtil.getFriendlyTime(ban.getEnd()));

event.disallow(PlayerLoginEvent.Result.KICK_BANNED, how + (hasReason ? "\n" + reason : "") + "\n" + end);
}
}

Expand Down Expand Up @@ -144,15 +152,15 @@ public void kick(CommandContext args, CommandSender sender) throws CommandExcept
}

@Command(aliases = {"ban"}, usage = "[-t end ] <target> [reason...]",
desc = "Ban a user or IP address (with the -i flag)", flags = "set:o", min = 1, max = -1)
desc = "Ban a user (and their address with the -i flag)", flags = "iset:o", min = 1, max = -1)
@CommandPermissions({"commandbook.bans.ban"})
public void ban(CommandContext args, CommandSender sender) throws CommandException {
UUID banID;
String playerName = args.getString(0);
String banAddress = null;
InetAddress banAddress = null;
long endDate = args.hasFlag('t') ? InputUtil.TimeParser.matchFutureDate(args.getFlag('t')) : 0L;
String message = args.argsLength() >= 2 ? args.getJoinedStrings(1)
: "Banned!";
String message = args.argsLength() >= 2 ? args.getJoinedStrings(1) : null;
boolean kicked = false;

final boolean hasExemptOverride = args.hasFlag('o')
&& CommandBook.inst().hasPermission(sender, "commandbook.bans.exempt.override");
Expand All @@ -173,6 +181,10 @@ public void ban(CommandContext args, CommandSender sender) throws CommandExcepti
// Grab their UUID
if (player == null) {
banID = CommandBook.server().getOfflinePlayer(playerName).getUniqueId();

if (args.hasFlag('i')) {
throw new CommandException("This player must be online to ban their IP address as well.");
}
} else {
banID = player.getUniqueId();

Expand All @@ -181,66 +193,77 @@ public void ban(CommandContext args, CommandSender sender) throws CommandExcepti
"(use -o flag to override if you have commandbook.bans.exempt.override)");
}

kicked = true;

// Need to kick & log
player.kickPlayer(message);
getBanDatabase().logKick(player, sender, message);
if (args.hasFlag('i')) {
CommandBook.inst().checkPermission(sender, "commandbook.bans.ban.ip");
banAddress = player.getAddress().getAddress();
for (Player aPlayer : CommandBook.server().getOnlinePlayers()) {
if (aPlayer.getAddress().getAddress().equals(banAddress)) {
player.kickPlayer(message == null ? "Banned!" : message);
getBanDatabase().logKick(player, sender, message);
}
}
} else {
player.kickPlayer(message == null ? "Banned!" : message);
getBanDatabase().logKick(player, sender, message);
}
}

sender.sendMessage(ChatColor.YELLOW + playerName + " banned and kicked.");
sender.sendMessage(ChatColor.YELLOW + playerName + " banned" + (kicked ? "" : " and kicked") + '.');

//Broadcast the Message
// Broadcast the Message
if (config.broadcastBans && !args.hasFlag('s')) {
CommandBook.server().broadcastMessage(ChatColor.YELLOW
+ ChatUtil.toColoredName(sender, ChatColor.YELLOW) + " has banned " + playerName
+ " - " + message);
+ (message == null ? "" : " - " + message));
}

getBanDatabase().ban(banID, playerName, banAddress, sender, message, endDate);
getBanDatabase().ban(banID, playerName, banAddress != null ? banAddress.getHostAddress() : null, sender, message, endDate);

if (!getBanDatabase().save()) {
sender.sendMessage(ChatColor.RED + "Bans database failed to save. See console.");
}
}

@Command(aliases = {"banip", "ipban"},
usage = "<target> [reason...]", desc = "Ban an IP address", flags = "st:",
min = 1, max = -1)
@CommandPermissions({"commandbook.bans.ban.ip"})
public void banIP(CommandContext args,
CommandSender sender) throws CommandException {

String message = args.argsLength() >= 2 ? args.getJoinedStrings(1)
: "Banned!";
long endDate = args.hasFlag('t') ? InputUtil.TimeParser.matchFutureDate(args.getFlag('t')) : 0L;

String addr = args.getString(0)
.replace("\r", "")
.replace("\n", "")
.replace("\0", "")
.replace("\b", "");

// Need to kick + log
for (Player player : CommandBook.server().getOnlinePlayers()) {
if (player.getAddress().getAddress().getHostAddress().equals(addr)) {
player.kickPlayer(message);
getBanDatabase().logKick(player, sender, message);
}
@Command(aliases = {"banip", "ipban"},
usage = "<target> [reason...]", desc = "Ban an IP address", flags = "st:",
min = 1, max = -1)
@CommandPermissions({"commandbook.bans.ban.ip"})
public void banIP(CommandContext args, CommandSender sender) throws CommandException {

String message = args.argsLength() >= 2 ? args.getJoinedStrings(1)
: null;
long endDate = args.hasFlag('t') ? InputUtil.TimeParser.matchFutureDate(args.getFlag('t')) : 0L;

String addr = args.getString(0)
.replace("\r", "")
.replace("\n", "")
.replace("\0", "")
.replace("\b", "");

// Need to kick + log
for (Player player : CommandBook.server().getOnlinePlayers()) {
if (player.getAddress().getAddress().getHostAddress().equals(addr)) {
player.kickPlayer(message == null ? "Banned!" : message);
getBanDatabase().logKick(player, sender, message);
}
}

getBanDatabase().ban(null, null, addr, sender, message, endDate);
getBanDatabase().ban(null, null, addr, sender, message, endDate);

sender.sendMessage(ChatColor.YELLOW + addr + " banned.");
sender.sendMessage(ChatColor.YELLOW + addr + " banned.");

if (!getBanDatabase().save()) {
sender.sendMessage(ChatColor.RED + "Bans database failed to save. See console.");
}
if (!getBanDatabase().save()) {
sender.sendMessage(ChatColor.RED + "Bans database failed to save. See console.");
}
}

@Command(aliases = {"unban"}, usage = "<target>", desc = "Unban a user", min = 1, max = -1)
@CommandPermissions({"commandbook.bans.unban"})
public void unban(CommandContext args, CommandSender sender) throws CommandException {
String message = args.argsLength() >= 2 ? args.getJoinedStrings(1)
: "Unbanned!";
String message = args.argsLength() >= 2 ? args.getJoinedStrings(1) : "Unbanned!";

String banName = args.getString(0)
.replace("\r", "")
Expand Down Expand Up @@ -287,29 +310,8 @@ public void unbanIP(CommandContext args,
}
}

@Command(aliases = {"isbanned"}, usage = "<target>", desc = "Check if a user is banned", min = 1, max = 1)
@CommandPermissions({"commandbook.bans.isbanned"})
public void isBanned(CommandContext args, CommandSender sender) throws CommandException {
String banName = args.getString(0)
.replace("\r", "")
.replace("\n", "")
.replace("\0", "")
.replace("\b", "");

UUID ID = CommandBook.server().getOfflinePlayer(banName).getUniqueId();

if (getBanDatabase().isBanned(ID)) {
sender.sendMessage(ChatColor.YELLOW + banName + " is banned.");
} else {
sender.sendMessage(ChatColor.YELLOW + banName + " NOT banned.");
}
}

private final SimpleDateFormat dateFormat =
new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");

@Command(aliases = {"baninfo"}, usage = "<target>", desc = "Check if a user is banned", min = 1, max = 1)
@CommandPermissions({"commandbook.bans.baninfo"})
@Command(aliases = {"baninfo", "isbanned"}, usage = "<target>", desc = "Check if a user is banned", min = 1, max = 1)
@CommandPermissions({"commandbook.bans.isbanned", "commandbook.bans.baninfo"})
public void banInfo(CommandContext args, CommandSender sender) throws CommandException {
String banName = args.getString(0)
.replace("\r", "")
Expand All @@ -322,12 +324,16 @@ public void banInfo(CommandContext args, CommandSender sender) throws CommandEx
Ban ban = getBanDatabase().getBanned(ID);

if (ban == null) {
sender.sendMessage(ChatColor.YELLOW + banName + " is NOT banned.");
sender.sendMessage(ChatColor.YELLOW + banName + " is " + ChatColor.RED + "not " + ChatColor.YELLOW + "banned.");
} else {
sender.sendMessage(ChatColor.YELLOW + "Ban for " + banName + ":" + ban.getAddress()
+ " for reason: '" + ban.getReason() + "' until " +
(ban.getEnd() == 0L ? " forever" : dateFormat.format(new Date(ban.getEnd()))));

String add = ban.getAddress() == null ? "" : " (Address: " + ban.getAddress() + ")";
sender.sendMessage(ChatColor.YELLOW + banName + add + ChatColor.RED + " is" + ChatColor.YELLOW + " banned.");
if (!CommandBook.inst().hasPermission(sender, "commandbook.bans.baninfo")) return;
sender.sendMessage(ChatColor.YELLOW + "Duration: " + (ban.getEnd() == 0L ? "Indefinite"
: "Until " + ChatUtil.getFriendlyTime(ban.getEnd())));
if (ban.getReason() != null) {
sender.sendMessage(ChatColor.YELLOW + "Reason: " + ban.getReason());
}
}
}

Expand Down
Expand Up @@ -110,6 +110,7 @@ public synchronized boolean load() {
long endDate = Long.parseLong(line[i++]);
if ("".equals(name) || "null".equals(name)) name = null;
if ("".equals(address) || "null".equals(address)) address = null;
if ("".equals(reason) || "null".equals(reason)) reason = null;
Ban ban = new Ban(ID, name, address, reason, startDate, endDate);
if (ID != null) {
UUIDBan.put(ID, ban);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/sk89q/commandbook/util/ChatUtil.java
Expand Up @@ -11,6 +11,8 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -198,4 +200,22 @@ public static String getTimeString(long time) {
hours, minutes, (hours % 12) == 0 ? 12 : hours % 12, minutes,
hours < 12 ? "am" : "pm");
}

public static String getFriendlyTime(long time) {
StringBuilder builder = new StringBuilder();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);

builder.append(calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.US));
builder.append(" ");
builder.append(calendar.get(Calendar.DAY_OF_MONTH));
builder.append(" ");
builder.append(calendar.get(Calendar.YEAR));
builder.append(" at ");
builder.append(calendar.get(Calendar.HOUR));
builder.append(":");
builder.append(String.format("%02d", calendar.get(Calendar.MINUTE)));
builder.append(calendar.get(Calendar.AM_PM) == 0 ? "AM" : "PM");
return builder.toString();
}
}

0 comments on commit e9aa903

Please sign in to comment.