* @param alias
* @return
*/
public static Boolean ban(CommandSender sender, String[] args, String command, String alias) {
// check if player is online
Player p = Bukkit.getServer().getPlayer(args[0]);
OfflinePlayer pl = null;
String pName;
Boolean isOffline = false;
if (p == null) {
// player is offline, get his saved record
isOffline = true;
pl = Bukkit.getOfflinePlayer(args[0]);
if (pl == null) {
LogHelper.showWarning("invalidPlayer", sender);
return true;
} else {
pName = pl.getName();
}
} else {
pName = p.getName();
}
String pDispName;
try {
pDispName = Nicknames.getNick(pName);
} catch (Exception ex){
pDispName = pName;
}
// check if we have expiration date present
Map<String, Integer> t;
try {
t = Utils.parseTime(args);
} catch (Throwable e) {
t = new HashMap<String, Integer>();
t.put("not_found", 1);
}
// make timestamp from parsed time, so it can be used in database operations
Timestamp stamp = new Timestamp(new java.util.Date().getTime());
if (!t.containsKey("not_found")) {
// add up required number of milliseconds to current date
stamp = new Timestamp((new java.util.Date().getTime()) + (t.get("seconds") * 1000) + (t.get("minutes") * 60 * 1000) + (t.get("hours") * 3600 * 1000) + (t.get("days") * 86400 * 1000));
}
// assemble reason, if one was provided
String reason = "";
if (args.length > 1) {
for (Integer i = (args[1].startsWith("t:") ? 2 : 1); i < args.length; i++) {
reason = reason + " " + args[i];
}
}
if (CommandsEX.sqlEnabled) {
// if we don't have time value and the player has a certain number of previous bans, inform admin
ResultSet res = SQLManager.query_res("SELECT Count(*) as Total FROM " + SQLManager.prefix + "bans WHERE player_name = ?", pName);
try {
while (res.next()) {
Integer total = res.getInt("Total");
// warn admin if temporary bans treshold was reached
if (total > CommandsEX.getConf().getInt("minTempBansWarn")) {
LogHelper.showInfo("bansTempBansTresholdReached1#####[" + total + " #####bansTempBansTresholdReached2", sender);
}
// store ban record in database
if (t.containsKey("not_found")) {
// no expiration - permaban
SQLManager.query("INSERT INTO " + SQLManager.prefix + "bans (player_name, creation_date, creator, reason) VALUES (?, ?, ?, ?)", pName, new Timestamp(new java.util.Date().getTime()), sender.getName() , reason);
} else {
// temporary ban
SQLManager.query("INSERT INTO " + SQLManager.prefix + "bans (player_name, creation_date, expiration_date, creator, reason) VALUES (?, ?, ?, ?, ?)", pName, new Timestamp(new java.util.Date().getTime()), stamp, sender.getName(), reason);
}
}
res.close();
} catch (Throwable e) {
// unable to ban player
LogHelper.showWarning("internalError", sender);
LogHelper.logSevere("[CommandsEX] " + _("dbWriteError", ""));
LogHelper.logDebug("Message: " + e.getMessage() + ", cause: " + e.getCause());
return true;
}
}
// ban player using server's banning system
if (isOffline) {
pl.setBanned(true);
} else {
p.setBanned(true);
}
// if we've banned the player temporarily, start timer
if (!t.containsKey("not_found")) {
Integer banTime = (t.get("seconds") + (t.get("minutes") * 60) + (t.get("hours") * 3600)+ (t.get("days") * 86400));
CommandsEX.plugin.getServer().getScheduler().scheduleSyncDelayedTask(CommandsEX.plugin, new DelayedPardon(pName), (20 * banTime));
}
// tell everyone if not disallowed in config
if (!CommandsEX.getConf().getBoolean("silentBans")) {
CommandsEX.plugin.getServer().broadcastMessage(ChatColor.LIGHT_PURPLE + (!reason.equals("") ? (pDispName + " " + _("bansBeingBannedForMessage", "") + reason) : pDispName + " " + _("bansBeingBannedMessage", "")) + (!t.containsKey("not_found") ? " " + _("for", "") + " " + args[1].replace("t:", "") : ""));
}
// at last, kick the player if still online
if (!isOffline) {
p.kickPlayer(ChatColor.RED + (!reason.equals("") ? (_("bansYouAreBannedForMessage", "") + reason) : _("bansGenericReason", "")));
}
return true;
}