Skip to content

Commit

Permalink
Merge pull request #43 from kforbro/master
Browse files Browse the repository at this point in the history
custom command denied messages for groups & cleanup code
  • Loading branch information
YouHaveTrouble authored Oct 26, 2021
2 parents 48fbd59 + a91af74 commit 9e25507
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -126,4 +128,20 @@ public static HashSet<String> getSuggestions(org.bukkit.entity.Player 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<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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void PlayerCommandSendEvent(org.bukkit.event.player.PlayerCommandPreproce
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));
audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + CommandWhitelistBukkit.getCommandDeniedMessage(label)));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -37,12 +38,12 @@ public void onPacketReceiving(PacketEvent event) {
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));
audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + CommandWhitelistBukkit.getCommandDeniedMessage(label)));
return;
}

HashSet<String> bannedSubCommands = CommandWhitelistBukkit.getSuggestions(player);
for (String bannedSubCommand : bannedSubCommands) {
if (string.toLowerCase().substring(1).startsWith(bannedSubCommand)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package eu.endermite.commandwhitelist.common;

import org.jetbrains.annotations.Nullable;

import java.util.*;

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> 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.permission = "commandwhitelist.group." + id;
this.commands.addAll(commands);
this.commandDeniedMessage = custom_command_denied_message;
this.subCommands.addAll(subCommands);
}

Expand All @@ -27,6 +30,10 @@ public HashSet<String> getCommands() {
return commands;
}

public @Nullable String getCommandDeniedMessage() {
return commandDeniedMessage;
}

public void addCommand(String command) {
commands.add(command);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public String permission() {

/**
* Allows to check specific group permission
*
* @param configCache
* @param groupId
* @return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ public boolean reloadConfig() {
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: []\"");
}

Expand All @@ -75,7 +77,9 @@ public boolean reloadConfig() {
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");
Expand Down Expand Up @@ -130,7 +134,8 @@ public CWGroup loadCWGroup(String id, ConfigSection section) {
}

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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
Expand All @@ -74,7 +74,7 @@ public void onUserCommandSendEvent(PlayerAvailableCommandsEvent event) {
if (player.hasPermission(CWPermission.BYPASS.permission())) return;
HashSet<String> allowedCommands = CommandWhitelistVelocity.getCommands(player);
event.getRootNode().getChildren().removeIf((commandNode) ->
server.getCommandManager().hasCommand(commandNode.getName())
server.getCommandManager().hasCommand(commandNode.getName())
&& !allowedCommands.contains(commandNode.getName())
);
}
Expand Down
14 changes: 7 additions & 7 deletions CommandWhitelistVelocity/src/main/resources/velocity-plugin.json
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -53,6 +52,7 @@ public void onEnable() {
public static CommandWhitelistWaterfall getPlugin() {
return plugin;
}

public static ConfigCache getConfigCache() {
return configCache;
}
Expand Down Expand Up @@ -106,4 +106,20 @@ public static HashSet<String> 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<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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 <group> <command>"));
audiences.sender(sender).sendMessage(Component.text("/" + label + " add <group> <command>"));
return;
case REMOVE:
if (!sender.hasPermission(CWPermission.ADMIN.permission())) {
Expand All @@ -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 <group> <command>"));
audiences.sender(sender).sendMessage(Component.text("/" + label + " remove <group> <command>"));
return;
case HELP:
default:
Expand All @@ -84,6 +84,6 @@ public Iterable<String> onTabComplete(CommandSender sender, String[] args) {
for (Map.Entry<String, Command> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +32,7 @@ public void onChatEvent(net.md_5.bungee.api.event.ChatEvent event) {
HashSet<String> 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;
}

Expand Down

0 comments on commit 9e25507

Please sign in to comment.