mirror of
https://github.com/YouHaveTrouble/CommandWhitelist.git
synced 2026-05-11 22:16:57 +00:00
Merge pull request #43 from kforbro/master
custom command denied messages for groups & cleanup code
This commit is contained in:
+20
-2
@@ -1,7 +1,10 @@
|
|||||||
package eu.endermite.commandwhitelist.bukkit;
|
package eu.endermite.commandwhitelist.bukkit;
|
||||||
|
|
||||||
import eu.endermite.commandwhitelist.bukkit.command.MainCommandExecutor;
|
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.PacketCommandPreProcessListener;
|
||||||
import eu.endermite.commandwhitelist.bukkit.listeners.protocollib.PacketCommandSendListener;
|
import eu.endermite.commandwhitelist.bukkit.listeners.protocollib.PacketCommandSendListener;
|
||||||
import eu.endermite.commandwhitelist.common.CWGroup;
|
import eu.endermite.commandwhitelist.common.CWGroup;
|
||||||
@@ -20,7 +23,6 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class CommandWhitelistBukkit extends JavaPlugin {
|
public class CommandWhitelistBukkit extends JavaPlugin {
|
||||||
@@ -126,4 +128,20 @@ public class CommandWhitelistBukkit extends JavaPlugin {
|
|||||||
}
|
}
|
||||||
return suggestionList;
|
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<String, CWGroup> groups = configCache.getGroupList();
|
||||||
|
for (CWGroup group : groups.values()) {
|
||||||
|
if (group.getCommands().contains(command)) {
|
||||||
|
if (group.getCommandDeniedMessage() == null || group.getCommandDeniedMessage().isEmpty()) continue;
|
||||||
|
commandDeniedMessage = group.getCommandDeniedMessage();
|
||||||
|
break; // get first message we find
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return commandDeniedMessage;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -24,7 +24,7 @@ public class PlayerCommandPreProcessListener implements Listener {
|
|||||||
HashSet<String> commands = CommandWhitelistBukkit.getCommands(player);
|
HashSet<String> commands = CommandWhitelistBukkit.getCommands(player);
|
||||||
if (!commands.contains(label)) {
|
if (!commands.contains(label)) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-2
@@ -12,6 +12,7 @@ import eu.endermite.commandwhitelist.common.CWPermission;
|
|||||||
import eu.endermite.commandwhitelist.common.CommandUtil;
|
import eu.endermite.commandwhitelist.common.CommandUtil;
|
||||||
import eu.endermite.commandwhitelist.common.ConfigCache;
|
import eu.endermite.commandwhitelist.common.ConfigCache;
|
||||||
import eu.endermite.commandwhitelist.common.commands.CWCommand;
|
import eu.endermite.commandwhitelist.common.commands.CWCommand;
|
||||||
|
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
|
|
||||||
@@ -37,12 +38,12 @@ public class PacketCommandPreProcessListener {
|
|||||||
ConfigCache config = CommandWhitelistBukkit.getConfigCache();
|
ConfigCache config = CommandWhitelistBukkit.getConfigCache();
|
||||||
String label = CommandUtil.getCommandLabel(string.toLowerCase());
|
String label = CommandUtil.getCommandLabel(string.toLowerCase());
|
||||||
HashSet<String> commands = CommandWhitelistBukkit.getCommands(player);
|
HashSet<String> commands = CommandWhitelistBukkit.getCommands(player);
|
||||||
|
BukkitAudiences audiences = CommandWhitelistBukkit.getAudiences();
|
||||||
if (!commands.contains(label)) {
|
if (!commands.contains(label)) {
|
||||||
event.setCancelled(true);
|
event.setCancelled(true);
|
||||||
CommandWhitelistBukkit.getAudiences().player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + config.command_denied));
|
audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + CommandWhitelistBukkit.getCommandDeniedMessage(label)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
HashSet<String> bannedSubCommands = CommandWhitelistBukkit.getSuggestions(player);
|
HashSet<String> bannedSubCommands = CommandWhitelistBukkit.getSuggestions(player);
|
||||||
for (String bannedSubCommand : bannedSubCommands) {
|
for (String bannedSubCommand : bannedSubCommands) {
|
||||||
if (string.toLowerCase().substring(1).startsWith(bannedSubCommand)) {
|
if (string.toLowerCase().substring(1).startsWith(bannedSubCommand)) {
|
||||||
|
|||||||
+9
-2
@@ -1,17 +1,20 @@
|
|||||||
package eu.endermite.commandwhitelist.common;
|
package eu.endermite.commandwhitelist.common;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class CWGroup {
|
public class CWGroup {
|
||||||
|
|
||||||
private final String id, permission;
|
private final String id, permission, commandDeniedMessage;
|
||||||
private final HashSet<String> commands = new HashSet<>();
|
private final HashSet<String> commands = new HashSet<>();
|
||||||
private final HashSet<String> subCommands = 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.id = id;
|
||||||
this.permission = "commandwhitelist.group." + id;
|
this.permission = "commandwhitelist.group." + id;
|
||||||
this.commands.addAll(commands);
|
this.commands.addAll(commands);
|
||||||
|
this.commandDeniedMessage = custom_command_denied_message;
|
||||||
this.subCommands.addAll(subCommands);
|
this.subCommands.addAll(subCommands);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,6 +30,10 @@ public class CWGroup {
|
|||||||
return commands;
|
return commands;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public @Nullable String getCommandDeniedMessage() {
|
||||||
|
return commandDeniedMessage;
|
||||||
|
}
|
||||||
|
|
||||||
public void addCommand(String command) {
|
public void addCommand(String command) {
|
||||||
commands.add(command);
|
commands.add(command);
|
||||||
}
|
}
|
||||||
|
|||||||
+1
@@ -18,6 +18,7 @@ public enum CWPermission {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Allows to check specific group permission
|
* Allows to check specific group permission
|
||||||
|
*
|
||||||
* @param configCache
|
* @param configCache
|
||||||
* @param groupId
|
* @param groupId
|
||||||
* @return
|
* @return
|
||||||
|
|||||||
+7
-2
@@ -51,9 +51,11 @@ public class ConfigCache {
|
|||||||
exampleCommands.add("example");
|
exampleCommands.add("example");
|
||||||
List<String> exampleSubCommands = new ArrayList<>();
|
List<String> exampleSubCommands = new ArrayList<>();
|
||||||
exampleSubCommands.add("example of");
|
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.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.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: []\"");
|
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<>();
|
List<String> defaultSubcommands = new ArrayList<>();
|
||||||
defaultSubcommands.add("help about");
|
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");
|
prefix = config.getString("messages.prefix");
|
||||||
command_denied = config.getString("messages.command_denied");
|
command_denied = config.getString("messages.command_denied");
|
||||||
@@ -130,7 +134,8 @@ public class ConfigCache {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<String> subCommands = section.getStringList(id + ".subcommands");
|
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) {
|
public void saveCWGroup(String id, CWGroup group) {
|
||||||
|
|||||||
+17
-1
@@ -8,7 +8,6 @@ import eu.endermite.commandwhitelist.waterfall.listeners.BungeeChatEventListener
|
|||||||
import eu.endermite.commandwhitelist.waterfall.listeners.BungeeTabcompleteListener;
|
import eu.endermite.commandwhitelist.waterfall.listeners.BungeeTabcompleteListener;
|
||||||
import eu.endermite.commandwhitelist.waterfall.listeners.WaterfallDefineCommandsListener;
|
import eu.endermite.commandwhitelist.waterfall.listeners.WaterfallDefineCommandsListener;
|
||||||
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
|
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.ChatColor;
|
||||||
import net.md_5.bungee.api.CommandSender;
|
import net.md_5.bungee.api.CommandSender;
|
||||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||||
@@ -53,6 +52,7 @@ public final class CommandWhitelistWaterfall extends Plugin {
|
|||||||
public static CommandWhitelistWaterfall getPlugin() {
|
public static CommandWhitelistWaterfall getPlugin() {
|
||||||
return plugin;
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ConfigCache getConfigCache() {
|
public static ConfigCache getConfigCache() {
|
||||||
return configCache;
|
return configCache;
|
||||||
}
|
}
|
||||||
@@ -106,4 +106,20 @@ public final class CommandWhitelistWaterfall extends Plugin {
|
|||||||
}
|
}
|
||||||
return suggestionList;
|
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<String, CWGroup> groups = configCache.getGroupList();
|
||||||
|
for (CWGroup group : groups.values()) {
|
||||||
|
if (group.getCommands().contains(command)) {
|
||||||
|
if (group.getCommandDeniedMessage() == null || group.getCommandDeniedMessage().isEmpty()) continue;
|
||||||
|
commandDeniedMessage = group.getCommandDeniedMessage();
|
||||||
|
break; // get first message we find
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return commandDeniedMessage;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-2
@@ -6,7 +6,6 @@ import eu.endermite.commandwhitelist.common.ConfigCache;
|
|||||||
import eu.endermite.commandwhitelist.common.commands.CWCommand;
|
import eu.endermite.commandwhitelist.common.commands.CWCommand;
|
||||||
import eu.endermite.commandwhitelist.waterfall.CommandWhitelistWaterfall;
|
import eu.endermite.commandwhitelist.waterfall.CommandWhitelistWaterfall;
|
||||||
import net.kyori.adventure.platform.bungeecord.BungeeAudiences;
|
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.connection.ProxiedPlayer;
|
||||||
import net.md_5.bungee.api.plugin.Listener;
|
import net.md_5.bungee.api.plugin.Listener;
|
||||||
import net.md_5.bungee.event.EventHandler;
|
import net.md_5.bungee.event.EventHandler;
|
||||||
@@ -33,7 +32,7 @@ public class BungeeChatEventListener implements Listener {
|
|||||||
HashSet<String> commands = CommandWhitelistWaterfall.getCommands(player);
|
HashSet<String> commands = CommandWhitelistWaterfall.getCommands(player);
|
||||||
if (!commands.contains(label)) {
|
if (!commands.contains(label)) {
|
||||||
event.setCancelled(true);
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user