use LiteralMeta instead of permission-less literal-names for subLiterals
This commit is contained in:
parent
5fd408e69e
commit
f18ce1c429
13 changed files with 68 additions and 40 deletions
|
|
@ -1,11 +1,14 @@
|
|||
package site.lab0x13.scrow.commands;
|
||||
|
||||
import site.lab0x13.scrow.commands.model.CommandContext;
|
||||
import site.lab0x13.scrow.commands.model.literal.LiteralMeta;
|
||||
import site.lab0x13.scrow.commands.model.literal.MessageStyle;
|
||||
import site.lab0x13.scrow.commands.model.literal.RootLiteral;
|
||||
import site.lab0x13.scrow.commands.parser.NodeTreeWalker;
|
||||
import site.lab0x13.scrow.commands.parser.WalkResult;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
|
@ -42,15 +45,21 @@ public class CommandHandler<C extends CommandContext<C>> {
|
|||
String input = null;
|
||||
switch (walkResult) {
|
||||
case WalkResult.LiteralUnknown<C> res -> {
|
||||
stream = res.lastLiteral().subLiteralNames().stream();
|
||||
stream = res.lastLiteral().subLiteralMetas().stream()
|
||||
.filter(literalMeta -> platformAdapter.checkPermission(ctx, literalMeta.permission()))
|
||||
.map(LiteralMeta::names)
|
||||
.flatMap(Collection::stream);
|
||||
input = res.input();
|
||||
}
|
||||
case WalkResult.LiteralExpected<C> res when trailingSpace ->
|
||||
stream = res.lastLiteral().subLiteralMetas().stream()
|
||||
.filter(literalMeta -> platformAdapter.checkPermission(ctx, literalMeta.permission()))
|
||||
.map(LiteralMeta::names)
|
||||
.flatMap(Collection::stream);
|
||||
case WalkResult.ArgumentIllegal<C> res -> {
|
||||
stream = res.arg().suggestionProvider().suggest(ctx, res.input()).stream();
|
||||
input = res.input();
|
||||
}
|
||||
case WalkResult.LiteralExpected<C> res when trailingSpace ->
|
||||
stream = res.lastLiteral().subLiteralNames().stream();
|
||||
case WalkResult.ArgumentExpected<C> res when trailingSpace ->
|
||||
stream = res.arg().suggestionProvider().suggest(ctx, "").stream();
|
||||
default -> {
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
package site.lab0x13.scrow.commands;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface PlatformAdapter<C> {
|
||||
|
||||
boolean checkPermission(C ctx, String permission);
|
||||
boolean checkPermission(C ctx, @Nullable String permission);
|
||||
|
||||
void sendMessage(C ctx, Component message);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import java.util.function.Supplier;
|
|||
|
||||
public final class DynamicLiteralBuilder<C extends CommandContext<C>> extends LiteralBuilder<C, DynamicLiteralBuilder<C>> {
|
||||
|
||||
private Supplier<List<String>> subLiteralNamesSupplier = Collections::emptyList;
|
||||
private Supplier<List<LiteralMeta>> subLiteralMetaSupplier = Collections::emptyList;
|
||||
private Function<String, @Nullable Literal<C>> subLiteralProvider = _ -> null;
|
||||
|
||||
DynamicLiteralBuilder(String... names) {
|
||||
|
|
@ -23,6 +23,16 @@ public final class DynamicLiteralBuilder<C extends CommandContext<C>> extends Li
|
|||
return this;
|
||||
}
|
||||
|
||||
public DynamicLiteralBuilder<C> withSubLiteralNames(Supplier<List<LiteralMeta>> names) {
|
||||
subLiteralMetaSupplier = names;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DynamicLiteralBuilder<C> withSubLiteralProvider(Function<String, @Nullable Literal<C>> literalProvider) {
|
||||
this.subLiteralProvider = literalProvider;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Literal<C> build() {
|
||||
return new DynamicLiteral<>(
|
||||
|
|
@ -37,19 +47,9 @@ public final class DynamicLiteralBuilder<C extends CommandContext<C>> extends Li
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<String> subLiteralNames() {
|
||||
return subLiteralNamesSupplier.get();
|
||||
public List<? extends LiteralMeta> subLiteralMetas() {
|
||||
return subLiteralMetaSupplier.get();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public DynamicLiteralBuilder<C> withSubLiteralNames(Supplier<List<String>> names) {
|
||||
subLiteralNamesSupplier = names;
|
||||
return this;
|
||||
}
|
||||
|
||||
public DynamicLiteralBuilder<C> withSubLiteralProvider(Function<String, @Nullable Literal<C>> literalProvider) {
|
||||
this.subLiteralProvider = literalProvider;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,12 +7,14 @@ import site.lab0x13.scrow.commands.model.Node;
|
|||
import site.lab0x13.scrow.commands.model.SuggestionProvider;
|
||||
import site.lab0x13.scrow.commands.model.argument.Argument;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @param <C> argument of the command context
|
||||
*/
|
||||
public interface Literal<C extends CommandContext<C>> extends Node<C> {
|
||||
public interface Literal<C extends CommandContext<C>> extends Node<C>, LiteralMeta {
|
||||
|
||||
static <C extends CommandContext<C>> StaticLiteralBuilder<C> builder(String... names) {
|
||||
return new StaticLiteralBuilder<>(names);
|
||||
|
|
@ -22,8 +24,6 @@ public interface Literal<C extends CommandContext<C>> extends Node<C> {
|
|||
return new DynamicLiteralBuilder<>(names);
|
||||
}
|
||||
|
||||
List<String> names();
|
||||
|
||||
@Nullable CommandExecutor<C> executor();
|
||||
|
||||
@Nullable Literal<C> getSubLiteral(String name);
|
||||
|
|
@ -31,14 +31,15 @@ public interface Literal<C extends CommandContext<C>> extends Node<C> {
|
|||
/**
|
||||
* @return aliases/names of literals
|
||||
*/
|
||||
List<String> subLiteralNames();
|
||||
List<? extends LiteralMeta> subLiteralMetas();
|
||||
|
||||
List<Argument<C, ?>> arguments();
|
||||
|
||||
String permission();
|
||||
|
||||
@Override
|
||||
default SuggestionProvider<C> suggestionProvider() {
|
||||
return (_, _) -> subLiteralNames();
|
||||
return (_, _) -> subLiteralMetas().stream()
|
||||
.map(LiteralMeta::names)
|
||||
.flatMap(Collection::stream)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package site.lab0x13.scrow.commands.model.literal;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface LiteralMeta {
|
||||
List<String> names();
|
||||
|
||||
String permission();
|
||||
}
|
||||
|
|
@ -37,8 +37,8 @@ public class RootLiteralWrapper<C extends CommandContext<C>> implements RootLite
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<String> subLiteralNames() {
|
||||
return literal.subLiteralNames();
|
||||
public List<? extends LiteralMeta> subLiteralMetas() {
|
||||
return literal.subLiteralMetas();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -30,9 +30,7 @@ public class StaticLiteral<C extends CommandContext<C>> extends DynamicLiteral<C
|
|||
}
|
||||
|
||||
@Override
|
||||
public List<String> subLiteralNames() {
|
||||
return subLiterals.stream()
|
||||
.flatMap(literal -> literal.names().stream())
|
||||
.toList();
|
||||
public List<? extends LiteralMeta> subLiteralMetas() {
|
||||
return subLiterals;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,12 +42,12 @@ public class NodeTreeWalker<C extends CommandContext<C>> {
|
|||
var nextLiteral = literal.getSubLiteral(token);
|
||||
if (nextLiteral == null)
|
||||
return new WalkResult.LiteralUnknown<>(ctx, literal, token);
|
||||
if (nextLiteral.permission() != null && platformAdapter.checkPermission(ctx, nextLiteral.permission()))
|
||||
if (platformAdapter.checkPermission(ctx, nextLiteral.permission()))
|
||||
return new WalkResult.LiteralUnknown<>(ctx, literal, token);
|
||||
literal = nextLiteral;
|
||||
}
|
||||
|
||||
if (!literal.subLiteralNames().isEmpty()) {
|
||||
if (!literal.subLiteralMetas().isEmpty()) {
|
||||
var isOptional = literal.executor() != null;
|
||||
return new WalkResult.LiteralExpected<>(ctx, literal, isOptional);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue