CommandFramework3 #94
@ -51,11 +51,13 @@ public abstract class SWCommand {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
helpMessage.accept(sender);
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
|
||||
// TODO: add better tab complete, command based
|
||||
List<String> strings = new ArrayList<>(subCommandTabCompletes.getOrDefault(args.length, new HashSet<>()));
|
||||
for (InternalTabComplete internalTabComplete : tabCompleteSet) {
|
||||
SWCommandUtils.TabComplete tabComplete = internalTabComplete.invoke(sender, args);
|
||||
|
@ -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<Class<?>, Function<String, ?>> MAPPER_FUNCTIONS = new HashMap<>();
|
||||
static final Map<Class<?>, TypeMapper<?>> MAPPER_FUNCTIONS = new HashMap<>();
|
||||
|
||||
static final Function<String, ?> 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<String> tabCompletes(String s) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
};
|
||||
|
||||
static final BiFunction<Class<Enum<?>>, 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<Boolean>() {
|
||||
@Override
|
||||
public Boolean map(String s) {
|
||||
return Boolean.parseBoolean(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(String s) {
|
||||
return Arrays.asList("true", "false");
|
||||
}
|
||||
});
|
||||
MAPPER_FUNCTIONS.put(Boolean.class, new TypeMapper<Boolean>() {
|
||||
@Override
|
||||
public Boolean map(String s) {
|
||||
return Boolean.parseBoolean(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(String s) {
|
||||
return Arrays.asList("true", "false");
|
||||
}
|
||||
});
|
||||
MAPPER_FUNCTIONS.put(float.class, new TypeMapper<Float>() {
|
||||
@Override
|
||||
public Float map(String s) {
|
||||
return Float.parseFloat(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(String s) {
|
||||
try {
|
||||
Float.parseFloat(s);
|
||||
return Collections.singletonList(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
});
|
||||
MAPPER_FUNCTIONS.put(Float.class, new TypeMapper<Float>() {
|
||||
@Override
|
||||
public Float map(String s) {
|
||||
return Float.parseFloat(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(String s) {
|
||||
try {
|
||||
Float.parseFloat(s);
|
||||
return Collections.singletonList(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
});
|
||||
MAPPER_FUNCTIONS.put(double.class, new TypeMapper<Double>() {
|
||||
@Override
|
||||
public Double map(String s) {
|
||||
return Double.parseDouble(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(String s) {
|
||||
try {
|
||||
Double.parseDouble(s);
|
||||
return Collections.singletonList(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
});
|
||||
MAPPER_FUNCTIONS.put(Double.class, new TypeMapper<Double>() {
|
||||
@Override
|
||||
public Double map(String s) {
|
||||
return Double.parseDouble(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(String s) {
|
||||
try {
|
||||
Double.parseDouble(s);
|
||||
return Collections.singletonList(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
});
|
||||
MAPPER_FUNCTIONS.put(int.class, new TypeMapper<Integer>() {
|
||||
@Override
|
||||
public Integer map(String s) {
|
||||
return Integer.parseInt(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(String s) {
|
||||
try {
|
||||
Integer.parseInt(s);
|
||||
return Collections.singletonList(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
});
|
||||
MAPPER_FUNCTIONS.put(Integer.class, new TypeMapper<Integer>() {
|
||||
@Override
|
||||
public Integer map(String s) {
|
||||
return Integer.parseInt(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(String s) {
|
||||
try {
|
||||
Integer.parseInt(s);
|
||||
return Collections.singletonList(s);
|
||||
} catch (NumberFormatException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
});
|
||||
MAPPER_FUNCTIONS.put(String.class, new TypeMapper<String>() {
|
||||
@Override
|
||||
public String map(String s) {
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(String s) {
|
||||
return Collections.singletonList(s);
|
||||
}
|
||||
});
|
||||
MAPPER_FUNCTIONS.put(StringBuilder.class, new TypeMapper<StringBuilder>() {
|
||||
@Override
|
||||
public StringBuilder map(String s) {
|
||||
return new StringBuilder(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(String s) {
|
||||
return Collections.singletonList(s);
|
||||
}
|
||||
});
|
||||
MAPPER_FUNCTIONS.put(Player.class, new TypeMapper<Player>() {
|
||||
@Override
|
||||
public Player map(String s) {
|
||||
return Bukkit.getPlayer(s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabCompletes(String s) {
|
||||
return Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static final CommandMap commandMap;
|
||||
@ -119,7 +267,7 @@ class SWCommandUtils {
|
||||
Class<Enum<?>> enumClass = (Class<Enum<?>>) 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 <K> void addMapper(Class<K> clazz, Function<String, K> mapper) {
|
||||
public static <K> void addMapper(Class<K> clazz, TypeMapper<K> mapper) {
|
||||
if (MAPPER_FUNCTIONS.containsKey(clazz)) return;
|
||||
MAPPER_FUNCTIONS.put(clazz, mapper);
|
||||
}
|
||||
|
29
SpigotCore_Main/src/de/steamwar/command/TypeMapper.java
Normale Datei
29
SpigotCore_Main/src/de/steamwar/command/TypeMapper.java
Normale Datei
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.command;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface TypeMapper<T> {
|
||||
|
||||
T map(String s);
|
||||
List<String> tabCompletes(String s);
|
||||
|
||||
}
|
In neuem Issue referenzieren
Einen Benutzer sperren