Essentials/Essentials/src/main/java/com/earth2me/essentials/commands/Commandban.java

77 lines
3.0 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import com.earth2me.essentials.Console;
import com.earth2me.essentials.OfflinePlayerStub;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.AdventureUtil;
2013-06-08 23:31:19 +02:00
import com.earth2me.essentials.utils.FormatUtil;
import net.ess3.api.TranslatableException;
2014-07-12 18:37:25 +02:00
import org.bukkit.BanList;
2011-11-18 18:42:26 +01:00
import org.bukkit.Server;
import java.util.Collections;
import java.util.List;
2015-04-15 06:06:16 +02:00
import java.util.logging.Level;
import static com.earth2me.essentials.I18n.tlLiteral;
2015-04-15 06:06:16 +02:00
public class Commandban extends EssentialsCommand {
public Commandban() {
super("ban");
}
@Override
public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
boolean nomatch = false;
if (args.length < 1) {
throw new NotEnoughArgumentsException();
}
User user;
try {
user = getPlayer(server, args, 0, true, true);
2020-10-03 19:46:05 +02:00
} catch (final PlayerNotFoundException e) {
2015-04-15 06:06:16 +02:00
nomatch = true;
user = ess.getUser(new OfflinePlayerStub(args[0], ess.getServer()));
2015-04-15 06:06:16 +02:00
}
if (!user.getBase().isOnline()) {
if (sender.isPlayer() && !ess.getUser(sender.getPlayer()).isAuthorized("essentials.ban.offline")) {
throw new TranslatableException("banExemptOffline");
2015-04-15 06:06:16 +02:00
}
} else if (user.isAuthorized("essentials.ban.exempt") && sender.isPlayer()) {
throw new TranslatableException("banExempt");
2015-04-15 06:06:16 +02:00
}
2015-04-15 06:06:16 +02:00
final String senderName = sender.isPlayer() ? sender.getPlayer().getDisplayName() : Console.NAME;
final String senderDisplayName = sender.isPlayer() ? sender.getPlayer().getDisplayName() : Console.DISPLAY_NAME;
2020-10-03 19:46:05 +02:00
final String banReason;
2015-04-15 06:06:16 +02:00
if (args.length > 1) {
banReason = FormatUtil.replaceFormat(getFinalArg(args, 1).replace("\\n", "\n").replace("|", "\n"));
} else {
banReason = tlLiteral("defaultBanReason");
2015-04-15 06:06:16 +02:00
}
2015-04-15 06:06:16 +02:00
ess.getServer().getBanList(BanList.Type.NAME).addBan(user.getName(), banReason, null, senderName);
final String banDisplay = tlLiteral("banFormat", banReason, senderDisplayName);
2013-05-26 18:58:04 +02:00
2015-04-15 06:06:16 +02:00
user.getBase().kickPlayer(banDisplay);
ess.getLogger().log(Level.INFO, AdventureUtil.miniToLegacy(tlLiteral("playerBanned", senderDisplayName, user.getName(), banDisplay)));
2013-05-26 18:58:04 +02:00
2015-04-15 06:06:16 +02:00
if (nomatch) {
sender.sendTl("userUnknown", user.getName());
2015-04-15 06:06:16 +02:00
}
2011-11-18 13:06:59 +01:00
ess.broadcastTl(null, u -> !u.isAuthorized("essentials.ban.notify"), "playerBanned", senderDisplayName, user.getName(), banReason);
2015-04-15 06:06:16 +02:00
}
@Override
2020-10-03 19:46:05 +02:00
protected List<String> getTabCompleteOptions(final Server server, final CommandSource sender, final String commandLabel, final String[] args) {
if (args.length == 1) {
return getPlayers(server, sender);
} else {
return Collections.emptyList();
}
}
}