From 0674c951aa70482bfb9238a5b87fc5ce9b2ca078 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 30 May 2022 14:52:21 +0200 Subject: [PATCH] Fix SWCommandUtils Add SWTypeMapperCreator --- src/de/steamwar/command/SWCommandUtils.java | 36 ++++++------------- .../steamwar/command/SWTypeMapperCreator.java | 28 +++++++++++++++ 2 files changed, 38 insertions(+), 26 deletions(-) create mode 100644 src/de/steamwar/command/SWTypeMapperCreator.java diff --git a/src/de/steamwar/command/SWCommandUtils.java b/src/de/steamwar/command/SWCommandUtils.java index 73468fa..331b9f9 100644 --- a/src/de/steamwar/command/SWCommandUtils.java +++ b/src/de/steamwar/command/SWCommandUtils.java @@ -38,7 +38,11 @@ public class SWCommandUtils { @Getter private final Map> GUARD_FUNCTIONS = new HashMap<>(); - static { + private SWTypeMapperCreator swTypeMapperCreator; + + public static , K, V> void init(SWTypeMapperCreator 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 , K, V> T createMapper(Function mapper, Function> tabCompleter) { + public static , K, V> T createMapper(Function mapper, Function> tabCompleter) { return createMapper(mapper, (commandSender, s) -> tabCompleter.apply(s)); } - public static , K, V> T createMapper(Function mapper, BiFunction> tabCompleter) { - return (T) new AbstractTypeMapper() { - @Override - public V map(K commandSender, String[] previousArguments, String s) { - return mapper.apply(s); - } - - @Override - public List tabCompletes(K commandSender, String[] previous, String s) { - return tabCompleter.apply(commandSender, s); - } - }; + public static , K, V> T createMapper(Function mapper, BiFunction> tabCompleter) { + return (T) swTypeMapperCreator.createTypeMapper(mapper, tabCompleter); } public static >, K> T createEnumMapper(Class> enumClass) { @@ -220,17 +214,7 @@ public class SWCommandUtils { for (Enum e : enumClass.getEnumConstants()) { enumMap.put(e.name().toLowerCase(), e); } - return (T) new AbstractTypeMapper>() { - @Override - public Enum map(Object commandSender, String[] previousArguments, String s) { - return enumMap.get(s.toLowerCase()); - } - - @Override - public Collection tabCompletes(Object commandSender, String[] previousArguments, String s) { - return enumMap.keySet(); - } - }; + return createMapper(s -> enumMap.get(s.toLowerCase()), (k, s) -> enumMap.keySet()); } private static Function numberMapper(Function mapper) { @@ -249,7 +233,7 @@ public class SWCommandUtils { }; } - private static Function> numberCompleter(Function mapper) { + private static Function> numberCompleter(Function mapper) { return s -> numberMapper(mapper).apply(s) != null ? Collections.singletonList(s) : Collections.emptyList(); diff --git a/src/de/steamwar/command/SWTypeMapperCreator.java b/src/de/steamwar/command/SWTypeMapperCreator.java new file mode 100644 index 0000000..33c5dcb --- /dev/null +++ b/src/de/steamwar/command/SWTypeMapperCreator.java @@ -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 . + */ + +package de.steamwar.command; + +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.Function; + +public interface SWTypeMapperCreator, K, V> { + T createTypeMapper(Function mapper, BiFunction> tabCompleter); +}