Implement CommandFramework via CommonCore
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed
Dieser Commit ist enthalten in:
Ursprung
da67bbcad1
Commit
1bb1c4a4c0
@ -1 +1 @@
|
|||||||
Subproject commit e83103d8afce8161a12d008d6ad99ba439243b27
|
Subproject commit 2d52bcbc60168e0e98219a32b4e7dfdc3d7ff485
|
@ -40,16 +40,6 @@ sourceSets {
|
|||||||
exclude '**/*.java', '**/*.kt'
|
exclude '**/*.java', '**/*.kt'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
test {
|
|
||||||
java {
|
|
||||||
srcDirs = ['testsrc']
|
|
||||||
}
|
|
||||||
resources {
|
|
||||||
srcDirs = ['testsrc']
|
|
||||||
exclude '**/*.java', '**/*.kt'
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@ -68,6 +58,8 @@ dependencies {
|
|||||||
testImplementation files("${project.rootDir}/lib/Spigot-1.15.jar")
|
testImplementation files("${project.rootDir}/lib/Spigot-1.15.jar")
|
||||||
testImplementation files("${project.rootDir}/lib/WorldEdit-1.12.jar")
|
testImplementation files("${project.rootDir}/lib/WorldEdit-1.12.jar")
|
||||||
|
|
||||||
|
compileOnly project(":CommonCore")
|
||||||
|
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
testImplementation 'org.hamcrest:hamcrest:2.2'
|
testImplementation 'org.hamcrest:hamcrest:2.2'
|
||||||
}
|
}
|
||||||
|
@ -1,76 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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.io.PrintStream;
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
|
|
||||||
public class CommandFrameworkException extends RuntimeException {
|
|
||||||
|
|
||||||
private InvocationTargetException invocationTargetException;
|
|
||||||
private String alias;
|
|
||||||
private String[] args;
|
|
||||||
|
|
||||||
private String message;
|
|
||||||
|
|
||||||
CommandFrameworkException(InvocationTargetException invocationTargetException, String alias, String[] args) {
|
|
||||||
super(invocationTargetException);
|
|
||||||
this.invocationTargetException = invocationTargetException;
|
|
||||||
this.alias = alias;
|
|
||||||
this.args = args;
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized String getBuildStackTrace() {
|
|
||||||
if (message != null) {
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
StackTraceElement[] stackTraceElements = invocationTargetException.getCause().getStackTrace();
|
|
||||||
StringBuilder st = new StringBuilder();
|
|
||||||
st.append(invocationTargetException.getCause().getClass().getTypeName());
|
|
||||||
if (invocationTargetException.getCause().getMessage() != null) {
|
|
||||||
st.append(": ").append(invocationTargetException.getCause().getMessage());
|
|
||||||
}
|
|
||||||
st.append("\n");
|
|
||||||
if (alias != null && !alias.isEmpty()) {
|
|
||||||
st.append("Performed command: ").append(alias).append(" ").append(String.join(" ", args)).append("\n");
|
|
||||||
}
|
|
||||||
for (int i = 0; i < stackTraceElements.length - invocationTargetException.getStackTrace().length; i++) {
|
|
||||||
st.append("\tat ").append(stackTraceElements[i].toString()).append("\n");
|
|
||||||
}
|
|
||||||
message = st.toString();
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void printStackTrace() {
|
|
||||||
printStackTrace(System.err);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void printStackTrace(PrintStream s) {
|
|
||||||
s.print(getBuildStackTrace());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void printStackTrace(PrintWriter s) {
|
|
||||||
s.print(getBuildStackTrace());
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
class CommandNoHelpException extends RuntimeException {
|
|
||||||
|
|
||||||
CommandNoHelpException() {}
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
public class CommandParseException extends RuntimeException {
|
|
||||||
|
|
||||||
public CommandParseException() {
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,217 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 lombok.AllArgsConstructor;
|
|
||||||
import lombok.Setter;
|
|
||||||
import lombok.ToString;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
import java.lang.reflect.Array;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@ToString
|
|
||||||
public class CommandPart {
|
|
||||||
private static final String[] EMPTY_ARRAY = new String[0];
|
|
||||||
|
|
||||||
@AllArgsConstructor
|
|
||||||
private static class CheckArgumentResult {
|
|
||||||
private final boolean success;
|
|
||||||
private final Object value;
|
|
||||||
}
|
|
||||||
|
|
||||||
private TypeMapper<?> typeMapper;
|
|
||||||
private GuardChecker guard;
|
|
||||||
private Class<?> varArgType;
|
|
||||||
private String optional;
|
|
||||||
private GuardCheckType guardCheckType;
|
|
||||||
|
|
||||||
private CommandPart next = null;
|
|
||||||
|
|
||||||
@Setter
|
|
||||||
private boolean ignoreAsArgument = false;
|
|
||||||
|
|
||||||
public CommandPart(TypeMapper<?> typeMapper, GuardChecker guard, Class<?> varArgType, String optional, GuardCheckType guardCheckType) {
|
|
||||||
this.typeMapper = typeMapper;
|
|
||||||
this.guard = guard;
|
|
||||||
this.varArgType = varArgType;
|
|
||||||
this.optional = optional;
|
|
||||||
this.guardCheckType = guardCheckType;
|
|
||||||
|
|
||||||
validatePart();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setNext(CommandPart next) {
|
|
||||||
if (varArgType != null) {
|
|
||||||
throw new IllegalArgumentException("There can't be a next part if this is a vararg part!");
|
|
||||||
}
|
|
||||||
this.next = next;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void validatePart() {
|
|
||||||
if (guardCheckType == GuardCheckType.TAB_COMPLETE) {
|
|
||||||
throw new IllegalArgumentException("Tab complete is not allowed as a guard check type!");
|
|
||||||
}
|
|
||||||
if (optional != null && varArgType != null) {
|
|
||||||
throw new IllegalArgumentException("A vararg part can't have an optional part!");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (optional != null) {
|
|
||||||
try {
|
|
||||||
typeMapper.map(null, EMPTY_ARRAY, optional);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new IllegalArgumentException("The optional part is not valid!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void generateArgumentArray(List<Object> current, CommandSender commandSender, String[] args, int startIndex) {
|
|
||||||
if (varArgType != null) {
|
|
||||||
Object array = Array.newInstance(varArgType, args.length - startIndex);
|
|
||||||
for (int i = startIndex; i < args.length; i++) {
|
|
||||||
CheckArgumentResult validArgument = checkArgument(null, commandSender, args, i);
|
|
||||||
if (!validArgument.success) {
|
|
||||||
throw new CommandParseException();
|
|
||||||
}
|
|
||||||
Array.set(array, i - startIndex, validArgument.value);
|
|
||||||
}
|
|
||||||
current.add(array);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
CheckArgumentResult validArgument = checkArgument(null, commandSender, args, startIndex);
|
|
||||||
if (!validArgument.success && optional == null) {
|
|
||||||
throw new CommandParseException();
|
|
||||||
}
|
|
||||||
if (!validArgument.success) {
|
|
||||||
if (!ignoreAsArgument) {
|
|
||||||
current.add(typeMapper.map(commandSender, EMPTY_ARRAY, optional));
|
|
||||||
}
|
|
||||||
if (next != null) {
|
|
||||||
next.generateArgumentArray(current, commandSender, args, startIndex);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!ignoreAsArgument) {
|
|
||||||
current.add(validArgument.value);
|
|
||||||
}
|
|
||||||
if (next != null) {
|
|
||||||
next.generateArgumentArray(current, commandSender, args, startIndex + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean guardCheck(CommandSender commandSender, String[] args, int startIndex) {
|
|
||||||
if (varArgType != null) {
|
|
||||||
for (int i = startIndex; i < args.length; i++) {
|
|
||||||
GuardResult guardResult = checkGuard(guardCheckType, commandSender, args, i);
|
|
||||||
if (guardResult == GuardResult.DENIED) {
|
|
||||||
throw new CommandNoHelpException();
|
|
||||||
}
|
|
||||||
if (guardResult == GuardResult.DENIED_WITH_HELP) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
GuardResult guardResult = checkGuard(guardCheckType, commandSender, args, startIndex);
|
|
||||||
if (guardResult == GuardResult.DENIED) {
|
|
||||||
if (optional != null && next != null) {
|
|
||||||
return next.guardCheck(commandSender, args, startIndex);
|
|
||||||
}
|
|
||||||
throw new CommandNoHelpException();
|
|
||||||
}
|
|
||||||
if (guardResult == GuardResult.DENIED_WITH_HELP) {
|
|
||||||
if (optional != null && next != null) {
|
|
||||||
return next.guardCheck(commandSender, args, startIndex);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (next != null) {
|
|
||||||
return next.guardCheck(commandSender, args, startIndex + 1);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void generateTabComplete(List<String> current, CommandSender commandSender, String[] args, int startIndex) {
|
|
||||||
if (varArgType != null) {
|
|
||||||
for (int i = startIndex; i < args.length - 1; i++) {
|
|
||||||
CheckArgumentResult validArgument = checkArgument(null, commandSender, args, i);
|
|
||||||
if (!validArgument.success) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
List<String> strings = typeMapper.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), args[args.length - 1]);
|
|
||||||
if (strings != null) {
|
|
||||||
current.addAll(strings);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (args.length - 1 > startIndex) {
|
|
||||||
CheckArgumentResult checkArgumentResult = checkArgument(GuardCheckType.TAB_COMPLETE, commandSender, args, startIndex);
|
|
||||||
if (checkArgumentResult.success && next != null) {
|
|
||||||
next.generateTabComplete(current, commandSender, args, startIndex + 1);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (optional != null && next != null) {
|
|
||||||
next.generateTabComplete(current, commandSender, args, startIndex);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> strings = typeMapper.tabCompletes(commandSender, Arrays.copyOf(args, startIndex), args[startIndex]);
|
|
||||||
if (strings != null) {
|
|
||||||
current.addAll(strings);
|
|
||||||
}
|
|
||||||
if (optional != null && next != null) {
|
|
||||||
next.generateTabComplete(current, commandSender, args, startIndex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private CheckArgumentResult checkArgument(GuardCheckType guardCheckType, CommandSender commandSender, String[] args, int index) {
|
|
||||||
try {
|
|
||||||
Object value = typeMapper.map(commandSender, Arrays.copyOf(args, index), args[index]);
|
|
||||||
if (value == null) {
|
|
||||||
return new CheckArgumentResult(false, null);
|
|
||||||
}
|
|
||||||
GuardResult guardResult = checkGuard(guardCheckType, commandSender, args, index);
|
|
||||||
switch (guardResult) {
|
|
||||||
case ALLOWED:
|
|
||||||
return new CheckArgumentResult(true, value);
|
|
||||||
case DENIED:
|
|
||||||
throw new CommandNoHelpException();
|
|
||||||
case DENIED_WITH_HELP:
|
|
||||||
default:
|
|
||||||
return new CheckArgumentResult(false, null);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
return new CheckArgumentResult(false, null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private GuardResult checkGuard(GuardCheckType guardCheckType, CommandSender commandSender, String[] args, int index) {
|
|
||||||
if (guard != null && guardCheckType != null) {
|
|
||||||
return guard.guard(commandSender, guardCheckType, Arrays.copyOf(args, index), args[index]);
|
|
||||||
}
|
|
||||||
return GuardResult.ALLOWED;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,26 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
public enum GuardCheckType {
|
|
||||||
COMMAND,
|
|
||||||
HELP_COMMAND,
|
|
||||||
TAB_COMPLETE
|
|
||||||
}
|
|
@ -22,7 +22,7 @@ package de.steamwar.command;
|
|||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface GuardChecker {
|
public interface GuardChecker extends AbstractGuardChecker<CommandSender> {
|
||||||
/**
|
/**
|
||||||
* While guarding the first parameter of the command the parameter s of this method is {@code null}
|
* While guarding the first parameter of the command the parameter s of this method is {@code null}
|
||||||
*/
|
*/
|
||||||
|
@ -1,26 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
public enum GuardResult {
|
|
||||||
ALLOWED,
|
|
||||||
DENIED_WITH_HELP,
|
|
||||||
DENIED
|
|
||||||
}
|
|
@ -28,58 +28,35 @@ import org.bukkit.command.Command;
|
|||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
import java.util.ArrayList;
|
||||||
import java.lang.reflect.Method;
|
import java.util.Arrays;
|
||||||
import java.lang.reflect.Parameter;
|
import java.util.List;
|
||||||
import java.util.*;
|
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.Supplier;
|
||||||
import java.util.function.IntPredicate;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public abstract class SWCommand {
|
public class SWCommand extends AbstractSWCommand<CommandSender> {
|
||||||
|
|
||||||
private boolean initialized = false;
|
static {
|
||||||
private final Command command;
|
TypeUtils.init();
|
||||||
private final List<SubCommand> commandList = new ArrayList<>();
|
}
|
||||||
private final List<SubCommand> commandHelpList = new ArrayList<>();
|
|
||||||
private final Map<String, TypeMapper<?>> localTypeMapper = new HashMap<>();
|
private Command command;
|
||||||
private final Map<String, GuardChecker> localGuardChecker = new HashMap<>();
|
|
||||||
|
|
||||||
@Setter
|
@Setter
|
||||||
private Message message = null;
|
private Message message = null;
|
||||||
private List<String> defaultHelpMessages = new ArrayList<>();
|
private List<String> defaultHelpMessages = new ArrayList<>();
|
||||||
|
|
||||||
protected SWCommand(String command) {
|
protected SWCommand(String command) {
|
||||||
this(command, new String[0]);
|
super(CommandSender.class, command);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected SWCommand(String command, String... aliases) {
|
protected SWCommand(String command, String[] aliases) {
|
||||||
this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) {
|
super(CommandSender.class, command, aliases);
|
||||||
@Override
|
|
||||||
public boolean execute(CommandSender sender, String alias, String[] args) {
|
|
||||||
if (!initialized) {
|
|
||||||
createMapping();
|
|
||||||
}
|
|
||||||
SWCommand.this.execute(sender, alias, args);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
|
|
||||||
if (!initialized) {
|
|
||||||
createMapping();
|
|
||||||
}
|
|
||||||
return SWCommand.this.tabComplete(sender, alias, args);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
unregister();
|
|
||||||
register();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is used for the tests!
|
@Override
|
||||||
SWCommand(boolean noRegister, String command, String... aliases) {
|
protected void createAndSafeCommand(String command, String[] aliases) {
|
||||||
this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) {
|
this.command = new Command(command, "", "/" + command, Arrays.asList(aliases)) {
|
||||||
@Override
|
@Override
|
||||||
public boolean execute(CommandSender sender, String alias, String[] args) {
|
public boolean execute(CommandSender sender, String alias, String[] args) {
|
||||||
@ -92,171 +69,33 @@ public abstract class SWCommand {
|
|||||||
return SWCommand.this.tabComplete(sender, alias, args);
|
return SWCommand.this.tabComplete(sender, alias, args);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
createMapping();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void execute(CommandSender sender, String alias, String[] args) {
|
@Override
|
||||||
try {
|
public void unregister() {
|
||||||
if (!commandList.stream().anyMatch(s -> s.invoke(sender, alias, args))) {
|
CommandRegistering.unregister(this.command);
|
||||||
commandHelpList.stream().anyMatch(s -> s.invoke(sender, alias, args));
|
|
||||||
}
|
|
||||||
} catch (CommandNoHelpException e) {
|
|
||||||
// Ignored
|
|
||||||
} catch (CommandFrameworkException e) {
|
|
||||||
if (Bukkit.getServer() != null) {
|
|
||||||
Bukkit.getLogger().log(Level.SEVERE, "", e);
|
|
||||||
Core.MESSAGE.sendPrefixless("COMMAND_SYSTEM_ERROR", sender);
|
|
||||||
}
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
|
@Override
|
||||||
String string = args[args.length - 1].toLowerCase();
|
public void register() {
|
||||||
return commandList.stream()
|
CommandRegistering.register(this.command);
|
||||||
.filter(s -> !s.noTabComplete)
|
|
||||||
.map(s -> s.tabComplete(sender, args))
|
|
||||||
.filter(Objects::nonNull)
|
|
||||||
.flatMap(Collection::stream)
|
|
||||||
.filter(s -> !s.isEmpty())
|
|
||||||
.filter(s -> s.toLowerCase().startsWith(string))
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized void createMapping() {
|
@Override
|
||||||
List<Method> methods = methods();
|
protected void commandSystemError(CommandSender sender, CommandFrameworkException e) {
|
||||||
for (Method method : methods) {
|
Bukkit.getLogger().log(Level.SEVERE, e.getMessage(), e);
|
||||||
addMapper(Mapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
|
Core.MESSAGE.sendPrefixless("COMMAND_SYSTEM_ERROR", sender);
|
||||||
(anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value(), typeMapper);
|
|
||||||
});
|
|
||||||
addMapper(ClassMapper.class, method, i -> i == 0, false, TypeMapper.class, (anno, typeMapper) -> {
|
|
||||||
(anno.local() ? localTypeMapper : SWCommandUtils.MAPPER_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), typeMapper);
|
|
||||||
});
|
|
||||||
addGuard(Guard.class, method, i -> i == 0, false, GuardChecker.class, (anno, guardChecker) -> {
|
|
||||||
(anno.local() ? localGuardChecker : SWCommandUtils.GUARD_FUNCTIONS).putIfAbsent(anno.value(), guardChecker);
|
|
||||||
});
|
|
||||||
addGuard(ClassGuard.class, method, i -> i == 0, false, GuardChecker.class, (anno, guardChecker) -> {
|
|
||||||
(anno.local() ? localGuardChecker : SWCommandUtils.GUARD_FUNCTIONS).putIfAbsent(anno.value().getTypeName(), guardChecker);
|
|
||||||
});
|
|
||||||
|
|
||||||
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
|
||||||
if (!anno.help()) return;
|
|
||||||
if (parameters.length != 2) {
|
|
||||||
Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking parameters or has too many");
|
|
||||||
}
|
|
||||||
if (!parameters[parameters.length - 1].isVarArgs()) {
|
|
||||||
Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the varArgs parameters as last Argument");
|
|
||||||
}
|
|
||||||
if (parameters[parameters.length - 1].getType().getComponentType() != String.class) {
|
|
||||||
Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the varArgs parameters of type '" + String.class.getTypeName() + "' as last Argument");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
commandHelpList.add(new SubCommand(this, method, anno.value(), new HashMap<>(), localGuardChecker, true, null, anno.noTabComplete()));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
for (Method method : methods) {
|
|
||||||
add(Register.class, method, i -> i > 0, true, null, (anno, parameters) -> {
|
|
||||||
if (anno.help()) return;
|
|
||||||
for (int i = 1; i < parameters.length; i++) {
|
|
||||||
Parameter parameter = parameters[i];
|
|
||||||
Class<?> clazz = parameter.getType();
|
|
||||||
if (parameter.isVarArgs() && i == parameters.length - 1) {
|
|
||||||
clazz = parameter.getType().getComponentType();
|
|
||||||
}
|
|
||||||
Mapper mapper = parameter.getAnnotation(Mapper.class);
|
|
||||||
if (clazz.isEnum() && mapper == null && !SWCommandUtils.MAPPER_FUNCTIONS.containsKey(clazz.getTypeName())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
String name = mapper != null ? mapper.value() : clazz.getTypeName();
|
|
||||||
if (!SWCommandUtils.MAPPER_FUNCTIONS.containsKey(name) && !localTypeMapper.containsKey(name)) {
|
|
||||||
Bukkit.getLogger().log(Level.WARNING, () -> "The parameter '" + parameter.toString() + "' is using an unsupported Mapper of type '" + name + "'");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
commandList.add(new SubCommand(this, method, anno.value(), localTypeMapper, localGuardChecker, false, anno.description(), anno.noTabComplete()));
|
|
||||||
});
|
|
||||||
|
|
||||||
this.commandList.sort((o1, o2) -> {
|
|
||||||
int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length);
|
|
||||||
if (compare != 0) {
|
|
||||||
return compare;
|
|
||||||
} else {
|
|
||||||
return Integer.compare(o1.comparableValue, o2.comparableValue);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
commandHelpList.sort((o1, o2) -> {
|
|
||||||
int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length);
|
|
||||||
if (compare != 0) {
|
|
||||||
return compare;
|
|
||||||
} else {
|
|
||||||
return Integer.compare(o1.method.getDeclaringClass() == SWCommand.class ? 1 : 0,
|
|
||||||
o2.method.getDeclaringClass() == SWCommand.class ? 1 : 0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
initialized = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T extends Annotation> void add(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, Parameter[]> consumer) {
|
@Override
|
||||||
T[] anno = SWCommandUtils.getAnnotation(method, annotation);
|
protected void commandSystemWarning(Supplier<String> message) {
|
||||||
if (anno == null || anno.length == 0) return;
|
Bukkit.getLogger().log(Level.WARNING, message);
|
||||||
|
|
||||||
Parameter[] parameters = method.getParameters();
|
|
||||||
if (!parameterTester.test(parameters.length)) {
|
|
||||||
Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking parameters or has too many");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (firstParameter && !CommandSender.class.isAssignableFrom(parameters[0].getType())) {
|
|
||||||
Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the first parameter of type '" + CommandSender.class.getTypeName() + "'");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (returnType != null && method.getReturnType() != returnType) {
|
|
||||||
Bukkit.getLogger().log(Level.WARNING, () -> "The method '" + method.toString() + "' is lacking the desired return type '" + returnType.getTypeName() + "'");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Arrays.stream(anno).forEach(t -> consumer.accept(t, parameters));
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T extends Annotation> void addMapper(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, TypeMapper<?>> consumer) {
|
|
||||||
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
|
|
||||||
try {
|
|
||||||
method.setAccessible(true);
|
|
||||||
consumer.accept(anno, (TypeMapper<?>) method.invoke(this));
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new SecurityException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private <T extends Annotation> void addGuard(Class<T> annotation, Method method, IntPredicate parameterTester, boolean firstParameter, Class<?> returnType, BiConsumer<T, GuardChecker> consumer) {
|
|
||||||
add(annotation, method, parameterTester, firstParameter, returnType, (anno, parameters) -> {
|
|
||||||
try {
|
|
||||||
method.setAccessible(true);
|
|
||||||
consumer.accept(anno, (GuardChecker) method.invoke(this));
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new SecurityException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addDefaultHelpMessage(String message) {
|
public void addDefaultHelpMessage(String message) {
|
||||||
defaultHelpMessages.add(message);
|
defaultHelpMessages.add(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Method> methods() {
|
|
||||||
List<Method> methods = new ArrayList<>(Arrays.asList(getClass().getDeclaredMethods()));
|
|
||||||
methods.addAll(Arrays.asList(SWCommand.class.getDeclaredMethods()));
|
|
||||||
return methods;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void unregister() {
|
|
||||||
CommandRegistering.unregister(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void register() {
|
|
||||||
CommandRegistering.register(command);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Register(help = true)
|
@Register(help = true)
|
||||||
private void internalHelp(Player p, String... args) {
|
private void internalHelp(Player p, String... args) {
|
||||||
if (message == null) {
|
if (message == null) {
|
||||||
@ -295,7 +134,7 @@ public abstract class SWCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void send(Player p, SubCommand subCommand) {
|
private void send(Player p, SubCommand<CommandSender> subCommand) {
|
||||||
try {
|
try {
|
||||||
for (String s : subCommand.description) {
|
for (String s : subCommand.description) {
|
||||||
String hover = "§8/§e" + command.getName() + " " + String.join(" ", subCommand.subCommand);
|
String hover = "§8/§e" + command.getName() + " " + String.join(" ", subCommand.subCommand);
|
||||||
@ -306,80 +145,4 @@ public abstract class SWCommand {
|
|||||||
Bukkit.getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e);
|
Bukkit.getLogger().log(Level.WARNING, "Failed to send description of registered method '" + subCommand.method + "' with description '" + subCommand.description + "'", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target({ElementType.METHOD})
|
|
||||||
@Repeatable(Register.Registeres.class)
|
|
||||||
protected @interface Register {
|
|
||||||
String[] value() default {};
|
|
||||||
|
|
||||||
boolean help() default false;
|
|
||||||
|
|
||||||
String[] description() default {};
|
|
||||||
|
|
||||||
boolean noTabComplete() default false;
|
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target({ElementType.METHOD})
|
|
||||||
@interface Registeres {
|
|
||||||
Register[] value();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
|
||||||
protected @interface Mapper {
|
|
||||||
String value();
|
|
||||||
|
|
||||||
boolean local() default false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target({ElementType.METHOD})
|
|
||||||
protected @interface ClassMapper {
|
|
||||||
Class<?> value();
|
|
||||||
|
|
||||||
boolean local() default false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target({ElementType.PARAMETER, ElementType.METHOD})
|
|
||||||
protected @interface Guard {
|
|
||||||
String value() default "";
|
|
||||||
|
|
||||||
boolean local() default false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target({ElementType.METHOD})
|
|
||||||
protected @interface ClassGuard {
|
|
||||||
Class<?> value();
|
|
||||||
|
|
||||||
boolean local() default false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target({ElementType.PARAMETER})
|
|
||||||
protected @interface StaticValue {
|
|
||||||
String[] value();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is the short form for 'allowImplicitSwitchExpressions'
|
|
||||||
* and can be set to true if you want to allow int as well as boolean as annotated parameter types.
|
|
||||||
* The value array needs to be at least 2 long for this flag to be considered.
|
|
||||||
* While using an int, the value will represent the index into the value array.
|
|
||||||
* While using a boolean, the value array must only be 2 long and the value will be {@code false}
|
|
||||||
* for the first index and {@code true} for the second index.
|
|
||||||
*/
|
|
||||||
boolean allowISE() default false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
@Target({ElementType.PARAMETER})
|
|
||||||
protected @interface OptionalValue {
|
|
||||||
/**
|
|
||||||
* Will pe parsed against the TypeMapper specified by the parameter or annotation.
|
|
||||||
*/
|
|
||||||
String value();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,312 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 de.steamwar.sql.BauweltMember;
|
|
||||||
import de.steamwar.sql.SchematicNode;
|
|
||||||
import de.steamwar.sql.SteamwarUser;
|
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.GameMode;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Parameter;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.function.BiFunction;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
public class SWCommandUtils {
|
|
||||||
|
|
||||||
private SWCommandUtils() {
|
|
||||||
throw new IllegalStateException("Utility Class");
|
|
||||||
}
|
|
||||||
|
|
||||||
static final Map<String, TypeMapper<?>> MAPPER_FUNCTIONS = new HashMap<>();
|
|
||||||
static final Map<String, GuardChecker> GUARD_FUNCTIONS = new HashMap<>();
|
|
||||||
|
|
||||||
static {
|
|
||||||
addMapper(boolean.class, Boolean.class, createMapper(Boolean::parseBoolean, s -> Arrays.asList("true", "false")));
|
|
||||||
addMapper(float.class, Float.class, createMapper(numberMapper(Float::parseFloat), numberCompleter(Float::parseFloat)));
|
|
||||||
addMapper(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble)));
|
|
||||||
addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt)));
|
|
||||||
addMapper(long.class, Long.class, createMapper(numberMapper(Long::parseLong), numberCompleter(Long::parseLong)));
|
|
||||||
MAPPER_FUNCTIONS.put(String.class.getTypeName(), createMapper(s -> s, Collections::singletonList));
|
|
||||||
MAPPER_FUNCTIONS.put(Player.class.getTypeName(), createMapper(Bukkit::getPlayer, s -> Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList())));
|
|
||||||
MAPPER_FUNCTIONS.put(GameMode.class.getTypeName(), createMapper(s -> {
|
|
||||||
s = s.toLowerCase();
|
|
||||||
if (s.equals("s") || s.equals("survival") || s.equals("0")) return GameMode.SURVIVAL;
|
|
||||||
if (s.equals("c") || s.equals("creative") || s.equals("1")) return GameMode.CREATIVE;
|
|
||||||
if (s.equals("sp") || s.equals("spectator") || s.equals("3")) return GameMode.SPECTATOR;
|
|
||||||
if (s.equals("a") || s.equals("adventure") || s.equals("2")) return GameMode.ADVENTURE;
|
|
||||||
return null;
|
|
||||||
}, s -> Arrays.asList("s", "survival", "0", "c", "creative", "1", "sp", "spectator", "3", "a", "adventure", "2")));
|
|
||||||
MAPPER_FUNCTIONS.put(SteamwarUser.class.getTypeName(), createMapper(SteamwarUser::get, s -> Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList())));
|
|
||||||
MAPPER_FUNCTIONS.put(SchematicNode.class.getTypeName(), new TypeMapper<SchematicNode>() {
|
|
||||||
@Override
|
|
||||||
public SchematicNode map(CommandSender commandSender, String[] previousArguments, String s) {
|
|
||||||
return SchematicNode.getNodeFromPath(SteamwarUser.get(((Player) commandSender).getUniqueId()), s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> tabCompletes(CommandSender commandSender, String[] strings, String s) {
|
|
||||||
return SchematicNode.getNodeTabcomplete(SteamwarUser.get(((Player) commandSender).getUniqueId()), s);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
MAPPER_FUNCTIONS.put(BauweltMember.class.getTypeName(), new TypeMapper<BauweltMember>() {
|
|
||||||
@Override
|
|
||||||
public BauweltMember map(CommandSender commandSender, String[] previousArguments, String s) {
|
|
||||||
if (!(commandSender instanceof Player)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
Player player = (Player) commandSender;
|
|
||||||
return BauweltMember.getMembers(player.getUniqueId())
|
|
||||||
.stream()
|
|
||||||
.filter(member -> SteamwarUser.get(member.getMemberID()).getUserName().equalsIgnoreCase(s))
|
|
||||||
.findAny()
|
|
||||||
.orElse(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> tabCompletes(CommandSender commandSender, String[] previousArguments, String s) {
|
|
||||||
if (!(commandSender instanceof Player)) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
Player player = (Player) commandSender;
|
|
||||||
return BauweltMember.getMembers(player.getUniqueId())
|
|
||||||
.stream()
|
|
||||||
.map(m -> SteamwarUser.get(m.getMemberID()).getUserName())
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void addMapper(Class<?> clazz, Class<?> alternativeClazz, TypeMapper<?> mapper) {
|
|
||||||
MAPPER_FUNCTIONS.put(clazz.getTypeName(), mapper);
|
|
||||||
MAPPER_FUNCTIONS.put(alternativeClazz.getTypeName(), mapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
static CommandPart generateCommandPart(boolean help, String[] subCommand, Parameter[] parameters, Map<String, TypeMapper<?>> localTypeMapper, Map<String, GuardChecker> localGuardChecker) {
|
|
||||||
CommandPart first = null;
|
|
||||||
CommandPart current = null;
|
|
||||||
for (String s : subCommand) {
|
|
||||||
CommandPart commandPart = new CommandPart(createMapper(s), null, null, null, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND);
|
|
||||||
commandPart.setIgnoreAsArgument(true);
|
|
||||||
if (current != null) {
|
|
||||||
current.setNext(commandPart);
|
|
||||||
}
|
|
||||||
current = commandPart;
|
|
||||||
if (first == null) {
|
|
||||||
first = current;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = 1; i < parameters.length; i++) {
|
|
||||||
Parameter parameter = parameters[i];
|
|
||||||
TypeMapper<?> typeMapper = getTypeMapper(parameter, localTypeMapper);
|
|
||||||
GuardChecker guardChecker = getGuardChecker(parameter, localGuardChecker);
|
|
||||||
Class<?> varArgType = parameter.isVarArgs() ? parameter.getType().getComponentType() : null;
|
|
||||||
SWCommand.OptionalValue optionalValue = parameter.getAnnotation(SWCommand.OptionalValue.class);
|
|
||||||
|
|
||||||
CommandPart commandPart = new CommandPart(typeMapper, guardChecker, varArgType, optionalValue != null ? optionalValue.value() : null, help ? GuardCheckType.HELP_COMMAND : GuardCheckType.COMMAND);
|
|
||||||
if (current != null) {
|
|
||||||
current.setNext(commandPart);
|
|
||||||
}
|
|
||||||
current = commandPart;
|
|
||||||
if (first == null) {
|
|
||||||
first = current;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return first;
|
|
||||||
}
|
|
||||||
|
|
||||||
static TypeMapper<?> getTypeMapper(Parameter parameter, Map<String, TypeMapper<?>> localTypeMapper) {
|
|
||||||
Class<?> clazz = parameter.getType();
|
|
||||||
if (parameter.isVarArgs()) {
|
|
||||||
clazz = clazz.getComponentType();
|
|
||||||
}
|
|
||||||
|
|
||||||
SWCommand.ClassMapper classMapper = parameter.getAnnotation(SWCommand.ClassMapper.class);
|
|
||||||
SWCommand.Mapper mapper = parameter.getAnnotation(SWCommand.Mapper.class);
|
|
||||||
if (clazz.isEnum() && classMapper == null && mapper == null && !MAPPER_FUNCTIONS.containsKey(clazz.getTypeName()) && !localTypeMapper.containsKey(clazz.getTypeName())) {
|
|
||||||
return createEnumMapper((Class<Enum<?>>) clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
String name = clazz.getTypeName();
|
|
||||||
if (classMapper != null) {
|
|
||||||
name = classMapper.value().getTypeName();
|
|
||||||
} else if (mapper != null) {
|
|
||||||
name = mapper.value();
|
|
||||||
} else {
|
|
||||||
SWCommand.StaticValue staticValue = parameter.getAnnotation(SWCommand.StaticValue.class);
|
|
||||||
if (staticValue != null) {
|
|
||||||
if (parameter.getType() == String.class) {
|
|
||||||
return createMapper(staticValue.value());
|
|
||||||
}
|
|
||||||
if (staticValue.allowISE()) {
|
|
||||||
if ((parameter.getType() == boolean.class || parameter.getType() == Boolean.class) && staticValue.value().length == 2) {
|
|
||||||
List<String> tabCompletes = new ArrayList<>(Arrays.asList(staticValue.value()));
|
|
||||||
return createMapper(s -> {
|
|
||||||
int index = tabCompletes.indexOf(s);
|
|
||||||
return index == -1 ? null : index != 0;
|
|
||||||
}, (commandSender, s) -> tabCompletes);
|
|
||||||
}
|
|
||||||
if ((parameter.getType() == int.class || parameter.getType() == Integer.class) && staticValue.value().length >= 2) {
|
|
||||||
List<String> tabCompletes = new ArrayList<>(Arrays.asList(staticValue.value()));
|
|
||||||
return createMapper(s -> {
|
|
||||||
int index = tabCompletes.indexOf(s);
|
|
||||||
return index == -1 ? null : index;
|
|
||||||
}, (commandSender, s) -> tabCompletes);
|
|
||||||
}
|
|
||||||
if ((parameter.getType() == long.class || parameter.getType() == Long.class) && staticValue.value().length >= 2) {
|
|
||||||
List<String> tabCompletes = new ArrayList<>(Arrays.asList(staticValue.value()));
|
|
||||||
return createMapper(s -> {
|
|
||||||
long index = tabCompletes.indexOf(s);
|
|
||||||
return index == -1 ? null : index;
|
|
||||||
}, (commandSender, s) -> tabCompletes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
TypeMapper<?> typeMapper = localTypeMapper.getOrDefault(name, MAPPER_FUNCTIONS.getOrDefault(name, null));
|
|
||||||
if (typeMapper == null) {
|
|
||||||
throw new IllegalArgumentException("No mapper found for " + name);
|
|
||||||
}
|
|
||||||
return typeMapper;
|
|
||||||
}
|
|
||||||
|
|
||||||
static GuardChecker getGuardChecker(Parameter parameter, Map<String, GuardChecker> localGuardChecker) {
|
|
||||||
Class<?> clazz = parameter.getType();
|
|
||||||
if (parameter.isVarArgs()) {
|
|
||||||
clazz = clazz.getComponentType();
|
|
||||||
}
|
|
||||||
|
|
||||||
SWCommand.ClassGuard classGuard = parameter.getAnnotation(SWCommand.ClassGuard.class);
|
|
||||||
if (classGuard != null) {
|
|
||||||
if (classGuard.value() != null) {
|
|
||||||
return getGuardChecker(classGuard.value().getTypeName(), localGuardChecker);
|
|
||||||
}
|
|
||||||
return getGuardChecker(clazz.getTypeName(), localGuardChecker);
|
|
||||||
}
|
|
||||||
|
|
||||||
SWCommand.Guard guard = parameter.getAnnotation(SWCommand.Guard.class);
|
|
||||||
if (guard != null) {
|
|
||||||
if (guard.value() != null && !guard.value().isEmpty()) {
|
|
||||||
return getGuardChecker(guard.value(), localGuardChecker);
|
|
||||||
}
|
|
||||||
return getGuardChecker(clazz.getTypeName(), localGuardChecker);
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static GuardChecker getGuardChecker(String s, Map<String, GuardChecker> localGuardChecker) {
|
|
||||||
GuardChecker guardChecker = localGuardChecker.getOrDefault(s, GUARD_FUNCTIONS.getOrDefault(s, null));
|
|
||||||
if (guardChecker == null) {
|
|
||||||
throw new IllegalArgumentException("No guard found for " + s);
|
|
||||||
}
|
|
||||||
return guardChecker;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> void addMapper(Class<T> clazz, TypeMapper<T> mapper) {
|
|
||||||
addMapper(clazz.getTypeName(), mapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void addMapper(String name, TypeMapper<?> mapper) {
|
|
||||||
MAPPER_FUNCTIONS.putIfAbsent(name, mapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void addGuard(Class<?> clazz, GuardChecker guardChecker) {
|
|
||||||
addGuard(clazz.getTypeName(), guardChecker);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void addGuard(String name, GuardChecker guardChecker) {
|
|
||||||
GUARD_FUNCTIONS.putIfAbsent(name, guardChecker);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TypeMapper<String> createMapper(String... values) {
|
|
||||||
List<String> strings = Arrays.asList(values);
|
|
||||||
return createMapper((s) -> strings.contains(s) ? s : null, s -> strings);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> TypeMapper<T> createMapper(Function<String, T> mapper, Function<String, List<String>> tabCompleter) {
|
|
||||||
return createMapper(mapper, (commandSender, s) -> tabCompleter.apply(s));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static <T> TypeMapper<T> createMapper(Function<String, T> mapper, BiFunction<CommandSender, String, List<String>> tabCompleter) {
|
|
||||||
return new TypeMapper<T>() {
|
|
||||||
@Override
|
|
||||||
public T map(CommandSender commandSender, String[] previousArguments, String s) {
|
|
||||||
return mapper.apply(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> tabCompletes(CommandSender commandSender, String[] previous, String s) {
|
|
||||||
return tabCompleter.apply(commandSender, s);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TypeMapper<Enum<?>> createEnumMapper(Class<Enum<?>> enumClass) {
|
|
||||||
Enum<?>[] enums = enumClass.getEnumConstants();
|
|
||||||
List<String> strings = Arrays.stream(enums).map(Enum::name).map(String::toLowerCase).collect(Collectors.toList());
|
|
||||||
return new TypeMapper<Enum<?>>() {
|
|
||||||
@Override
|
|
||||||
public Enum<?> map(CommandSender commandSender, String[] previousArguments, String s) {
|
|
||||||
for (Enum<?> e : enums) {
|
|
||||||
if (e.name().equalsIgnoreCase(s)) return e;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<String> tabCompletes(CommandSender commandSender, String[] previousArguments, String s) {
|
|
||||||
return strings;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static <T> Function<String, T> numberMapper(Function<String, T> mapper) {
|
|
||||||
return s -> {
|
|
||||||
if (s.equalsIgnoreCase("nan")) return null;
|
|
||||||
try {
|
|
||||||
return mapper.apply(s);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
// Ignored
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
return mapper.apply(s.replace(',', '.'));
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Function<String, List<String>> numberCompleter(Function<String, ?> mapper) {
|
|
||||||
return s -> numberMapper(mapper).apply(s) != null
|
|
||||||
? Collections.singletonList(s)
|
|
||||||
: Collections.emptyList();
|
|
||||||
}
|
|
||||||
|
|
||||||
static <T extends Annotation> T[] getAnnotation(Method method, Class<T> annotation) {
|
|
||||||
if (method.getAnnotations().length != 1) return null;
|
|
||||||
return method.getDeclaredAnnotationsByType(annotation);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,119 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Parameter;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
class SubCommand {
|
|
||||||
|
|
||||||
private SWCommand swCommand;
|
|
||||||
Method method;
|
|
||||||
String[] description;
|
|
||||||
String[] subCommand;
|
|
||||||
private Predicate<CommandSender> commandSenderPredicate;
|
|
||||||
private Function<CommandSender, ?> commandSenderFunction;
|
|
||||||
GuardChecker guardChecker;
|
|
||||||
boolean noTabComplete;
|
|
||||||
int comparableValue;
|
|
||||||
|
|
||||||
private CommandPart commandPart;
|
|
||||||
|
|
||||||
SubCommand(SWCommand swCommand, Method method, String[] subCommand, Map<String, TypeMapper<?>> localTypeMapper, Map<String, GuardChecker> localGuardChecker, boolean help, String[] description, boolean noTabComplete) {
|
|
||||||
this.swCommand = swCommand;
|
|
||||||
this.method = method;
|
|
||||||
this.description = description;
|
|
||||||
this.noTabComplete = noTabComplete;
|
|
||||||
this.subCommand = subCommand;
|
|
||||||
|
|
||||||
Parameter[] parameters = method.getParameters();
|
|
||||||
comparableValue = parameters[parameters.length - 1].isVarArgs() ? Integer.MAX_VALUE : -parameters.length;
|
|
||||||
|
|
||||||
guardChecker = SWCommandUtils.getGuardChecker(parameters[0], localGuardChecker);
|
|
||||||
|
|
||||||
commandPart = SWCommandUtils.generateCommandPart(help, subCommand, parameters, localTypeMapper, localGuardChecker);
|
|
||||||
commandSenderPredicate = sender -> parameters[0].getType().isAssignableFrom(sender.getClass());
|
|
||||||
commandSenderFunction = sender -> parameters[0].getType().cast(sender);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean invoke(CommandSender commandSender, String alias, String[] args) {
|
|
||||||
try {
|
|
||||||
if (!commandSenderPredicate.test(commandSender)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (commandPart == null) {
|
|
||||||
if (args.length != 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
method.setAccessible(true);
|
|
||||||
method.invoke(swCommand, commandSenderFunction.apply(commandSender));
|
|
||||||
} else {
|
|
||||||
List<Object> objects = new ArrayList<>();
|
|
||||||
commandPart.generateArgumentArray(objects, commandSender, args, 0);
|
|
||||||
if (guardChecker != null) {
|
|
||||||
GuardResult guardResult = guardChecker.guard(commandSender, GuardCheckType.COMMAND, new String[0], null);
|
|
||||||
switch (guardResult) {
|
|
||||||
case ALLOWED:
|
|
||||||
break;
|
|
||||||
case DENIED:
|
|
||||||
throw new CommandNoHelpException();
|
|
||||||
case DENIED_WITH_HELP:
|
|
||||||
default:
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
commandPart.guardCheck(commandSender, args, 0);
|
|
||||||
objects.add(0, commandSenderFunction.apply(commandSender));
|
|
||||||
method.setAccessible(true);
|
|
||||||
method.invoke(swCommand, objects.toArray());
|
|
||||||
}
|
|
||||||
} catch (CommandNoHelpException e) {
|
|
||||||
throw e;
|
|
||||||
} catch (CommandParseException e) {
|
|
||||||
return false;
|
|
||||||
} catch (InvocationTargetException e) {
|
|
||||||
throw new CommandFrameworkException(e, alias, args);
|
|
||||||
} catch (IllegalAccessException | RuntimeException e) {
|
|
||||||
throw new SecurityException(e.getMessage(), e);
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> tabComplete(CommandSender commandSender, String[] args) {
|
|
||||||
if (guardChecker != null && guardChecker.guard(commandSender, GuardCheckType.TAB_COMPLETE, new String[0], null) != GuardResult.ALLOWED) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
if (commandPart == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
List<String> list = new ArrayList<>();
|
|
||||||
commandPart.generateTabComplete(list, commandSender, args, 0);
|
|
||||||
return list;
|
|
||||||
}
|
|
||||||
}
|
|
@ -23,7 +23,7 @@ import org.bukkit.command.CommandSender;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface TypeMapper<T> {
|
public interface TypeMapper<T> extends AbstractTypeMapper<CommandSender, T> {
|
||||||
/**
|
/**
|
||||||
* The CommandSender can be null!
|
* The CommandSender can be null!
|
||||||
*/
|
*/
|
||||||
@ -37,6 +37,4 @@ public interface TypeMapper<T> {
|
|||||||
default T map(String[] previousArguments, String s) {
|
default T map(String[] previousArguments, String s) {
|
||||||
throw new SecurityException();
|
throw new SecurityException();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> tabCompletes(CommandSender commandSender, String[] previousArguments, String s);
|
|
||||||
}
|
}
|
||||||
|
88
SpigotCore_Main/src/de/steamwar/command/TypeUtils.java
Normale Datei
88
SpigotCore_Main/src/de/steamwar/command/TypeUtils.java
Normale Datei
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
* 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 de.steamwar.sql.BauweltMember;
|
||||||
|
import de.steamwar.sql.SchematicNode;
|
||||||
|
import de.steamwar.sql.SteamwarUser;
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.GameMode;
|
||||||
|
import org.bukkit.command.CommandSender;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@UtilityClass
|
||||||
|
public class TypeUtils {
|
||||||
|
|
||||||
|
public static void init() {
|
||||||
|
SWCommandUtils.addMapper(Player.class, SWCommandUtils.createMapper(Bukkit::getPlayer, s -> Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList())));
|
||||||
|
SWCommandUtils.addMapper(GameMode.class, SWCommandUtils.createMapper(s -> {
|
||||||
|
s = s.toLowerCase();
|
||||||
|
if (s.equals("s") || s.equals("survival") || s.equals("0")) return GameMode.SURVIVAL;
|
||||||
|
if (s.equals("c") || s.equals("creative") || s.equals("1")) return GameMode.CREATIVE;
|
||||||
|
if (s.equals("sp") || s.equals("spectator") || s.equals("3")) return GameMode.SPECTATOR;
|
||||||
|
if (s.equals("a") || s.equals("adventure") || s.equals("2")) return GameMode.ADVENTURE;
|
||||||
|
return null;
|
||||||
|
}, s -> Arrays.asList("s", "survival", "0", "c", "creative", "1", "sp", "spectator", "3", "a", "adventure", "2")));
|
||||||
|
SWCommandUtils.addMapper(SteamwarUser.class, SWCommandUtils.createMapper(SteamwarUser::get, s -> Bukkit.getOnlinePlayers().stream().map(Player::getName).collect(Collectors.toList())));
|
||||||
|
SWCommandUtils.addMapper(SchematicNode.class, new TypeMapper<SchematicNode>() {
|
||||||
|
@Override
|
||||||
|
public SchematicNode map(CommandSender commandSender, String[] previousArguments, String s) {
|
||||||
|
return SchematicNode.getNodeFromPath(SteamwarUser.get(((Player) commandSender).getUniqueId()), s);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<String> tabCompletes(CommandSender sender, String[] previousArguments, String s) {
|
||||||
|
return SchematicNode.getNodeTabcomplete(SteamwarUser.get(((Player) sender).getUniqueId()), s);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
SWCommandUtils.addMapper(BauweltMember.class, new TypeMapper<BauweltMember>() {
|
||||||
|
@Override
|
||||||
|
public BauweltMember map(CommandSender commandSender, String[] previousArguments, String s) {
|
||||||
|
if (!(commandSender instanceof Player)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Player player = (Player) commandSender;
|
||||||
|
return BauweltMember.getMembers(player.getUniqueId())
|
||||||
|
.stream()
|
||||||
|
.filter(member -> SteamwarUser.get(member.getMemberID()).getUserName().equalsIgnoreCase(s))
|
||||||
|
.findAny()
|
||||||
|
.orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<String> tabCompletes(CommandSender sender, String[] previousArguments, String s) {
|
||||||
|
if (!(sender instanceof Player)) {
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
Player player = (Player) sender;
|
||||||
|
return BauweltMember.getMembers(player.getUniqueId())
|
||||||
|
.stream()
|
||||||
|
.map(m -> SteamwarUser.get(m.getMemberID()).getUserName())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -1,122 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
import org.bukkit.Server;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
import org.bukkit.permissions.Permission;
|
|
||||||
import org.bukkit.permissions.PermissionAttachment;
|
|
||||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
|
||||||
import org.bukkit.plugin.Plugin;
|
|
||||||
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
public class TestCommandSender implements CommandSender {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void sendMessage(String s) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void sendMessage(String[] strings) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Server getServer() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getName() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Spigot spigot() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isPermissionSet(String s) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isPermissionSet(Permission permission) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasPermission(String s) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean hasPermission(Permission permission) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PermissionAttachment addAttachment(Plugin plugin, String s, boolean b) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PermissionAttachment addAttachment(Plugin plugin) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PermissionAttachment addAttachment(Plugin plugin, String s, boolean b, int i) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public PermissionAttachment addAttachment(Plugin plugin, int i) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void removeAttachment(PermissionAttachment permissionAttachment) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void recalculatePermissions() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Set<PermissionAttachmentInfo> getEffectivePermissions() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isOp() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setOp(boolean b) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
public class ExecutionIdentifier extends RuntimeException {
|
|
||||||
public ExecutionIdentifier() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutionIdentifier(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutionIdentifier(String message, Throwable cause) {
|
|
||||||
super(message, cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutionIdentifier(Throwable cause) {
|
|
||||||
super(cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExecutionIdentifier(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
|
||||||
super(message, cause, enableSuppression, writableStackTrace);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
public class SimpleCommand extends SWCommand {
|
|
||||||
|
|
||||||
public SimpleCommand() {
|
|
||||||
super(true, "simple");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Register
|
|
||||||
public void execute(CommandSender sender) {
|
|
||||||
throw new ExecutionIdentifier("Simple execute without any parameters");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,289 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 de.steamwar.TestCommandSender;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
|
||||||
import static org.hamcrest.Matchers.*;
|
|
||||||
|
|
||||||
public class SimpleCommandPartTest {
|
|
||||||
|
|
||||||
private CommandPart stringCommandPart;
|
|
||||||
private CommandPart intCommandPart;
|
|
||||||
private CommandPart chainedCommandPart;
|
|
||||||
private CommandPart varArgCommandPart;
|
|
||||||
|
|
||||||
private CommandPart simpleGuardPart;
|
|
||||||
|
|
||||||
private CommandPart optionalCommandPart;
|
|
||||||
|
|
||||||
private CommandPart optionalCommandPartMultipleNext;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() throws Exception {
|
|
||||||
stringCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND);
|
|
||||||
intCommandPart = new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, GuardCheckType.COMMAND);
|
|
||||||
|
|
||||||
chainedCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND);
|
|
||||||
chainedCommandPart.setNext(new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, GuardCheckType.COMMAND));
|
|
||||||
|
|
||||||
varArgCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, String.class, null, GuardCheckType.COMMAND);
|
|
||||||
|
|
||||||
simpleGuardPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), (commandSender, guardCheckType, previousArguments, s) -> s.equals("hello") ? GuardResult.DENIED : GuardResult.ALLOWED, null, null, GuardCheckType.COMMAND);
|
|
||||||
|
|
||||||
optionalCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, "hello", GuardCheckType.COMMAND);
|
|
||||||
optionalCommandPart.setNext(new CommandPart(SWCommandUtils.createMapper("hello2", "world2"), null, null, null, GuardCheckType.COMMAND));
|
|
||||||
|
|
||||||
optionalCommandPartMultipleNext = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, "hello", GuardCheckType.COMMAND);
|
|
||||||
CommandPart next = new CommandPart(SWCommandUtils.createMapper("hello2", "world2"), null, null, null, GuardCheckType.COMMAND);
|
|
||||||
next.setNext(new CommandPart(SWCommandUtils.createMapper("hello3", "world3"), null, null, null, GuardCheckType.COMMAND));
|
|
||||||
optionalCommandPartMultipleNext.setNext(next);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCommandPartTabCompleteNoArguments() {
|
|
||||||
List<String> tabComplete = new ArrayList<>();
|
|
||||||
stringCommandPart.generateTabComplete(tabComplete, new TestCommandSender(), new String[]{""}, 0);
|
|
||||||
assertThat(tabComplete.size(), is(2));
|
|
||||||
assertThat(tabComplete.get(0), is("hello"));
|
|
||||||
assertThat(tabComplete.get(1), is("world"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = CommandParseException.class)
|
|
||||||
public void testCommandExecuteInvalidArgument() {
|
|
||||||
stringCommandPart.generateArgumentArray(new ArrayList<>(), new TestCommandSender(), new String[]{""}, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCommandExecuteValidArgument() {
|
|
||||||
List<Object> argumentArray = new ArrayList<>();
|
|
||||||
stringCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello"}, 0);
|
|
||||||
assertThat(argumentArray.size(), is(1));
|
|
||||||
assertThat(argumentArray.get(0), instanceOf(String.class));
|
|
||||||
assertThat(argumentArray.get(0), is("hello"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCommandExecuteValidOtherArgument() {
|
|
||||||
List<Object> argumentArray = new ArrayList<>();
|
|
||||||
stringCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"world"}, 0);
|
|
||||||
assertThat(argumentArray.size(), is(1));
|
|
||||||
assertThat(argumentArray.get(0), instanceOf(String.class));
|
|
||||||
assertThat(argumentArray.get(0), is("world"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = CommandParseException.class)
|
|
||||||
public void testCommandExecuteNonNumberArgument() {
|
|
||||||
intCommandPart.generateArgumentArray(new ArrayList<>(), new TestCommandSender(), new String[]{"world"}, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCommandExecuteValidNumberArgument() {
|
|
||||||
List<Object> argumentArray = new ArrayList<>();
|
|
||||||
intCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"0"}, 0);
|
|
||||||
assertThat(argumentArray.size(), is(1));
|
|
||||||
assertThat(argumentArray.get(0), instanceOf(int.class));
|
|
||||||
assertThat(argumentArray.get(0), is(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testChainedCommandExecuteValidArgument() {
|
|
||||||
List<Object> argumentArray = new ArrayList<>();
|
|
||||||
chainedCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello", "0"}, 0);
|
|
||||||
assertThat(argumentArray.size(), is(2));
|
|
||||||
assertThat(argumentArray.get(0), instanceOf(String.class));
|
|
||||||
assertThat(argumentArray.get(0), is("hello"));
|
|
||||||
assertThat(argumentArray.get(1), instanceOf(int.class));
|
|
||||||
assertThat(argumentArray.get(1), is(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testChainedCommandTabComplete() {
|
|
||||||
List<String> tabCompletes = new ArrayList<>();
|
|
||||||
chainedCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{""}, 0);
|
|
||||||
assertThat(tabCompletes.size(), is(2));
|
|
||||||
assertThat(tabCompletes.get(0), is("hello"));
|
|
||||||
assertThat(tabCompletes.get(1), is("world"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testChainedCommandTabCompleteOther() {
|
|
||||||
List<String> tabCompletes = new ArrayList<>();
|
|
||||||
chainedCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", ""}, 0);
|
|
||||||
assertThat(tabCompletes.size(), is(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testVarArgsCommandTabComplete() {
|
|
||||||
List<String> tabCompletes = new ArrayList<>();
|
|
||||||
varArgCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello"}, 0);
|
|
||||||
assertThat(tabCompletes.size(), is(2));
|
|
||||||
assertThat(tabCompletes.get(0), is("hello"));
|
|
||||||
assertThat(tabCompletes.get(1), is("world"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testVarArgsCommandTabCompleteDeeper() {
|
|
||||||
List<String> tabCompletes = new ArrayList<>();
|
|
||||||
varArgCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", "world", "hello", "world"}, 0);
|
|
||||||
System.out.println(tabCompletes);
|
|
||||||
assertThat(tabCompletes.size(), is(2));
|
|
||||||
assertThat(tabCompletes.get(0), is("hello"));
|
|
||||||
assertThat(tabCompletes.get(1), is("world"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testVarArgsCommandArgumentParsing() {
|
|
||||||
List<Object> argumentArray = new ArrayList<>();
|
|
||||||
varArgCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello"}, 0);
|
|
||||||
assertThat(argumentArray.size(), is(1));
|
|
||||||
assertThat(argumentArray.get(0), instanceOf(String[].class));
|
|
||||||
assertThat((String[]) argumentArray.get(0), arrayWithSize(1));
|
|
||||||
assertThat((String[]) argumentArray.get(0), is(new String[]{"hello"}));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testVarArgsCommandArgumentParsingDeeper() {
|
|
||||||
List<Object> argumentArray = new ArrayList<>();
|
|
||||||
varArgCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello", "world", "hello", "world"}, 0);
|
|
||||||
assertThat(argumentArray.size(), is(1));
|
|
||||||
assertThat(argumentArray.get(0), instanceOf(String[].class));
|
|
||||||
assertThat((String[]) argumentArray.get(0), arrayWithSize(4));
|
|
||||||
assertThat((String[]) argumentArray.get(0), is(new String[]{"hello", "world", "hello", "world"}));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGuardCommandExecute() {
|
|
||||||
List<Object> argumentArray = new ArrayList<>();
|
|
||||||
simpleGuardPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello"}, 0);
|
|
||||||
assertThat(argumentArray.size(), is(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = CommandNoHelpException.class)
|
|
||||||
public void testGuardGuardCheck() {
|
|
||||||
simpleGuardPart.guardCheck(new TestCommandSender(), new String[]{"hello"}, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGuardCommandExecuteValid() {
|
|
||||||
List<Object> argumentArray = new ArrayList<>();
|
|
||||||
simpleGuardPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"world"}, 0);
|
|
||||||
assertThat(argumentArray.size(), is(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testGuardGuardCheckValid() {
|
|
||||||
boolean guardResult = simpleGuardPart.guardCheck(new TestCommandSender(), new String[]{"world"}, 0);
|
|
||||||
assertThat(guardResult, is(true));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOptionalCommandPartTabComplete() {
|
|
||||||
List<String> tabCompletes = new ArrayList<>();
|
|
||||||
optionalCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{""}, 0);
|
|
||||||
assertThat(tabCompletes.size(), is(4));
|
|
||||||
assertThat(tabCompletes.get(0), is("hello"));
|
|
||||||
assertThat(tabCompletes.get(1), is("world"));
|
|
||||||
assertThat(tabCompletes.get(2), is("hello2"));
|
|
||||||
assertThat(tabCompletes.get(3), is("world2"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOptionalCommandPartTabCompleteSecond() {
|
|
||||||
List<String> tabCompletes = new ArrayList<>();
|
|
||||||
optionalCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", ""}, 0);
|
|
||||||
assertThat(tabCompletes.size(), is(2));
|
|
||||||
assertThat(tabCompletes.get(0), is("hello2"));
|
|
||||||
assertThat(tabCompletes.get(1), is("world2"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = CommandParseException.class)
|
|
||||||
public void testOptionalCommandPartExecution() {
|
|
||||||
optionalCommandPart.generateArgumentArray(new ArrayList<>(), new TestCommandSender(), new String[]{""}, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOptionalCommandPartExecutionValid() {
|
|
||||||
List<Object> argumentArray = new ArrayList<>();
|
|
||||||
optionalCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello2"}, 0);
|
|
||||||
assertThat(argumentArray.size(), is(2));
|
|
||||||
assertThat(argumentArray.get(0), is("hello"));
|
|
||||||
assertThat(argumentArray.get(1), is("hello2"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test(expected = CommandParseException.class)
|
|
||||||
public void testOptionalCommandPartExecutionInvalid() {
|
|
||||||
optionalCommandPart.generateArgumentArray(new ArrayList<>(), new TestCommandSender(), new String[]{"hello"}, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOptionalCommandPartExecutionFullyValid() {
|
|
||||||
List<Object> argumentArray = new ArrayList<>();
|
|
||||||
optionalCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"world", "hello2"}, 0);
|
|
||||||
assertThat(argumentArray.size(), is(2));
|
|
||||||
assertThat(argumentArray.get(0), is("world"));
|
|
||||||
assertThat(argumentArray.get(1), is("hello2"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOptionalCommandPartExecutionMultipleNext() {
|
|
||||||
List<Object> argumentArray = new ArrayList<>();
|
|
||||||
optionalCommandPartMultipleNext.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"world", "hello2", "hello3"}, 0);
|
|
||||||
assertThat(argumentArray.size(), is(3));
|
|
||||||
assertThat(argumentArray.get(0), is("world"));
|
|
||||||
assertThat(argumentArray.get(1), is("hello2"));
|
|
||||||
assertThat(argumentArray.get(2), is("hello3"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testOptionalCommandPartExecutionMultipleTabComplete() {
|
|
||||||
List<String> tabCompletes = new ArrayList<>();
|
|
||||||
optionalCommandPartMultipleNext.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{""}, 0);
|
|
||||||
assertThat(tabCompletes.size(), is(4));
|
|
||||||
assertThat(tabCompletes.get(0), is("hello"));
|
|
||||||
assertThat(tabCompletes.get(1), is("world"));
|
|
||||||
assertThat(tabCompletes.get(2), is("hello2"));
|
|
||||||
assertThat(tabCompletes.get(3), is("world2"));
|
|
||||||
|
|
||||||
tabCompletes = new ArrayList<>();
|
|
||||||
optionalCommandPartMultipleNext.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", ""}, 0);
|
|
||||||
assertThat(tabCompletes.size(), is(2));
|
|
||||||
assertThat(tabCompletes.get(0), is("hello2"));
|
|
||||||
assertThat(tabCompletes.get(1), is("world2"));
|
|
||||||
|
|
||||||
tabCompletes = new ArrayList<>();
|
|
||||||
optionalCommandPartMultipleNext.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", "world2", ""}, 0);
|
|
||||||
assertThat(tabCompletes.size(), is(2));
|
|
||||||
assertThat(tabCompletes.get(0), is("hello3"));
|
|
||||||
assertThat(tabCompletes.get(1), is("world3"));
|
|
||||||
|
|
||||||
tabCompletes = new ArrayList<>();
|
|
||||||
optionalCommandPartMultipleNext.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"world2", ""}, 0);
|
|
||||||
assertThat(tabCompletes.size(), is(2));
|
|
||||||
assertThat(tabCompletes.get(0), is("hello3"));
|
|
||||||
assertThat(tabCompletes.get(1), is("world3"));
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,61 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 de.steamwar.TestCommandSender;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
|
||||||
import static org.hamcrest.Matchers.is;
|
|
||||||
|
|
||||||
public class SimpleCommandTest {
|
|
||||||
|
|
||||||
private SimpleCommand simpleCommand;
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() throws Exception {
|
|
||||||
simpleCommand = new SimpleCommand();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testCommandParsing() {
|
|
||||||
try {
|
|
||||||
simpleCommand.execute(new TestCommandSender(), "", new String[]{});
|
|
||||||
} catch (CommandFrameworkException commandFrameworkException) {
|
|
||||||
if (commandFrameworkException.getCause().getCause() instanceof ExecutionIdentifier) {
|
|
||||||
ExecutionIdentifier executionIdentifier = (ExecutionIdentifier) commandFrameworkException.getCause().getCause();
|
|
||||||
assertThat(executionIdentifier.getMessage(), is("Simple execute without any parameters"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUnknownCommandParsing() {
|
|
||||||
try {
|
|
||||||
simpleCommand.execute(new TestCommandSender(), "", new String[]{"unknown"});
|
|
||||||
} catch (SecurityException securityException) {
|
|
||||||
securityException.printStackTrace();
|
|
||||||
assert false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
In neuem Issue referenzieren
Einen Benutzer sperren