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

Add blacklist support to CommandWhitelist #98

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class CWGroup {
private final String id, permission, commandDeniedMessage;
private final HashSet<String> commands = new HashSet<>();
private final HashSet<String> subCommands = new HashSet<>();
private final HashSet<String> blacklist = new HashSet<>();

public CWGroup(String id, Collection<String> commands, Collection<String> subCommands, String custom_command_denied_message) {
this.id = id;
Expand Down Expand Up @@ -54,12 +55,26 @@ public void removeSubCommand(String subCommand) {
subCommands.remove(subCommand);
}

public HashSet<String> getBlacklist() {
return blacklist;
}

public void addToBlacklist(String command) {
blacklist.add(command);
}

public void removeFromBlacklist(String command) {
blacklist.remove(command);
}

public HashMap<String, Object> serialize() {
HashMap<String, Object> serializedGroup = new LinkedHashMap<>();
List<String> commands = new ArrayList<>(this.commands);
List<String> subCommands = new ArrayList<>(this.subCommands);
List<String> blacklist = new ArrayList<>(this.blacklist);
serializedGroup.put("commands", commands);
serializedGroup.put("subcommands", subCommands);
serializedGroup.put("blacklist", blacklist);
return serializedGroup;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ public static boolean removeFromWhitelist(ConfigCache configCache, String comman
return true;
}

public static boolean addToBlacklist(ConfigCache configCache, String command, String group) {
CWGroup cwGroup = configCache.getGroupList().get(group);
if (cwGroup == null) return false;
cwGroup.addToBlacklist(command);
configCache.saveCWGroup(group, cwGroup);
return true;
}

public static boolean removeFromBlacklist(ConfigCache configCache, String command, String group) {
CWGroup cwGroup = configCache.getGroupList().get(group);
if (cwGroup == null)
return false;
cwGroup.removeFromBlacklist(command);
configCache.saveCWGroup(group, cwGroup);
return true;
}

public static Component helpComponent(String baseCommand, boolean showReloadCommand, boolean showAdminCommands) {
final TextComponent.Builder builder = Component.text();
builder.append(miniMessage.deserialize("<rainbow><bold>CommandWhitelist by YouHaveTrouble</bold>"), Component.newline());
Expand All @@ -48,12 +65,16 @@ public static Component helpComponent(String baseCommand, boolean showReloadComm
builder.append(Component.text(baseCommand + " add <group> <command>", NamedTextColor.AQUA), Component.text(" - Add a command to selected permission group", NamedTextColor.BLUE));
builder.append(Component.newline());
builder.append(Component.text(baseCommand + " remove <group> <command>", NamedTextColor.AQUA), Component.text(" - Removes a command from selected permission group", NamedTextColor.BLUE));
builder.append(Component.newline());
builder.append(Component.text(baseCommand + " blacklist add <group> <command>", NamedTextColor.AQUA), Component.text(" - Add a command to the blacklist of selected permission group", NamedTextColor.BLUE));
builder.append(Component.newline());
builder.append(Component.text(baseCommand + " blacklist remove <group> <command>", NamedTextColor.AQUA), Component.text(" - Removes a command from the blacklist of selected permission group", NamedTextColor.BLUE));
}
return builder.build();
}

public enum CommandType {
ADD, REMOVE, HELP, RELOAD, DUMP
ADD, REMOVE, HELP, RELOAD, DUMP, BLACKLIST
}

public enum ImplementationType {
Expand All @@ -79,6 +100,8 @@ public static List<String> commandSuggestions(
list.add("add");
list.add("remove");
list.add("dump");
list.add("blacklist add");
list.add("blacklist remove");
}
}
return list;
Expand All @@ -93,9 +116,13 @@ public static List<String> commandSuggestions(
list.add("remove");
if ("dump".startsWith(args[0]) && adminPerm)
list.add("dump");
if ("blacklist".startsWith(args[0]) && adminPerm) {
list.add("blacklist add");
list.add("blacklist remove");
}
return list;
case 2:
if (args[0].equalsIgnoreCase("add") || args[0].equalsIgnoreCase("remove")) {
if (args[0].equalsIgnoreCase("add") || args[0].equalsIgnoreCase("remove") || args[0].equalsIgnoreCase("blacklist add") || args[0].equalsIgnoreCase("blacklist remove")) {
if (!adminPerm) return list;
for (String s : config.getGroupList().keySet()) {
if (s.startsWith(args[1]))
Expand All @@ -104,7 +131,7 @@ public static List<String> commandSuggestions(
}
return list;
case 3:
if (args[0].equalsIgnoreCase("remove")) {
if (args[0].equalsIgnoreCase("remove") || args[0].equalsIgnoreCase("blacklist remove")) {
if (!adminPerm) return list;
CWGroup group = config.getGroupList().get(args[1]);
if (group == null) return list;
Expand All @@ -114,7 +141,7 @@ public static List<String> commandSuggestions(
}
return list;
}
if (args[0].equalsIgnoreCase("add")) {
if (args[0].equalsIgnoreCase("add") || args[0].equalsIgnoreCase("blacklist add")) {
if (!adminPerm) return list;
CWGroup group = config.getGroupList().get(args[1]);
if (group == null) return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ public void onUserCommandExecuteEvent(CommandExecuteEvent event) {
return;
}
}

HashSet<String> blacklistedCommands = getBlacklistedCommands(player);
if (blacklistedCommands.contains(label)) {
event.setResult(CommandExecuteEvent.CommandResult.denied());
player.sendMessage(CWCommand.miniMessage.deserialize(configCache.prefix + configCache.command_denied));
}
}

/**
Expand All @@ -133,6 +139,9 @@ public void onUserTabCompleteEvent(TabCompleteEvent event) {

event.getSuggestions().clear();
event.getSuggestions().addAll(newSuggestions);

HashSet<String> blacklistedCommands = getBlacklistedCommands(player);
event.getSuggestions().removeIf(blacklistedCommands::contains);
}

public ConfigCache getConfigCache() {
Expand Down Expand Up @@ -176,4 +185,16 @@ public ArrayList<String> getServerCommands() {
return new ArrayList<>(server.getCommandManager().getAliases());
}

public HashSet<String> getBlacklistedCommands(Player player) {
HashSet<String> blacklistedCommands = new HashSet<>();
HashMap<String, CWGroup> groups = configCache.getGroupList();
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
CWGroup group = s.getValue();
if (s.getKey().equalsIgnoreCase("default"))
blacklistedCommands.addAll(group.getBlacklist());
else if (player.hasPermission(group.getPermission()))
blacklistedCommands.addAll(group.getBlacklist());
}
return blacklistedCommands;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,79 @@ public void register() {
)
)
)
.then(LiteralArgumentBuilder.<CommandSource>literal("blacklist")
.requires(src -> src.hasPermission(CWPermission.ADMIN.permission()))
.then(LiteralArgumentBuilder.<CommandSource>literal("add")
.then(RequiredArgumentBuilder.<CommandSource, String>argument("group", StringArgumentType.word())
.suggests((ctx, builder) -> {
plugin.getConfigCache().getGroupList().keySet().forEach(builder::suggest);
return builder.buildFuture();
})
.then(RequiredArgumentBuilder.<CommandSource, String>argument("command", StringArgumentType.word())
.suggests((ctx, builder) -> {
CWGroup group = plugin.getConfigCache().getGroupList().get(ctx.getArgument("group", String.class));
if (group == null) return builder.buildFuture();

for (String cmd : plugin.getServerCommands()) {
if (cmd.charAt(0) == '/')
cmd = cmd.substring(1);
if (cmd.indexOf(':') != -1) {
String[] cmdSplit = cmd.split(":");
if (cmdSplit.length < 2) continue;
cmd = cmdSplit[1];
}
if (group.getBlacklist().contains(cmd)) continue;
builder.suggest(cmd);
}
return builder.buildFuture();
})
.executes(ctx -> {
CommandSource source = ctx.getSource();
ConfigCache configCache = plugin.getConfigCache();
String arg1 = ctx.getArgument("group", String.class);
String arg2 = ctx.getArgument("command", String.class);

if (CWCommand.addToBlacklist(configCache, arg2, arg1))
source.sendMessage(CWCommand.miniMessage.deserialize(String.format(configCache.prefix + configCache.added_to_whitelist, arg2, arg1)));
else
source.sendMessage(CWCommand.miniMessage.deserialize(String.format(configCache.prefix + configCache.group_doesnt_exist, arg1)));
return Command.SINGLE_SUCCESS;
})
)
)
)
.then(LiteralArgumentBuilder.<CommandSource>literal("remove")
.then(RequiredArgumentBuilder.<CommandSource, String>argument("group", StringArgumentType.word())
.suggests((ctx, builder) -> {
plugin.getConfigCache().getGroupList().keySet().forEach(builder::suggest);
return builder.buildFuture();
})
.then(RequiredArgumentBuilder.<CommandSource, String>argument("command", StringArgumentType.word())
.suggests((ctx, builder) -> {
CWGroup group = plugin.getConfigCache().getGroupList().get(ctx.getArgument("group", String.class));
if (group == null) return builder.buildFuture();

for (String s : group.getBlacklist()) {
builder.suggest(s);
}
return builder.buildFuture();
})
.executes(ctx -> {
CommandSource source = ctx.getSource();
ConfigCache configCache = plugin.getConfigCache();
String arg1 = ctx.getArgument("group", String.class);
String arg2 = ctx.getArgument("command", String.class);

if (CWCommand.removeFromBlacklist(configCache, arg2, arg1))
source.sendMessage(CWCommand.miniMessage.deserialize(String.format(configCache.prefix + configCache.removed_from_whitelist, arg2, arg1)));
else
source.sendMessage(CWCommand.miniMessage.deserialize(String.format(configCache.prefix + configCache.group_doesnt_exist, arg1)));
return Command.SINGLE_SUCCESS;
})
)
)
)
)
.then(LiteralArgumentBuilder.<CommandSource>literal("dump")
.requires(src -> src.hasPermission(CWPermission.ADMIN.permission()))
.executes(ctx -> {
Expand Down