refactor: use lombok, reduce code surface

dependabot/gradle/org.projectlombok-lombok-1.18.34
William 5 months ago
parent 513aa24d03
commit e5c8975339
No known key found for this signature in database

@ -24,6 +24,9 @@ package net.william278.uniform;
import com.mojang.brigadier.arguments.*;
import com.mojang.brigadier.suggestion.SuggestionProvider;
import com.mojang.brigadier.tree.LiteralCommandNode;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.william278.uniform.element.ArgumentElement;
import net.william278.uniform.element.CommandElement;
import net.william278.uniform.element.LiteralElement;
@ -31,27 +34,29 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;
@SuppressWarnings("unused")
@RequiredArgsConstructor
public abstract class Command<S> {
@Getter
private final String name;
@Getter
private final String[] aliases;
private @Nullable Predicate<S> condition;
private @Nullable CommandExecutor<S> defaultExecutor;
@Nullable
@Getter(AccessLevel.PACKAGE)
private Predicate<S> condition;
@Nullable
@Getter(AccessLevel.PACKAGE)
private CommandExecutor<S> defaultExecutor;
@Getter(AccessLevel.PACKAGE)
private final List<CommandSyntax<S>> syntaxes = new ArrayList<>();
@Getter(AccessLevel.PACKAGE)
private final List<Command<S>> subCommands = new ArrayList<>();
public Command(@NotNull String name, @NotNull String... aliases) {
this.name = name;
this.aliases = aliases;
}
public Command(@NotNull String name) {
this(name, new String[0]);
}
@ -80,76 +85,64 @@ public abstract class Command<S> {
this.subCommands.add(command);
}
public final @NotNull LiteralCommandNode<S> build() {
@NotNull
public final LiteralCommandNode<S> build() {
return Graph.create(this).build();
}
public final @NotNull String getName() {
return this.name;
}
public final @NotNull String[] getAliases() {
return this.aliases;
}
final @Nullable Predicate<S> getCondition() {
return this.condition;
}
final @Nullable CommandExecutor<S> getDefaultExecutor() {
return this.defaultExecutor;
}
final @NotNull Collection<CommandSyntax<S>> getSyntaxes() {
return this.syntaxes;
}
final @NotNull Collection<Command<S>> getSubCommands() {
return this.subCommands;
}
protected static <S> @NotNull LiteralElement<S> literalArg(@NotNull String name) {
@NotNull
protected static <S> LiteralElement<S> literalArg(@NotNull String name) {
return new LiteralElement<>(name);
}
protected static <S> @NotNull ArgumentElement<S, String> stringArg(@NotNull String name) {
@NotNull
protected static <S> ArgumentElement<S, String> stringArg(@NotNull String name) {
return argument(name, StringArgumentType.string());
}
protected static <S> @NotNull ArgumentElement<S, Integer> integerArg(@NotNull String name) {
@NotNull
protected static <S> ArgumentElement<S, Integer> integerArg(@NotNull String name) {
return argument(name, IntegerArgumentType.integer());
}
protected static <S> @NotNull ArgumentElement<S, Integer> integerArg(@NotNull String name, int min) {
@NotNull
protected static <S> ArgumentElement<S, Integer> integerArg(@NotNull String name, int min) {
return argument(name, IntegerArgumentType.integer(min));
}
protected static <S> @NotNull ArgumentElement<S, Integer> integerArg(@NotNull String name, int min, int max) {
@NotNull
protected static <S> ArgumentElement<S, Integer> integerArg(@NotNull String name, int min, int max) {
return argument(name, IntegerArgumentType.integer(min, max));
}
protected static <S> @NotNull ArgumentElement<S, Float> floatArg(@NotNull String name) {
@NotNull
protected static <S> ArgumentElement<S, Float> floatArg(@NotNull String name) {
return argument(name, FloatArgumentType.floatArg());
}
protected static <S> @NotNull ArgumentElement<S, Float> floatArg(@NotNull String name, float min) {
@NotNull
protected static <S> ArgumentElement<S, Float> floatArg(@NotNull String name, float min) {
return argument(name, FloatArgumentType.floatArg(min));
}
protected static <S> @NotNull ArgumentElement<S, Float> floatArg(@NotNull String name, float min, float max) {
@NotNull
protected static <S> ArgumentElement<S, Float> floatArg(@NotNull String name, float min, float max) {
return argument(name, FloatArgumentType.floatArg(min, max));
}
protected static <S> @NotNull ArgumentElement<S, Boolean> booleanArg(@NotNull String name) {
@NotNull
protected static <S> ArgumentElement<S, Boolean> booleanArg(@NotNull String name) {
return argument(name, BoolArgumentType.bool());
}
protected static <S, T> @NotNull ArgumentElement<S, T> argument(@NotNull String name, @NotNull ArgumentType<T> type,
@NotNull
protected static <S, T> ArgumentElement<S, T> argument(@NotNull String name, @NotNull ArgumentType<T> type,
@Nullable SuggestionProvider<S> suggestionProvider) {
return new ArgumentElement<>(name, type, suggestionProvider);
}
protected static <S, T> @NotNull ArgumentElement<S, T> argument(@NotNull String name, @NotNull ArgumentType<T> type) {
@NotNull
protected static <S, T> ArgumentElement<S, T> argument(@NotNull String name, @NotNull ArgumentType<T> type) {
return argument(name, type, null);
}

@ -27,4 +27,5 @@ import org.jetbrains.annotations.NotNull;
public interface CommandExecutor<S> {
void execute(@NotNull CommandContext<S> context);
}

@ -28,5 +28,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.function.Predicate;
public record CommandSyntax<S>(@Nullable Predicate<S> condition, @NotNull CommandExecutor<S> executor, @NotNull List<CommandElement<S>> elements) {
public record CommandSyntax<S>(@Nullable Predicate<S> condition, @NotNull CommandExecutor<S> executor,
@NotNull List<CommandElement<S>> elements) {
}

@ -71,4 +71,5 @@ record ConversionNode<S>(@NotNull CommandElement<S> element, @Nullable Execution
return new Node<>(this.element, this.execution, List.of(nodes));
}
}

@ -21,7 +21,6 @@
package net.william278.uniform;
import com.mojang.brigadier.builder.ArgumentBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -30,13 +29,13 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.Predicate;
record Execution<S>(@NotNull Predicate<S> predicate, @Nullable CommandExecutor<S> defaultExecutor, @Nullable CommandExecutor<S> executor,
@Nullable Predicate<S> condition) implements Predicate<S> {
record Execution<S>(@NotNull Predicate<S> predicate, @Nullable CommandExecutor<S> defaultExecutor,
@Nullable CommandExecutor<S> executor, @Nullable Predicate<S> condition) implements Predicate<S> {
private static final Executor CACHED_EXECUTOR = Executors.newCachedThreadPool();
static <S> @NotNull Execution<S> fromCommand(@NotNull Command<S> command) {
@NotNull
static <S> Execution<S> fromCommand(@NotNull Command<S> command) {
CommandExecutor<S> defaultExecutor = command.getDefaultExecutor();
Predicate<S> defaultCondition = command.getCondition();
@ -52,7 +51,8 @@ record Execution<S>(@NotNull Predicate<S> predicate, @Nullable CommandExecutor<S
return new Execution<>(source -> defaultCondition == null || defaultCondition.test(source), defaultExecutor, executor, condition);
}
static <S> @NotNull Execution<S> fromSyntax(@NotNull CommandSyntax<S> syntax) {
@NotNull
static <S> Execution<S> fromSyntax(@NotNull CommandSyntax<S> syntax) {
CommandExecutor<S> executor = syntax.executor();
Predicate<S> condition = syntax.condition();
return new Execution<>(source -> condition == null || condition.test(source), null, executor, condition);
@ -72,10 +72,12 @@ record Execution<S>(@NotNull Predicate<S> predicate, @Nullable CommandExecutor<S
}
}
private static <S> com.mojang.brigadier.@NotNull Command<S> convertExecutor(@NotNull CommandExecutor<S> executor) {
@NotNull
private static <S> com.mojang.brigadier.Command<S> convertExecutor(@NotNull CommandExecutor<S> executor) {
return context -> {
CACHED_EXECUTOR.execute(() -> executor.execute(context));
return 1;
};
}
}

@ -29,7 +29,6 @@ import org.jetbrains.annotations.NotNull;
record Graph<S>(@NotNull Node<S> root) {
static <S> @NotNull Graph<S> create(@NotNull Command<S> command) {
return new Graph<>(Node.command(command));
}
@ -38,11 +37,13 @@ record Graph<S>(@NotNull Node<S> root) {
return new LiteralElement<>(command.getName());
}
@NotNull LiteralCommandNode<S> build() {
@NotNull
LiteralCommandNode<S> build() {
CommandNode<S> node = this.root.build();
if (!(node instanceof LiteralCommandNode<S> literalNode)) {
throw new IllegalStateException("Root node is somehow not a literal node. This should be impossible.");
}
return literalNode;
}
}

@ -46,4 +46,5 @@ record Node<S>(@NotNull CommandElement<S> element, @Nullable Execution<S> execut
return builder.build();
}
}

@ -33,7 +33,8 @@ public record ArgumentElement<S, T>(@NotNull String name, @NotNull ArgumentType<
@Nullable SuggestionProvider<S> suggestionProvider) implements CommandElement<S> {
@Override
public @NotNull ArgumentBuilder<S, ?> toBuilder() {
@NotNull
public ArgumentBuilder<S, ?> toBuilder() {
var builder = RequiredArgumentBuilder.<S, T>argument(this.name, this.type);
if (this.suggestionProvider != null) builder.suggests(this.suggestionProvider);
return builder;

@ -26,5 +26,7 @@ import org.jetbrains.annotations.NotNull;
public interface CommandElement<S> {
@NotNull ArgumentBuilder<S, ?> toBuilder();
@NotNull
ArgumentBuilder<S, ?> toBuilder();
}

@ -28,7 +28,8 @@ import org.jetbrains.annotations.NotNull;
public record LiteralElement<S>(@NotNull String name) implements CommandElement<S> {
@Override
public @NotNull ArgumentBuilder<S, ?> toBuilder() {
@NotNull
public ArgumentBuilder<S, ?> toBuilder() {
return LiteralArgumentBuilder.literal(this.name);
}
}
Loading…
Cancel
Save