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;
}
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;
}
Aggregations