SteamWar/SpigotCore
Archiviert
13
0

Simplify SubCommand to conform to TypeMapper.tabComplete

Add messages to why it did not work
Dieser Commit ist enthalten in:
yoyosource 2021-03-30 08:55:18 +02:00
Ursprung 1d1d95b78f
Commit 35758f1000
2 geänderte Dateien mit 20 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.command; package de.steamwar.command;
import org.bukkit.Bukkit;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
@ -28,6 +29,7 @@ import java.lang.reflect.Parameter;
import java.util.*; import java.util.*;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.function.IntPredicate; import java.util.function.IntPredicate;
import java.util.logging.Level;
public abstract class SWCommand { public abstract class SWCommand {
@ -94,9 +96,11 @@ public abstract class SWCommand {
return; return;
} }
if (!parameters[parameters.length - 1].isVarArgs()) { if (!parameters[parameters.length - 1].isVarArgs()) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument");
return; return;
} }
if (parameters[parameters.length - 1].getType().getComponentType() != String.class) { if (parameters[parameters.length - 1].getType().getComponentType() != String.class) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
return; return;
} }
commandHelpSet.add(new SubCommand(this, method, anno.value())); commandHelpSet.add(new SubCommand(this, method, anno.value()));
@ -121,7 +125,10 @@ public abstract class SWCommand {
if (mapper != null) { if (mapper != null) {
name = mapper.value(); name = mapper.value();
} }
if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) return; if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name)) {
Bukkit.getLogger().log(Level.WARNING, "The parameter '" + parameter.toString() + "' is using an unsupported Mapper of type '" + name + "'");
return;
}
} }
commandSet.add(new SubCommand(this, method, anno.value())); commandSet.add(new SubCommand(this, method, anno.value()));
}); });
@ -136,12 +143,15 @@ public abstract class SWCommand {
Parameter[] parameters = method.getParameters(); Parameter[] parameters = method.getParameters();
if (!parameterTester.test(parameters.length)) { if (!parameterTester.test(parameters.length)) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking parameters or has too many");
return; return;
} }
if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) { if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'");
return; return;
} }
if (returnType != null && method.getReturnType() != returnType) { if (returnType != null && method.getReturnType() != returnType) {
Bukkit.getLogger().log(Level.WARNING, "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'");
return; return;
} }
consumer.accept(anno, parameters); consumer.accept(anno, parameters);

Datei anzeigen

@ -94,9 +94,9 @@ class SubCommand {
objects[0] = commandSenderFunction.apply(commandSender); objects[0] = commandSenderFunction.apply(commandSender);
method.setAccessible(true); method.setAccessible(true);
method.invoke(swCommand, objects); method.invoke(swCommand, objects);
} catch (IllegalArgumentException | IllegalAccessException e) { } catch (IllegalAccessException | RuntimeException e) {
throw new SecurityException(e.getMessage(), e); throw new SecurityException(e.getMessage(), e);
} catch (InvocationTargetException | RuntimeException e) { } catch (InvocationTargetException e) {
Bukkit.getLogger().log(Level.INFO, e.getMessage(), e); Bukkit.getLogger().log(Level.INFO, e.getMessage(), e);
return false; return false;
} }
@ -105,23 +105,23 @@ class SubCommand {
List<String> tabComplete(CommandSender commandSender, String[] args) { List<String> tabComplete(CommandSender commandSender, String[] args) {
if (!varArgs && args.length < arguments.length - 1) { if (!varArgs && args.length < arguments.length - 1) {
return Collections.emptyList(); return null;
} }
List<String> argsList = new LinkedList<>(Arrays.asList(args)); List<String> argsList = new LinkedList<>(Arrays.asList(args));
for (String value : subCommand) { for (String value : subCommand) {
String s = argsList.remove(0); String s = argsList.remove(0);
if (argsList.isEmpty()) return Collections.singletonList(value); if (argsList.isEmpty()) return Collections.singletonList(value);
if (!value.equals(s)) return Collections.emptyList(); if (!value.equals(s)) return null;
} }
for (TypeMapper<?> argument : arguments) { for (TypeMapper<?> argument : arguments) {
String s = argsList.remove(0); String s = argsList.remove(0);
if (argsList.isEmpty()) return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); if (argsList.isEmpty()) return argument.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
try { try {
if (argument.map(s) == null) { if (argument.map(s) == null) {
return Collections.emptyList(); return null;
} }
} catch (Exception e) { } catch (Exception e) {
return Collections.emptyList(); return null;
} }
} }
if (varArgs && !argsList.isEmpty()) { if (varArgs && !argsList.isEmpty()) {
@ -130,13 +130,13 @@ class SubCommand {
if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s); if (argsList.isEmpty()) return arguments[arguments.length - 1].tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), s);
try { try {
if (arguments[arguments.length - 1].map(s) == null) { if (arguments[arguments.length - 1].map(s) == null) {
return Collections.emptyList(); return null;
} }
} catch (Exception e) { } catch (Exception e) {
return Collections.emptyList(); return null;
} }
} }
} }
return Collections.emptyList(); return null;
} }
} }