mirror of
https://github.com/YouHaveTrouble/CommandWhitelist.git
synced 2026-05-12 06:26:57 +00:00
spigot add/remove dynamic commands
This commit is contained in:
@@ -5,8 +5,11 @@ import org.bukkit.ChatColor;
|
|||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.command.TabExecutor;
|
import org.bukkit.command.TabExecutor;
|
||||||
|
import org.bukkit.help.HelpTopic;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
public class MainCommand implements TabExecutor {
|
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) {
|
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||||
if (args.length > 0) {
|
if (args.length > 0) {
|
||||||
if (args[0].equalsIgnoreCase("reload")) {
|
if (args[0].equalsIgnoreCase("reload")) {
|
||||||
if (sender.hasPermission("commandwhitelist.reload")) {
|
if (!sender.hasPermission("commandwhitelist.reload")) {
|
||||||
CommandWhitelist.getPlugin().reloadPluginConfig(sender);
|
|
||||||
} else {
|
|
||||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + CommandWhitelist.getConfigCache().getNoPermission()));
|
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 <group> <command>");
|
||||||
|
}
|
||||||
|
|
||||||
|
} 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 <group> <command>");
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + CommandWhitelist.getConfigCache().getNoSubCommand()));
|
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + CommandWhitelist.getConfigCache().getNoSubCommand()));
|
||||||
}
|
}
|
||||||
@@ -27,6 +67,9 @@ public class MainCommand implements TabExecutor {
|
|||||||
if (sender.hasPermission("commandwhitelist.reload")) {
|
if (sender.hasPermission("commandwhitelist.reload")) {
|
||||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&9/cw reload &b- Reload plugin configuration"));
|
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&9/cw reload &b- Reload plugin configuration"));
|
||||||
}
|
}
|
||||||
|
if (sender.hasPermission("commandwhitelist.admin")) {
|
||||||
|
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&9/cw add <group> <command> &b- Add command to group"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -34,10 +77,63 @@ public class MainCommand implements TabExecutor {
|
|||||||
@Override
|
@Override
|
||||||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
|
||||||
List<String> list = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
||||||
if(args.length == 1) {
|
if (args.length == 1) {
|
||||||
if ("restart".startsWith(args[0]) && sender.hasPermission("commandwhitelist.reload")) {
|
if ("restart".startsWith(args[0]) && sender.hasPermission("commandwhitelist.reload")) {
|
||||||
list.add("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<String, List<String>> 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;
|
return list;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package eu.endermite.commandwhitelist.spigot.config;
|
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.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -8,11 +9,15 @@ import java.util.Set;
|
|||||||
|
|
||||||
public class ConfigCache {
|
public class ConfigCache {
|
||||||
|
|
||||||
|
private FileConfiguration config;
|
||||||
private HashMap<String, List<String>> permList = new HashMap<>();
|
private HashMap<String, List<String>> permList = new HashMap<>();
|
||||||
private String prefix, commandDenied, noPermission, noSubCommand, configReloaded;
|
private final String prefix, commandDenied, noPermission, noSubCommand, configReloaded, whitelistedCommand,
|
||||||
private List<String> commandDeniedList;
|
removedWhitelistedCommand, noSuchGroup;
|
||||||
|
private final List<String> commandDeniedList;
|
||||||
|
|
||||||
public ConfigCache(Configuration config) {
|
public ConfigCache(FileConfiguration config) {
|
||||||
|
|
||||||
|
this.config = config;
|
||||||
|
|
||||||
prefix = config.getString("messages.prefix");
|
prefix = config.getString("messages.prefix");
|
||||||
commandDenied = config.getString("messages.command-denied", null);
|
commandDenied = config.getString("messages.command-denied", null);
|
||||||
@@ -20,6 +25,9 @@ public class ConfigCache {
|
|||||||
noPermission = config.getString("messages.no-permission");
|
noPermission = config.getString("messages.no-permission");
|
||||||
noSubCommand = config.getString("messages.no-such-subcommand");
|
noSubCommand = config.getString("messages.no-such-subcommand");
|
||||||
configReloaded = config.getString("messages.config-reloaded");
|
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<String> perms = config.getConfigurationSection("commands").getKeys(false);
|
Set<String> perms = config.getConfigurationSection("commands").getKeys(false);
|
||||||
for (String s : perms) {
|
for (String s : perms) {
|
||||||
@@ -30,6 +38,28 @@ public class ConfigCache {
|
|||||||
public HashMap<String, List<String>> getPermList() {
|
public HashMap<String, List<String>> getPermList() {
|
||||||
return permList;
|
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 getPrefix() {return prefix;}
|
||||||
public String getCommandDenied() {return commandDenied;}
|
public String getCommandDenied() {return commandDenied;}
|
||||||
@@ -39,4 +69,13 @@ public class ConfigCache {
|
|||||||
public String getNoPermission() {return noPermission;}
|
public String getNoPermission() {return noPermission;}
|
||||||
public String getNoSubCommand() {return noSubCommand;}
|
public String getNoSubCommand() {return noSubCommand;}
|
||||||
public String getConfigReloaded() {return configReloaded;}
|
public String getConfigReloaded() {return configReloaded;}
|
||||||
|
public String getWhitelistedCommand() {
|
||||||
|
return whitelistedCommand;
|
||||||
|
}
|
||||||
|
public String getRemovedWhitelistedCommand() {
|
||||||
|
return removedWhitelistedCommand;
|
||||||
|
}
|
||||||
|
public String getNoSuchGroup() {
|
||||||
|
return noSuchGroup;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ messages:
|
|||||||
no-permission: "&cYou don't have permission to do this."
|
no-permission: "&cYou don't have permission to do this."
|
||||||
no-such-subcommand: "&cNo subcommand by that name."
|
no-such-subcommand: "&cNo subcommand by that name."
|
||||||
config-reloaded: "&eConfiguration reloaded."
|
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:
|
commands:
|
||||||
# Permissions that control what commands players can use
|
# Permissions that control what commands players can use
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ commands:
|
|||||||
permissions:
|
permissions:
|
||||||
commandwhitelist.reload:
|
commandwhitelist.reload:
|
||||||
default: OP
|
default: OP
|
||||||
|
commandwhitelist.admin:
|
||||||
|
default: OP
|
||||||
commandwhitelist.bypass:
|
commandwhitelist.bypass:
|
||||||
default: OP
|
default: OP
|
||||||
commandwhitelist.commands.default:
|
commandwhitelist.commands.default:
|
||||||
|
|||||||
Reference in New Issue
Block a user