Add tempbanip command (#3291)

This commit is contained in:
Josh Roy 2020-05-27 15:35:20 -04:00 committed by GitHub
parent fdef1062f0
commit 53e7c83254
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,87 @@
package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.Console;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.FormatUtil;
import org.bukkit.BanList;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import static com.earth2me.essentials.I18n.tl;
public class Commandtempbanip extends EssentialsCommand {
public Commandtempbanip() {
super("tempbanip");
}
@Override
public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
if (args.length < 2) {
throw new NotEnoughArgumentsException();
}
final String senderName = sender.isPlayer() ? sender.getPlayer().getDisplayName() : Console.NAME;
String ipAddress;
if (FormatUtil.validIP(args[0])) {
ipAddress = args[0];
} else {
try {
User player = getPlayer(server, args, 0, true, true);
ipAddress = player.getLastLoginAddress();
} catch (PlayerNotFoundException ex) {
ipAddress = args[0];
}
}
if (ipAddress.isEmpty()) {
throw new PlayerNotFoundException();
}
final String time = getFinalArg(args, 1);
final long banTimestamp = DateUtil.parseDateDiff(time, true);
String banReason = DateUtil.removeTimePattern(time);
final long maxBanLength = ess.getSettings().getMaxTempban() * 1000;
if (maxBanLength > 0 && ((banTimestamp - GregorianCalendar.getInstance().getTimeInMillis()) > maxBanLength) && sender.isPlayer() && !(ess.getUser(sender.getPlayer()).isAuthorized("essentials.tempban.unlimited"))) {
sender.sendMessage(tl("oversizedTempban"));
throw new NoChargeException();
}
if (banReason.length() < 2) {
banReason = tl("defaultBanReason");
}
String banDisplay = tl("banFormat", banReason, senderName);
ess.getServer().getBanList(BanList.Type.IP).addBan(ipAddress, banReason, new Date(banTimestamp), senderName);
final String message = tl("playerTempBanIpAddress", senderName, ipAddress, DateUtil.formatDateDiff(banTimestamp), banReason);
for (Player player : ess.getServer().getOnlinePlayers()) {
if (player.getAddress().getAddress().getHostAddress().equalsIgnoreCase(ipAddress)) {
player.kickPlayer(banDisplay);
}
}
server.getLogger().log(Level.INFO, message);
ess.broadcastMessage("essentials.banip.notify", message);
}
@Override
protected List<String> getTabCompleteOptions(Server server, CommandSource sender, String commandLabel, String[] args) {
if (args.length == 1) {
// TODO: Also list IP addresses?
return getPlayers(server, sender);
} else {
// Note: following args are both date diffs _and_ messages; ideally we'd mix with the vanilla handler
return COMMON_DATE_DIFFS;
}
}
}

View File

@ -392,6 +392,7 @@ payToggleOff=\u00a76You are no longer accepting payments.
payToggleOn=\u00a76You are now accepting payments.
pendingTeleportCancelled=\u00a74Pending teleportation request cancelled.
playerBanIpAddress=\u00a76Player\u00a7c {0} \u00a76banned IP address\u00a7c {1} \u00a76for\: \u00a7c{2}\u00a76.
playerTempBanIpAddress=\u00a76Player\u00a7c {0} \u00a76temporarily banned IP address \u00a7c{1}\u00a76 for \u00a7c{2}\u00a76\: \u00a7c{3}\u00a76.
playerBanned=\u00a76Player\u00a7c {0} \u00a76banned\u00a7c {1} \u00a76for\: \u00a7c{2}\u00a76.
playerJailed=\u00a76Player\u00a7c {0} \u00a76jailed.
playerJailedFor=\u00a76Player\u00a7c {0} \u00a76jailed for\u00a7c {1}\u00a76.

View File

@ -428,6 +428,10 @@ commands:
description: Temporary ban a user.
usage: /<command> <playername> <datediff> <reason>
aliases: [etempban]
tempbanip:
description: Temporarily ban an IP Address.
usage: /<command> <playername> <datediff> <reason>
aliases: [etempbanip]
thunder:
description: Enable/disable thunder.
usage: /<command> <true/false> [duration]