Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#499 - Adds banlist command #518

Merged
merged 6 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ dependency-reduced-pom.xml
.project
.settings
.db
.DS_Store
1 change: 1 addition & 0 deletions src/main/java/net/glowstone/GlowServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ private void loadPlugins() {
commandMap.register("minecraft", new ListCommand());
commandMap.register("minecraft", new BanCommand());
commandMap.register("minecraft", new BanIpCommand());
commandMap.register("minecraft", new BanListCommand());
commandMap.register("minecraft", new GiveCommand());
commandMap.register("minecraft", new DifficultyCommand());
commandMap.register("minecraft", new KillCommand());
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/net/glowstone/command/minecraft/BanListCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.glowstone.command.minecraft;

import net.glowstone.command.CommandUtils;
import org.bukkit.BanEntry;
import org.bukkit.BanList;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.defaults.VanillaCommand;
import org.bukkit.util.StringUtil;

import java.util.*;
import java.util.stream.Collectors;

public class BanListCommand extends VanillaCommand {

private static final List<String> BAN_TYPES = Arrays.asList("ips", "players");

public BanListCommand() {
super("banlist", "Displays the server's blacklist.", "/banlist [ips|players]", Collections.emptyList());
setPermission("minecraft.command.ban.list");
}

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!testPermission(sender)) return false;

BanList.Type banType;

if (args.length > 0) {
if ("ips".equalsIgnoreCase(args[0])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A switch can still be used. Just make the first argument lowercase.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, less modifications would have been performed

banType = BanList.Type.IP;
} else if ("players".equalsIgnoreCase(args[0])) {
banType = BanList.Type.NAME;
} else {
sender.sendMessage(ChatColor.RED + "Invalid parameter '" + args[0] + "'. Usage: " + usageMessage);
return false;
}
} else {
banType = BanList.Type.NAME;
}

final Set<BanEntry> banEntries = Bukkit.getBanList(banType).getBanEntries();

if (banEntries.isEmpty()) {
sender.sendMessage("There are no banned players");
} else {
final List<String> targets = banEntries.stream().map(BanEntry::getTarget).collect(Collectors.toList());
sender.sendMessage("There are " + banEntries.size() + " banned players: ");
sender.sendMessage(CommandUtils.prettyPrint(targets.toArray(new String[targets.size()])));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps check if there are no banned players here so an empty message isn't sent?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, the check is done

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, whoops, completely skimmed over it

}

return true;
}

@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
return args.length == 0 ? super.tabComplete(sender, alias, args) : Collections.emptyList();
}
}