Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Velocity Support #14

Merged
merged 5 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Velocity/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>me.fixeddev</groupId>
<artifactId>commandflow</artifactId>
<version>0.5.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<version>${version.velocity}</version>
<artifactId>commandflow-velocity</artifactId>

<repositories>
<repository>
<id>velocity</id>
<url>https://nexus.velocitypowered.com/snapshots/</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>me.fixeddev</groupId>
<artifactId>commandflow-universal</artifactId>
<version>${version.universal}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.velocitypowered</groupId>
<artifactId>velocity-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>text-serializer-gson</artifactId>
<version>3.0.0-stripped</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package me.fixeddev.commandflow.velocity;

import net.kyori.adventure.text.Component;

public class MessageUtils {

public static Component kyoriToVelocityKyori(net.kyori.text.Component component) {
net.kyori.text.serializer.gson.GsonComponentSerializer componentSerializer = net.kyori.text.serializer.gson.GsonComponentSerializer.INSTANCE;

String serializedComponent = componentSerializer.serialize(component);

return net.kyori.adventure.text.serializer.gson.GsonComponentSerializer.gson().deserialize(serializedComponent);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package me.fixeddev.commandflow.velocity;

import com.velocitypowered.api.command.CommandSource;
import me.fixeddev.commandflow.Authorizer;
import me.fixeddev.commandflow.Namespace;

public class VelocityAuthorizer implements Authorizer {

@Override
public boolean isAuthorized(Namespace namespace, String permission) {
if (permission.isEmpty()) {
return true;
}

CommandSource sender = namespace.getObject(CommandSource.class, VelocityCommandManager.SENDER_NAMESPACE);
return sender.hasPermission(permission);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package me.fixeddev.commandflow.velocity;

import com.velocitypowered.api.proxy.ProxyServer;
import me.fixeddev.commandflow.CommandContext;
import me.fixeddev.commandflow.CommandManager;
import me.fixeddev.commandflow.Namespace;
import me.fixeddev.commandflow.SimpleCommandManager;
import me.fixeddev.commandflow.Authorizer;
import me.fixeddev.commandflow.ParseResult;
import me.fixeddev.commandflow.command.Command;
import me.fixeddev.commandflow.exception.CommandException;
import me.fixeddev.commandflow.executor.Executor;
import me.fixeddev.commandflow.input.InputTokenizer;
import me.fixeddev.commandflow.translator.Translator;
import me.fixeddev.commandflow.usage.UsageBuilder;
import net.kyori.text.serializer.legacy.LegacyComponentSerializer;

import java.util.*;

public class VelocityCommandManager implements CommandManager {

public static final String SENDER_NAMESPACE = "SENDER";

protected final ProxyServer proxyServer;
protected final CommandManager commandManager;
protected final Object plugin;

protected final Map<String, VelocityCommandWrapper> wrapperMap;

public VelocityCommandManager(ProxyServer proxyServer, Object plugin) {
this(proxyServer, new SimpleCommandManager(), plugin);
}

public VelocityCommandManager(ProxyServer proxyServer, CommandManager commandManager, Object plugin) {
this.proxyServer = proxyServer;
this.commandManager = commandManager;
this.plugin = plugin;

wrapperMap = new HashMap<>();

setAuthorizer(new VelocityAuthorizer());
getTranslator().setProvider(new VelocityDefaultTranslationProvider());
getTranslator().setConverterFunction(LegacyComponentSerializer.INSTANCE::deserialize);
}

@Override
public void registerCommand(Command command) {
commandManager.registerCommand(command);

VelocityCommandWrapper velocityCommandWrapper = new VelocityCommandWrapper(command, commandManager, getTranslator());
wrapperMap.put(command.getName(), velocityCommandWrapper);

proxyServer.getCommandManager().register(command.getName(), velocityCommandWrapper, command.getAliases().toArray(new String[0]));
}

@Override
public void registerCommands(List<Command> commandList) {
for (Command command : commandList) {
registerCommand(command);
}
}

@Override
public void unregisterCommand(Command command) {
commandManager.unregisterCommand(command);

VelocityCommandWrapper velocityCommandWrapper = wrapperMap.get(command.getName());
if (velocityCommandWrapper != null) {
proxyServer.getCommandManager().unregister(command.getName());
}
}

@Override
public void unregisterCommands(List<Command> commands) {
commandManager.unregisterCommands(commands);
}

@Override
public void unregisterAll() {
commandManager.unregisterAll();
}

@Override
public Set<Command> getCommands() {
return commandManager.getCommands();
}

@Override
public boolean exists(String commandName) {
return commandManager.exists(commandName);
}

@Override
public Authorizer getAuthorizer() {
return commandManager.getAuthorizer();
}

@Override
public void setAuthorizer(Authorizer authorizer) {
commandManager.setAuthorizer(authorizer);
}

@Override
public InputTokenizer getInputTokenizer() {
return commandManager.getInputTokenizer();
}

@Override
public void setInputTokenizer(InputTokenizer tokenizer) {
commandManager.setInputTokenizer(tokenizer);
}

@Override
public Executor getExecutor() {
return commandManager.getExecutor();
}

@Override
public void setExecutor(Executor executor) {
commandManager.setExecutor(executor);
}

@Override
public Translator getTranslator() {
return commandManager.getTranslator();
}

@Override
public void setTranslator(Translator translator) {
commandManager.setTranslator(translator);
}

@Override
public UsageBuilder getUsageBuilder() {
return commandManager.getUsageBuilder();
}

@Override
public void setUsageBuilder(UsageBuilder usageBuilder) {
commandManager.setUsageBuilder(usageBuilder);
}

@Override
public Optional<Command> getCommand(String commandName) {
return commandManager.getCommand(commandName);
}

@Override
public boolean execute(Namespace accessor, List<String> arguments) throws CommandException {
return commandManager.execute(accessor, arguments);
}

@Override
public List<String> getSuggestions(Namespace accessor, List<String> arguments) {
return commandManager.getSuggestions(accessor, arguments);
}

@Override
public boolean execute(Namespace accessor, String line) throws CommandException {
return commandManager.execute(accessor, line);
}

@Override
public List<String> getSuggestions(Namespace accessor, String line) {
return commandManager.getSuggestions(accessor, line);
}

@Override
public boolean execute(CommandContext commandContext) throws CommandException {
return commandManager.execute(commandContext);
}

@Override
public ParseResult parse(Namespace accessor, List<String> arguments) throws CommandException {
return commandManager.parse(accessor, arguments);
}

@Override
public ParseResult parse(Namespace accessor, String line) throws CommandException {
return commandManager.parse(accessor, line);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package me.fixeddev.commandflow.velocity;

import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import me.fixeddev.commandflow.CommandManager;
import me.fixeddev.commandflow.Namespace;
import me.fixeddev.commandflow.NamespaceImpl;
import me.fixeddev.commandflow.command.Command;
import me.fixeddev.commandflow.exception.ArgumentParseException;
import me.fixeddev.commandflow.exception.CommandException;
import me.fixeddev.commandflow.exception.CommandUsage;
import me.fixeddev.commandflow.exception.InvalidSubCommandException;
import me.fixeddev.commandflow.exception.NoMoreArgumentsException;
import me.fixeddev.commandflow.exception.NoPermissionsException;
import me.fixeddev.commandflow.translator.Translator;
import net.kyori.text.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class VelocityCommandWrapper implements SimpleCommand {

protected final CommandManager commandManager;
protected final Translator translator;

protected final String[] aliases;
protected final String permission;
protected final String command;

public VelocityCommandWrapper(Command command, CommandManager commandManager,
Translator translator) {

this.command = command.getName();
this.commandManager = commandManager;
this.translator = translator;

this.aliases = command.getAliases().toArray(new String[0]);
this.permission = command.getPermission();
}


@Override
public void execute(Invocation invocation) {
CommandSource commandSource = invocation.source();
String[] args = invocation.arguments();

List<String> argumentLine = new ArrayList<>();
argumentLine.add(this.command);
argumentLine.addAll(Arrays.asList(args));

Namespace namespace = new NamespaceImpl();
namespace.setObject(CommandSource.class, VelocityCommandManager.SENDER_NAMESPACE, commandSource);


try {
commandManager.execute(namespace, argumentLine);
} catch (CommandUsage e) {
CommandException exceptionToSend = e;
if (e.getCause() instanceof ArgumentParseException) {
exceptionToSend = (ArgumentParseException) e.getCause();
}

sendMessageToSender(exceptionToSend, commandSource, namespace);

} catch (InvalidSubCommandException e) {
sendMessageToSender(e, commandSource, namespace);

throw new CommandException("An internal parse exception occurred while executing the command " + this.command, e);
} catch (ArgumentParseException | NoMoreArgumentsException | NoPermissionsException e) {
sendMessageToSender(e, commandSource, namespace);
} catch (CommandException e) {
CommandException exceptionToSend = e;

if (e.getCause() instanceof CommandException) {
exceptionToSend = (CommandException) e.getCause();
}

sendMessageToSender(exceptionToSend, commandSource, namespace);

throw new CommandException("An unexpected exception occurred while executing the command " + this.command, exceptionToSend);
}
}

public String getPermission() {
return permission;
}

public String[] getAliases() {
return aliases;
}

@Override
public List<String> suggest(Invocation invocation) {
CommandSource commandSource = invocation.source();
String[] strings = invocation.arguments();

List<String> argumentList = Arrays.asList(strings);

Namespace namespace = new NamespaceImpl();
namespace.setObject(CommandSource.class, VelocityCommandManager.SENDER_NAMESPACE, commandSource);

return commandManager.getSuggestions(namespace, argumentList);
}

private void sendMessageToSender(CommandException exception, CommandSource commandSource, Namespace namespace) {
Component component = exception.getMessageComponent();
Component translatedComponent = translator.translate(component, namespace);

commandSource.sendMessage(MessageUtils.kyoriToVelocityKyori(translatedComponent));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package me.fixeddev.commandflow.velocity;

import me.fixeddev.commandflow.translator.DefaultMapTranslationProvider;

public class VelocityDefaultTranslationProvider extends DefaultMapTranslationProvider {

public VelocityDefaultTranslationProvider() {
translations.put("player.offline", "The player %s is offline!");
translations.put("sender.unknown", "The sender for the command is unknown!");
translations.put("sender.only-player", "Only players can execute this command!");
}
}
Loading