mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-30 20:11:26 +01:00
Dynamic arguments now also have an optional dynamic restriction
This commit is contained in:
parent
cf6fbd3d34
commit
66fc6779b6
@ -1,6 +1,8 @@
|
|||||||
package net.minestom.server.command.builder.arguments;
|
package net.minestom.server.command.builder.arguments;
|
||||||
|
|
||||||
|
import net.minestom.server.utils.callback.validator.StringArrayValidator;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
@ -10,6 +12,10 @@ import java.util.regex.Pattern;
|
|||||||
*/
|
*/
|
||||||
public class ArgumentDynamicStringArray extends Argument<String[]> {
|
public class ArgumentDynamicStringArray extends Argument<String[]> {
|
||||||
|
|
||||||
|
public static final int RESTRICTION_ERROR = 1;
|
||||||
|
|
||||||
|
private StringArrayValidator dynamicRestriction;
|
||||||
|
|
||||||
public ArgumentDynamicStringArray(String id) {
|
public ArgumentDynamicStringArray(String id) {
|
||||||
super(id, true, true);
|
super(id, true, true);
|
||||||
}
|
}
|
||||||
@ -27,6 +33,29 @@ public class ArgumentDynamicStringArray extends Argument<String[]> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getConditionResult(@NotNull String[] value) {
|
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;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package net.minestom.server.command.builder.arguments;
|
package net.minestom.server.command.builder.arguments;
|
||||||
|
|
||||||
|
import net.minestom.server.utils.callback.validator.StringValidator;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Same as {@link ArgumentWord} with the exception
|
* Same as {@link ArgumentWord} with the exception
|
||||||
@ -8,12 +10,20 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
*/
|
*/
|
||||||
public class ArgumentDynamicWord extends Argument<String> {
|
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) {
|
public ArgumentDynamicWord(String id) {
|
||||||
super(id);
|
super(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getCorrectionResult(@NotNull String value) {
|
public int getCorrectionResult(@NotNull String value) {
|
||||||
|
if (value.contains(" "))
|
||||||
|
return SPACE_ERROR;
|
||||||
|
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,6 +35,28 @@ public class ArgumentDynamicWord extends Argument<String> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getConditionResult(@NotNull String value) {
|
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;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ public class ArgumentWord extends Argument<String> {
|
|||||||
protected String[] restrictions;
|
protected String[] restrictions;
|
||||||
|
|
||||||
public ArgumentWord(String id) {
|
public ArgumentWord(String id) {
|
||||||
super(id, false);
|
super(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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[]> {
|
||||||
|
}
|
@ -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> {
|
||||||
|
}
|
@ -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);
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
package demo.commands;
|
package demo.commands;
|
||||||
|
|
||||||
import net.minestom.server.MinecraftServer;
|
|
||||||
import net.minestom.server.command.CommandSender;
|
import net.minestom.server.command.CommandSender;
|
||||||
import net.minestom.server.command.builder.Arguments;
|
import net.minestom.server.command.builder.Arguments;
|
||||||
import net.minestom.server.command.builder.Command;
|
import net.minestom.server.command.builder.Command;
|
||||||
@ -18,7 +17,11 @@ public class TestCommand extends Command {
|
|||||||
//addSyntax(this::execute, dynamicWord);
|
//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) -> {
|
setDefaultExecutor((source, args) -> {
|
||||||
System.out.println("DEFAULT");
|
System.out.println("DEFAULT");
|
||||||
@ -26,9 +29,7 @@ public class TestCommand extends Command {
|
|||||||
});
|
});
|
||||||
|
|
||||||
addSyntax((source, args) -> {
|
addSyntax((source, args) -> {
|
||||||
int number = args.getInteger("number");
|
System.out.println("HEY IT WORKS");
|
||||||
source.sendMessage("set view to " + number);
|
|
||||||
MinecraftServer.setEntityViewDistance(number);
|
|
||||||
}, test);
|
}, test);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user