SteamWar/SpigotCore
Archiviert
13
0

CMDoS because Brigadier

Dieser Commit ist enthalten in:
yoyosource 2021-07-08 23:43:14 +02:00
Ursprung 834969959a
Commit bc966247a8

Datei anzeigen

@ -24,54 +24,33 @@ import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder; import com.mojang.brigadier.builder.RequiredArgumentBuilder;
import net.minecraft.server.v1_15_R1.CommandListenerWrapper; import net.minecraft.server.v1_15_R1.CommandListenerWrapper;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Parameter; import java.lang.reflect.Parameter;
import java.util.*; import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.IntPredicate;
import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
class SWCommandBrigadier implements SWCommand.SWCommandInterface { class SWCommandBrigadier implements SWCommand.SWCommandInterface {
private final String command;
private final String[] aliases;
private final SWCommand swCommand; private final SWCommand swCommand;
private Command command;
private final List<SubCommand> commandList = new ArrayList<>(); private final List<SubCommand> commandList = new ArrayList<>();
private final List<SubCommand> commandHelpList = new ArrayList<>(); private final List<SubCommand> commandHelpList = new ArrayList<>();
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>(); private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
protected SWCommandBrigadier(SWCommand swCommand, String command, String... aliases) { protected SWCommandBrigadier(SWCommand swCommand, String command, String... aliases) {
this.swCommand = swCommand; this.command = command;
this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) { this.aliases = aliases;
@Override
public boolean execute(CommandSender sender, String alias, String[] args) {
if (commandList.stream().anyMatch(s -> s.invoke(sender, args))) return false;
commandHelpList.stream().anyMatch(s -> s.invoke(sender, args));
return false;
}
@Override this.swCommand = swCommand;
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
String string = args[args.length - 1].toLowerCase();
return commandList.stream()
.map(s -> s.tabComplete(sender, args))
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.filter(s -> !s.isEmpty())
.filter(s -> s.toLowerCase().startsWith(string))
.collect(Collectors.toList());
}
};
SWCommandUtils.createList(swCommand, commandList, commandHelpList, localTypeMapper, this::createSubCommand); SWCommandUtils.createList(swCommand, commandList, commandHelpList, localTypeMapper, this::createSubCommand);
Stream.of(commandList, commandHelpList).flatMap(List::stream).forEach(subCommand -> { Stream.of(commandList, commandHelpList).flatMap(List::stream).forEach(subCommand -> {
register(this.command.getName(), subCommand); register(command, subCommand);
for (String s : this.command.getAliases()) { for (String s : aliases) {
register(s, subCommand); register(s, subCommand);
} }
}); });
@ -105,7 +84,7 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface {
argumentBuilder = requiredArgumentBuilder; argumentBuilder = requiredArgumentBuilder;
} }
if (current.varArgType != null) { if (current.varArgType != null) {
// TODO: UNSUPORTED // TODO: UNSUPPORTED
/*Parameter parameter = parameters[parameters.length - 1]; /*Parameter parameter = parameters[parameters.length - 1];
Class<?> parameterType = parameter.getType(); Class<?> parameterType = parameter.getType();
TypeMapper<?> typeMapper = arguments[arguments.length - 1]; TypeMapper<?> typeMapper = arguments[arguments.length - 1];
@ -154,17 +133,15 @@ class SWCommandBrigadier implements SWCommand.SWCommandInterface {
@Override @Override
public void unregister() { public void unregister() {
SWCommandUtils.knownCommandMap.remove(command.getName()); SWCommandUtils.knownCommandMap.remove(command);
command.getAliases().forEach(SWCommandUtils.knownCommandMap::remove); Arrays.stream(aliases).forEach(SWCommandUtils.knownCommandMap::remove);
command.unregister(SWCommandUtils.commandMap);
} }
@Override @Override
public void register() { public void register() {
SWCommandUtils.commandMap.register("steamwar", this.command);
Stream.of(commandList, commandHelpList).flatMap(List::stream).forEach(subCommand -> { Stream.of(commandList, commandHelpList).flatMap(List::stream).forEach(subCommand -> {
register(command.getName(), subCommand); register(command, subCommand);
for (String s : command.getAliases()) { for (String s : aliases) {
register(s, subCommand); register(s, subCommand);
} }
}); });