Skip to content

Commit

Permalink
Merge "Improved Velocity Module" #69 from 4drian3d/velocity
Browse files Browse the repository at this point in the history
Improved Velocity Module
  • Loading branch information
YouHaveTrouble authored Apr 29, 2023
2 parents dfa254f + c36ac05 commit ea54ae3
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import eu.endermite.commandwhitelist.common.CWGroup;
import eu.endermite.commandwhitelist.common.ConfigCache;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import java.util.ArrayList;
Expand Down Expand Up @@ -31,20 +32,20 @@ public static boolean removeFromWhitelist(ConfigCache configCache, String comman
}

public static Component helpComponent(String baseCommand, boolean showReloadCommand, boolean showAdminCommands) {
Component component = miniMessage.deserialize("<rainbow><bold>CommandWhitelist by YouHaveTrouble</bold>")
.append(Component.newline());
component = component.append(Component.text(baseCommand + " help").color(NamedTextColor.AQUA).append(Component.text(" - Displays this message").color(NamedTextColor.BLUE)));
final TextComponent.Builder builder = Component.text();
builder.append(miniMessage.deserialize("<rainbow><bold>CommandWhitelist by YouHaveTrouble</bold>"), Component.newline());
builder.append(Component.text(baseCommand + " help", NamedTextColor.AQUA), Component.text(" - Displays this message", NamedTextColor.BLUE));
if (showReloadCommand) {
component = component.append(Component.newline());
component = component.append(Component.text(baseCommand + " reload").color(NamedTextColor.AQUA).append(Component.text(" - Reloads plugin configuration").color(NamedTextColor.BLUE)));
builder.append(Component.newline());
builder.append(Component.text(baseCommand + " reload", NamedTextColor.AQUA), Component.text(" - Reloads plugin configuration", NamedTextColor.BLUE));
}
if (showAdminCommands) {
component = component.append(Component.newline());
component = component.append(Component.text(baseCommand + " add <group> <command>").color(NamedTextColor.AQUA).append(Component.text(" - Add a command to selected permission group").color(NamedTextColor.BLUE)));
component = component.append(Component.newline());
component = component.append(Component.text(baseCommand + " remove <group> <command>").color(NamedTextColor.AQUA).append(Component.text(" - Removes a command from selected permission group").color(NamedTextColor.BLUE)));
builder.append(Component.newline());
builder.append(Component.text(baseCommand + " add <group> <command>", NamedTextColor.AQUA), Component.text(" - Add a command to selected permission group", NamedTextColor.BLUE));
builder.append(Component.newline());
builder.append(Component.text(baseCommand + " remove <group> <command>", NamedTextColor.AQUA), Component.text(" - Removes a command from selected permission group", NamedTextColor.BLUE));
}
return component;
return builder.build();
}

public enum CommandType {
Expand Down
10 changes: 10 additions & 0 deletions CommandWhitelistVelocity/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@
<shadedPattern>eu.endermite.bstats</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<excludes>
<exclude>net.kyori:adventure-api</exclude>
<exclude>net.kyori:adventure-key</exclude>
<exclude>net.kyori:examination-api</exclude>
<exclude>net.kyori:examination-string</exclude>
<exclude>org.jetbrains:annotations</exclude>
<exclude>org.intellij.lang:annotations</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.endermite.commandwhitelist.velocity;

import com.velocitypowered.api.command.CommandMeta;
import com.google.inject.Injector;
import com.mojang.brigadier.Command;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.command.CommandExecuteEvent;
Expand All @@ -15,13 +16,11 @@
import eu.endermite.commandwhitelist.common.ConfigCache;
import eu.endermite.commandwhitelist.common.commands.CWCommand;
import eu.endermite.commandwhitelist.velocity.command.VelocityMainCommand;
import net.kyori.adventure.identity.Identity;
import org.bstats.charts.SimplePie;
import org.bstats.velocity.Metrics;
import org.slf4j.Logger;

import javax.inject.Inject;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -30,59 +29,65 @@

public class CommandWhitelistVelocity {

private static CommandWhitelistVelocity plugin;
private static ProxyServer server;
private static ConfigCache configCache;
private static Path folder;
private static Logger logger;
private final ProxyServer server;
private ConfigCache configCache;
private final Path folder;
private final Logger logger;
private final Metrics.Factory metricsFactory;
private final Injector injector;

@Inject
public CommandWhitelistVelocity(ProxyServer server, Logger logger, @DataDirectory final Path folder, Metrics.Factory metricsFactory) {
CommandWhitelistVelocity.server = server;
CommandWhitelistVelocity.folder = folder;
CommandWhitelistVelocity.plugin = this;
CommandWhitelistVelocity.logger = logger;
public CommandWhitelistVelocity(
ProxyServer server,
Logger logger,
@DataDirectory final Path folder,
Metrics.Factory metricsFactory,
Injector injector
) {
this.server = server;
this.folder = folder;
this.logger = logger;
this.metricsFactory = metricsFactory;

this.injector = injector;
}

private static void reloadConfig() {
private void reloadConfig() {
if (configCache == null)
configCache = new ConfigCache(new File(String.valueOf(folder), "config.yml"), false, logger);
configCache = new ConfigCache(folder.resolve("config.yml").toFile(), false, logger);
else
configCache.reloadConfig();
}

public static void reloadConfig(CommandSource source) {
server.getScheduler().buildTask(plugin, () -> {
public int reloadConfig(CommandSource source) {
server.getScheduler().buildTask(this, () -> {
reloadConfig();
source.sendMessage(Identity.nil(), CWCommand.miniMessage.deserialize(getConfigCache().prefix + getConfigCache().config_reloaded));
source.sendMessage(CWCommand.miniMessage.deserialize(getConfigCache().prefix + getConfigCache().config_reloaded));
}).schedule();
return Command.SINGLE_SUCCESS;
}

@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
reloadConfig();
CommandMeta commandMeta = server.getCommandManager().metaBuilder("vcw").build();
server.getCommandManager().register(commandMeta, new VelocityMainCommand());
injector.getInstance(VelocityMainCommand.class).register();
Metrics metrics = metricsFactory.make(this, 8704);
metrics.addCustomChart(new SimplePie("proxy", () -> "Velocity"));
}

@Subscribe
@SuppressWarnings("UnstableApiUsage")
public void onUserCommandSendEvent(PlayerAvailableCommandsEvent event) {
Player player = event.getPlayer();
if (player.hasPermission(CWPermission.BYPASS.permission())) return;
HashSet<String> allowedCommands = CommandWhitelistVelocity.getCommands(player);
HashSet<String> allowedCommands = getCommands(player);
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) {
public void onUserCommandExecuteEvent(CommandExecuteEvent event) {
if (!(event.getCommandSource() instanceof Player)) return;
Player player = (Player) event.getCommandSource();

Expand All @@ -91,25 +96,21 @@ public void onUserCommandExecuteEvent(com.velocitypowered.api.event.command.Comm
// Workaround for velocity executing "/ command" as valid command
String command = event.getCommand().trim();

HashSet<String> allowedCommands = CommandWhitelistVelocity.getCommands(player);
HashSet<String> allowedCommands = getCommands(player);
String label = CommandUtil.getCommandLabel(command);
if (server.getCommandManager().hasCommand(label) && !allowedCommands.contains(label))
event.setResult(CommandExecuteEvent.CommandResult.forwardToServer());
}

public static ConfigCache getConfigCache() {
public ConfigCache getConfigCache() {
return configCache;
}

public static Path getConfigPath() {
return folder;
}

/**
* @param player Velocity Player
* @return commands available to the player
*/
public static HashSet<String> getCommands(Player player) {
public HashSet<String> getCommands(Player player) {
HashMap<String, CWGroup> groups = configCache.getGroupList();
HashSet<String> commandList = new HashSet<>();
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
Expand All @@ -126,7 +127,7 @@ else if (player.hasPermission(group.getPermission()))
* @param player Velocity Player
* @return subcommands unavailable for the player
*/
public static HashSet<String> getSuggestions(Player player, HashMap<String, CWGroup> groups) {
public HashSet<String> getSuggestions(Player player, HashMap<String, CWGroup> groups) {
HashSet<String> suggestionList = new HashSet<>();
for (Map.Entry<String, CWGroup> s : groups.entrySet()) {
if (s.getKey().equalsIgnoreCase("default"))
Expand All @@ -137,7 +138,7 @@ public static HashSet<String> getSuggestions(Player player, HashMap<String, CWGr
return suggestionList;
}

public static ArrayList<String> getServerCommands() {
public ArrayList<String> getServerCommands() {
return new ArrayList<>(server.getCommandManager().getAliases());
}

Expand Down
Loading

0 comments on commit ea54ae3

Please sign in to comment.