From ba96fc3d905b3c1c0acbae4f0cebc601fde19437 Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Wed, 6 May 2020 18:04:01 +0300 Subject: [PATCH] Subcommand interfaces --- .../djrapitops/plan/commands/Arguments.java | 7 + .../plan/commands/use/CMDSender.java | 34 +++++ .../plan/commands/use/MessageBuilder.java | 42 ++++++ .../plan/commands/use/Subcommand.java | 133 ++++++++++++++++++ .../plan/commands/use/SubcommandBuilder.java | 48 +++++++ 5 files changed, 264 insertions(+) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/commands/use/CMDSender.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/commands/use/MessageBuilder.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/commands/use/Subcommand.java create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/commands/use/SubcommandBuilder.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/Arguments.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/Arguments.java index e496b5b70..e029ea523 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/Arguments.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/Arguments.java @@ -16,6 +16,7 @@ */ package com.djrapitops.plan.commands; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -62,4 +63,10 @@ public class Arguments { public List asList() { return args; } + + public Arguments removeFirst() { + List copy = new ArrayList<>(args); + if (!copy.isEmpty()) copy.remove(0); + return new Arguments(copy); + } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/use/CMDSender.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/CMDSender.java new file mode 100644 index 000000000..6f2a993e4 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/CMDSender.java @@ -0,0 +1,34 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.commands.use; + +import java.util.Optional; +import java.util.UUID; + +public interface CMDSender { + + MessageBuilder buildMessage(); + + Optional getPlayerName(); + + boolean hasPermission(String permission); + + Optional getUUID(); + + void send(String message); + +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/use/MessageBuilder.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/MessageBuilder.java new file mode 100644 index 000000000..63b8e1cd4 --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/MessageBuilder.java @@ -0,0 +1,42 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.commands.use; + +import java.util.UUID; +import java.util.function.Supplier; + +public interface MessageBuilder { + + MessageBuilder defineColorCharacter(char colorChar); + + MessageBuilder append(String msg); + + MessageBuilder link(String address); + + MessageBuilder click(Supplier clicker); + + MessageBuilder hover(String text); + + MessageBuilder hover(String... text); + + MessageBuilder indent(int spaces); + + MessageBuilder tabular(CharSequence columnSeparator); + + void send(); + +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/use/Subcommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/Subcommand.java new file mode 100644 index 000000000..f97b52c8d --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/Subcommand.java @@ -0,0 +1,133 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.commands.use; + +import com.djrapitops.plan.commands.Arguments; + +import java.util.*; +import java.util.function.BiConsumer; +import java.util.function.BiFunction; + +public class Subcommand { + + private final Set aliases; + private final Set requiredPermissions; + private final List inDepthDescription; + private String primaryAlias; + private String description; + private BiConsumer executor; + private BiFunction> argumentResolver; + + private Subcommand() { + aliases = new HashSet<>(); + requiredPermissions = new HashSet<>(); + inDepthDescription = new ArrayList<>(); + } + + public static SubcommandBuilder builder() { + return new Builder(); + } + + public String getPrimaryAlias() { + return primaryAlias; + } + + public Set getAliases() { + return aliases; + } + + public Set getRequiredPermissions() { + return requiredPermissions; + } + + public String getDescription() { + return description; + } + + public List getInDepthDescription() { + return inDepthDescription; + } + + public BiConsumer getExecutor() { + return executor; + } + + public BiFunction> getArgumentResolver() { + return argumentResolver; + } + + public static class Builder implements SubcommandBuilder { + private Subcommand subcommand; + + private Builder() {} + + @Override + public SubcommandBuilder alias(String alias) { + subcommand.aliases.add(alias); + if (subcommand.primaryAlias == null) subcommand.primaryAlias = alias; + return this; + } + + @Override + public SubcommandBuilder aliases(String... aliases) { + for (String alias : aliases) { + alias(alias); + } + return this; + } + + @Override + public SubcommandBuilder requirePermission(String permission) { + subcommand.requiredPermissions.add(permission); + return this; + } + + @Override + public SubcommandBuilder description(String description) { + subcommand.description = description; + return this; + } + + @Override + public SubcommandBuilder inDepthDescription(String... lines) { + subcommand.inDepthDescription.addAll(Arrays.asList(lines)); + return this; + } + + @Override + public SubcommandBuilder onCommand(BiConsumer executor) { + subcommand.executor = executor; + return this; + } + + @Override + public SubcommandBuilder onTabComplete(BiFunction> resolver) { + subcommand.argumentResolver = resolver; + return this; + } + + @Override + public Subcommand build() { + if (subcommand.executor == null) throw new IllegalStateException("Command executor not defined."); + if (subcommand.primaryAlias == null || subcommand.aliases.isEmpty()) { + throw new IllegalStateException("Command aliases not defined."); + } + return subcommand; + } + } + +} diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/use/SubcommandBuilder.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/SubcommandBuilder.java new file mode 100644 index 000000000..6a2984fdd --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/use/SubcommandBuilder.java @@ -0,0 +1,48 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.commands.use; + +import com.djrapitops.plan.commands.Arguments; +import org.apache.commons.lang3.StringUtils; + +import java.util.List; +import java.util.function.BiConsumer; +import java.util.function.BiFunction; + +public interface SubcommandBuilder { + + SubcommandBuilder alias(String alias); + + SubcommandBuilder aliases(String... aliases); + + SubcommandBuilder requirePermission(String permission); + + SubcommandBuilder description(String description); + + default SubcommandBuilder inDepthDescription(String inDepthDescription) { + return inDepthDescription(StringUtils.split(inDepthDescription, '\n')); + } + + SubcommandBuilder inDepthDescription(String... lines); + + SubcommandBuilder onCommand(BiConsumer executor); + + SubcommandBuilder onTabComplete(BiFunction> resolver); + + Subcommand build(); + +} \ No newline at end of file