Compare commits

..

13 Commits

Author SHA1 Message Date
YouHaveTrouble 568e7a5ca4 check if player is temporary 2021-12-24 17:38:57 +01:00
YouHaveTrouble 7cb94cce4b small fixes and missing author for waterfall 2021-12-24 17:38:42 +01:00
YouHaveTrouble 7f6315b6b5 bump version 2021-11-25 02:06:48 +01:00
YouHaveTrouble bbc0e44660 fix errors with spigot specifically.
use paper folks.
2021-11-25 02:03:20 +01:00
YouHaveTrouble 9e255076b0 Merge pull request #43 from kforbro/master
custom command denied messages for groups & cleanup code
2021-10-26 15:48:22 +02:00
kforbro a91af744fd use isEmpty() instead of equals("") 2021-10-26 16:42:59 +03:00
kforbro 930f73d60a add an additional condition 2021-10-26 15:54:39 +03:00
YouHaveTrouble c5fdaa95f8 waterfall impl 2021-10-26 14:06:36 +02:00
YouHaveTrouble 87fc3d7da3 default message in the method itself
so we don't have to check for empty string every time we want to use it
2021-10-26 14:05:59 +02:00
YouHaveTrouble 0cc2633604 this can be null 2021-10-26 14:04:21 +02:00
YouHaveTrouble d6909b4f25 no touchy maven pls 2021-10-26 14:03:51 +02:00
kforbro cd3d527119 cleanup code 2021-10-26 12:52:40 +03:00
kforbro d43bde0750 Custom command denied message for the group 2021-10-26 12:48:59 +03:00
21 changed files with 109 additions and 50 deletions
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>eu.endermite.commandwhitelist</groupId>
<artifactId>CommandWhitelist</artifactId>
<version>2.2.1</version>
<version>2.2.3</version>
</parent>
<artifactId>Bukkit</artifactId>
@@ -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 org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
public class CommandWhitelistBukkit extends JavaPlugin {
@@ -67,8 +69,13 @@ public class CommandWhitelistBukkit extends JavaPlugin {
private void reloadPluginConfig() {
File configFile = new File("plugins/CommandWhitelist/config.yml");
if (configCache == null)
configCache = new ConfigCache(configFile, true, getSLF4JLogger());
if (configCache == null) {
try {
configCache = new ConfigCache(configFile, true, getSLF4JLogger());
} catch (NoSuchMethodError e) {
configCache = new ConfigCache(configFile, true, null);
}
}
else
configCache.reloadConfig();
}
@@ -76,9 +83,11 @@ public class CommandWhitelistBukkit extends JavaPlugin {
public void reloadPluginConfig(CommandSender sender) {
getServer().getScheduler().runTaskAsynchronously(this, () -> {
reloadPluginConfig();
for (Player p : Bukkit.getOnlinePlayers()) {
p.updateCommands();
}
try {
for (Player p : Bukkit.getOnlinePlayers()) {
p.updateCommands();
}
} catch (Exception ignored) {}
audiences.sender(sender).sendMessage(CWCommand.miniMessage.parse(configCache.prefix + configCache.config_reloaded));
});
}
@@ -126,4 +135,20 @@ public class CommandWhitelistBukkit extends JavaPlugin {
}
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;
}
}
@@ -24,7 +24,7 @@ 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));
audiences.player(player).sendMessage(CWCommand.miniMessage.parse(config.prefix + CommandWhitelistBukkit.getCommandDeniedMessage(label)));
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,12 +38,12 @@ 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));
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)) {
@@ -27,8 +27,7 @@ public class PacketCommandSendListener {
@Override
public void onPacketSending(PacketEvent event) {
Player player = event.getPlayer();
if (player.hasPermission(CWPermission.BYPASS.permission())) return;
if (!event.isPlayerTemporary() && player.hasPermission(CWPermission.BYPASS.permission())) return;
HashSet<String> commandList = CommandWhitelistBukkit.getCommands(player);
PacketContainer packet = event.getPacket();
RootCommandNode<?> node = (RootCommandNode<?>) packet.getModifier().getValues().get(0);
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>eu.endermite.commandwhitelist</groupId>
<artifactId>CommandWhitelist</artifactId>
<version>2.2.1</version>
<version>2.2.3</version>
</parent>
<artifactId>Common</artifactId>
@@ -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);
}
@@ -27,6 +30,10 @@ public class CWGroup {
return commands;
}
public @Nullable String getCommandDeniedMessage() {
return commandDeniedMessage;
}
public void addCommand(String command) {
commands.add(command);
}
@@ -18,6 +18,7 @@ public enum CWPermission {
/**
* Allows to check specific group permission
*
* @param configCache
* @param groupId
* @return
@@ -17,15 +17,17 @@ public class CommandUtil {
public static List<String> filterSuggestions(String buffer, Collection<String> suggestions, Collection<String> blockedSubCommands) {
if (buffer.startsWith("/"))
buffer = buffer.substring(1);
List<String> suggestionsList = new ArrayList<>(suggestions);
if (suggestions.isEmpty() || blockedSubCommands.isEmpty()) return suggestionsList;
for (String s : blockedSubCommands) {
String slast = getLastArgument(s);
String scommand = cutLastArgument(s);
if (buffer.startsWith(scommand)) {
while (suggestions.contains(slast))
suggestions.remove(slast);
String slast = getLastArgument(s);
while (suggestionsList.contains(slast))
suggestionsList.remove(slast);
}
}
return new ArrayList<>(suggestions);
return suggestionsList;
}
/**
@@ -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");
@@ -128,9 +132,12 @@ public class ConfigCache {
if (commands.contains(cmd)) continue;
commands.add(cmd);
}
List<String> subCommands = section.getStringList(id + ".subcommands");
return new CWGroup(id, commands, subCommands);
List<String> subCommands = new ArrayList<>();
for (String subCmd : section.getStringList(id + ".subcommands")) {
subCommands.add(String.valueOf(subCmd));
}
String customCommandDeniedMessage = section.getString(id + ".custom_command_denied_message");
return new CWGroup(id, commands, subCommands, customCommandDeniedMessage);
}
public void saveCWGroup(String id, CWGroup group) {
@@ -143,6 +150,10 @@ public class ConfigCache {
}
private void warn(String log) {
if (logger == null) {
System.out.println("WARNING: "+log);
return;
}
if (logger instanceof org.slf4j.Logger) {
((org.slf4j.Logger) logger).warn(log);
return;
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>eu.endermite.commandwhitelist</groupId>
<artifactId>CommandWhitelist</artifactId>
<version>2.2.1</version>
<version>2.2.3</version>
</parent>
<artifactId>Velocity</artifactId>
@@ -55,7 +55,7 @@ public class CommandWhitelistVelocity {
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 class CommandWhitelistVelocity {
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 class CommandWhitelistVelocity {
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())
);
}
@@ -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"
}
+1 -1
View File
@@ -6,7 +6,7 @@
<parent>
<groupId>eu.endermite.commandwhitelist</groupId>
<artifactId>CommandWhitelist</artifactId>
<version>2.2.1</version>
<version>2.2.3</version>
</parent>
<artifactId>Waterfall</artifactId>
@@ -8,7 +8,6 @@ import eu.endermite.commandwhitelist.waterfall.listeners.BungeeChatEventListener
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 final class CommandWhitelistWaterfall extends Plugin {
public static CommandWhitelistWaterfall getPlugin() {
return plugin;
}
public static ConfigCache getConfigCache() {
return configCache;
}
@@ -106,4 +106,20 @@ public final class CommandWhitelistWaterfall extends Plugin {
}
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;
}
}
@@ -52,7 +52,7 @@ public class BungeeMainCommand extends Command implements TabExecutor {
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())) {
@@ -65,7 +65,7 @@ public class BungeeMainCommand extends Command implements TabExecutor {
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:
@@ -84,6 +84,6 @@ public class BungeeMainCommand extends Command implements TabExecutor {
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()));
}
}
@@ -6,7 +6,6 @@ import eu.endermite.commandwhitelist.common.ConfigCache;
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;
@@ -33,7 +32,7 @@ public class BungeeChatEventListener implements Listener {
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;
}
@@ -13,15 +13,13 @@ public class BungeeTabcompleteListener implements Listener {
public void onTabcomplete(net.md_5.bungee.api.event.TabCompleteEvent event) {
if (!(event.getReceiver() instanceof ProxiedPlayer)) return;
ProxiedPlayer player = (ProxiedPlayer) event.getReceiver();
if (event.getSuggestions().isEmpty()) return;
if (player.hasPermission(CWPermission.BYPASS.permission())) return;
CommandUtil.filterSuggestions(
event.getCursor(),
event.getSuggestions(),
CommandWhitelistWaterfall.getSuggestions(player)
);
}
}
@@ -15,8 +15,7 @@ public class WaterfallDefineCommandsListener implements Listener {
public void onProxyDefineCommandsEvent(io.github.waterfallmc.waterfall.event.ProxyDefineCommandsEvent event) {
if (event.getReceiver() instanceof ProxiedPlayer) {
ProxiedPlayer player = (ProxiedPlayer) event.getReceiver();
if (player.hasPermission(CWPermission.BYPASS.permission()))
return;
if (player.hasPermission(CWPermission.BYPASS.permission())) return;
HashMap<String, Command> commandHashMap = new HashMap<>();
CommandWhitelistWaterfall.getCommands(player).forEach(cmdName ->
CommandWhitelistWaterfall.getPlugin().getProxy().getPluginManager().getCommands()
@@ -1,4 +1,5 @@
name: CommandWhitelist
author: YouHaveTrouble
version: ${project.version}
main: eu.endermite.commandwhitelist.waterfall.CommandWhitelistWaterfall
description: You decide what commands players can use or tab complete on your server!
+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>eu.endermite.commandwhitelist</groupId>
<artifactId>CommandWhitelist</artifactId>
<version>2.2.1</version>
<version>2.2.3</version>
<modules>
<module>CommandWhitelistCommon</module>
<module>CommandWhitelistBukkit</module>