Search in sources :

Example 1 with CommandType

use of dev.projectg.crossplatforms.command.CommandType in project CrossplatForms by ProjectG-Plugins.

the class ProxyCommandManager method load.

private void load() {
    if (configManager.getConfig(GeneralConfig.class).isEmpty()) {
        return;
    }
    GeneralConfig config = configManager.getConfig(GeneralConfig.class).get();
    commandManager.setSetting(CommandManager.ManagerSettings.ALLOW_UNSAFE_REGISTRATION, config.isUnsafeCommandRegistration());
    registeredCommands.clear();
    for (ProxyCommand command : config.getCommands().values()) {
        String name = command.getName();
        if (command.getPermission() == null || command.getPermission().isBlank()) {
            command.setPermission(Constants.ID + ".shortcut." + name);
        }
        CommandType type = command.getMethod();
        if (type == CommandType.REGISTER) {
            if (commandManager.getCommandTree().getNamedNode(name) == null) {
                // any references to the command are done through the map so that it can be updated after a reload
                if (commandManager.isCommandRegistrationAllowed()) {
                    Logger.getLogger().debug("Registering shortcut command: " + command.getName());
                    commandManager.command(commandManager.commandBuilder(name).permission((origin) -> {
                        ProxyCommand target = registeredCommands.get(name);
                        if (target == null) {
                            // no longer present in the config
                            return false;
                        } else {
                            return origin.isPlayer() && origin.hasPermission(target.getPermission());
                        }
                    }).handler((context) -> {
                        FormPlayer player = Objects.requireNonNull(serverHandler.getPlayer(context.getSender().getUUID().orElseThrow()));
                        registeredCommands.get(name).run(player, interfaceManager, bedrockHandler);
                    }));
                } else {
                    Logger.getLogger().warn("Unable to register shortcut command '" + name + "' because registration is no longer possible. Restart the server for changes to apply.");
                }
            }
            // store the command
            registeredCommands.put(name, command);
        } else if (type == CommandType.INTERCEPT_CANCEL || type == CommandType.INTERCEPT_PASS) {
            serverHandler.registerProxyCommand(command);
        } else {
            Logger.getLogger().severe("Unhandled CommandType enum constant: " + type);
        }
    }
}
Also used : Constants(dev.projectg.crossplatforms.Constants) ReloadableRegistry(dev.projectg.crossplatforms.reloadable.ReloadableRegistry) ConfigManager(dev.projectg.crossplatforms.config.ConfigManager) CommandManager(cloud.commandframework.CommandManager) CommandOrigin(dev.projectg.crossplatforms.command.CommandOrigin) HashMap(java.util.HashMap) ServerHandler(dev.projectg.crossplatforms.handler.ServerHandler) FormPlayer(dev.projectg.crossplatforms.handler.FormPlayer) Objects(java.util.Objects) InterfaceManager(dev.projectg.crossplatforms.interfacing.InterfaceManager) CrossplatForms(dev.projectg.crossplatforms.CrossplatForms) BedrockHandler(dev.projectg.crossplatforms.handler.BedrockHandler) CommandType(dev.projectg.crossplatforms.command.CommandType) Map(java.util.Map) Logger(dev.projectg.crossplatforms.Logger) GeneralConfig(dev.projectg.crossplatforms.config.GeneralConfig) Reloadable(dev.projectg.crossplatforms.reloadable.Reloadable) FormPlayer(dev.projectg.crossplatforms.handler.FormPlayer) CommandType(dev.projectg.crossplatforms.command.CommandType) GeneralConfig(dev.projectg.crossplatforms.config.GeneralConfig)

Example 2 with CommandType

use of dev.projectg.crossplatforms.command.CommandType in project CrossplatForms by ProjectG-Plugins.

the class VelocityHandler method onCommandExecute.

@Subscribe
public void onCommandExecute(CommandExecuteEvent event) {
    CommandSource source = event.getCommandSource();
    if (!event.getResult().isAllowed() || !(source instanceof Player)) {
        return;
    }
    String input = event.getCommand();
    Logger.get().debug("preprocess command: [" + event.getCommand() + "] -> [" + input + "]");
    InterceptCommand command = findCommand(input);
    if (command != null) {
        Player player = (Player) source;
        CommandType type = command.getMethod();
        BedrockHandler bedrockHandler = CrossplatForms.getInstance().getBedrockHandler();
        if (command.getPlatform().matches(player.getUniqueId(), bedrockHandler)) {
            String permission = command.getPermission();
            if (permission == null || player.hasPermission(permission)) {
                command.run(new VelocityPlayer(player));
                if (type == CommandType.INTERCEPT_CANCEL) {
                    // todo: if this sends a message about denial we might have to replace the common with a dummy
                    event.setResult(CommandExecuteEvent.CommandResult.denied());
                }
            }
        }
    }
}
Also used : FormPlayer(dev.projectg.crossplatforms.handler.FormPlayer) Player(com.velocitypowered.api.proxy.Player) CommandType(dev.projectg.crossplatforms.command.CommandType) InterceptCommand(dev.projectg.crossplatforms.command.custom.InterceptCommand) BedrockHandler(dev.projectg.crossplatforms.handler.BedrockHandler) ConsoleCommandSource(com.velocitypowered.api.proxy.ConsoleCommandSource) CommandSource(com.velocitypowered.api.command.CommandSource) Subscribe(com.velocitypowered.api.event.Subscribe)

Example 3 with CommandType

use of dev.projectg.crossplatforms.command.CommandType in project CrossplatForms by ProjectG-Plugins.

the class CustomCommandManager method load.

private void load() {
    serverHandler.clearInterceptCommands();
    if (!configManager.getConfig(GeneralConfig.class).isPresent()) {
        return;
    }
    GeneralConfig config = configManager.getConfig(GeneralConfig.class).get();
    commandManager.setSetting(CommandManager.ManagerSettings.ALLOW_UNSAFE_REGISTRATION, config.isUnsafeCommandRegistration());
    List<Arguments> currentCommands = new ArrayList<>();
    for (CustomCommand command : config.getCommands().values()) {
        if (command == null) {
            continue;
        }
        CommandType type = command.getMethod();
        if (type == CommandType.REGISTER) {
            if (command instanceof RegisteredCommand) {
                RegisteredCommand registered = (RegisteredCommand) command;
                registerCommand(registered);
                currentCommands.add(registered.literals());
            } else {
                throw new IllegalStateException("CustomCommand has method type REGISTER but is not a RegisteredCommand: " + command);
            }
        } else if (type == CommandType.INTERCEPT_CANCEL || type == CommandType.INTERCEPT_PASS) {
            if (command instanceof InterceptCommand) {
                interceptCommand((InterceptCommand) command);
            } else {
                throw new IllegalStateException("CustomCommand has method type INTERCEPT_CANCEL or INTERCEPT_PASS but is not a ProxiedCommand: " + command);
            }
        } else {
            logger.severe("Unhandled CommandType enum: " + type + ". Not registering command with name: " + command.getIdentifier());
        }
    }
    for (Map.Entry<Arguments, RegisteredCommand> entry : registeredCommands.entrySet()) {
        // enable commands that are current
        // disable commands that are no longer current
        // cannot remove old commands from the map because a double reload could result in cloud raising exceptions due to duplicate nodes/arguments
        entry.getValue().enable(currentCommands.contains(entry.getKey()));
    }
}
Also used : CommandType(dev.projectg.crossplatforms.command.CommandType) GeneralConfig(dev.projectg.crossplatforms.config.GeneralConfig) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

CommandType (dev.projectg.crossplatforms.command.CommandType)3 GeneralConfig (dev.projectg.crossplatforms.config.GeneralConfig)2 BedrockHandler (dev.projectg.crossplatforms.handler.BedrockHandler)2 FormPlayer (dev.projectg.crossplatforms.handler.FormPlayer)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CommandManager (cloud.commandframework.CommandManager)1 CommandSource (com.velocitypowered.api.command.CommandSource)1 Subscribe (com.velocitypowered.api.event.Subscribe)1 ConsoleCommandSource (com.velocitypowered.api.proxy.ConsoleCommandSource)1 Player (com.velocitypowered.api.proxy.Player)1 Constants (dev.projectg.crossplatforms.Constants)1 CrossplatForms (dev.projectg.crossplatforms.CrossplatForms)1 Logger (dev.projectg.crossplatforms.Logger)1 CommandOrigin (dev.projectg.crossplatforms.command.CommandOrigin)1 InterceptCommand (dev.projectg.crossplatforms.command.custom.InterceptCommand)1 ConfigManager (dev.projectg.crossplatforms.config.ConfigManager)1 ServerHandler (dev.projectg.crossplatforms.handler.ServerHandler)1 InterfaceManager (dev.projectg.crossplatforms.interfacing.InterfaceManager)1 Reloadable (dev.projectg.crossplatforms.reloadable.Reloadable)1