Custom command denied message for the group

This commit is contained in:
kforbro
2021-10-26 12:48:59 +03:00
parent 48fbd59a50
commit d43bde0750
6 changed files with 43 additions and 10 deletions
+1 -1
View File
@@ -34,7 +34,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
@@ -126,4 +126,19 @@ public class CommandWhitelistBukkit extends JavaPlugin {
}
return suggestionList;
}
/**
* @param command command
* @return custom command denied message
*/
public static String getCommandDeniedMessage(String command) {
String commandDeniedMessage = "";
HashMap<String, CWGroup> groups = configCache.getGroupList();
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
if (s.getValue().getCommands().contains(command)) {
commandDeniedMessage = s.getValue().getCustomCommandDeniedMessage();
}
}
return commandDeniedMessage;
}
}
@@ -24,7 +24,12 @@ public class PlayerCommandPreProcessListener implements Listener {
HashSet<String> 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;
}
@@ -12,6 +12,7 @@ import eu.endermite.commandwhitelist.common.CWPermission;
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 class PacketCommandPreProcessListener {
ConfigCache config = CommandWhitelistBukkit.getConfigCache();
String label = CommandUtil.getCommandLabel(string.toLowerCase());
HashSet<String> 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;
}
@@ -4,14 +4,15 @@ import java.util.*;
public class CWGroup {
private final String id, permission;
private final String id, permission, custom_command_denied_message;
private final HashSet<String> commands = new HashSet<>();
private final HashSet<String> subCommands = new HashSet<>();
public CWGroup(String id, Collection<String> commands, Collection<String> subCommands) {
public CWGroup(String id, Collection<String> commands, Collection<String> 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 class CWGroup {
return permission;
}
public HashSet<String> getCommands() {
return commands;
}
public HashSet<String> getCommands() { return commands; }
public String getCustomCommandDeniedMessage() { return custom_command_denied_message; }
public void addCommand(String command) {
commands.add(command);
@@ -51,9 +51,11 @@ public class ConfigCache {
exampleCommands.add("example");
List<String> 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.<group_name> 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.<group_name> 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 class ConfigCache {
List<String> 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 class ConfigCache {
}
List<String> 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) {