diff --git a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentDynamicStringArray.java b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentDynamicStringArray.java index e254e07f4..71689b35e 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentDynamicStringArray.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentDynamicStringArray.java @@ -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 { + 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 { @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. + *

+ * 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; + } + } diff --git a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentDynamicWord.java b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentDynamicWord.java index c4b88384e..df259bebe 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentDynamicWord.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentDynamicWord.java @@ -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 { + 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 { @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. + *

+ * 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; + } } diff --git a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentWord.java b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentWord.java index d596d3266..5a28ec3e2 100644 --- a/src/main/java/net/minestom/server/command/builder/arguments/ArgumentWord.java +++ b/src/main/java/net/minestom/server/command/builder/arguments/ArgumentWord.java @@ -20,7 +20,7 @@ public class ArgumentWord extends Argument { protected String[] restrictions; public ArgumentWord(String id) { - super(id, false); + super(id); } /** diff --git a/src/main/java/net/minestom/server/utils/callback/validator/StringArrayValidator.java b/src/main/java/net/minestom/server/utils/callback/validator/StringArrayValidator.java new file mode 100644 index 000000000..a818399d8 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/callback/validator/StringArrayValidator.java @@ -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 { +} diff --git a/src/main/java/net/minestom/server/utils/callback/validator/StringValidator.java b/src/main/java/net/minestom/server/utils/callback/validator/StringValidator.java new file mode 100644 index 000000000..ca9e1a133 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/callback/validator/StringValidator.java @@ -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 { +} diff --git a/src/main/java/net/minestom/server/utils/callback/validator/Validator.java b/src/main/java/net/minestom/server/utils/callback/validator/Validator.java new file mode 100644 index 000000000..081f77971 --- /dev/null +++ b/src/main/java/net/minestom/server/utils/callback/validator/Validator.java @@ -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 { + + /** + * 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); + +} diff --git a/src/test/java/demo/commands/TestCommand.java b/src/test/java/demo/commands/TestCommand.java index e1396cbea..456458317 100644 --- a/src/test/java/demo/commands/TestCommand.java +++ b/src/test/java/demo/commands/TestCommand.java @@ -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); }