mirror of
https://github.com/YouHaveTrouble/CommandWhitelist.git
synced 2026-05-12 06:26:57 +00:00
dynamic adding/removing commands for bungee
This commit is contained in:
@@ -4,7 +4,7 @@ import com.google.common.io.ByteStreams;
|
||||
import eu.endermite.commandwhitelist.bungee.command.BungeeMainCommand;
|
||||
import eu.endermite.commandwhitelist.bungee.config.BungeeConfigCache;
|
||||
import eu.endermite.commandwhitelist.bungee.listeners.BungeeChatEventListener;
|
||||
import eu.endermite.commandwhitelist.bungee.listeners.BungeeTabCompleteListener;
|
||||
import eu.endermite.commandwhitelist.bungee.listeners.WaterfallDefineCommandsListener;
|
||||
import eu.endermite.commandwhitelist.bungee.metrics.BungeeMetrics;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
@@ -31,12 +31,12 @@ public final class CommandWhitelistBungee extends Plugin {
|
||||
this.getProxy().getPluginManager().registerListener(this, new BungeeChatEventListener());
|
||||
try {
|
||||
Class.forName("io.github.waterfallmc.waterfall.conf.WaterfallConfiguration");
|
||||
this.getProxy().getPluginManager().registerListener(this, new BungeeTabCompleteListener());
|
||||
this.getProxy().getPluginManager().registerListener(this, new WaterfallDefineCommandsListener());
|
||||
} catch (ClassNotFoundException e) {
|
||||
getLogger().severe(ChatColor.DARK_RED+"Bungee tab completion blocker requires Waterfall other Waterfall fork.");
|
||||
}
|
||||
|
||||
getProxy().getPluginManager().registerCommand(this, new BungeeMainCommand());
|
||||
getProxy().getPluginManager().registerCommand(this, new BungeeMainCommand("bcw"));
|
||||
|
||||
int pluginId = 8704;
|
||||
new BungeeMetrics(this, pluginId);
|
||||
|
||||
@@ -4,15 +4,18 @@ import eu.endermite.commandwhitelist.bungee.CommandWhitelistBungee;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.plugin.Command;
|
||||
import net.md_5.bungee.api.plugin.TabExecutor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class BungeeMainCommand extends Command {
|
||||
public BungeeMainCommand() {
|
||||
super("bungeecommandwhitelist", "none", "bcw");
|
||||
public class BungeeMainCommand extends Command implements TabExecutor {
|
||||
|
||||
public BungeeMainCommand(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sender, String[] args) {
|
||||
if (args.length > 0) {
|
||||
if (args[0].equalsIgnoreCase("reload")) {
|
||||
@@ -21,6 +24,41 @@ public class BungeeMainCommand extends Command {
|
||||
} else {
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelistBungee.getConfigCache().getPrefix() + CommandWhitelistBungee.getConfigCache().getNoPermission()));
|
||||
}
|
||||
} else if (args[0].equalsIgnoreCase("add")) {
|
||||
if (!sender.hasPermission("commandwhitelist.admin")) {
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelistBungee.getConfigCache().getPrefix() + CommandWhitelistBungee.getConfigCache().getNoPermission()));
|
||||
return;
|
||||
}
|
||||
if (args.length >= 3) {
|
||||
if (CommandWhitelistBungee.getConfigCache().addCommand(args[2], args[1])) {
|
||||
String msg = String.format(CommandWhitelistBungee.getConfigCache().getWhitelistedCommand(), args[2], args[1]);
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
|
||||
} else {
|
||||
String msg = CommandWhitelistBungee.getConfigCache().getNoSuchGroup();
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("/bcw add <group> <command>");
|
||||
}
|
||||
return;
|
||||
} else if (args[0].equalsIgnoreCase("remove")) {
|
||||
|
||||
if (!sender.hasPermission("commandwhitelist.admin")) {
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelistBungee.getConfigCache().getPrefix() + CommandWhitelistBungee.getConfigCache().getNoPermission()));
|
||||
return;
|
||||
}
|
||||
if (args.length >= 3) {
|
||||
if (CommandWhitelistBungee.getConfigCache().removeCommand(args[2], args[1])) {
|
||||
String msg = String.format(CommandWhitelistBungee.getConfigCache().getRemovedWhitelistedCommand(), args[2], args[1]);
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
|
||||
} else {
|
||||
String msg = CommandWhitelistBungee.getConfigCache().getNoSuchGroup();
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
|
||||
}
|
||||
} else {
|
||||
sender.sendMessage("/bcw remove <group> <command>");
|
||||
}
|
||||
|
||||
} else {
|
||||
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelistBungee.getConfigCache().getPrefix() + CommandWhitelistBungee.getConfigCache().getNoSubCommand()));
|
||||
}
|
||||
@@ -31,4 +69,59 @@ public class BungeeMainCommand extends Command {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
|
||||
List<String> list = new ArrayList<>();
|
||||
if (args.length == 1) {
|
||||
if ("reload".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")) {
|
||||
if (!sender.hasPermission("commandwhitelist.admin"))
|
||||
return list;
|
||||
for (Map.Entry<String, List<String>> s : CommandWhitelistBungee.getConfigCache().getPermList().entrySet()) {
|
||||
if (s.getKey().startsWith(args[1])) {
|
||||
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 : CommandWhitelistBungee.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 (Map.Entry<String, Command> command : CommandWhitelistBungee.getPlugin().getProxy().getPluginManager().getCommands()) {
|
||||
if (!command.getKey().startsWith("/"))
|
||||
list.add(command.getKey());
|
||||
|
||||
for (String alias: command.getValue().getAliases()) {
|
||||
if (!alias.startsWith("/"))
|
||||
list.add(alias);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +1,35 @@
|
||||
package eu.endermite.commandwhitelist.bungee.config;
|
||||
|
||||
import eu.endermite.commandwhitelist.bungee.CommandWhitelistBungee;
|
||||
import net.md_5.bungee.config.Configuration;
|
||||
|
||||
import net.md_5.bungee.config.ConfigurationProvider;
|
||||
import net.md_5.bungee.config.YamlConfiguration;
|
||||
import java.io.File;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public class BungeeConfigCache {
|
||||
|
||||
private final Configuration config;
|
||||
private final HashMap<String, List<String>> permList = new HashMap<>();
|
||||
private final String prefix, commandDenied, noPermission, noSubCommand, configReloaded;
|
||||
private final String prefix, commandDenied, noPermission, noSubCommand, configReloaded, whitelistedCommand,
|
||||
removedWhitelistedCommand, noSuchGroup;
|
||||
private List<String> commandDeniedList;
|
||||
|
||||
public BungeeConfigCache(Configuration config) {
|
||||
|
||||
this.config = config;
|
||||
|
||||
prefix = config.getString("messages.prefix");
|
||||
commandDenied = config.getString("messages.command-denied", null);
|
||||
commandDeniedList = config.getStringList("messages.command-denied");
|
||||
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");
|
||||
|
||||
Collection<String> perms = config.getSection("commands").getKeys();
|
||||
for (String s : perms) {
|
||||
@@ -30,6 +40,28 @@ public class BungeeConfigCache {
|
||||
public HashMap<String, List<String>> 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));
|
||||
ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, new File(CommandWhitelistBungee.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));
|
||||
ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, new File(CommandWhitelistBungee.getPlugin().getDataFolder(), "config.yml"));
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public String getPrefix() {return prefix;}
|
||||
public String getCommandDenied() {return commandDenied;}
|
||||
public List<String> getCommandDeniedList() {
|
||||
@@ -38,5 +70,13 @@ public class BungeeConfigCache {
|
||||
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;
|
||||
}
|
||||
}
|
||||
+2
-4
@@ -3,12 +3,10 @@ package eu.endermite.commandwhitelist.bungee.listeners;
|
||||
import eu.endermite.commandwhitelist.api.RandomStuff;
|
||||
import eu.endermite.commandwhitelist.bungee.CommandWhitelistBungee;
|
||||
import eu.endermite.commandwhitelist.bungee.config.BungeeConfigCache;
|
||||
import eu.endermite.commandwhitelist.spigot.CommandWhitelist;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.plugin.Listener;
|
||||
import net.md_5.bungee.event.EventHandler;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -52,7 +50,7 @@ public class BungeeChatEventListener implements Listener {
|
||||
if (!found) {
|
||||
event.setCancelled(true);
|
||||
BungeeConfigCache config = CommandWhitelistBungee.getConfigCache();
|
||||
player.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelist.getConfigCache().getPrefix() + RandomStuff.getMessage(config.getCommandDeniedList(), config.getCommandDenied())));
|
||||
player.sendMessage(ChatColor.translateAlternateColorCodes('&', CommandWhitelistBungee.getConfigCache().getPrefix() + RandomStuff.getMessage(config.getCommandDeniedList(), config.getCommandDenied())));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ import net.md_5.bungee.event.EventHandler;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class BungeeTabCompleteListener implements Listener {
|
||||
public class WaterfallDefineCommandsListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onProxyDefineCommandsEvent(io.github.waterfallmc.waterfall.event.ProxyDefineCommandsEvent event) {
|
||||
@@ -60,7 +60,6 @@ public class ConfigCache {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public String getPrefix() {return prefix;}
|
||||
public String getCommandDenied() {return commandDenied;}
|
||||
public List<String> getCommandDeniedList() {
|
||||
@@ -78,4 +77,4 @@ public class ConfigCache {
|
||||
public String getNoSuchGroup() {
|
||||
return noSuchGroup;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,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
|
||||
|
||||
Reference in New Issue
Block a user