Fix SWCommandUtils

Add SWTypeMapperCreator
Dieser Commit ist enthalten in:
yoyosource 2022-05-30 14:52:21 +02:00 committet von Lixfel
Ursprung f619797824
Commit 800e507330
2 geänderte Dateien mit 38 neuen und 26 gelöschten Zeilen

Datei anzeigen

@ -38,7 +38,11 @@ public class SWCommandUtils {
@Getter
private final Map<String, AbstractGuardChecker<?>> GUARD_FUNCTIONS = new HashMap<>();
static {
private SWTypeMapperCreator swTypeMapperCreator;
public static <T extends AbstractTypeMapper<K, V>, K, V> void init(SWTypeMapperCreator<T, K, V> swTypeMapperCreator) {
SWCommandUtils.swTypeMapperCreator = swTypeMapperCreator;
addMapper(boolean.class, Boolean.class, createMapper(s -> {
if (s.equalsIgnoreCase("true")) return true;
if (s.equalsIgnoreCase("false")) return false;
@ -197,22 +201,12 @@ public class SWCommandUtils {
return createMapper((s) -> strings.contains(s) ? s : null, s -> strings);
}
public static <T extends AbstractTypeMapper<K, V>, K, V> T createMapper(Function<String, V> mapper, Function<String, List<String>> tabCompleter) {
public static <T extends AbstractTypeMapper<K, V>, K, V> T createMapper(Function<String, V> mapper, Function<String, Collection<String>> tabCompleter) {
return createMapper(mapper, (commandSender, s) -> tabCompleter.apply(s));
}
public static <T extends AbstractTypeMapper<K, V>, K, V> T createMapper(Function<String, V> mapper, BiFunction<K, String, List<String>> tabCompleter) {
return (T) new AbstractTypeMapper<K, V>() {
@Override
public V map(K commandSender, String[] previousArguments, String s) {
return mapper.apply(s);
}
@Override
public List<String> tabCompletes(K commandSender, String[] previous, String s) {
return tabCompleter.apply(commandSender, s);
}
};
public static <T extends AbstractTypeMapper<K, V>, K, V> T createMapper(Function<String, V> mapper, BiFunction<K, String, Collection<String>> tabCompleter) {
return (T) swTypeMapperCreator.createTypeMapper(mapper, tabCompleter);
}
public static <T extends AbstractTypeMapper<K, Enum<?>>, K> T createEnumMapper(Class<Enum<?>> enumClass) {
@ -220,17 +214,7 @@ public class SWCommandUtils {
for (Enum<?> e : enumClass.getEnumConstants()) {
enumMap.put(e.name().toLowerCase(), e);
}
return (T) new AbstractTypeMapper<Object, Enum<?>>() {
@Override
public Enum<?> map(Object commandSender, String[] previousArguments, String s) {
return enumMap.get(s.toLowerCase());
}
@Override
public Collection<String> tabCompletes(Object commandSender, String[] previousArguments, String s) {
return enumMap.keySet();
}
};
return createMapper(s -> enumMap.get(s.toLowerCase()), (k, s) -> enumMap.keySet());
}
private static <T> Function<String, T> numberMapper(Function<String, T> mapper) {
@ -249,7 +233,7 @@ public class SWCommandUtils {
};
}
private static Function<String, List<String>> numberCompleter(Function<String, ?> mapper) {
private static Function<String, Collection<String>> numberCompleter(Function<String, ?> mapper) {
return s -> numberMapper(mapper).apply(s) != null
? Collections.singletonList(s)
: Collections.emptyList();

Datei anzeigen

@ -0,0 +1,28 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 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.command;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
public interface SWTypeMapperCreator<T extends AbstractTypeMapper<K, V>, K, V> {
T createTypeMapper(Function<String, V> mapper, BiFunction<K, String, List<String>> tabCompleter);
}