mirror of
https://github.com/YouHaveTrouble/CommandWhitelist.git
synced 2026-05-12 14:36:56 +00:00
basic velocity support
This commit is contained in:
@@ -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<String> getCommands(Player player) {
|
||||
public static List<String> getCommands(org.bukkit.entity.Player player) {
|
||||
List<String> commandList = new ArrayList<>();
|
||||
for (Map.Entry<String, List<String>> s : CommandWhitelist.getConfigCache().getPermList().entrySet()) {
|
||||
if (s.getKey().equalsIgnoreCase("default"))
|
||||
@@ -21,7 +20,7 @@ public class CommandsList {
|
||||
return commandList;
|
||||
}
|
||||
|
||||
public static List<String> getCommands(ProxiedPlayer player) {
|
||||
public static List<String> getCommands(net.md_5.bungee.api.connection.ProxiedPlayer player) {
|
||||
List<String> commandList = new ArrayList<>();
|
||||
for (Map.Entry<String, List<String>> s : CommandWhitelistBungee.getConfigCache().getPermList().entrySet()) {
|
||||
if (s.getKey().equalsIgnoreCase("default"))
|
||||
@@ -32,9 +31,19 @@ public class CommandsList {
|
||||
return commandList;
|
||||
}
|
||||
|
||||
public static List<String> getSuggestions(Player player) {
|
||||
List<String> suggestionList = new ArrayList<>();
|
||||
public static List<String> getCommands(com.velocitypowered.api.proxy.Player player) {
|
||||
List<String> commandList = new ArrayList<>();
|
||||
for (Map.Entry<String, List<String>> 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<String> getSuggestions(org.bukkit.entity.Player player) {
|
||||
List<String> suggestionList = new ArrayList<>();
|
||||
for (Map.Entry<String, List<String>> s : CommandWhitelist.getConfigCache().getPermSubList().entrySet()) {
|
||||
if (player.hasPermission("commandwhitelist.subcommands." + s.getKey()))
|
||||
continue;
|
||||
|
||||
@@ -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<String> 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<String> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<List<String>> suggestAsync(Invocation invocation) {
|
||||
CommandSource source = invocation.source();
|
||||
String[] args = invocation.arguments();
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
List<String> suggestions = new ArrayList<>();
|
||||
if (args.length == 1) {
|
||||
if (source.hasPermission("commandwhitelist.reload") && "reload".startsWith(args[0]))
|
||||
suggestions.add("reload");
|
||||
}
|
||||
return suggestions;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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<String, List<String>> 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<String, Object> set : groups.entrySet()) {
|
||||
this.permList.put(set.getKey(), (List<String>) set.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<String, List<String>> getPermList() {
|
||||
return permList;
|
||||
}
|
||||
public String getNoPermission() {
|
||||
return noPermission.replaceAll("&", "§");
|
||||
}
|
||||
public String getNoSubCommand() {return noSubCommand.replaceAll("&", "§");}
|
||||
public String getConfigReloaded() {return configReloaded.replaceAll("&", "§");}
|
||||
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
@@ -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
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
Reference in New Issue
Block a user