diff --git a/pom.xml b/pom.xml index eaf9975..75c48db 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ eu.endermite CommandWhitelist - 1.5.2 + 1.6.0-BETA jar CommandWhitelist @@ -74,6 +74,10 @@ papermc https://papermc.io/repo/repository/maven-public/ + + velocity + https://repo.velocitypowered.com/snapshots/ + @@ -95,6 +99,12 @@ 1.16-R0.4-SNAPSHOT provided + + com.velocitypowered + velocity-api + 1.1.0-SNAPSHOT + provided + com.comphenix.protocol ProtocolLib diff --git a/src/main/java/eu/endermite/commandwhitelist/api/CommandsList.java b/src/main/java/eu/endermite/commandwhitelist/api/CommandsList.java index 2cdc046..8cd5b06 100644 --- a/src/main/java/eu/endermite/commandwhitelist/api/CommandsList.java +++ b/src/main/java/eu/endermite/commandwhitelist/api/CommandsList.java @@ -2,15 +2,14 @@ package eu.endermite.commandwhitelist.api; import eu.endermite.commandwhitelist.bungee.CommandWhitelistBungee; import eu.endermite.commandwhitelist.spigot.CommandWhitelist; -import net.md_5.bungee.api.connection.ProxiedPlayer; -import org.bukkit.entity.Player; +import eu.endermite.commandwhitelist.velocity.CommandWhitelistVelocity; import java.util.ArrayList; import java.util.List; import java.util.Map; public class CommandsList { - public static List getCommands(Player player) { + public static List getCommands(org.bukkit.entity.Player player) { List commandList = new ArrayList<>(); for (Map.Entry> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) { if (s.getKey().equalsIgnoreCase("default")) @@ -21,7 +20,7 @@ public class CommandsList { return commandList; } - public static List getCommands(ProxiedPlayer player) { + public static List getCommands(net.md_5.bungee.api.connection.ProxiedPlayer player) { List commandList = new ArrayList<>(); for (Map.Entry> s : CommandWhitelistBungee.getConfigCache().getPermList().entrySet()) { if (s.getKey().equalsIgnoreCase("default")) @@ -32,9 +31,19 @@ public class CommandsList { return commandList; } - public static List getSuggestions(Player player) { - List suggestionList = new ArrayList<>(); + public static List getCommands(com.velocitypowered.api.proxy.Player player) { + List commandList = new ArrayList<>(); + for (Map.Entry> s : CommandWhitelistVelocity.getConfigCache().getPermList().entrySet()) { + if (s.getKey().equalsIgnoreCase("default")) + commandList.addAll(s.getValue()); + else if (player.hasPermission("commandwhitelist.commands." + s.getKey())) + commandList.addAll(s.getValue()); + } + return commandList; + } + public static List getSuggestions(org.bukkit.entity.Player player) { + List suggestionList = new ArrayList<>(); for (Map.Entry> s : CommandWhitelist.getConfigCache().getPermSubList().entrySet()) { if (player.hasPermission("commandwhitelist.subcommands." + s.getKey())) continue; diff --git a/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java b/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java new file mode 100644 index 0000000..c5df832 --- /dev/null +++ b/src/main/java/eu/endermite/commandwhitelist/velocity/CommandWhitelistVelocity.java @@ -0,0 +1,112 @@ +package eu.endermite.commandwhitelist.velocity; + +import com.moandjiezana.toml.Toml; +import com.velocitypowered.api.command.CommandMeta; +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.event.Subscribe; +import com.velocitypowered.api.event.command.CommandExecuteEvent; +import com.velocitypowered.api.event.command.PlayerAvailableCommandsEvent; +import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; +import com.velocitypowered.api.plugin.Plugin; +import com.velocitypowered.api.plugin.annotation.DataDirectory; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.ProxyServer; +import eu.endermite.commandwhitelist.api.CommandsList; +import eu.endermite.commandwhitelist.velocity.command.VelocityMainCommand; +import eu.endermite.commandwhitelist.velocity.config.VelocityConfigCache; +import net.kyori.adventure.identity.Identity; +import net.kyori.adventure.text.Component; +import org.slf4j.Logger; +import javax.inject.Inject; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +public class CommandWhitelistVelocity { + + private static CommandWhitelistVelocity plugin; + private static ProxyServer server; + private static VelocityConfigCache configCache; + private static Path folder; + + @Inject + public CommandWhitelistVelocity(ProxyServer server, Logger logger, @DataDirectory final Path folder) { + CommandWhitelistVelocity.server = server; + CommandWhitelistVelocity.folder = folder; + CommandWhitelistVelocity.plugin = this; + } + + private static Toml loadConfig(Path path) { + File folder = path.toFile(); + File file = new File(folder, "config.toml"); + if (!file.getParentFile().exists()) + file.getParentFile().mkdirs(); + + if (!file.exists()) { + try (InputStream input = CommandWhitelistVelocity.class.getResourceAsStream("/" + file.getName())) { + if (input != null) { + Files.copy(input, file.toPath()); + } else { + file.createNewFile(); + } + } catch (IOException exception) { + exception.printStackTrace(); + return null; + } + } + return new Toml().read(file); + } + + private static void reloadConfig() { + configCache = new VelocityConfigCache(loadConfig(folder)); + } + + public static void reloadConfig(CommandSource source) { + server.getScheduler().buildTask(plugin, () -> { + reloadConfig(); + source.sendMessage(Identity.nil(), Component.text(getConfigCache().getConfigReloaded())); + }).schedule(); + } + + @Subscribe + public void onProxyInitialization(ProxyInitializeEvent event) { + reloadConfig(); + CommandMeta commandMeta = server.getCommandManager().metaBuilder("vcw").build(); + server.getCommandManager().register(commandMeta, new VelocityMainCommand()); + } + + @Subscribe + public void onUserCommandSendEvent(PlayerAvailableCommandsEvent event) { + if (event.getPlayer().hasPermission("commandwhitelist.bypass")) + return; + List allowedCommands = CommandsList.getCommands(event.getPlayer()); + event.getRootNode().getChildren().removeIf((commandNode) -> + server.getCommandManager().hasCommand(commandNode.getName()) + && !allowedCommands.contains(commandNode.getName()) + ); + } + + @Subscribe + public void onUserCommandExecuteEvent(com.velocitypowered.api.event.command.CommandExecuteEvent event) { + if (!(event.getCommandSource() instanceof Player)) + return; + Player player = (Player) event.getCommandSource(); + + if (player.hasPermission("commandwhitelist.bypass")) + return; + + List allowedCommands = CommandsList.getCommands(player); + String command = event.getCommand().split(" ")[0]; + if (server.getCommandManager().hasCommand(command) + && !allowedCommands.contains(command)) + event.setResult(CommandExecuteEvent.CommandResult.forwardToServer()); + } + + public static VelocityConfigCache getConfigCache() { + return configCache; + } + +} diff --git a/src/main/java/eu/endermite/commandwhitelist/velocity/command/VelocityMainCommand.java b/src/main/java/eu/endermite/commandwhitelist/velocity/command/VelocityMainCommand.java new file mode 100644 index 0000000..2a71643 --- /dev/null +++ b/src/main/java/eu/endermite/commandwhitelist/velocity/command/VelocityMainCommand.java @@ -0,0 +1,48 @@ +package eu.endermite.commandwhitelist.velocity.command; + +import com.velocitypowered.api.command.CommandSource; +import com.velocitypowered.api.command.SimpleCommand; +import eu.endermite.commandwhitelist.velocity.CommandWhitelistVelocity; +import net.kyori.adventure.text.Component; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CompletableFuture; + +public class VelocityMainCommand implements SimpleCommand { + + @Override + public void execute(final Invocation invocation) { + CommandSource source = invocation.source(); + String[] args = invocation.arguments(); + if (args.length > 0) { + if (args.length == 1 && args[0].equalsIgnoreCase("reload")) { + if (source.hasPermission("commandwhitelist.reload")) { + CommandWhitelistVelocity.reloadConfig(source); + } else { + source.sendMessage(Component.text(CommandWhitelistVelocity.getConfigCache().getNoPermission())); + } + } + } else { + source.sendMessage(Component.text("&bCommand Whitelist by YouHaveTrouble".replaceAll("&", "§"))); + if (source.hasPermission("commandwhitelist.reload")) { + source.sendMessage(Component.text("&9/vcw reload &b- Reload velocity plugin configuration".replaceAll("&", "§"))); + } + } + } + + @Override + public CompletableFuture> suggestAsync(Invocation invocation) { + CommandSource source = invocation.source(); + String[] args = invocation.arguments(); + return CompletableFuture.supplyAsync(() -> { + List suggestions = new ArrayList<>(); + if (args.length == 1) { + if (source.hasPermission("commandwhitelist.reload") && "reload".startsWith(args[0])) + suggestions.add("reload"); + } + return suggestions; + }); + } +} diff --git a/src/main/java/eu/endermite/commandwhitelist/velocity/config/VelocityConfigCache.java b/src/main/java/eu/endermite/commandwhitelist/velocity/config/VelocityConfigCache.java new file mode 100644 index 0000000..d26d585 --- /dev/null +++ b/src/main/java/eu/endermite/commandwhitelist/velocity/config/VelocityConfigCache.java @@ -0,0 +1,38 @@ +package eu.endermite.commandwhitelist.velocity.config; + +import com.moandjiezana.toml.Toml; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class VelocityConfigCache { + + private HashMap> permList = new HashMap<>(); + private final String noPermission, noSubCommand, configReloaded; + + + public VelocityConfigCache(Toml config) { + + Toml messages = config.getTable("messages"); + noPermission = messages.getString("no-permission", "&cYou don't have permission to do this."); + noSubCommand = messages.getString("no-such-subcommand", "&cNo subcommand by that name."); + configReloaded = messages.getString("config-reloaded", "&eConfiguration reloaded."); + + Toml groups = config.getTable("commands"); + + for (Map.Entry set : groups.entrySet()) { + this.permList.put(set.getKey(), (List) set.getValue()); + } + } + + public HashMap> getPermList() { + return permList; + } + public String getNoPermission() { + return noPermission.replaceAll("&", "§"); + } + public String getNoSubCommand() {return noSubCommand.replaceAll("&", "§");} + public String getConfigReloaded() {return configReloaded.replaceAll("&", "§");} + + +} diff --git a/src/main/resources/bungeeconfig.yml b/src/main/resources/bungeeconfig.yml index 8c26a47..3473f40 100644 --- a/src/main/resources/bungeeconfig.yml +++ b/src/main/resources/bungeeconfig.yml @@ -13,9 +13,7 @@ commands: # Permissions that control what commands players can use # you can add unlimited amount of whitelists - # commandwhitelist.commands.default - # this will NOT be automatically given to players by default! - # you have to give this permission to default droup by yourself as there is no automatic way to do it for a plugin + # this will be automatically given to players by default default: - glist - server diff --git a/src/main/resources/config.toml b/src/main/resources/config.toml new file mode 100644 index 0000000..8d43452 --- /dev/null +++ b/src/main/resources/config.toml @@ -0,0 +1,24 @@ +# This is velocity version of the config. +[messages] +prefix="CommandWhitelist > " +command-denied="No such command." +subcommand-denied="You cannot use this subcommand" +no-permission="&cYou don't have permission to do this." +no-such-subcommand="&cNo subcommand by that name." +config-reloaded="&eConfiguration reloaded." +added-to-whitelist="&eWhitelisted command &6%s &efor permission &6%s" +removed-from-whitelist="&eRemoved command &6%s &efrom permission &6%s" +group-doesnt-exist="&cGroup doesn't exist or error occured" + +# Permissions that control what commands players can use +# you can add unlimited amount of whitelists +[commands] +# this will be automatically given to players by default +default= [ + "velocity", + "server", +] +# commandwhitelist.commands.example +example= [ + "example" +] \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b5d02df..cc3db0c 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -13,7 +13,6 @@ commands: # Permissions that control what commands players can use # you can add unlimited amount of whitelists - # commandwhitelist.commands.default # this will be automatically given to players by default default: - help diff --git a/src/main/resources/velocity-plugin.json b/src/main/resources/velocity-plugin.json new file mode 100644 index 0000000..f494ceb --- /dev/null +++ b/src/main/resources/velocity-plugin.json @@ -0,0 +1,9 @@ +{ + "id":"commandwhitelist", + "name":"CommandWhitelist", + "version":"${project.version}", + "description":"Control what commands players can use", + "authors":["YouHaveTrouble"], + "dependencies":[], + "main":"eu.endermite.commandwhitelist.velocity.CommandWhitelistVelocity" +} \ No newline at end of file