Verified Commit e97b4ace authored by Yannick Schinko's avatar Yannick Schinko
Browse files

Added basic and generic command support

Very, very basic... (not even command completion or error handling)
parent dced5d80
......@@ -3,9 +3,11 @@ package team.aura_dev.aurasudo.platform.bungeecord;
import java.nio.file.Path;
import net.md_5.bungee.api.ProxyServer;
import team.aura_dev.aurasudo.api.AuraSudo;
import team.aura_dev.aurasudo.platform.bungeecord.command.CommandWrapperBungeeCord;
import team.aura_dev.aurasudo.platform.bungeecord.listener.PlayerEventListenerBungeeCord;
import team.aura_dev.aurasudo.platform.bungeecord.player.PlayerManagerBungeeCord;
import team.aura_dev.aurasudo.platform.common.AuraSudoBase;
import team.aura_dev.aurasudo.platform.common.command.BaseCommand;
import team.aura_dev.aurasudo.platform.common.player.PlayerManagerCommon;
import team.aura_dev.lib.multiplatformcore.DependencyClassLoader;
......@@ -46,4 +48,11 @@ public class AuraSudoBungeeCord extends AuraSudoBase {
protected void registerEventListeners() {
server.getPluginManager().registerListener(plugin, new PlayerEventListenerBungeeCord(this));
}
@Override
protected void registerCommand(BaseCommand command) {
server
.getPluginManager()
.registerCommand(plugin, new CommandWrapperBungeeCord(playerManager, command));
}
}
package team.aura_dev.aurasudo.platform.bungeecord.command;
import java.util.Arrays;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.plugin.Command;
import team.aura_dev.aurasudo.api.player.PlayerManager;
import team.aura_dev.aurasudo.platform.common.command.BaseCommand;
public class CommandWrapperBungeeCord extends Command {
protected final PlayerManager playerManager;
protected final BaseCommand command;
/**
* Construct a new native command wrapper for the {@link BaseCommand}.
*
* @param playerManager The {@link PlayerManager} to convert native player objects into our player
* objects
* @param command The underlying {@link BaseCommand}
*/
public CommandWrapperBungeeCord(PlayerManager playerManager, BaseCommand command) {
super(command.getBaseCommand(), command.COMMAND.getPermission(), command.getAliasesAsArray());
this.playerManager = playerManager;
this.command = command;
}
@Override
public void execute(CommandSender sender, String[] args) {
// Sadly we don't know the alias being used, so we need to pass the base command
command.execute(
playerManager.fromNativePlayer(sender), command.getBaseCommand(), Arrays.asList(args));
}
}
......@@ -12,6 +12,7 @@ import net.luckperms.api.LuckPermsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import team.aura_dev.aurasudo.api.AuraSudoApi;
import team.aura_dev.aurasudo.platform.common.command.BaseCommand;
import team.aura_dev.aurasudo.platform.common.config.ConfigLoader;
import team.aura_dev.aurasudo.platform.common.context.SudoContextCalculator;
import team.aura_dev.aurasudo.platform.common.dependency.RuntimeDependencies;
......@@ -100,6 +101,8 @@ public abstract class AuraSudoBase implements AuraSudoApi, AuraSudoBaseBootstrap
protected abstract void registerEventListeners();
protected abstract void registerCommand(BaseCommand command);
// ============================================================================================
// Actual plugin functionality starts here
// ============================================================================================
......
package team.aura_dev.aurasudo.platform.common.command;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import lombok.Getter;
import team.aura_dev.aurasudo.api.player.PlayerData;
import team.aura_dev.aurasudo.platform.common.permission.Permission;
public abstract class BaseCommand {
public final Permission BASE;
public final Permission COMMAND;
@Getter protected final LinkedHashSet<String> aliases;
public BaseCommand(String baseCommand, String... aliases) {
this(baseCommand, Arrays.asList(aliases));
}
public BaseCommand(String baseCommand, Collection<String> aliases) {
this.BASE = new Permission(Permission.COMMAND, baseCommand);
this.COMMAND = new Permission(BASE, "base");
this.aliases = new LinkedHashSet<>();
this.aliases.add(baseCommand);
this.aliases.addAll(aliases);
}
public String getBaseCommand() {
return aliases.iterator().next();
}
public List<String> getAliasesAsList() {
return new ArrayList(aliases);
}
public String[] getAliasesAsArray() {
return aliases.toArray(new String[] {});
}
public abstract void execute(PlayerData player, String alias, Collection<String> arguments)
throws CommandExecutionException;
}
package team.aura_dev.aurasudo.platform.common.command;
public class CommandExecutionException extends RuntimeException {
public CommandExecutionException() {}
public CommandExecutionException(String message) {
super(message);
}
public CommandExecutionException(String message, Throwable cause) {
super(message, cause);
}
public CommandExecutionException(Throwable cause) {
super(cause);
}
}
package team.aura_dev.aurasudo.platform.common.command;
import javax.annotation.Nullable;
import lombok.Getter;
import team.aura_dev.aurasudo.platform.common.permission.Permission;
public class PermissionException extends RuntimeException {
@Getter protected final Permission permission;
public PermissionException() {
this(null);
}
public PermissionException(@Nullable Permission permission) {
this.permission = permission;
}
}
......@@ -4,6 +4,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.AccessController;
import java.security.PrivilegedAction;
import team.aura_dev.aurasudo.platform.common.command.BaseCommand;
import team.aura_dev.aurasudo.platform.common.player.PlayerManagerCommon;
import team.aura_dev.lib.multiplatformcore.DependencyClassLoader;
......@@ -41,4 +42,9 @@ public class AuraSudoTest extends AuraSudoBase {
protected void registerEventListeners() {
// TODO: Implement when needed
}
@Override
protected void registerCommand(BaseCommand command) {
// TODO: Implement when needed
}
}
......@@ -4,7 +4,9 @@ import cn.nukkit.Server;
import java.nio.file.Path;
import team.aura_dev.aurasudo.api.AuraSudo;
import team.aura_dev.aurasudo.platform.common.AuraSudoBase;
import team.aura_dev.aurasudo.platform.common.command.BaseCommand;
import team.aura_dev.aurasudo.platform.common.player.PlayerManagerCommon;
import team.aura_dev.aurasudo.platform.nukkit.command.CommandWrapperNukkit;
import team.aura_dev.aurasudo.platform.nukkit.listener.PlayerEventListenerNukkit;
import team.aura_dev.aurasudo.platform.nukkit.player.PlayerManagerNukkit;
import team.aura_dev.lib.multiplatformcore.DependencyClassLoader;
......@@ -46,4 +48,9 @@ public class AuraSudoNukkit extends AuraSudoBase {
protected void registerEventListeners() {
server.getPluginManager().registerEvents(new PlayerEventListenerNukkit(this), plugin);
}
@Override
protected void registerCommand(BaseCommand command) {
server.getCommandRegistry().register(plugin, new CommandWrapperNukkit(playerManager, command));
}
}
package team.aura_dev.aurasudo.platform.nukkit.command;
import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import cn.nukkit.command.data.CommandData;
import java.util.Arrays;
import team.aura_dev.aurasudo.api.player.PlayerManager;
import team.aura_dev.aurasudo.platform.common.command.BaseCommand;
public class CommandWrapperNukkit extends Command {
protected final PlayerManager playerManager;
protected final BaseCommand command;
/**
* Construct a new native command wrapper for the {@link BaseCommand}.
*
* @param playerManager The {@link PlayerManager} to convert native player objects into our player
* objects
* @param command The underlying {@link BaseCommand}
*/
public CommandWrapperNukkit(PlayerManager playerManager, BaseCommand command) {
super(
CommandData.builder(command.getBaseCommand())
.addPermission(command.COMMAND.getPermission())
.setAliases(command.getAliasesAsArray())
.build());
this.playerManager = playerManager;
this.command = command;
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
command.execute(playerManager.fromNativePlayer(sender), commandLabel, Arrays.asList(args));
return true;
}
}
package team.aura_dev.aurasudo.platform.spigot;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Path;
import lombok.SneakyThrows;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.CommandMap;
import team.aura_dev.aurasudo.api.AuraSudo;
import team.aura_dev.aurasudo.platform.common.AuraSudoBase;
import team.aura_dev.aurasudo.platform.common.command.BaseCommand;
import team.aura_dev.aurasudo.platform.common.player.PlayerManagerCommon;
import team.aura_dev.aurasudo.platform.spigot.command.CommandWrapperSpigot;
import team.aura_dev.aurasudo.platform.spigot.listener.PlayerEventListenerSpigot;
import team.aura_dev.aurasudo.platform.spigot.player.PlayerManagerSpigot;
import team.aura_dev.lib.multiplatformcore.DependencyClassLoader;
public class AuraSudoSpigot extends AuraSudoBase {
private final AuraSudoSpigotBootstrap plugin;
private final Server server;
private final Method getCommandMapMethod;
public AuraSudoSpigot(
DependencyClassLoader classLoader, AuraSudoSpigotBootstrap plugin, Path configDir) {
DependencyClassLoader classLoader, AuraSudoSpigotBootstrap plugin, Path configDir)
throws NoSuchMethodException {
super(classLoader, configDir);
this.plugin = plugin;
this.server = plugin.getServer();
getCommandMapMethod = server.getClass().getDeclaredMethod("getCommandMap");
getCommandMapMethod.setAccessible(true);
// Instance is initialized
AuraSudo.setApi(this);
......@@ -41,4 +56,11 @@ public class AuraSudoSpigot extends AuraSudoBase {
protected void registerEventListeners() {
Bukkit.getPluginManager().registerEvents(new PlayerEventListenerSpigot(this), plugin);
}
@SneakyThrows({InvocationTargetException.class, IllegalAccessException.class})
@Override
protected void registerCommand(BaseCommand command) {
((CommandMap) getCommandMapMethod.invoke(server))
.register(ID, new CommandWrapperSpigot(playerManager, command));
}
}
package team.aura_dev.aurasudo.platform.spigot.command;
import java.util.Arrays;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import team.aura_dev.aurasudo.api.player.PlayerManager;
import team.aura_dev.aurasudo.platform.common.command.BaseCommand;
public class CommandWrapperSpigot extends Command {
protected final PlayerManager playerManager;
protected final BaseCommand command;
/**
* Construct a new native command wrapper for the {@link BaseCommand}.
*
* @param playerManager The {@link PlayerManager} to convert native player objects into our player
* objects
* @param command The underlying {@link BaseCommand}
*/
public CommandWrapperSpigot(PlayerManager playerManager, BaseCommand command) {
super(command.getBaseCommand(), "", "", command.getAliasesAsList());
setPermission(command.COMMAND.getPermission());
this.playerManager = playerManager;
this.command = command;
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
command.execute(playerManager.fromNativePlayer(sender), commandLabel, Arrays.asList(args));
return true;
}
}
......@@ -5,10 +5,14 @@ import java.util.Collection;
import java.util.Collections;
import org.spongepowered.api.Platform;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
import team.aura_dev.aurasudo.api.AuraSudo;
import team.aura_dev.aurasudo.platform.common.AuraSudoBase;
import team.aura_dev.aurasudo.platform.common.command.BaseCommand;
import team.aura_dev.aurasudo.platform.common.dependency.RuntimeDependencies;
import team.aura_dev.aurasudo.platform.common.player.PlayerManagerCommon;
import team.aura_dev.aurasudo.platform.sponge.command.CommandExecutorSponge;
import team.aura_dev.aurasudo.platform.sponge.listener.PlayerEventListenerSponge;
import team.aura_dev.aurasudo.platform.sponge.player.PlayerManagerSponge;
import team.aura_dev.lib.multiplatformcore.DependencyClassLoader;
......@@ -52,4 +56,16 @@ public class AuraSudoSponge extends AuraSudoBase {
protected void registerEventListeners() {
Sponge.getEventManager().registerListeners(plugin, new PlayerEventListenerSponge(this));
}
@Override
protected void registerCommand(BaseCommand command) {
CommandSpec commandSpec =
CommandSpec.builder()
.permission(command.COMMAND.getPermission())
.arguments(GenericArguments.allOf(GenericArguments.string(CommandExecutorSponge.ARGS)))
.executor(new CommandExecutorSponge(playerManager, command))
.build();
Sponge.getCommandManager().register(plugin, commandSpec, command.getAliasesAsList());
}
}
package team.aura_dev.aurasudo.platform.sponge.command;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.text.Text;
import team.aura_dev.aurasudo.api.player.PlayerManager;
import team.aura_dev.aurasudo.platform.common.command.BaseCommand;
public class CommandExecutorSponge implements CommandExecutor {
public static final Text ARGS = Text.of("args");
protected final PlayerManager playerManager;
protected final BaseCommand command;
/**
* Construct a new native command executor for the {@link BaseCommand}.
*
* @param playerManager The {@link PlayerManager} to convert native player objects into our player
* objects
* @param command The underlying {@link BaseCommand}
*/
public CommandExecutorSponge(PlayerManager playerManager, BaseCommand command) {
this.playerManager = playerManager;
this.command = command;
}
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
// Sadly we don't know the alias being used, so we need to pass the base command
command.execute(
playerManager.fromNativePlayer(src), command.getBaseCommand(), args.getAll(ARGS));
return CommandResult.success();
}
}
......@@ -4,7 +4,9 @@ import com.velocitypowered.api.proxy.ProxyServer;
import java.nio.file.Path;
import team.aura_dev.aurasudo.api.AuraSudo;
import team.aura_dev.aurasudo.platform.common.AuraSudoBase;
import team.aura_dev.aurasudo.platform.common.command.BaseCommand;
import team.aura_dev.aurasudo.platform.common.player.PlayerManagerCommon;
import team.aura_dev.aurasudo.platform.velocity.command.CommandWrapperVelocity;
import team.aura_dev.aurasudo.platform.velocity.listener.PlayerEventListenerVelocity;
import team.aura_dev.aurasudo.platform.velocity.player.PlayerManagerVelocity;
import team.aura_dev.lib.multiplatformcore.DependencyClassLoader;
......@@ -46,4 +48,14 @@ public class AuraSudoVelocity extends AuraSudoBase {
protected void registerEventListeners() {
server.getEventManager().register(plugin, new PlayerEventListenerVelocity(this));
}
@Override
protected void registerCommand(BaseCommand command) {
server
.getCommandManager()
.register(
command.getBaseCommand(),
new CommandWrapperVelocity(playerManager, command),
command.getAliasesAsArray());
}
}
package team.aura_dev.aurasudo.platform.velocity.command;
import com.velocitypowered.api.command.Command;
import com.velocitypowered.api.command.CommandSource;
import java.util.Arrays;
import team.aura_dev.aurasudo.api.player.PlayerManager;
import team.aura_dev.aurasudo.platform.common.command.BaseCommand;
public class CommandWrapperVelocity implements Command {
protected final PlayerManager playerManager;
protected final BaseCommand command;
/**
* Construct a new native command wrapper for the {@link BaseCommand}.
*
* @param playerManager The {@link PlayerManager} to convert native player objects into our player
* objects
* @param command The underlying {@link BaseCommand}
*/
public CommandWrapperVelocity(PlayerManager playerManager, BaseCommand command) {
this.playerManager = playerManager;
this.command = command;
}
@Override
public void execute(CommandSource source, String[] args) {
// Sadly we don't know the alias being used, so we need to pass the base command
command.execute(
playerManager.fromNativePlayer(source), command.getBaseCommand(), Arrays.asList(args));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment