From a33c8a3a1e204dee549d0534b44e6b73371f1a63 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 31 Mar 2021 14:43:04 +0200 Subject: [PATCH 1/2] Fix SubCommand.tabComplete with noVarArgs returning too early --- SpigotCore_Main/src/de/steamwar/command/SWCommand.java | 2 +- .../src/de/steamwar/command/SubCommand.java | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index 240766b..72cda65 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -130,7 +130,7 @@ public abstract class SWCommand { if (mapper != null) { name = mapper.value(); } - if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) { + if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name) && !localTypeMapper.containsKey(name)) { Bukkit.getLogger().log(Level.WARNING, "The parameter '" + parameter.toString() + "' is using an unsupported Mapper of type '" + name + "'"); return; } diff --git a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java index 1ce4c11..343cbd0 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SubCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SubCommand.java @@ -101,37 +101,41 @@ class SubCommand { } List tabComplete(CommandSender commandSender, String[] args) { - if (varArgType == null && args.length < arguments.length + subCommand.length - 1) { + if (varArgType == null && args.length > arguments.length + subCommand.length) { return null; } + int index = 0; List argsList = new LinkedList<>(Arrays.asList(args)); for (String value : subCommand) { String s = argsList.remove(0); if (argsList.isEmpty()) return Collections.singletonList(value); if (!value.equalsIgnoreCase(s)) return null; + index++; } for (TypeMapper argument : arguments) { String s = argsList.remove(0); if (argsList.isEmpty()) return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); try { - if (argument.map(Arrays.copyOf(args, argsList.size()), s) == null) { + if (argument.map(Arrays.copyOf(args, index), s) == null) { return null; } } catch (Exception e) { return null; } + index++; } if (varArgType != null && !argsList.isEmpty()) { while (!argsList.isEmpty()) { String s = argsList.remove(0); if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); try { - if (arguments[arguments.length - 1].map(Arrays.copyOf(args, argsList.size()), s) == null) { + if (arguments[arguments.length - 1].map(Arrays.copyOf(args, index), s) == null) { return null; } } catch (Exception e) { return null; } + index++; } } return null; From b6aa9d760c5c3461f911131e7f762d29fb75c241 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 31 Mar 2021 14:45:47 +0200 Subject: [PATCH 2/2] Remove TestCommand.java --- .../src/de/steamwar/acommand/TestCommand.java | 82 ------------------- 1 file changed, 82 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/acommand/TestCommand.java diff --git a/SpigotCore_Main/src/de/steamwar/acommand/TestCommand.java b/SpigotCore_Main/src/de/steamwar/acommand/TestCommand.java deleted file mode 100644 index 8444aba..0000000 --- a/SpigotCore_Main/src/de/steamwar/acommand/TestCommand.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * 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 . - */ - -package de.steamwar.acommand; - -import de.steamwar.command.SWCommand; -import de.steamwar.command.TypeMapper; -import org.bukkit.Material; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -public class TestCommand extends SWCommand { - - public TestCommand() { - // Register this command as 'test' - super("test"); - } - - // One Help Command, the first Parameter should be some kind of CommandSender - // The second argument can only be a varArgs string of what arguments were tried to map - @Register(help = true) - public void testHelp(Player player, String... args) { - player.sendMessage("This is your help message"); - } - - // One Command, the first Parameter should be some kind of CommandSender - @Register - public void test(Player player) { - - } - - // Another Command, subCommands can be implemented with the Register Annotation, - // you can use custom Mappers by putting a Mapper Annotation on a Parameter - @Register({"two"}) - public void testTwo(Player player, int i, @Mapper("solidMaterial") Material material) { - - } - - // Add Custom Mapper when this command is registered, all Mapper of you class will be - // created first and than the Commands. Do not use this mapper outside your class, as - // it can only create failures. Use on your own risk. Definition order should be considered. - @Mapper("solidMaterial") - public TypeMapper materialTypeMapper() { - List tabCompletes = Arrays.stream(Material.values()) - .filter(Material::isSolid) - .map(Material::name) - .map(String::toLowerCase) - .collect(Collectors.toList()); - return new TypeMapper() { - @Override - public Material map(String[] previous, String s) { - return Material.valueOf(s.toUpperCase()); - } - - @Override - public List tabCompletes(CommandSender commandSender, String[] previous, String s) { - return tabCompletes; - } - }; - } - -}