Fix a bunch of errors
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2022-06-16 16:46:22 +02:00
Ursprung a7d41ddc80
Commit a8880e53db
9 geänderte Dateien mit 184 neuen und 27 gelöschten Zeilen

Datei anzeigen

@ -69,9 +69,11 @@ public abstract class AbstractSWCommand<T> {
protected final void execute(T sender, String alias, String[] args) { protected final void execute(T sender, String alias, String[] args) {
initialize(); initialize();
List<Runnable> errors = new ArrayList<>();
try { try {
if (!commandList.stream().anyMatch(s -> s.invoke(sender, alias, args))) { if (!commandList.stream().anyMatch(s -> s.invoke(errors::add, sender, alias, args))) {
commandHelpList.stream().anyMatch(s -> s.invoke(sender, alias, args)); errors.forEach(Runnable::run);
commandHelpList.stream().anyMatch(s -> s.invoke((ignore) -> {}, sender, alias, args));
} }
} catch (CommandNoHelpException e) { } catch (CommandNoHelpException e) {
// Ignored // Ignored

Datei anzeigen

@ -20,6 +20,7 @@
package de.steamwar.command; package de.steamwar.command;
import java.util.Collection; import java.util.Collection;
import java.util.function.BiConsumer;
public interface AbstractTypeMapper<K, T> extends AbstractValidator<K, T> { public interface AbstractTypeMapper<K, T> extends AbstractValidator<K, T> {
/** /**
@ -27,5 +28,10 @@ public interface AbstractTypeMapper<K, T> extends AbstractValidator<K, T> {
*/ */
T map(K sender, String[] previousArguments, String s); T map(K sender, String[] previousArguments, String s);
@Override
default boolean validate(K sender, T value, BiConsumer<String, Object[]> messageSender) {
return true;
}
Collection<String> tabCompletes(K sender, String[] previousArguments, String s); Collection<String> tabCompletes(K sender, String[] previousArguments, String s);
} }

Datei anzeigen

@ -21,6 +21,7 @@ package de.steamwar.command;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
@FunctionalInterface
public interface AbstractValidator<K, T> { public interface AbstractValidator<K, T> {
/** /**
@ -31,7 +32,5 @@ public interface AbstractValidator<K, T> {
* @param messageSender The message sender to send messages to the player. Never send messages directly to the player. * @param messageSender The message sender to send messages to the player. Never send messages directly to the player.
* @return The result of the validation. * @return The result of the validation.
*/ */
default boolean validate(K sender, T value, BiConsumer<String, Object[]> messageSender) { boolean validate(K sender, T value, BiConsumer<String, Object[]> messageSender);
return true;
}
} }

Datei anzeigen

@ -23,9 +23,11 @@ import lombok.AllArgsConstructor;
import lombok.Setter; import lombok.Setter;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.function.Consumer;
class CommandPart<T> { class CommandPart<T> {
@ -86,11 +88,11 @@ class CommandPart<T> {
} }
} }
public void generateArgumentArray(List<Object> current, T sender, String[] args, int startIndex) { public void generateArgumentArray(Consumer<Runnable> errors, List<Object> current, T sender, String[] args, int startIndex) {
if (varArgType != null) { if (varArgType != null) {
Object array = Array.newInstance(varArgType, args.length - startIndex); Object array = Array.newInstance(varArgType, args.length - startIndex);
for (int i = startIndex; i < args.length; i++) { for (int i = startIndex; i < args.length; i++) {
CheckArgumentResult validArgument = checkArgument(null, sender, args, i); CheckArgumentResult validArgument = checkArgument(errors, null, sender, args, i);
if (!validArgument.success) { if (!validArgument.success) {
throw new CommandParseException(); throw new CommandParseException();
} }
@ -100,7 +102,7 @@ class CommandPart<T> {
return; return;
} }
CheckArgumentResult validArgument = checkArgument(null, sender, args, startIndex); CheckArgumentResult validArgument = checkArgument(errors, null, sender, args, startIndex);
if (!validArgument.success && optional == null) { if (!validArgument.success && optional == null) {
throw new CommandParseException(); throw new CommandParseException();
} }
@ -109,7 +111,7 @@ class CommandPart<T> {
current.add(typeMapper.map(sender, EMPTY_ARRAY, optional)); current.add(typeMapper.map(sender, EMPTY_ARRAY, optional));
} }
if (next != null) { if (next != null) {
next.generateArgumentArray(current, sender, args, startIndex); next.generateArgumentArray(errors, current, sender, args, startIndex);
} }
return; return;
} }
@ -117,7 +119,7 @@ class CommandPart<T> {
current.add(validArgument.value); current.add(validArgument.value);
} }
if (next != null) { if (next != null) {
next.generateArgumentArray(current, sender, args, startIndex + 1); next.generateArgumentArray(errors, current, sender, args, startIndex + 1);
} }
} }
@ -181,7 +183,7 @@ class CommandPart<T> {
public void generateTabComplete(List<String> current, T sender, String[] args, int startIndex) { public void generateTabComplete(List<String> current, T sender, String[] args, int startIndex) {
if (varArgType != null) { if (varArgType != null) {
for (int i = startIndex; i < args.length - 1; i++) { for (int i = startIndex; i < args.length - 1; i++) {
CheckArgumentResult validArgument = checkArgument(GuardCheckType.TAB_COMPLETE, sender, args, i); CheckArgumentResult validArgument = checkArgument((ignore) -> {}, GuardCheckType.TAB_COMPLETE, sender, args, i);
if (!validArgument.success) { if (!validArgument.success) {
return; return;
} }
@ -194,7 +196,7 @@ class CommandPart<T> {
} }
if (args.length - 1 > startIndex) { if (args.length - 1 > startIndex) {
CheckArgumentResult checkArgumentResult = checkArgument(GuardCheckType.TAB_COMPLETE, sender, args, startIndex); CheckArgumentResult checkArgumentResult = checkArgument((ignore) -> {}, GuardCheckType.TAB_COMPLETE, sender, args, startIndex);
if (checkArgumentResult.success && next != null) { if (checkArgumentResult.success && next != null) {
next.generateTabComplete(current, sender, args, startIndex + 1); next.generateTabComplete(current, sender, args, startIndex + 1);
return; return;
@ -214,19 +216,21 @@ class CommandPart<T> {
} }
} }
private CheckArgumentResult checkArgument(GuardCheckType guardCheckType, T sender, String[] args, int index) { private CheckArgumentResult checkArgument(Consumer<Runnable> errors, GuardCheckType guardCheckType, T sender, String[] args, int index) {
try { try {
Object value = typeMapper.map(sender, Arrays.copyOf(args, index), args[index]); Object value = typeMapper.map(sender, Arrays.copyOf(args, index), args[index]);
if (value == null) {
return new CheckArgumentResult(false, null);
}
if (validator != null) { if (validator != null) {
if (!validator.validate(sender, value, (s, objects) -> { if (!validator.validate(sender, value, (s, objects) -> {
// ignore errors.accept(() -> {
command.sendMessage(sender, s, objects);
});
})) { })) {
return new CheckArgumentResult(false, null); return new CheckArgumentResult(false, null);
} }
return new CheckArgumentResult(true, value); return new CheckArgumentResult(value != null, value);
}
if (value == null) {
return new CheckArgumentResult(false, null);
} }
GuardResult guardResult = checkGuard(guardCheckType, sender, args, index); GuardResult guardResult = checkGuard(guardCheckType, sender, args, index);

Datei anzeigen

@ -183,7 +183,7 @@ public class SWCommandUtils {
AbstractSWCommand.Validator validator = parameter.getAnnotation(AbstractSWCommand.Validator.class); AbstractSWCommand.Validator validator = parameter.getAnnotation(AbstractSWCommand.Validator.class);
if (validator != null) { if (validator != null) {
if (validator.value() != null) { if (validator.value() != null && !validator.value().isEmpty()) {
return getValidator(validator.value(), localValidator); return getValidator(validator.value(), localValidator);
} }
return getValidator(clazz.getTypeName(), localValidator); return getValidator(clazz.getTypeName(), localValidator);
@ -191,12 +191,9 @@ public class SWCommandUtils {
AbstractSWCommand.ErrorMessage errorMessage = parameter.getAnnotation(AbstractSWCommand.ErrorMessage.class); AbstractSWCommand.ErrorMessage errorMessage = parameter.getAnnotation(AbstractSWCommand.ErrorMessage.class);
if (errorMessage != null) { if (errorMessage != null) {
return new AbstractValidator<T, String>() { return (AbstractValidator<T, String>) (sender, value, messageSender) -> {
@Override if (value == null) messageSender.accept(errorMessage.value(), new Object[0]);
public boolean validate(T sender, String value, BiConsumer<String, Object[]> messageSender) { return value != null;
if (value == null) messageSender.accept(errorMessage.value(), new Object[0]);
return value != null;
}
}; };
} }
return null; return null;

Datei anzeigen

@ -25,6 +25,7 @@ import java.lang.reflect.Parameter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -62,7 +63,7 @@ public class SubCommand<T> {
senderFunction = t -> parameters[0].getType().cast(t); senderFunction = t -> parameters[0].getType().cast(t);
} }
boolean invoke(T sender, String alias, String[] args) { boolean invoke(Consumer<Runnable> errors, T sender, String alias, String[] args) {
try { try {
if (!senderPredicate.test(sender)) { if (!senderPredicate.test(sender)) {
return false; return false;
@ -94,7 +95,7 @@ public class SubCommand<T> {
method.invoke(abstractSWCommand, senderFunction.apply(sender)); method.invoke(abstractSWCommand, senderFunction.apply(sender));
} else { } else {
List<Object> objects = new ArrayList<>(); List<Object> objects = new ArrayList<>();
commandPart.generateArgumentArray(objects, sender, args, 0); commandPart.generateArgumentArray(errors, objects, sender, args, 0);
if (validator != null) { if (validator != null) {
if (!validator.validate(sender, sender, (s, objectArgs) -> { if (!validator.validate(sender, sender, (s, objectArgs) -> {
abstractSWCommand.sendMessage(sender, s, objectArgs); abstractSWCommand.sendMessage(sender, s, objectArgs);

Datei anzeigen

@ -0,0 +1,60 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
import de.steamwar.command.dto.ExecutionIdentifier;
import de.steamwar.command.dto.TestSWCommand;
import de.steamwar.command.dto.TestValidator;
public class ValidatorCommand extends TestSWCommand {
public ValidatorCommand() {
super("testvalidator");
}
@Register
public void test(@Validator String sender) {
throw new ExecutionIdentifier("RunTest");
}
@Override
protected void sendMessage(String sender, String message, Object[] args) {
if (message.equals("Hello World")) {
throw new ExecutionIdentifier("RunSendMessageWithHelloWorldParameter");
}
}
@Register
public void onError(String sender, @ErrorMessage("Hello World") int error) {
throw new ExecutionIdentifier("RunOnError");
}
@Register
public void onError(String sender, double error) {
throw new ExecutionIdentifier("RunOnErrorDouble");
}
@ClassValidator(value = String.class, local = true)
public TestValidator<String> validator() {
return (sender, value, messageSender) -> {
throw new ExecutionIdentifier("RunValidator");
};
}
}

Datei anzeigen

@ -0,0 +1,63 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command;
import de.steamwar.command.dto.ExecutionIdentifier;
import org.junit.Test;
import static de.steamwar.AssertionUtils.assertCMDFramework;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class ValidatorCommandTest {
@Test
public void testValidator() {
ValidatorCommand cmd = new ValidatorCommand();
try {
cmd.execute("test", "", new String[0]);
assertThat(true, is(false));
} catch (Exception e) {
assertThat(e.getMessage(), is("RunValidator"));
}
}
@Test
public void testErrorMessage() {
ValidatorCommand cmd = new ValidatorCommand();
try {
cmd.execute("test", "", new String[]{"Hello"});
assertThat(true, is(false));
} catch (Exception e) {
assertThat(e.getMessage(), is("RunSendMessageWithHelloWorldParameter"));
}
}
@Test
public void testErrorNoMessage() {
ValidatorCommand cmd = new ValidatorCommand();
try {
cmd.execute("test", "", new String[]{"0.0"});
assertThat(true, is(false));
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunOnErrorDouble");
}
}
}

Datei anzeigen

@ -0,0 +1,25 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.command.dto;
import de.steamwar.command.AbstractValidator;
public interface TestValidator<T> extends AbstractValidator<String, T> {
}