Fix SubCommand.tabComplete with noVarArgs returning too early #95
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<Material> materialTypeMapper() {
|
||||
List<String> tabCompletes = Arrays.stream(Material.values())
|
||||
.filter(Material::isSolid)
|
||||
.map(Material::name)
|
||||
.map(String::toLowerCase)
|
||||
.collect(Collectors.toList());
|
||||
return new TypeMapper<Material>() {
|
||||
@Override
|
||||
public Material map(String[] previous, String s) {
|
||||
return Material.valueOf(s.toUpperCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(CommandSender commandSender, String[] previous, String s) {
|
||||
return tabCompletes;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -101,37 +101,41 @@ class SubCommand {
|
||||
}
|
||||
|
||||
List<String> 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<String> 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;
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren