From d43bde0750fdd6a617de502870569e49ffe43fa5 Mon Sep 17 00:00:00 2001 From: kforbro Date: Tue, 26 Oct 2021 12:48:59 +0300 Subject: [PATCH 1/8] Custom command denied message for the group --- CommandWhitelistBukkit/pom.xml | 2 +- .../bukkit/CommandWhitelistBukkit.java | 15 +++++++++++++++ .../PlayerCommandPreProcessListener.java | 7 ++++++- .../PacketCommandPreProcessListener.java | 9 ++++++++- .../commandwhitelist/common/CWGroup.java | 11 ++++++----- .../commandwhitelist/common/ConfigCache.java | 9 +++++++-- 6 files changed, 43 insertions(+), 10 deletions(-) diff --git a/CommandWhitelistBukkit/pom.xml b/CommandWhitelistBukkit/pom.xml index 01c2306..606a2ab 100644 --- a/CommandWhitelistBukkit/pom.xml +++ b/CommandWhitelistBukkit/pom.xml @@ -34,7 +34,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.3.0-SNAPSHOT + 3.2.4 package diff --git a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java index 4470ac2..0b0fb16 100644 --- a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java +++ b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java @@ -126,4 +126,19 @@ public static HashSet getSuggestions(org.bukkit.entity.Player player) { } return suggestionList; } + + /** + * @param command command + * @return custom command denied message + */ + public static String getCommandDeniedMessage(String command) { + String commandDeniedMessage = ""; + HashMap groups = configCache.getGroupList(); + for (Map.Entry s : groups.entrySet()) { + if (s.getValue().getCommands().contains(command)) { + commandDeniedMessage = s.getValue().getCustomCommandDeniedMessage(); + } + } + return commandDeniedMessage; + } } diff --git a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/PlayerCommandPreProcessListener.java b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/PlayerCommandPreProcessListener.java index 96006cd..eed5454 100644 --- a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/PlayerCommandPreProcessListener.java +++ b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/PlayerCommandPreProcessListener.java @@ -24,7 +24,12 @@ public void PlayerCommandSendEvent(org.bukkit.event.player.PlayerCommandPreproce HashSet commands = CommandWhitelistBukkit.getCommands(player); if (!commands.contains(label)) { event.setCancelled(true); - audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + config.command_denied)); + String customCommandDeniedMessage = CommandWhitelistBukkit.getCommandDeniedMessage(label); + if (!customCommandDeniedMessage.equals("")) { + audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + customCommandDeniedMessage)); + } else { + audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + config.command_denied)); + } return; } diff --git a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/protocollib/PacketCommandPreProcessListener.java b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/protocollib/PacketCommandPreProcessListener.java index 2ba481b..28e5c77 100644 --- a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/protocollib/PacketCommandPreProcessListener.java +++ b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/protocollib/PacketCommandPreProcessListener.java @@ -12,6 +12,7 @@ import eu.endermite.commandwhitelist.common.CommandUtil; import eu.endermite.commandwhitelist.common.ConfigCache; import eu.endermite.commandwhitelist.common.commands.CWCommand; +import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; @@ -37,9 +38,15 @@ public void onPacketReceiving(PacketEvent event) { ConfigCache config = CommandWhitelistBukkit.getConfigCache(); String label = CommandUtil.getCommandLabel(string.toLowerCase()); HashSet commands = CommandWhitelistBukkit.getCommands(player); + BukkitAudiences audiences = CommandWhitelistBukkit.getAudiences(); if (!commands.contains(label)) { event.setCancelled(true); - CommandWhitelistBukkit.getAudiences().player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + config.command_denied)); + String customCommandDeniedMessage = CommandWhitelistBukkit.getCommandDeniedMessage(label); + if (!customCommandDeniedMessage.equals("")) { + audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + customCommandDeniedMessage)); + } else { + audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + config.command_denied)); + } return; } diff --git a/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWGroup.java b/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWGroup.java index 3563b8e..f3cc4b0 100644 --- a/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWGroup.java +++ b/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWGroup.java @@ -4,14 +4,15 @@ public class CWGroup { - private final String id, permission; + private final String id, permission, custom_command_denied_message; private final HashSet commands = new HashSet<>(); private final HashSet subCommands = new HashSet<>(); - public CWGroup(String id, Collection commands, Collection subCommands) { + public CWGroup(String id, Collection commands, Collection subCommands, String custom_command_denied_message) { this.id = id; this.permission = "commandwhitelist.group."+id; this.commands.addAll(commands); + this.custom_command_denied_message = custom_command_denied_message; this.subCommands.addAll(subCommands); } @@ -23,9 +24,9 @@ public String getPermission() { return permission; } - public HashSet getCommands() { - return commands; - } + public HashSet getCommands() { return commands; } + + public String getCustomCommandDeniedMessage() { return custom_command_denied_message; } public void addCommand(String command) { commands.add(command); diff --git a/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/ConfigCache.java b/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/ConfigCache.java index 32d0042..c45cf77 100644 --- a/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/ConfigCache.java +++ b/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/ConfigCache.java @@ -51,9 +51,11 @@ public boolean reloadConfig() { exampleCommands.add("example"); List exampleSubCommands = new ArrayList<>(); exampleSubCommands.add("example of"); + String exampleCustomCommandDeniedMessage = "You don't have commandwhitelist.group.example permission."; config.addExample("groups.example.commands", exampleCommands, "This is the WHITELIST of commands that players will be able to see/use in the group \"example\""); config.addExample("groups.example.subcommands", exampleSubCommands, "This is the BLACKLIST of subcommands that players will NOT be able to see/use in the group \"example\""); + config.addExample("groups.example.custom_command_denied_message", exampleCustomCommandDeniedMessage, "This is a custom message that players will see if they do not have commandwhitelist.group. permission.\ncommandwhitelist.group.example in this case\nIf you don't want to use a custom message, set custom_command_denid_message: \"\""); config.addComment("groups.example", "All groups except from default require commandwhitelist.group. permission\ncommandwhitelist.group.example in this case\n If you wish to leave the list empty, put \"commands: []\" or \"subcommands: []\""); } @@ -75,7 +77,9 @@ public boolean reloadConfig() { List defaultSubcommands = new ArrayList<>(); defaultSubcommands.add("help about"); - config.addDefault("groups.default", new CWGroup("default", defaultCommands, defaultSubcommands).serialize()); + String defaultCustomCommandDeniedMessage = ""; + + config.addDefault("groups.default", new CWGroup("default", defaultCommands, defaultSubcommands, defaultCustomCommandDeniedMessage).serialize()); prefix = config.getString("messages.prefix"); command_denied = config.getString("messages.command_denied"); @@ -130,7 +134,8 @@ public CWGroup loadCWGroup(String id, ConfigSection section) { } List subCommands = section.getStringList(id + ".subcommands"); - return new CWGroup(id, commands, subCommands); + String customCommandDeniedMessage = section.getString(id + ".custom_command_denied_message"); + return new CWGroup(id, commands, subCommands, customCommandDeniedMessage); } public void saveCWGroup(String id, CWGroup group) { From cd3d52711943d5f020e30964572d40081cb28347 Mon Sep 17 00:00:00 2001 From: kforbro Date: Tue, 26 Oct 2021 12:52:40 +0300 Subject: [PATCH 2/8] cleanup code --- .../bukkit/CommandWhitelistBukkit.java | 6 ++++-- .../endermite/commandwhitelist/common/CWGroup.java | 10 +++++++--- .../commandwhitelist/common/CWPermission.java | 1 + .../velocity/CommandWhitelistVelocity.java | 6 +++--- .../src/main/resources/velocity-plugin.json | 14 +++++++------- .../waterfall/CommandWhitelistWaterfall.java | 4 ++-- .../waterfall/command/BungeeMainCommand.java | 6 +++--- .../listeners/BungeeChatEventListener.java | 1 - 8 files changed, 27 insertions(+), 21 deletions(-) diff --git a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java index 0b0fb16..d7f958d 100644 --- a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java +++ b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java @@ -1,7 +1,10 @@ package eu.endermite.commandwhitelist.bukkit; import eu.endermite.commandwhitelist.bukkit.command.MainCommandExecutor; -import eu.endermite.commandwhitelist.bukkit.listeners.*; +import eu.endermite.commandwhitelist.bukkit.listeners.AsyncTabCompleteBlockerListener; +import eu.endermite.commandwhitelist.bukkit.listeners.PlayerCommandPreProcessListener; +import eu.endermite.commandwhitelist.bukkit.listeners.PlayerCommandSendListener; +import eu.endermite.commandwhitelist.bukkit.listeners.TabCompleteBlockerListener; import eu.endermite.commandwhitelist.bukkit.listeners.protocollib.PacketCommandPreProcessListener; import eu.endermite.commandwhitelist.bukkit.listeners.protocollib.PacketCommandSendListener; import eu.endermite.commandwhitelist.common.CWGroup; @@ -20,7 +23,6 @@ import java.io.File; import java.util.HashMap; import java.util.HashSet; - import java.util.Map; public class CommandWhitelistBukkit extends JavaPlugin { diff --git a/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWGroup.java b/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWGroup.java index f3cc4b0..a1be925 100644 --- a/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWGroup.java +++ b/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWGroup.java @@ -10,7 +10,7 @@ public class CWGroup { public CWGroup(String id, Collection commands, Collection subCommands, String custom_command_denied_message) { this.id = id; - this.permission = "commandwhitelist.group."+id; + this.permission = "commandwhitelist.group." + id; this.commands.addAll(commands); this.custom_command_denied_message = custom_command_denied_message; this.subCommands.addAll(subCommands); @@ -24,9 +24,13 @@ public String getPermission() { return permission; } - public HashSet getCommands() { return commands; } + public HashSet getCommands() { + return commands; + } - public String getCustomCommandDeniedMessage() { return custom_command_denied_message; } + public String getCustomCommandDeniedMessage() { + return custom_command_denied_message; + } public void addCommand(String command) { commands.add(command); diff --git a/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWPermission.java b/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWPermission.java index ac3dad4..57cf792 100644 --- a/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWPermission.java +++ b/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWPermission.java @@ -18,6 +18,7 @@ public String permission() { /** * Allows to check specific group permission + * * @param configCache * @param groupId * @return diff --git a/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java b/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java index 62201d3..9a85c6d 100644 --- a/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java +++ b/CommandWhitelistVelocity/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java @@ -55,7 +55,7 @@ private static void reloadConfig() { public static void reloadConfig(CommandSource source) { server.getScheduler().buildTask(plugin, () -> { reloadConfig(); - source.sendMessage(Identity.nil(), CWCommand.miniMessage.parse(getConfigCache().prefix+getConfigCache().config_reloaded)); + source.sendMessage(Identity.nil(), CWCommand.miniMessage.parse(getConfigCache().prefix + getConfigCache().config_reloaded)); }).schedule(); } @@ -65,7 +65,7 @@ public void onProxyInitialization(ProxyInitializeEvent event) { CommandMeta commandMeta = server.getCommandManager().metaBuilder("vcw").build(); server.getCommandManager().register(commandMeta, new VelocityMainCommand()); Metrics metrics = metricsFactory.make(this, 8704); - metrics.addCustomChart(new SimplePie("proxy", ()-> "Velocity")); + metrics.addCustomChart(new SimplePie("proxy", () -> "Velocity")); } @Subscribe @@ -74,7 +74,7 @@ public void onUserCommandSendEvent(PlayerAvailableCommandsEvent event) { if (player.hasPermission(CWPermission.BYPASS.permission())) return; HashSet allowedCommands = CommandWhitelistVelocity.getCommands(player); event.getRootNode().getChildren().removeIf((commandNode) -> - server.getCommandManager().hasCommand(commandNode.getName()) + server.getCommandManager().hasCommand(commandNode.getName()) && !allowedCommands.contains(commandNode.getName()) ); } diff --git a/CommandWhitelistVelocity/src/main/resources/velocity-plugin.json b/CommandWhitelistVelocity/src/main/resources/velocity-plugin.json index c53f31b..735a687 100644 --- a/CommandWhitelistVelocity/src/main/resources/velocity-plugin.json +++ b/CommandWhitelistVelocity/src/main/resources/velocity-plugin.json @@ -1,9 +1,9 @@ { - "id":"commandwhitelist", - "name":"CommandWhitelist", - "version":"${project.version}", - "description":"You decide what commands players can use or tab complete on your server!", - "authors":["YouHaveTrouble"], - "dependencies":[], - "main":"eu.endermite.commandwhitelist.velocity.CommandWhitelistVelocity" + "id": "commandwhitelist", + "name": "CommandWhitelist", + "version": "${project.version}", + "description": "You decide what commands players can use or tab complete on your server!", + "authors": ["YouHaveTrouble"], + "dependencies": [], + "main": "eu.endermite.commandwhitelist.velocity.CommandWhitelistVelocity" } \ No newline at end of file diff --git a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java index e9ef04b..bdb08ea 100644 --- a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java +++ b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java @@ -8,7 +8,6 @@ import eu.endermite.commandwhitelist.waterfall.listeners.BungeeTabcompleteListener; import eu.endermite.commandwhitelist.waterfall.listeners.WaterfallDefineCommandsListener; import net.kyori.adventure.platform.bungeecord.BungeeAudiences; -import net.kyori.adventure.text.minimessage.MiniMessage; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.connection.ProxiedPlayer; @@ -30,7 +29,7 @@ public final class CommandWhitelistWaterfall extends Plugin { @Override public void onEnable() { plugin = this; - getLogger().info("Running on "+ ChatColor.DARK_AQUA+getProxy().getName()); + getLogger().info("Running on " + ChatColor.DARK_AQUA + getProxy().getName()); loadConfig(); audiences = BungeeAudiences.create(this); Metrics metrics = new Metrics(this, 8704); @@ -53,6 +52,7 @@ public void onEnable() { public static CommandWhitelistWaterfall getPlugin() { return plugin; } + public static ConfigCache getConfigCache() { return configCache; } diff --git a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/command/BungeeMainCommand.java b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/command/BungeeMainCommand.java index 6c2ab56..bff163a 100644 --- a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/command/BungeeMainCommand.java +++ b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/command/BungeeMainCommand.java @@ -52,7 +52,7 @@ public void execute(CommandSender sender, String[] args) { else audiences.sender(sender).sendMessage(CWCommand.miniMessage.parse(configCache.prefix + configCache.group_doesnt_exist)); } else - audiences.sender(sender).sendMessage(Component.text("/"+label+" add ")); + audiences.sender(sender).sendMessage(Component.text("/" + label + " add ")); return; case REMOVE: if (!sender.hasPermission(CWPermission.ADMIN.permission())) { @@ -65,7 +65,7 @@ public void execute(CommandSender sender, String[] args) { else audiences.sender(sender).sendMessage(CWCommand.miniMessage.parse(configCache.prefix + configCache.group_doesnt_exist)); } else - audiences.sender(sender).sendMessage(Component.text("/"+label+" remove ")); + audiences.sender(sender).sendMessage(Component.text("/" + label + " remove ")); return; case HELP: default: @@ -84,6 +84,6 @@ public Iterable onTabComplete(CommandSender sender, String[] args) { for (Map.Entry command : CommandWhitelistWaterfall.getPlugin().getProxy().getPluginManager().getCommands()) { serverCommands.add(command.getValue().getName()); } - return CWCommand.commandSuggestions(CommandWhitelistWaterfall.getConfigCache(), serverCommands, args, sender.hasPermission(CWPermission.RELOAD.permission()),sender.hasPermission(CWPermission.ADMIN.permission())); + return CWCommand.commandSuggestions(CommandWhitelistWaterfall.getConfigCache(), serverCommands, args, sender.hasPermission(CWPermission.RELOAD.permission()), sender.hasPermission(CWPermission.ADMIN.permission())); } } diff --git a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/listeners/BungeeChatEventListener.java b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/listeners/BungeeChatEventListener.java index 8e155a3..02277d5 100644 --- a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/listeners/BungeeChatEventListener.java +++ b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/listeners/BungeeChatEventListener.java @@ -6,7 +6,6 @@ import eu.endermite.commandwhitelist.common.commands.CWCommand; import eu.endermite.commandwhitelist.waterfall.CommandWhitelistWaterfall; import net.kyori.adventure.platform.bungeecord.BungeeAudiences; -import net.kyori.adventure.text.minimessage.MiniMessage; import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Listener; import net.md_5.bungee.event.EventHandler; From d6909b4f2585186305fabe23a49a550b865d7428 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Tue, 26 Oct 2021 14:03:51 +0200 Subject: [PATCH 3/8] no touchy maven pls --- CommandWhitelistBukkit/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CommandWhitelistBukkit/pom.xml b/CommandWhitelistBukkit/pom.xml index 606a2ab..01c2306 100644 --- a/CommandWhitelistBukkit/pom.xml +++ b/CommandWhitelistBukkit/pom.xml @@ -34,7 +34,7 @@ org.apache.maven.plugins maven-shade-plugin - 3.2.4 + 3.3.0-SNAPSHOT package From 0cc2633604933463d0cde247aaeea4b986d32b68 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Tue, 26 Oct 2021 14:04:21 +0200 Subject: [PATCH 4/8] this can be null --- .../eu/endermite/commandwhitelist/common/CWGroup.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWGroup.java b/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWGroup.java index a1be925..86e8581 100644 --- a/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWGroup.java +++ b/CommandWhitelistCommon/src/main/java/eu/endermite/commandwhitelist/common/CWGroup.java @@ -1,10 +1,12 @@ package eu.endermite.commandwhitelist.common; +import org.jetbrains.annotations.Nullable; + import java.util.*; public class CWGroup { - private final String id, permission, custom_command_denied_message; + private final String id, permission, commandDeniedMessage; private final HashSet commands = new HashSet<>(); private final HashSet subCommands = new HashSet<>(); @@ -12,7 +14,7 @@ public CWGroup(String id, Collection commands, Collection subCom this.id = id; this.permission = "commandwhitelist.group." + id; this.commands.addAll(commands); - this.custom_command_denied_message = custom_command_denied_message; + this.commandDeniedMessage = custom_command_denied_message; this.subCommands.addAll(subCommands); } @@ -28,8 +30,8 @@ public HashSet getCommands() { return commands; } - public String getCustomCommandDeniedMessage() { - return custom_command_denied_message; + public @Nullable String getCommandDeniedMessage() { + return commandDeniedMessage; } public void addCommand(String command) { From 87fc3d7da3a8585b41efa7307137c61d34b948c6 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Tue, 26 Oct 2021 14:05:59 +0200 Subject: [PATCH 5/8] default message in the method itself so we don't have to check for empty string every time we want to use it --- .../bukkit/CommandWhitelistBukkit.java | 13 +++++++------ .../listeners/PlayerCommandPreProcessListener.java | 7 +------ .../PacketCommandPreProcessListener.java | 8 +------- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java index d7f958d..c93893a 100644 --- a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java +++ b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java @@ -130,15 +130,16 @@ public static HashSet getSuggestions(org.bukkit.entity.Player player) { } /** - * @param command command - * @return custom command denied message + * @return Command denied message. Will use custom if command exists in any group. */ public static String getCommandDeniedMessage(String command) { - String commandDeniedMessage = ""; + String commandDeniedMessage = configCache.command_denied; HashMap groups = configCache.getGroupList(); - for (Map.Entry s : groups.entrySet()) { - if (s.getValue().getCommands().contains(command)) { - commandDeniedMessage = s.getValue().getCustomCommandDeniedMessage(); + for (CWGroup group : groups.values()) { + if (group.getCommands().contains(command)) { + if (group.getCommandDeniedMessage() == null) continue; + commandDeniedMessage = group.getCommandDeniedMessage(); + break; // get first message we find } } return commandDeniedMessage; diff --git a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/PlayerCommandPreProcessListener.java b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/PlayerCommandPreProcessListener.java index eed5454..cae568f 100644 --- a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/PlayerCommandPreProcessListener.java +++ b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/PlayerCommandPreProcessListener.java @@ -24,12 +24,7 @@ public void PlayerCommandSendEvent(org.bukkit.event.player.PlayerCommandPreproce HashSet commands = CommandWhitelistBukkit.getCommands(player); if (!commands.contains(label)) { event.setCancelled(true); - String customCommandDeniedMessage = CommandWhitelistBukkit.getCommandDeniedMessage(label); - if (!customCommandDeniedMessage.equals("")) { - audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + customCommandDeniedMessage)); - } else { - audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + config.command_denied)); - } + audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + CommandWhitelistBukkit.getCommandDeniedMessage(label))); return; } diff --git a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/protocollib/PacketCommandPreProcessListener.java b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/protocollib/PacketCommandPreProcessListener.java index 28e5c77..629fbec 100644 --- a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/protocollib/PacketCommandPreProcessListener.java +++ b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/listeners/protocollib/PacketCommandPreProcessListener.java @@ -41,15 +41,9 @@ public void onPacketReceiving(PacketEvent event) { BukkitAudiences audiences = CommandWhitelistBukkit.getAudiences(); if (!commands.contains(label)) { event.setCancelled(true); - String customCommandDeniedMessage = CommandWhitelistBukkit.getCommandDeniedMessage(label); - if (!customCommandDeniedMessage.equals("")) { - audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + customCommandDeniedMessage)); - } else { - audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + config.command_denied)); - } + audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + CommandWhitelistBukkit.getCommandDeniedMessage(label))); return; } - HashSet bannedSubCommands = CommandWhitelistBukkit.getSuggestions(player); for (String bannedSubCommand : bannedSubCommands) { if (string.toLowerCase().substring(1).startsWith(bannedSubCommand)) { From c5fdaa95f86462e7eb67dde9d2d7b0cacd349f61 Mon Sep 17 00:00:00 2001 From: YouHaveTrouble Date: Tue, 26 Oct 2021 14:06:36 +0200 Subject: [PATCH 6/8] waterfall impl --- .../waterfall/CommandWhitelistWaterfall.java | 16 ++++++++++++++++ .../listeners/BungeeChatEventListener.java | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java index bdb08ea..69fb13f 100644 --- a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java +++ b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java @@ -106,4 +106,20 @@ public static HashSet getSuggestions(ProxiedPlayer player) { } return suggestionList; } + + /** + * @return Command denied message. Will use custom if command exists in any group. + */ + public static String getCommandDeniedMessage(String command) { + String commandDeniedMessage = configCache.command_denied; + HashMap groups = configCache.getGroupList(); + for (CWGroup group : groups.values()) { + if (group.getCommands().contains(command)) { + if (group.getCommandDeniedMessage() == null) continue; + commandDeniedMessage = group.getCommandDeniedMessage(); + break; // get first message we find + } + } + return commandDeniedMessage; + } } diff --git a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/listeners/BungeeChatEventListener.java b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/listeners/BungeeChatEventListener.java index 02277d5..4ba380f 100644 --- a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/listeners/BungeeChatEventListener.java +++ b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/listeners/BungeeChatEventListener.java @@ -32,7 +32,7 @@ public void onChatEvent(net.md_5.bungee.api.event.ChatEvent event) { HashSet commands = CommandWhitelistWaterfall.getCommands(player); if (!commands.contains(label)) { event.setCancelled(true); - CommandWhitelistWaterfall.getAudiences().player(player).sendMessage(CWCommand.miniMessage.parse(configCache.prefix + configCache.command_denied)); + CommandWhitelistWaterfall.getAudiences().player(player).sendMessage(CWCommand.miniMessage.parse(configCache.prefix + CommandWhitelistWaterfall.getCommandDeniedMessage(label))); return; } From 930f73d60ac3a8339620069dfbb7ca033f93eed0 Mon Sep 17 00:00:00 2001 From: kforbro Date: Tue, 26 Oct 2021 15:54:39 +0300 Subject: [PATCH 7/8] add an additional condition --- .../commandwhitelist/bukkit/CommandWhitelistBukkit.java | 2 +- .../commandwhitelist/waterfall/CommandWhitelistWaterfall.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java index c93893a..a7beac1 100644 --- a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java +++ b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java @@ -137,7 +137,7 @@ public static String getCommandDeniedMessage(String command) { HashMap groups = configCache.getGroupList(); for (CWGroup group : groups.values()) { if (group.getCommands().contains(command)) { - if (group.getCommandDeniedMessage() == null) continue; + if (group.getCommandDeniedMessage() == null || group.getCommandDeniedMessage().equals("")) continue; commandDeniedMessage = group.getCommandDeniedMessage(); break; // get first message we find } diff --git a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java index 69fb13f..fcc7351 100644 --- a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java +++ b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java @@ -115,7 +115,7 @@ public static String getCommandDeniedMessage(String command) { HashMap groups = configCache.getGroupList(); for (CWGroup group : groups.values()) { if (group.getCommands().contains(command)) { - if (group.getCommandDeniedMessage() == null) continue; + if (group.getCommandDeniedMessage() == null || group.getCommandDeniedMessage().equals("")) continue; commandDeniedMessage = group.getCommandDeniedMessage(); break; // get first message we find } From a91af744fd994fe380ef3a2055126bdb8a6f6fe6 Mon Sep 17 00:00:00 2001 From: kforbro Date: Tue, 26 Oct 2021 16:42:59 +0300 Subject: [PATCH 8/8] use isEmpty() instead of equals("") --- .../commandwhitelist/bukkit/CommandWhitelistBukkit.java | 2 +- .../commandwhitelist/waterfall/CommandWhitelistWaterfall.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java index a7beac1..5dcaaf4 100644 --- a/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java +++ b/CommandWhitelistBukkit/src/main/java/eu/endermite/commandwhitelist/bukkit/CommandWhitelistBukkit.java @@ -137,7 +137,7 @@ public static String getCommandDeniedMessage(String command) { HashMap groups = configCache.getGroupList(); for (CWGroup group : groups.values()) { if (group.getCommands().contains(command)) { - if (group.getCommandDeniedMessage() == null || group.getCommandDeniedMessage().equals("")) continue; + if (group.getCommandDeniedMessage() == null || group.getCommandDeniedMessage().isEmpty()) continue; commandDeniedMessage = group.getCommandDeniedMessage(); break; // get first message we find } diff --git a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java index fcc7351..2633d60 100644 --- a/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java +++ b/CommandWhitelistWaterfall/src/main/java/eu/endermite/commandwhitelist/waterfall/CommandWhitelistWaterfall.java @@ -115,7 +115,7 @@ public static String getCommandDeniedMessage(String command) { HashMap groups = configCache.getGroupList(); for (CWGroup group : groups.values()) { if (group.getCommands().contains(command)) { - if (group.getCommandDeniedMessage() == null || group.getCommandDeniedMessage().equals("")) continue; + if (group.getCommandDeniedMessage() == null || group.getCommandDeniedMessage().isEmpty()) continue; commandDeniedMessage = group.getCommandDeniedMessage(); break; // get first message we find }