Search in sources :

Example 1 with ITabCompleter

use of io.github.wysohn.triggerreactor.core.manager.trigger.command.ITabCompleter in project TriggerReactor by wysohn.

the class CommandTriggerManager method registerCommand.

@Override
protected boolean registerCommand(String triggerName, CommandTrigger trigger) {
    String[] commandArr = new String[1 + trigger.getAliases().length];
    commandArr[0] = triggerName;
    for (int i = 1; i < commandArr.length; i++) commandArr[i] = trigger.getAliases()[i - 1];
    commandManager.register(plugin.getMain(), new CommandCallable() {

        @Override
        public CommandResult process(CommandSource source, String arguments) throws CommandException {
            if (!(source instanceof Player)) {
                source.sendMessage(Text.of("CommandTrigger works only for Players."));
                return CommandResult.success();
            }
            ICommandSender commandSender = plugin.getPlayer(source.getName());
            String[] args = arguments.split(" ");
            execute(plugin.createPlayerCommandEvent(commandSender, triggerName, args), (Player) source, triggerName, args, trigger);
            return CommandResult.success();
        }

        @Override
        public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException {
            String[] args;
            if (arguments.length() > 0) {
                args = arguments.split(" ");
            } else {
                args = new String[0];
            }
            ITabCompleter tabCompleter = Optional.ofNullable(trigger.getTabCompleters()).filter(iTabCompleters -> iTabCompleters.length >= args.length).map(iTabCompleters -> iTabCompleters[args.length - 1]).orElse(ITabCompleter.EMPTY);
            String partial = args[args.length - 1];
            if (partial.length() < 1) {
                // show hint if nothing is entered yet
                return tabCompleter.getHint();
            } else {
                return tabCompleter.getCandidates(partial);
            }
        }

        @Override
        public boolean testPermission(CommandSource source) {
            for (String permission : trigger.getPermissions()) {
                if (!source.hasPermission(permission))
                    return false;
            }
            return true;
        }

        @Override
        public Optional<Text> getShortDescription(CommandSource source) {
            return Optional.empty();
        }

        @Override
        public Optional<Text> getHelp(CommandSource source) {
            return Optional.empty();
        }

        @Override
        public Text getUsage(CommandSource source) {
            return null;
        }
    }, commandArr).ifPresent(commandMapping -> mappings.put(triggerName, commandMapping));
    return false;
}
Also used : AbstractCommandTriggerManager(io.github.wysohn.triggerreactor.core.manager.trigger.command.AbstractCommandTriggerManager) Location(org.spongepowered.api.world.Location) Sponge(org.spongepowered.api.Sponge) HashMap(java.util.HashMap) TriggerReactorCore(io.github.wysohn.triggerreactor.core.main.TriggerReactorCore) File(java.io.File) ICommandSender(io.github.wysohn.triggerreactor.core.bridge.ICommandSender) List(java.util.List) Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) Map(java.util.Map) Optional(java.util.Optional) CommandTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.command.CommandTrigger) Player(org.spongepowered.api.entity.living.player.Player) Nullable(javax.annotation.Nullable) ITabCompleter(io.github.wysohn.triggerreactor.core.manager.trigger.command.ITabCompleter) org.spongepowered.api.command(org.spongepowered.api.command) Player(org.spongepowered.api.entity.living.player.Player) ITabCompleter(io.github.wysohn.triggerreactor.core.manager.trigger.command.ITabCompleter) Text(org.spongepowered.api.text.Text) Nullable(javax.annotation.Nullable) ICommandSender(io.github.wysohn.triggerreactor.core.bridge.ICommandSender) Location(org.spongepowered.api.world.Location)

Example 2 with ITabCompleter

use of io.github.wysohn.triggerreactor.core.manager.trigger.command.ITabCompleter in project TriggerReactor by wysohn.

the class CommandTriggerManager method registerCommand.

@Override
protected boolean registerCommand(String triggerName, CommandTrigger trigger) {
    if (commandMap.containsKey(triggerName) && overridens.containsKey(triggerName))
        return false;
    PluginCommand command = createCommand(plugin, triggerName);
    command.setAliases(Arrays.stream(trigger.getAliases()).collect(Collectors.toList()));
    command.setTabCompleter((sender, command12, alias, args) -> {
        ITabCompleter tabCompleter = Optional.ofNullable(trigger.getTabCompleters()).filter(iTabCompleters -> iTabCompleters.length >= args.length).map(iTabCompleters -> iTabCompleters[args.length - 1]).orElse(ITabCompleter.EMPTY);
        String partial = args[args.length - 1];
        if (partial.length() < 1) {
            // show hint if nothing is entered yet
            return tabCompleter.getHint();
        } else {
            return tabCompleter.getCandidates(partial);
        }
    });
    command.setExecutor((sender, command1, label, args) -> {
        if (!(sender instanceof Player)) {
            sender.sendMessage("CommandTrigger works only for Players.");
            return true;
        }
        ICommandSender commandSender = plugin.getPlayer(sender.getName());
        execute(plugin.createPlayerCommandEvent(commandSender, label, args), (Player) sender, triggerName, args, trigger);
        return true;
    });
    Optional.ofNullable(commandMap.get(triggerName)).ifPresent(c -> overridens.put(triggerName, c));
    commandMap.put(triggerName, command);
    // register aliases manually here
    for (String alias : trigger.getAliases()) {
        Optional.ofNullable(commandMap.get(alias)).ifPresent(c -> overridens.put(alias, c));
        commandMap.put(alias, command);
    }
    return true;
}
Also used : Plugin(org.bukkit.plugin.Plugin) AbstractCommandTriggerManager(io.github.wysohn.triggerreactor.core.manager.trigger.command.AbstractCommandTriggerManager) Arrays(java.util.Arrays) HashMap(java.util.HashMap) Player(org.bukkit.entity.Player) TriggerReactorCore(io.github.wysohn.triggerreactor.core.main.TriggerReactorCore) PluginCommand(org.bukkit.command.PluginCommand) Constructor(java.lang.reflect.Constructor) Collectors(java.util.stream.Collectors) File(java.io.File) ICommandSender(io.github.wysohn.triggerreactor.core.bridge.ICommandSender) Map(java.util.Map) Optional(java.util.Optional) CommandTrigger(io.github.wysohn.triggerreactor.core.manager.trigger.command.CommandTrigger) ChatColor(org.bukkit.ChatColor) Command(org.bukkit.command.Command) ITabCompleter(io.github.wysohn.triggerreactor.core.manager.trigger.command.ITabCompleter) ITabCompleter(io.github.wysohn.triggerreactor.core.manager.trigger.command.ITabCompleter) Player(org.bukkit.entity.Player) PluginCommand(org.bukkit.command.PluginCommand) ICommandSender(io.github.wysohn.triggerreactor.core.bridge.ICommandSender)

Aggregations

ICommandSender (io.github.wysohn.triggerreactor.core.bridge.ICommandSender)2 TriggerReactorCore (io.github.wysohn.triggerreactor.core.main.TriggerReactorCore)2 AbstractCommandTriggerManager (io.github.wysohn.triggerreactor.core.manager.trigger.command.AbstractCommandTriggerManager)2 CommandTrigger (io.github.wysohn.triggerreactor.core.manager.trigger.command.CommandTrigger)2 ITabCompleter (io.github.wysohn.triggerreactor.core.manager.trigger.command.ITabCompleter)2 File (java.io.File)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Constructor (java.lang.reflect.Constructor)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Nullable (javax.annotation.Nullable)1 ChatColor (org.bukkit.ChatColor)1 Command (org.bukkit.command.Command)1 PluginCommand (org.bukkit.command.PluginCommand)1 Player (org.bukkit.entity.Player)1 Plugin (org.bukkit.plugin.Plugin)1 Sponge (org.spongepowered.api.Sponge)1