SteamWar/SpigotCore
Archiviert
13
0

Complete CommandNode

Dieser Commit ist enthalten in:
yoyosource 2021-07-10 11:27:50 +02:00
Ursprung 9760612d3e
Commit ba14ccb68c
2 geänderte Dateien mit 67 neuen und 20 gelöschten Zeilen

Datei anzeigen

@ -38,7 +38,9 @@ class CommandNode {
private final TypeMapper<?> typeMapper;
private boolean varArg = false;
private List<CommandNode> commandNodeList = new ArrayList<>();
private Method executor;
private List<String> helpMessages = new ArrayList<>();
private Predicate<CommandSender> commandSenderPredicate;
private Function<CommandSender, Object> commandSenderObjectFunction;
@ -62,6 +64,10 @@ class CommandNode {
commandSenderObjectFunction = parameter.getType()::cast;
}
public void addHelpMessage(String helpMessage) {
this.helpMessages.add(helpMessage);
}
public void setVarArg(boolean varArg) {
if (commandNodeList.isEmpty()) {
this.varArg = varArg;
@ -71,24 +77,9 @@ class CommandNode {
}
public List<String> tabComplete(CommandSender commandSender, int index, String[] args) {
try {
if (index == args.length - 1) {
return typeMapper.tabCompletes(commandSender, Arrays.copyOf(args, index), args[args.length - 1]);
}
if (typeMapper.map(commandSender, Arrays.copyOf(args, index), args[index]) == null) {
return Collections.emptyList();
}
if (varArg) {
return tabComplete(commandSender, index + 1, args);
} else {
return commandNodeList.stream()
.map(commandNode -> commandNode.tabComplete(commandSender, index + 1, args))
.flatMap(List::stream)
.collect(Collectors.toList());
}
} catch (Exception e) {
return Collections.emptyList();
}
return internalTabCompleteAndSuggest((commandSender1, integer, strings) -> {
return typeMapper.tabCompletes(commandSender1, Arrays.copyOf(strings, integer), strings[strings.length - 1]);
}, commandSender, index, args);
}
public boolean execute(CommandSender commandSender, int index, String[] args, List<Object> mappedObjects) {
@ -135,7 +126,32 @@ class CommandNode {
.findFirst().orElse(false);
}
public List<String> suggest(CommandSender commandSender, int index, String[] args, List<Object> mappedObjects) {
return null;
public List<String> suggest(CommandSender commandSender, int index, String[] args) {
return internalTabCompleteAndSuggest((commandSender1, integer, strings) -> helpMessages, commandSender, index, args);
}
private List<String> internalTabCompleteAndSuggest(TriFunction<CommandSender, Integer, String[], List<String>> returnFunction, CommandSender commandSender, int index, String[] args) {
try {
if (index == args.length - 1) {
return returnFunction.apply(commandSender, index, args);
}
if (typeMapper.map(commandSender, Arrays.copyOf(args, index), args[index]) == null) {
return Collections.emptyList();
}
if (varArg) {
return internalTabCompleteAndSuggest(returnFunction, commandSender, index + 1, args);
} else {
return commandNodeList.stream()
.map(commandNode -> commandNode.internalTabCompleteAndSuggest(returnFunction, commandSender, index + 1, args))
.flatMap(List::stream)
.collect(Collectors.toList());
}
} catch (Exception e) {
return Collections.emptyList();
}
}
private interface TriFunction<I1, I2, I3, O> {
O apply(I1 i1, I2 i2, I3 i3);
}
}

Datei anzeigen

@ -0,0 +1,31 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
import java.lang.reflect.Method;
import java.util.Map;
public class CommandPart {
public CommandPart(SWCommand swCommand, Method method, String[] subCommand, Map<String, TypeMapper<?>> localTypeMapper) {
}
}