Update AbstractTypeMapper to allow Collection as return type

Dieser Commit ist enthalten in:
yoyosource 2022-04-27 10:43:00 +02:00
Ursprung f3363ecedf
Commit db254ed13b
3 geänderte Dateien mit 12 neuen und 13 gelöschten Zeilen

Datei anzeigen

@ -1,6 +1,6 @@
package de.steamwar.command; package de.steamwar.command;
import java.util.List; import java.util.Collection;
public interface AbstractTypeMapper<K, T> { public interface AbstractTypeMapper<K, T> {
/** /**
@ -8,5 +8,5 @@ public interface AbstractTypeMapper<K, T> {
*/ */
T map(K sender, String[] previousArguments, String s); T map(K sender, String[] previousArguments, String s);
List<String> tabCompletes(K sender, String[] previousArguments, String s); Collection<String> tabCompletes(K sender, String[] previousArguments, String s);
} }

Datei anzeigen

@ -5,6 +5,7 @@ import lombok.Setter;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.List; import java.util.List;
class CommandPart<T> { class CommandPart<T> {
@ -138,7 +139,7 @@ class CommandPart<T> {
return; return;
} }
} }
List<String> strings = typeMapper.tabCompletes(sender, Arrays.copyOf(args, args.length - 1), args[args.length - 1]); Collection<String> strings = typeMapper.tabCompletes(sender, Arrays.copyOf(args, args.length - 1), args[args.length - 1]);
if (strings != null) { if (strings != null) {
current.addAll(strings); current.addAll(strings);
} }
@ -157,7 +158,7 @@ class CommandPart<T> {
return; return;
} }
List<String> strings = typeMapper.tabCompletes(sender, Arrays.copyOf(args, startIndex), args[startIndex]); Collection<String> strings = typeMapper.tabCompletes(sender, Arrays.copyOf(args, startIndex), args[startIndex]);
if (strings != null) { if (strings != null) {
current.addAll(strings); current.addAll(strings);
} }

Datei anzeigen

@ -9,7 +9,6 @@ import java.lang.reflect.Parameter;
import java.util.*; import java.util.*;
import java.util.function.BiFunction; import java.util.function.BiFunction;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors;
@UtilityClass @UtilityClass
public class SWCommandUtils { public class SWCommandUtils {
@ -198,20 +197,19 @@ public class SWCommandUtils {
} }
public static AbstractTypeMapper<?, Enum<?>> createEnumMapper(Class<Enum<?>> enumClass) { public static AbstractTypeMapper<?, Enum<?>> createEnumMapper(Class<Enum<?>> enumClass) {
Enum<?>[] enums = enumClass.getEnumConstants(); Map<String, Enum<?>> enumMap = new HashMap<>();
List<String> strings = Arrays.stream(enums).map(Enum::name).map(String::toLowerCase).collect(Collectors.toList()); for (Enum<?> e : enumClass.getEnumConstants()) {
enumMap.put(e.name(), e);
}
return new AbstractTypeMapper<Object, Enum<?>>() { return new AbstractTypeMapper<Object, Enum<?>>() {
@Override @Override
public Enum<?> map(Object commandSender, String[] previousArguments, String s) { public Enum<?> map(Object commandSender, String[] previousArguments, String s) {
for (Enum<?> e : enums) { return enumMap.get(s);
if (e.name().equalsIgnoreCase(s)) return e;
}
return null;
} }
@Override @Override
public List<String> tabCompletes(Object commandSender, String[] previousArguments, String s) { public Collection<String> tabCompletes(Object commandSender, String[] previousArguments, String s) {
return strings; return enumMap.keySet();
} }
}; };
} }