forked from SteamWar/SteamWar
Initial copy from other repository (nothing is fixed here)
This commit is contained in:
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2023 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.mapper;
|
||||
|
||||
import de.steamwar.command.AbstractTypeMapper;
|
||||
import de.steamwar.command.PreviousArguments;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
public class BooleanMapper implements AbstractTypeMapper<Object, Boolean> {
|
||||
|
||||
@Override
|
||||
public Boolean map(Object sender, PreviousArguments previousArguments, String s) {
|
||||
if (s.equalsIgnoreCase("true")) return true;
|
||||
if (s.equalsIgnoreCase("false")) return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> tabComplete(Object sender, PreviousArguments previousArguments, String s) {
|
||||
return Arrays.asList("true", "false");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2023 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.mapper;
|
||||
|
||||
import de.steamwar.command.AbstractTypeMapper;
|
||||
import de.steamwar.command.PreviousArguments;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class EnumMapper implements AbstractTypeMapper<Object, Enum<?>> {
|
||||
|
||||
private Map<String, Enum<?>> enumMap;
|
||||
private Set<String> tabCompletions = new HashSet<>();
|
||||
|
||||
public EnumMapper(Class<Enum<?>> enumClass) {
|
||||
Map<String, Enum<?>> enumMap = new HashMap<>();
|
||||
for (Enum<?> e : enumClass.getEnumConstants()) {
|
||||
enumMap.put(e.name().toLowerCase(), e);
|
||||
tabCompletions.add(e.name());
|
||||
}
|
||||
this.enumMap = enumMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enum<?> map(Object sender, PreviousArguments previousArguments, String s) {
|
||||
return enumMap.get(s.toLowerCase());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> tabComplete(Object sender, PreviousArguments previousArguments, String s) {
|
||||
return tabCompletions;
|
||||
}
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2023 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.mapper;
|
||||
|
||||
import de.steamwar.command.AbstractTypeMapper;
|
||||
import de.steamwar.command.PreviousArguments;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class NumberMapper implements AbstractTypeMapper<Object, Number> {
|
||||
|
||||
private Function<String, Number> function;
|
||||
private boolean comma;
|
||||
|
||||
public NumberMapper(Function<String, Number> function, boolean comma) {
|
||||
this.function = function;
|
||||
this.comma = comma;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number map(Object sender, PreviousArguments previousArguments, String s) {
|
||||
if (s.equalsIgnoreCase("nan")) return null;
|
||||
try {
|
||||
return function.apply(s);
|
||||
} catch (NumberFormatException e) {
|
||||
// Ignored
|
||||
}
|
||||
if (!comma) return null;
|
||||
try {
|
||||
return function.apply(s.replace(',', '.'));
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> tabComplete(Object sender, PreviousArguments previousArguments, String s) {
|
||||
List<String> strings = new ArrayList<>();
|
||||
strings.add(s);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
strings.add(s + i);
|
||||
}
|
||||
strings = strings.stream()
|
||||
.map(t -> {
|
||||
Number number = map(sender, previousArguments, t);
|
||||
if (number == null) return null;
|
||||
String value = number + "";
|
||||
if (value.endsWith(".0")) return value.substring(0, value.length() - 2);
|
||||
return value;
|
||||
})
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
if (comma) {
|
||||
Number number = map(sender, previousArguments, s + ".");
|
||||
if (number != null) {
|
||||
String value = number + "";
|
||||
if (value.endsWith(".0")) {
|
||||
strings.add(value.substring(0, value.length() - 1));
|
||||
} else {
|
||||
strings.add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (s.isEmpty()) {
|
||||
strings.add("-");
|
||||
}
|
||||
strings = strings.stream()
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
strings.remove("-0");
|
||||
return strings;
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2023 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.mapper;
|
||||
|
||||
import de.steamwar.command.AbstractTypeMapper;
|
||||
import de.steamwar.command.OptionEnum;
|
||||
import de.steamwar.command.PreviousArguments;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class OptionEnumMapper implements AbstractTypeMapper<Object, Enum<?>> {
|
||||
|
||||
private Class<OptionEnum<?>> enumClass;
|
||||
private List<OptionEnum<?>> optionEnums = new ArrayList<>();
|
||||
|
||||
public <T extends Enum<T> & OptionEnum<T>> OptionEnumMapper(Class<T> enumClass) {
|
||||
this.enumClass = (Class) enumClass;
|
||||
|
||||
for (Enum<?> enumConstant : enumClass.getEnumConstants()) {
|
||||
optionEnums.add((OptionEnum<?>) enumConstant);
|
||||
}
|
||||
}
|
||||
|
||||
private Set<OptionEnum<?>> getRemoved(PreviousArguments previousArguments) {
|
||||
Set<OptionEnum<?>> removed = new HashSet<>();
|
||||
for (OptionEnum<?> optionEnum : previousArguments.getAll(enumClass)) {
|
||||
removed.add(optionEnum);
|
||||
Collections.addAll(removed, (OptionEnum<?>[]) optionEnum.getRemoved());
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Enum<?> map(Object sender, PreviousArguments previousArguments, String s) {
|
||||
Set<OptionEnum<?>> removed = getRemoved(previousArguments);
|
||||
|
||||
for (OptionEnum<?> optionEnum : optionEnums) {
|
||||
if (removed.contains(optionEnum)) {
|
||||
continue;
|
||||
}
|
||||
List<String> strings = optionEnum.getTabCompletes();
|
||||
for (String string : strings) {
|
||||
if (string.equalsIgnoreCase(s)) {
|
||||
return (Enum<?>) optionEnum;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> tabComplete(Object sender, PreviousArguments previousArguments, String s) {
|
||||
Set<OptionEnum<?>> removed = getRemoved(previousArguments);
|
||||
|
||||
List<String> tabCompletes = new ArrayList<>();
|
||||
for (OptionEnum<?> optionEnum : optionEnums) {
|
||||
if (!removed.contains(optionEnum)) {
|
||||
tabCompletes.addAll(optionEnum.getTabCompletes());
|
||||
}
|
||||
}
|
||||
return tabCompletes;
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2023 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.mapper;
|
||||
|
||||
import de.steamwar.command.AbstractTypeMapper;
|
||||
import de.steamwar.command.PreviousArguments;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SimpleMapper implements AbstractTypeMapper<Object, String> {
|
||||
|
||||
private List<String> elements;
|
||||
private Set<String> checkElements;
|
||||
|
||||
public SimpleMapper(List<String> elements) {
|
||||
this.elements = elements;
|
||||
this.checkElements = elements.stream()
|
||||
.map(String::toLowerCase)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public SimpleMapper(String... elements) {
|
||||
this(List.of(elements));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String map(Object sender, PreviousArguments previousArguments, String s) {
|
||||
if (checkElements.contains(s.toLowerCase())) return s;
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> tabComplete(Object sender, PreviousArguments previousArguments, String s) {
|
||||
return elements;
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2023 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.mapper.internal;
|
||||
|
||||
import de.steamwar.command.AbstractTypeMapper;
|
||||
import de.steamwar.command.PreviousArguments;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ContainsFilter<T, K> extends DelegatingMapper<T, K> {
|
||||
|
||||
public ContainsFilter(AbstractTypeMapper<T, K> delegate) {
|
||||
super(delegate, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> tabComplete(T sender, PreviousArguments previousArguments, String s) {
|
||||
Collection<String> tabCompletes = super.tabComplete(sender, previousArguments, s);
|
||||
if (tabCompletes == null) return Collections.emptyList();
|
||||
String t = s.toLowerCase();
|
||||
return tabCompletes.stream()
|
||||
.filter(tabComplete -> tabComplete.toLowerCase().contains(t))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2023 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.mapper.internal;
|
||||
|
||||
import de.steamwar.command.AbstractTypeMapper;
|
||||
import de.steamwar.command.PreviousArguments;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.Collection;
|
||||
|
||||
public class DelegatingMapper<K, T> implements AbstractTypeMapper<K, T> {
|
||||
|
||||
protected final AbstractTypeMapper<K, T> delegate;
|
||||
protected final Annotation annotation;
|
||||
|
||||
public DelegatingMapper(AbstractTypeMapper<K, T> delegate, Annotation annotation) {
|
||||
this.delegate = delegate;
|
||||
this.annotation = annotation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T map(K sender, PreviousArguments previousArguments, String s) {
|
||||
return delegate.map(sender, previousArguments, s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> tabComplete(K sender, PreviousArguments previousArguments, String s) {
|
||||
return delegate.tabComplete(sender, previousArguments, s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean appendNextElementTabCompletions() {
|
||||
return delegate.appendNextElementTabCompletions();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String normalize(K sender, String s) {
|
||||
return delegate.normalize(sender, s);
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2023 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.mapper.internal;
|
||||
|
||||
import de.steamwar.command.AbstractTypeMapper;
|
||||
import de.steamwar.command.PreviousArguments;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StartsWithFilter<T, K> extends DelegatingMapper<T, K> {
|
||||
|
||||
public StartsWithFilter(AbstractTypeMapper<T, K> delegate) {
|
||||
super(delegate, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> tabComplete(T sender, PreviousArguments previousArguments, String s) {
|
||||
Collection<String> tabCompletes = super.tabComplete(sender, previousArguments, s);
|
||||
if (tabCompletes == null) return Collections.emptyList();
|
||||
return tabCompletes.stream()
|
||||
.filter(tabComplete -> tabComplete.toLowerCase().startsWith(s.toLowerCase()) || s.toLowerCase().startsWith(tabComplete.toLowerCase()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user