Added new Brigadier commands related APIs for bungee/paper/cli + a few javadoc
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package fr.pandacube.lib.cli.commands;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import com.mojang.brigadier.arguments.ArgumentType;
|
||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.context.ParsedCommandNode;
|
||||
import com.mojang.brigadier.suggestion.SuggestionProvider;
|
||||
import com.mojang.brigadier.tree.LiteralCommandNode;
|
||||
|
||||
import fr.pandacube.lib.commands.BrigadierCommand;
|
||||
import fr.pandacube.lib.commands.BrigadierSuggestionsUtil;
|
||||
import fr.pandacube.lib.commands.SuggestionsSupplier;
|
||||
import fr.pandacube.lib.util.Log;
|
||||
|
||||
public abstract class CLIBrigadierCommand extends BrigadierCommand<Object> {
|
||||
|
||||
public CLIBrigadierCommand() {
|
||||
LiteralCommandNode<Object> commandNode = buildCommand().build();
|
||||
postBuildCommand(commandNode);
|
||||
String[] aliases = getAliases();
|
||||
if (aliases == null)
|
||||
aliases = new String[0];
|
||||
|
||||
CLIBrigadierDispatcher.instance.register(commandNode);
|
||||
|
||||
|
||||
for (String alias : aliases) {
|
||||
CLIBrigadierDispatcher.instance.register(literal(alias)
|
||||
.requires(commandNode.getRequirement())
|
||||
.executes(commandNode.getCommand())
|
||||
.redirect(commandNode)
|
||||
.build()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract LiteralArgumentBuilder<Object> buildCommand();
|
||||
|
||||
protected String[] getAliases() {
|
||||
return new String[0];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public boolean isPlayer(Object sender) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isConsole(Object sender) {
|
||||
return true;
|
||||
}
|
||||
|
||||
public Predicate<Object> hasPermission(String permission) {
|
||||
return sender -> true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
protected SuggestionProvider<Object> wrapSuggestions(SuggestionsSupplier<Object> suggestions) {
|
||||
return wrapSuggestions(suggestions, Function.identity());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
package fr.pandacube.lib.cli.commands;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.mojang.brigadier.suggestion.Suggestion;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
|
||||
import fr.pandacube.lib.chat.Chat;
|
||||
import fr.pandacube.lib.commands.BrigadierDispatcher;
|
||||
import fr.pandacube.lib.util.Log;
|
||||
import jline.console.completer.Completer;
|
||||
import net.kyori.adventure.text.ComponentLike;
|
||||
|
||||
public class CLIBrigadierDispatcher extends BrigadierDispatcher<Object> implements Completer {
|
||||
|
||||
public static final CLIBrigadierDispatcher instance = new CLIBrigadierDispatcher();
|
||||
|
||||
|
||||
private static final Object sender = new Object();
|
||||
|
||||
|
||||
public int execute(String commandWithoutSlash) {
|
||||
return execute(sender, commandWithoutSlash);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int complete(String buffer, int cursor, List<CharSequence> candidates) {
|
||||
|
||||
String bufferBeforeCursor = buffer.substring(0, cursor);
|
||||
|
||||
Suggestions completeResult = getSuggestions(bufferBeforeCursor);
|
||||
|
||||
completeResult.getList().stream()
|
||||
.map(Suggestion::getText)
|
||||
.forEach(candidates::add);
|
||||
|
||||
return completeResult.getRange().getStart();
|
||||
}
|
||||
|
||||
public Suggestions getSuggestions(String buffer) {
|
||||
return getSuggestions(sender, buffer);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void sendSenderMessage(Object sender, ComponentLike message) {
|
||||
Log.info(Chat.chatComponent(message).getLegacyText());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user