diff --git a/shared/src/main/java/net/codersky/mcutils/cmd/MCCommand.java b/shared/src/main/java/net/codersky/mcutils/cmd/MCCommand.java index c292ff1..bae7bb7 100644 --- a/shared/src/main/java/net/codersky/mcutils/cmd/MCCommand.java +++ b/shared/src/main/java/net/codersky/mcutils/cmd/MCCommand.java @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.function.Function; public interface MCCommand> { @@ -68,13 +69,13 @@ default boolean removesEventPatterns() { * {@code arg} position or if {@code converter} returns {@code null}. * * @param the type of the result of the {@code converter} {@link Function}. + * @param converter the {@link Function} that will convert the {@link String} + * found at the specified {@code arg} position. The {@link String} passed + * to the {@link Function} will never be {@code null}. * @param arg the array position of the argument to get, can be out of bounds. * @param args the array of arguments to use. * @param def the default value to return if {@code arg} is out of bounds or * {@code converter} returns {@code null}. - * @param converter the {@link Function} that will convert the {@link String} - * found at the specified {@code arg} position. The {@link String} passed - * to the {@link Function} will never be {@code null}. * * @return The argument as converted by {@code converter} if found on the {@code args} array * and {@code converter} doesn't return {@code null}. {@code def} otherwise. @@ -84,7 +85,7 @@ default boolean removesEventPatterns() { * @since MCUtils 1.0.0 */ @Nullable - default T asGeneric(int arg, @NotNull String[] args, @Nullable T def, @NotNull Function converter) { + default T asGeneric(@NotNull Function converter, int arg, @NotNull String[] args, @Nullable T def) { if (args.length <= arg) return def; final T converted = converter.apply(args[arg]); @@ -97,11 +98,11 @@ default T asGeneric(int arg, @NotNull String[] args, @Nullable T def, @NotNu * {@code arg} position or if {@code converter} returns {@code null}. * * @param the type of the result of the {@code converter} {@link Function}. - * @param arg the array position of the argument to get, can be out of bounds. - * @param args the array of arguments to use. * @param converter the {@link Function} that will convert the {@link String} * found at the specified {@code arg} position. The {@link String} passed * to the {@link Function} will never be {@code null}. + * @param arg the array position of the argument to get, can be out of bounds. + * @param args the array of arguments to use. * * @return The argument as converted by {@code converter} if found * on the {@code args} array, {@code null} otherwise. @@ -111,7 +112,7 @@ default T asGeneric(int arg, @NotNull String[] args, @Nullable T def, @NotNu * @since MCUtils 1.0.0 */ @Nullable - default T asGeneric(int arg, @NotNull String[] args, @NotNull Function converter) { + default T asGeneric(@NotNull Function converter, int arg, @NotNull String[] args) { return args.length > arg ? converter.apply(args[arg]) : null; } @@ -119,6 +120,8 @@ default T asGeneric(int arg, @NotNull String[] args, @NotNull Functionarg of the args array to a {@link String}, this * method won't do any actual conversion and will just return the argument if found, def if not. @@ -133,52 +136,53 @@ default T asGeneric(int arg, @NotNull String[] args, @NotNull Function arg ? args[arg] : def; - if (result == null) - return null; return removesEventPatterns() ? MCStrings.stripEventPatterns(result) : result; } /** * Converts the specified arg of the args array to a {@link String}, this - * method will apply modifier to the argument only if one is found and def isn't {@code null}. - * If no argument is found on the arg position and def is {@code null}, - * {@code null} will be returned and the modifier won't be applied. + * method won't do any actual conversion and will just return the argument if found, null if not. * - * @param modifier A {@link Function} to modify the resulting {@link String} that will be applied - * only if the {@link String} is not {@code null}. * @param arg the array position of the argument to get, can be out of bounds. * @param args the array of arguments to use. - * @param def the default value to return if arg is out of bounds. * * @return The argument as a {@link String} if found on the args array, null otherwise. * - * @throws NullPointerException if modifier is {@code null}. - * * @since MCUtils 1.0.0 */ @Nullable - default String asString(@NotNull Function modifier, int arg, @NotNull String[] args, @Nullable String def) { - final String result = asString(arg, args, def); - return result == null ? null : modifier.apply(result); + default String asString(int arg, @NotNull String[] args) { + final String result = args.length > arg ? args[arg] : null; + return result != null && removesEventPatterns() ? MCStrings.stripEventPatterns(result) : result; } + // With modifier // + /** * Converts the specified arg of the args array to a {@link String}, this - * method won't do any actual conversion and will just return the argument if found, null if not. + * method will apply modifier to the argument only if one is found and def isn't {@code null}. + * If no argument is found on the arg position and def is {@code null}, + * {@code null} will be returned and the modifier won't be applied. * + * @param modifier A {@link Function} to modify the resulting {@link String} that will be applied + * only if the {@link String} is not {@code null}. * @param arg the array position of the argument to get, can be out of bounds. * @param args the array of arguments to use. + * @param def the default value to return if arg is out of bounds. * * @return The argument as a {@link String} if found on the args array, null otherwise. * + * @throws NullPointerException if modifier is {@code null}. + * * @since MCUtils 1.0.0 */ - @Nullable - default String asString(int arg, @NotNull String[] args) { - return asString(arg, args, null); + @NotNull + default String asString(@NotNull Function modifier, int arg, @NotNull String[] args, @NotNull String def) { + return modifier.apply(asString(arg, args, def)); } /** @@ -207,6 +211,20 @@ default String asString(@NotNull Function modifier, int arg, @No * String ranges */ + @NotNull + private String buildRange(@Nullable Function modifier, int fromArg, @NotNull String[] args, @NotNull String first) { + final StringBuilder builder = new StringBuilder(first); + final boolean removeEvents = removesEventPatterns(); + Function finalModifier = null; + if (modifier == null && removeEvents) + finalModifier = MCStrings::stripEventPatterns; + else if (modifier != null) + finalModifier = str -> modifier.apply(MCStrings.stripEventPatterns(str)); + for (int i = fromArg + 1; i < args.length; i++) + builder.append(' ').append(finalModifier == null ? args[i] : finalModifier.apply(args[i])); + return builder.toString(); + } + /** * Gets a range of arguments starting at fromArg all the way to * the end of args, then, converts that range of arguments to a @@ -222,15 +240,10 @@ default String asString(@NotNull Function modifier, int arg, @No * * @since MCUtils 1.0.0 */ - @Nullable - default String asStringRange(int fromArg, @NotNull String[] args, @Nullable String def) { - String str = asString(fromArg, args, null); - if (str == null) - return def; - final StringBuilder builder = new StringBuilder().append(str); - for (int i = fromArg + 1; i < args.length; i++) - builder.append(' ').append(MCStrings.stripEventPatterns(args[i])); - return builder.toString(); + @NotNull + default String asStringRange(int fromArg, @NotNull String[] args, @NotNull String def) { + final String first = asString(fromArg, args); + return first == null ? def : buildRange(null, fromArg, args, first); } /** @@ -249,7 +262,8 @@ default String asStringRange(int fromArg, @NotNull String[] args, @Nullable Stri */ @Nullable default String asStringRange(int fromArg, @NotNull String[] args) { - return asStringRange(fromArg, args, null); + final String first = asString(fromArg, args); + return first == null ? null : buildRange(null, fromArg, args, first); } /** @@ -270,10 +284,10 @@ default String asStringRange(int fromArg, @NotNull String[] args) { * * @since MCUtils 1.0.0 */ - @Nullable - default String asStringRange(@NotNull Function modifier, int fromArg, @NotNull String[] args, @Nullable String def) { - final String range = asStringRange(fromArg, args, def); - return range == null ? def : modifier.apply(range); + @NotNull + default String asStringRange(@NotNull Function modifier, int fromArg, @NotNull String[] args, @NotNull String def) { + final String first = asString(fromArg, args); + return first == null ? def : buildRange(modifier, fromArg, args, first); } /** @@ -294,7 +308,8 @@ default String asStringRange(@NotNull Function modifier, int fro */ @Nullable default String asStringRange(@NotNull Function modifier, int fromArg, @NotNull String[] args) { - return asStringRange(modifier, fromArg, args, null); + final String first = asString(fromArg, args); + return first == null ? null : buildRange(modifier, fromArg, args, first); } /*