From c8b8aa945df2c64727fe0669f27e7535e73f577d Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Mon, 2 Nov 2020 04:18:38 +0100 Subject: [PATCH] spigot add/remove dynamic commands --- .../spigot/command/MainCommand.java | 104 +++++++++++++++++- .../spigot/config/ConfigCache.java | 47 +++++++- src/main/resources/config.yml | 3 + src/main/resources/plugin.yml | 2 + 4 files changed, 148 insertions(+), 8 deletions(-) diff --git a/src/main/java/eu/endermite/commandwhitelist/spigot/command/MainCommand.java b/src/main/java/eu/endermite/commandwhitelist/spigot/command/MainCommand.java index 4659236..e2516f1 100644 --- a/src/main/java/eu/endermite/commandwhitelist/spigot/command/MainCommand.java +++ b/src/main/java/eu/endermite/commandwhitelist/spigot/command/MainCommand.java @@ -5,8 +5,11 @@ import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.command.TabExecutor; +import org.bukkit.help.HelpTopic; + import java.util.ArrayList; import java.util.List; +import java.util.Map; public class MainCommand implements TabExecutor { @@ -14,11 +17,48 @@ public class MainCommand implements TabExecutor { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (args.length > 0) { if (args[0].equalsIgnoreCase("reload")) { - if (sender.hasPermission("commandwhitelist.reload")) { - CommandWhitelist.getPlugin().reloadPluginConfig(sender); - } else { + if (!sender.hasPermission("commandwhitelist.reload")) { sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + CommandWhitelist.getConfigCache().getNoPermission())); + return true; } + CommandWhitelist.getPlugin().reloadPluginConfig(sender); + + } else if (args[0].equalsIgnoreCase("add")) { + + if (!sender.hasPermission("commandwhitelist.admin")) { + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + CommandWhitelist.getConfigCache().getNoPermission())); + return true; + } + if (args.length >= 3) { + if (CommandWhitelist.getConfigCache().addCommand(args[2], args[1])) { + String msg = String.format(CommandWhitelist.getConfigCache().getWhitelistedCommand(), args[2], args[1]); + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg)); + } else { + String msg = CommandWhitelist.getConfigCache().getNoSuchGroup(); + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg)); + } + } else { + sender.sendMessage("/cw add "); + } + + } else if (args[0].equalsIgnoreCase("remove")) { + + if (!sender.hasPermission("commandwhitelist.admin")) { + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + CommandWhitelist.getConfigCache().getNoPermission())); + return true; + } + if (args.length >= 3) { + if (CommandWhitelist.getConfigCache().removeCommand(args[2], args[1])) { + String msg = String.format(CommandWhitelist.getConfigCache().getRemovedWhitelistedCommand(), args[2], args[1]); + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg)); + } else { + String msg = CommandWhitelist.getConfigCache().getNoSuchGroup(); + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg)); + } + } else { + sender.sendMessage("/cw remove "); + } + } else { sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + CommandWhitelist.getConfigCache().getNoSubCommand())); } @@ -27,6 +67,9 @@ public class MainCommand implements TabExecutor { if (sender.hasPermission("commandwhitelist.reload")) { sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&9/cw reload &b- Reload plugin configuration")); } + if (sender.hasPermission("commandwhitelist.admin")) { + sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&9/cw add &b- Add command to group")); + } } return true; } @@ -34,10 +77,63 @@ public class MainCommand implements TabExecutor { @Override public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { List list = new ArrayList<>(); - if(args.length == 1) { + if (args.length == 1) { if ("restart".startsWith(args[0]) && sender.hasPermission("commandwhitelist.reload")) { list.add("reload"); } + if ("add".startsWith(args[0]) && sender.hasPermission("commandwhitelist.admin")) { + list.add("add"); + } + if ("remove".startsWith(args[0]) && sender.hasPermission("commandwhitelist.admin")) { + list.add("remove"); + } + } else if (args.length == 2) { + + if (args[0].equalsIgnoreCase("add") || args[0].equalsIgnoreCase("remove")) { + for (Map.Entry> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) { + if (s.getKey().startsWith(args[1]) && sender.hasPermission("commandwhitelist.admin")) { + list.add(s.getKey()); + } + } + } + } else if (args.length == 3) { + if (args[0].equalsIgnoreCase("remove")) { + if (!sender.hasPermission("commandwhitelist.admin")) + return list; + try { + for (String s : CommandWhitelist.getConfigCache().getPermList().get(args[1])) { + if (s.startsWith(args[2])) { + list.add(s); + } + } + } catch (NullPointerException ignored) { + } + return list; + } + if (args[0].equalsIgnoreCase("add")) { + if (!sender.hasPermission("commandwhitelist.admin")) + return list; + + for (HelpTopic s : CommandWhitelist.getPlugin().getServer().getHelpMap().getHelpTopics()) { + String cmd = s.getName(); + if (!cmd.startsWith("/")) + continue; + try { + if (cmd.contains(":")) { + cmd = cmd.split(":")[1]; + } + } catch (Exception e) { + continue; + } + cmd = cmd.replace("/", ""); + if (cmd.startsWith(args[2])) { + list.add(cmd); + } + } + + return list; + } + } return list; } diff --git a/src/main/java/eu/endermite/commandwhitelist/spigot/config/ConfigCache.java b/src/main/java/eu/endermite/commandwhitelist/spigot/config/ConfigCache.java index 9c5699a..011821f 100644 --- a/src/main/java/eu/endermite/commandwhitelist/spigot/config/ConfigCache.java +++ b/src/main/java/eu/endermite/commandwhitelist/spigot/config/ConfigCache.java @@ -1,6 +1,7 @@ package eu.endermite.commandwhitelist.spigot.config; -import org.bukkit.configuration.Configuration; +import eu.endermite.commandwhitelist.spigot.CommandWhitelist; +import org.bukkit.configuration.file.FileConfiguration; import java.util.HashMap; import java.util.List; @@ -8,11 +9,15 @@ import java.util.Set; public class ConfigCache { + private FileConfiguration config; private HashMap> permList = new HashMap<>(); - private String prefix, commandDenied, noPermission, noSubCommand, configReloaded; - private List commandDeniedList; + private final String prefix, commandDenied, noPermission, noSubCommand, configReloaded, whitelistedCommand, + removedWhitelistedCommand, noSuchGroup; + private final List commandDeniedList; - public ConfigCache(Configuration config) { + public ConfigCache(FileConfiguration config) { + + this.config = config; prefix = config.getString("messages.prefix"); commandDenied = config.getString("messages.command-denied", null); @@ -20,6 +25,9 @@ public class ConfigCache { noPermission = config.getString("messages.no-permission"); noSubCommand = config.getString("messages.no-such-subcommand"); configReloaded = config.getString("messages.config-reloaded"); + whitelistedCommand = config.getString("messages.added-to-whitelist", "&eWhitelisted command &6%s &efor permission &6%s"); + removedWhitelistedCommand = config.getString("messages.removed-from-whitelist", "&eRemoved command &6%s &efrom permission &6%s"); + noSuchGroup = config.getString("messages.group-doesnt-exist", "&cGroup %s doesn't exist"); Set perms = config.getConfigurationSection("commands").getKeys(false); for (String s : perms) { @@ -30,6 +38,28 @@ public class ConfigCache { public HashMap> getPermList() { return permList; } + public boolean addCommand(String command, String group) { + try { + this.permList.get(group).add(command); + this.config.set("commands."+group, permList.get(group)); + config.save(CommandWhitelist.getPlugin().getDataFolder()+"/config.yml"); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + public boolean removeCommand(String command, String group) { + try { + this.permList.get(group).remove(command); + this.config.set("commands."+group, permList.get(group)); + config.save(CommandWhitelist.getPlugin().getDataFolder()+"/config.yml"); + return true; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } public String getPrefix() {return prefix;} public String getCommandDenied() {return commandDenied;} @@ -39,4 +69,13 @@ public class ConfigCache { public String getNoPermission() {return noPermission;} public String getNoSubCommand() {return noSubCommand;} public String getConfigReloaded() {return configReloaded;} + public String getWhitelistedCommand() { + return whitelistedCommand; + } + public String getRemovedWhitelistedCommand() { + return removedWhitelistedCommand; + } + public String getNoSuchGroup() { + return noSuchGroup; + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index ae8edcc..fc3b434 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -4,6 +4,9 @@ messages: no-permission: "&cYou don't have permission to do this." no-such-subcommand: "&cNo subcommand by that name." config-reloaded: "&eConfiguration reloaded." + added-to-whitelist: "&eWhitelisted command &6%s &efor permission &6%s" + removed-from-whitelist: "&eRemoved command &6%s &efrom permission &6%s" + group-doesnt-exist: "&cGroup or command doesn't exist" commands: # Permissions that control what commands players can use diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 6ea1291..38136e9 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -15,6 +15,8 @@ commands: permissions: commandwhitelist.reload: default: OP + commandwhitelist.admin: + default: OP commandwhitelist.bypass: default: OP commandwhitelist.commands.default: