Dynamic arguments now also have an optional dynamic restriction

This commit is contained in:
themode 2020-11-07 03:40:37 +01:00
parent cf6fbd3d34
commit 66fc6779b6
7 changed files with 103 additions and 6 deletions

View File

@ -1,6 +1,8 @@
package net.minestom.server.command.builder.arguments;
import net.minestom.server.utils.callback.validator.StringArrayValidator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.regex.Pattern;
@ -10,6 +12,10 @@ import java.util.regex.Pattern;
*/
public class ArgumentDynamicStringArray extends Argument<String[]> {
public static final int RESTRICTION_ERROR = 1;
private StringArrayValidator dynamicRestriction;
public ArgumentDynamicStringArray(String id) {
super(id, true, true);
}
@ -27,6 +33,29 @@ public class ArgumentDynamicStringArray extends Argument<String[]> {
@Override
public int getConditionResult(@NotNull String[] value) {
// true if 'value' is valid based on the dynamic restriction
final boolean restrictionCheck = dynamicRestriction != null ?
dynamicRestriction.isValid(value) : true;
if (!restrictionCheck) {
return RESTRICTION_ERROR;
}
return SUCCESS;
}
/**
* Sets the dynamic restriction of this dynamic argument.
* <p>
* Will be called once the argument condition is checked.
*
* @param dynamicRestriction the dynamic restriction, can be null to disable
* @return 'this' for chaining
*/
public ArgumentDynamicStringArray fromRestrictions(@Nullable StringArrayValidator dynamicRestriction) {
this.dynamicRestriction = dynamicRestriction;
return this;
}
}

View File

@ -1,6 +1,8 @@
package net.minestom.server.command.builder.arguments;
import net.minestom.server.utils.callback.validator.StringValidator;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Same as {@link ArgumentWord} with the exception
@ -8,12 +10,20 @@ import org.jetbrains.annotations.NotNull;
*/
public class ArgumentDynamicWord extends Argument<String> {
public static final int SPACE_ERROR = 1;
public static final int RESTRICTION_ERROR = 2;
private StringValidator dynamicRestriction;
public ArgumentDynamicWord(String id) {
super(id);
}
@Override
public int getCorrectionResult(@NotNull String value) {
if (value.contains(" "))
return SPACE_ERROR;
return SUCCESS;
}
@ -25,6 +35,28 @@ public class ArgumentDynamicWord extends Argument<String> {
@Override
public int getConditionResult(@NotNull String value) {
// true if 'value' is valid based on the dynamic restriction
final boolean restrictionCheck = dynamicRestriction != null ?
dynamicRestriction.isValid(value) : true;
if (!restrictionCheck) {
return RESTRICTION_ERROR;
}
return SUCCESS;
}
/**
* Sets the dynamic restriction of this dynamic argument.
* <p>
* Will be called once the argument condition is checked.
*
* @param dynamicRestriction the dynamic restriction, can be null to disable
* @return 'this' for chaining
*/
public ArgumentDynamicWord fromRestrictions(@Nullable StringValidator dynamicRestriction) {
this.dynamicRestriction = dynamicRestriction;
return this;
}
}

View File

@ -20,7 +20,7 @@ public class ArgumentWord extends Argument<String> {
protected String[] restrictions;
public ArgumentWord(String id) {
super(id, false);
super(id);
}
/**

View File

@ -0,0 +1,8 @@
package net.minestom.server.utils.callback.validator;
/**
* Interface used when a string array needs to be validated dynamically.
*/
@FunctionalInterface
public interface StringArrayValidator extends Validator<String[]> {
}

View File

@ -0,0 +1,8 @@
package net.minestom.server.utils.callback.validator;
/**
* Interface used when a string needs to be validated dynamically.
*/
@FunctionalInterface
public interface StringValidator extends Validator<String> {
}

View File

@ -0,0 +1,19 @@
package net.minestom.server.utils.callback.validator;
import org.jetbrains.annotations.NotNull;
/**
* Interface used when a value needs to be validated dynamically.
*/
@FunctionalInterface
public interface Validator<T> {
/**
* Gets if a value is valid based on a condition.
*
* @param value the value to check
* @return true if the value is valid, false otherwise
*/
boolean isValid(@NotNull T value);
}

View File

@ -1,6 +1,5 @@
package demo.commands;
import net.minestom.server.MinecraftServer;
import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.Arguments;
import net.minestom.server.command.builder.Command;
@ -18,7 +17,11 @@ public class TestCommand extends Command {
//addSyntax(this::execute, dynamicWord);
}
Argument test = ArgumentType.Integer("number");
Argument test = ArgumentType.DynamicWord("testArg").fromRestrictions(value -> value.contains("a"));
test.setCallback((source, value, error) -> {
System.out.println("ERROR " + error);
});
setDefaultExecutor((source, args) -> {
System.out.println("DEFAULT");
@ -26,9 +29,7 @@ public class TestCommand extends Command {
});
addSyntax((source, args) -> {
int number = args.getInteger("number");
source.sendMessage("set view to " + number);
MinecraftServer.setEntityViewDistance(number);
System.out.println("HEY IT WORKS");
}, test);
}