From 08171c241c7833a4616d8bc36a788a2ea6e0be39 Mon Sep 17 00:00:00 2001 From: broccolai Date: Thu, 10 Sep 2020 16:44:18 +0100 Subject: [PATCH] Add string component --- .../components/standard/StringComponent.java | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 cloud-core/src/main/java/com/intellectualsites/commands/components/standard/StringComponent.java diff --git a/cloud-core/src/main/java/com/intellectualsites/commands/components/standard/StringComponent.java b/cloud-core/src/main/java/com/intellectualsites/commands/components/standard/StringComponent.java new file mode 100644 index 00000000..1c53ad2d --- /dev/null +++ b/cloud-core/src/main/java/com/intellectualsites/commands/components/standard/StringComponent.java @@ -0,0 +1,106 @@ +package com.intellectualsites.commands.components.standard; + +import com.intellectualsites.commands.components.CommandComponent; +import com.intellectualsites.commands.components.parser.ComponentParseResult; +import com.intellectualsites.commands.components.parser.ComponentParser; +import com.intellectualsites.commands.context.CommandContext; +import com.intellectualsites.commands.sender.CommandSender; + +import java.util.Queue; +import java.util.StringJoiner; +import javax.annotation.Nonnull; + +@SuppressWarnings("unused") +public class StringComponent extends CommandComponent { + private final boolean greedy; + + public StringComponent(final boolean required, @Nonnull final String name, + final boolean greedy, @Nonnull final String defaultValue) { + super(required, name, new StringParser<>(greedy), defaultValue); + this.greedy = greedy; + } + + @Nonnull + public static StringComponent.Builder newBuilder(@Nonnull final String name) { + return new StringComponent.Builder<>(name); + } + + @Nonnull + public static CommandComponent required(@Nonnull final String name) { + return StringComponent.newBuilder(name).asRequired().build(); + } + + @Nonnull + public static CommandComponent optional(@Nonnull final String name) { + return StringComponent.newBuilder(name).asOptional().build(); + } + + @Nonnull + public static CommandComponent optional(@Nonnull final String name, + final String defaultNum) { + return StringComponent.newBuilder(name).asOptionalWithDefault(defaultNum).build(); + } + + public static final class Builder extends CommandComponent.Builder { + + private boolean greedy = false; + + protected Builder(@Nonnull final String name) { + super(name); + } + + @Nonnull + public Builder withGreedy(final boolean greedy) { + this.greedy = greedy; + return this; + } + + @Nonnull + @Override + public StringComponent build() { + return new StringComponent<>(this.required, this.name, this.greedy, this.defaultValue); + } + + } + + /** + * Get the greedy boolean + * + * @return Greedy boolean + */ + public boolean isGreedy() { + return greedy; + } + + private static final class StringParser implements ComponentParser { + private final boolean greedy; + + public StringParser(final boolean greedy) { + this.greedy = greedy; + } + + @Nonnull + @Override + public ComponentParseResult parse(@Nonnull final CommandContext commandContext, + @Nonnull final Queue inputQueue) { + final String input = inputQueue.peek(); + if (input == null) { + return ComponentParseResult.failure(new NullPointerException("No input was provided")); + } + + if (!greedy) { + inputQueue.remove(); + return ComponentParseResult.success(input); + } + + final StringJoiner sj = new StringJoiner(" "); + final int size = inputQueue.size(); + + for (int i = 0; i < size; i++) { + sj.add(inputQueue.remove()); + } + + return ComponentParseResult.success(sj.toString()); + } + } +}