From d028b27ec5940a697cde64e5767595eaf5d5fca9 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 13 Mar 2021 15:58:56 +0100 Subject: [PATCH] Add TypeMapper --- .../src/de/steamwar/command/SWCommand.java | 2 + .../de/steamwar/command/SWCommandUtils.java | 182 ++++++++++++++++-- .../src/de/steamwar/command/TypeMapper.java | 29 +++ 3 files changed, 196 insertions(+), 17 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/command/TypeMapper.java diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java index dbd5a66..350d574 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommand.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommand.java @@ -51,11 +51,13 @@ public abstract class SWCommand { return false; } } + helpMessage.accept(sender); return false; } @Override public List tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException { + // TODO: add better tab complete, command based List strings = new ArrayList<>(subCommandTabCompletes.getOrDefault(args.length, new HashSet<>())); for (InternalTabComplete internalTabComplete : tabCompleteSet) { SWCommandUtils.TabComplete tabComplete = internalTabComplete.invoke(sender, args); diff --git a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java index 09e1436..de7c1d2 100644 --- a/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java +++ b/SpigotCore_Main/src/de/steamwar/command/SWCommandUtils.java @@ -30,6 +30,7 @@ import java.lang.reflect.Parameter; import java.util.*; import java.util.function.BiFunction; import java.util.function.Function; +import java.util.stream.Collectors; class SWCommandUtils { @@ -37,10 +38,18 @@ class SWCommandUtils { throw new IllegalStateException("Utility Class"); } - static final Map, Function> MAPPER_FUNCTIONS = new HashMap<>(); + static final Map, TypeMapper> MAPPER_FUNCTIONS = new HashMap<>(); - static final Function ERROR_FUNCTION = s -> { - throw new SecurityException(s); + static final TypeMapper ERROR_FUNCTION = new TypeMapper<>() { + @Override + public Object map(String s) { + throw new SecurityException(); + } + + @Override + public List tabCompletes(String s) { + return Collections.emptyList(); + } }; static final BiFunction>, String, Enum> ENUM_MAPPER = (enumClass, s) -> { @@ -52,18 +61,157 @@ class SWCommandUtils { }; static { - MAPPER_FUNCTIONS.put(boolean.class, Boolean::parseBoolean); - MAPPER_FUNCTIONS.put(Boolean.class, Boolean::parseBoolean); - MAPPER_FUNCTIONS.put(float.class, Float::parseFloat); - MAPPER_FUNCTIONS.put(Float.class, Float::parseFloat); - MAPPER_FUNCTIONS.put(double.class, Double::parseDouble); - MAPPER_FUNCTIONS.put(Double.class, Double::parseDouble); - MAPPER_FUNCTIONS.put(int.class, Integer::parseInt); - MAPPER_FUNCTIONS.put(Integer.class, Integer::parseInt); - MAPPER_FUNCTIONS.put(String.class, s -> s); - MAPPER_FUNCTIONS.put(StringBuilder.class, StringBuilder::new); - MAPPER_FUNCTIONS.put(Player.class, Bukkit::getPlayer); - MAPPER_FUNCTIONS.put(UUID.class, UUID::fromString); + MAPPER_FUNCTIONS.put(boolean.class, new TypeMapper() { + @Override + public Boolean map(String s) { + return Boolean.parseBoolean(s); + } + + @Override + public List tabCompletes(String s) { + return Arrays.asList("true", "false"); + } + }); + MAPPER_FUNCTIONS.put(Boolean.class, new TypeMapper() { + @Override + public Boolean map(String s) { + return Boolean.parseBoolean(s); + } + + @Override + public List tabCompletes(String s) { + return Arrays.asList("true", "false"); + } + }); + MAPPER_FUNCTIONS.put(float.class, new TypeMapper() { + @Override + public Float map(String s) { + return Float.parseFloat(s); + } + + @Override + public List tabCompletes(String s) { + try { + Float.parseFloat(s); + return Collections.singletonList(s); + } catch (NumberFormatException e) { + return Collections.emptyList(); + } + } + }); + MAPPER_FUNCTIONS.put(Float.class, new TypeMapper() { + @Override + public Float map(String s) { + return Float.parseFloat(s); + } + + @Override + public List tabCompletes(String s) { + try { + Float.parseFloat(s); + return Collections.singletonList(s); + } catch (NumberFormatException e) { + return Collections.emptyList(); + } + } + }); + MAPPER_FUNCTIONS.put(double.class, new TypeMapper() { + @Override + public Double map(String s) { + return Double.parseDouble(s); + } + + @Override + public List tabCompletes(String s) { + try { + Double.parseDouble(s); + return Collections.singletonList(s); + } catch (NumberFormatException e) { + return Collections.emptyList(); + } + } + }); + MAPPER_FUNCTIONS.put(Double.class, new TypeMapper() { + @Override + public Double map(String s) { + return Double.parseDouble(s); + } + + @Override + public List tabCompletes(String s) { + try { + Double.parseDouble(s); + return Collections.singletonList(s); + } catch (NumberFormatException e) { + return Collections.emptyList(); + } + } + }); + MAPPER_FUNCTIONS.put(int.class, new TypeMapper() { + @Override + public Integer map(String s) { + return Integer.parseInt(s); + } + + @Override + public List tabCompletes(String s) { + try { + Integer.parseInt(s); + return Collections.singletonList(s); + } catch (NumberFormatException e) { + return Collections.emptyList(); + } + } + }); + MAPPER_FUNCTIONS.put(Integer.class, new TypeMapper() { + @Override + public Integer map(String s) { + return Integer.parseInt(s); + } + + @Override + public List tabCompletes(String s) { + try { + Integer.parseInt(s); + return Collections.singletonList(s); + } catch (NumberFormatException e) { + return Collections.emptyList(); + } + } + }); + MAPPER_FUNCTIONS.put(String.class, new TypeMapper() { + @Override + public String map(String s) { + return s; + } + + @Override + public List tabCompletes(String s) { + return Collections.singletonList(s); + } + }); + MAPPER_FUNCTIONS.put(StringBuilder.class, new TypeMapper() { + @Override + public StringBuilder map(String s) { + return new StringBuilder(s); + } + + @Override + public List tabCompletes(String s) { + return Collections.singletonList(s); + } + }); + MAPPER_FUNCTIONS.put(Player.class, new TypeMapper() { + @Override + public Player map(String s) { + return Bukkit.getPlayer(s); + } + + @Override + public List tabCompletes(String s) { + return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList()); + } + }); } static final CommandMap commandMap; @@ -119,7 +267,7 @@ class SWCommandUtils { Class> enumClass = (Class>) clazz; return s -> ENUM_MAPPER.apply(enumClass, s); } else { - return s -> MAPPER_FUNCTIONS.getOrDefault(clazz, ERROR_FUNCTION).apply(s); + return s -> MAPPER_FUNCTIONS.getOrDefault(clazz, ERROR_FUNCTION).map(s); } } @@ -131,7 +279,7 @@ class SWCommandUtils { } } - public static void addMapper(Class clazz, Function mapper) { + public static void addMapper(Class clazz, TypeMapper mapper) { if (MAPPER_FUNCTIONS.containsKey(clazz)) return; MAPPER_FUNCTIONS.put(clazz, mapper); } diff --git a/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java b/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java new file mode 100644 index 0000000..119c217 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/command/TypeMapper.java @@ -0,0 +1,29 @@ +/* + * 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 . + */ + +package de.steamwar.command; + +import java.util.List; + +public interface TypeMapper { + + T map(String s); + List tabCompletes(String s); + +}