Subcommand interfaces

This commit is contained in:
Risto Lahtela 2020-05-06 18:04:01 +03:00
parent 70af3ed7e1
commit ba96fc3d90
5 changed files with 264 additions and 0 deletions

View File

@ -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<String> asList() {
return args;
}
public Arguments removeFirst() {
List<String> copy = new ArrayList<>(args);
if (!copy.isEmpty()) copy.remove(0);
return new Arguments(copy);
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.commands.use;
import java.util.Optional;
import java.util.UUID;
public interface CMDSender {
MessageBuilder buildMessage();
Optional<String> getPlayerName();
boolean hasPermission(String permission);
Optional<UUID> getUUID();
void send(String message);
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<UUID> clicker);
MessageBuilder hover(String text);
MessageBuilder hover(String... text);
MessageBuilder indent(int spaces);
MessageBuilder tabular(CharSequence columnSeparator);
void send();
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<String> aliases;
private final Set<String> requiredPermissions;
private final List<String> inDepthDescription;
private String primaryAlias;
private String description;
private BiConsumer<CMDSender, Arguments> executor;
private BiFunction<CMDSender, Arguments, List<String>> 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<String> getAliases() {
return aliases;
}
public Set<String> getRequiredPermissions() {
return requiredPermissions;
}
public String getDescription() {
return description;
}
public List<String> getInDepthDescription() {
return inDepthDescription;
}
public BiConsumer<CMDSender, Arguments> getExecutor() {
return executor;
}
public BiFunction<CMDSender, Arguments, List<String>> 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<CMDSender, Arguments> executor) {
subcommand.executor = executor;
return this;
}
@Override
public SubcommandBuilder onTabComplete(BiFunction<CMDSender, Arguments, List<String>> 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;
}
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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<CMDSender, Arguments> executor);
SubcommandBuilder onTabComplete(BiFunction<CMDSender, Arguments, List<String>> resolver);
Subcommand build();
}