From e5c8975339c7718c3aa7f5794ce59cc9ddabe3d9 Mon Sep 17 00:00:00 2001 From: William Date: Thu, 13 Jun 2024 13:47:19 +0100 Subject: [PATCH] refactor: use lombok, reduce code surface --- .../java/net/william278/uniform/Command.java | 85 +++++++++---------- .../william278/uniform/CommandExecutor.java | 1 + .../net/william278/uniform/CommandSyntax.java | 4 +- .../william278/uniform/ConversionNode.java | 1 + .../net/william278/uniform/Execution.java | 16 ++-- .../java/net/william278/uniform/Graph.java | 5 +- .../java/net/william278/uniform/Node.java | 1 + .../uniform/element/ArgumentElement.java | 3 +- .../uniform/element/CommandElement.java | 4 +- .../uniform/element/LiteralElement.java | 3 +- 10 files changed, 64 insertions(+), 59 deletions(-) diff --git a/common/src/main/java/net/william278/uniform/Command.java b/common/src/main/java/net/william278/uniform/Command.java index c5acdf6..21d575b 100644 --- a/common/src/main/java/net/william278/uniform/Command.java +++ b/common/src/main/java/net/william278/uniform/Command.java @@ -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 { + @Getter private final String name; + @Getter private final String[] aliases; - private @Nullable Predicate condition; - private @Nullable CommandExecutor defaultExecutor; - + @Nullable + @Getter(AccessLevel.PACKAGE) + private Predicate condition; + @Nullable + @Getter(AccessLevel.PACKAGE) + private CommandExecutor defaultExecutor; + @Getter(AccessLevel.PACKAGE) private final List> syntaxes = new ArrayList<>(); + @Getter(AccessLevel.PACKAGE) private final List> 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 { this.subCommands.add(command); } - public final @NotNull LiteralCommandNode build() { + @NotNull + public final LiteralCommandNode 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 getCondition() { - return this.condition; - } - - final @Nullable CommandExecutor getDefaultExecutor() { - return this.defaultExecutor; - } - - final @NotNull Collection> getSyntaxes() { - return this.syntaxes; - } - - final @NotNull Collection> getSubCommands() { - return this.subCommands; - } - - protected static @NotNull LiteralElement literalArg(@NotNull String name) { + @NotNull + protected static LiteralElement literalArg(@NotNull String name) { return new LiteralElement<>(name); } - protected static @NotNull ArgumentElement stringArg(@NotNull String name) { + @NotNull + protected static ArgumentElement stringArg(@NotNull String name) { return argument(name, StringArgumentType.string()); } - protected static @NotNull ArgumentElement integerArg(@NotNull String name) { + @NotNull + protected static ArgumentElement integerArg(@NotNull String name) { return argument(name, IntegerArgumentType.integer()); } - protected static @NotNull ArgumentElement integerArg(@NotNull String name, int min) { + @NotNull + protected static ArgumentElement integerArg(@NotNull String name, int min) { return argument(name, IntegerArgumentType.integer(min)); } - protected static @NotNull ArgumentElement integerArg(@NotNull String name, int min, int max) { + @NotNull + protected static ArgumentElement integerArg(@NotNull String name, int min, int max) { return argument(name, IntegerArgumentType.integer(min, max)); } - protected static @NotNull ArgumentElement floatArg(@NotNull String name) { + @NotNull + protected static ArgumentElement floatArg(@NotNull String name) { return argument(name, FloatArgumentType.floatArg()); } - protected static @NotNull ArgumentElement floatArg(@NotNull String name, float min) { + @NotNull + protected static ArgumentElement floatArg(@NotNull String name, float min) { return argument(name, FloatArgumentType.floatArg(min)); } - protected static @NotNull ArgumentElement floatArg(@NotNull String name, float min, float max) { + @NotNull + protected static ArgumentElement floatArg(@NotNull String name, float min, float max) { return argument(name, FloatArgumentType.floatArg(min, max)); } - protected static @NotNull ArgumentElement booleanArg(@NotNull String name) { + @NotNull + protected static ArgumentElement booleanArg(@NotNull String name) { return argument(name, BoolArgumentType.bool()); } - protected static @NotNull ArgumentElement argument(@NotNull String name, @NotNull ArgumentType type, - @Nullable SuggestionProvider suggestionProvider) { + @NotNull + protected static ArgumentElement argument(@NotNull String name, @NotNull ArgumentType type, + @Nullable SuggestionProvider suggestionProvider) { return new ArgumentElement<>(name, type, suggestionProvider); } - protected static @NotNull ArgumentElement argument(@NotNull String name, @NotNull ArgumentType type) { + @NotNull + protected static ArgumentElement argument(@NotNull String name, @NotNull ArgumentType type) { return argument(name, type, null); } diff --git a/common/src/main/java/net/william278/uniform/CommandExecutor.java b/common/src/main/java/net/william278/uniform/CommandExecutor.java index 1d1e34b..3e4d0bf 100644 --- a/common/src/main/java/net/william278/uniform/CommandExecutor.java +++ b/common/src/main/java/net/william278/uniform/CommandExecutor.java @@ -27,4 +27,5 @@ import org.jetbrains.annotations.NotNull; public interface CommandExecutor { void execute(@NotNull CommandContext context); + } diff --git a/common/src/main/java/net/william278/uniform/CommandSyntax.java b/common/src/main/java/net/william278/uniform/CommandSyntax.java index d919a70..4ee5126 100644 --- a/common/src/main/java/net/william278/uniform/CommandSyntax.java +++ b/common/src/main/java/net/william278/uniform/CommandSyntax.java @@ -28,5 +28,7 @@ import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.function.Predicate; -public record CommandSyntax(@Nullable Predicate condition, @NotNull CommandExecutor executor, @NotNull List> elements) { +public record CommandSyntax(@Nullable Predicate condition, @NotNull CommandExecutor executor, + @NotNull List> elements) { + } diff --git a/common/src/main/java/net/william278/uniform/ConversionNode.java b/common/src/main/java/net/william278/uniform/ConversionNode.java index ac992f6..aadb19c 100644 --- a/common/src/main/java/net/william278/uniform/ConversionNode.java +++ b/common/src/main/java/net/william278/uniform/ConversionNode.java @@ -71,4 +71,5 @@ record ConversionNode(@NotNull CommandElement element, @Nullable Execution return new Node<>(this.element, this.execution, List.of(nodes)); } + } \ No newline at end of file diff --git a/common/src/main/java/net/william278/uniform/Execution.java b/common/src/main/java/net/william278/uniform/Execution.java index 3821f33..214b5e9 100644 --- a/common/src/main/java/net/william278/uniform/Execution.java +++ b/common/src/main/java/net/william278/uniform/Execution.java @@ -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(@NotNull Predicate predicate, @Nullable CommandExecutor defaultExecutor, @Nullable CommandExecutor executor, - @Nullable Predicate condition) implements Predicate { +record Execution(@NotNull Predicate predicate, @Nullable CommandExecutor defaultExecutor, + @Nullable CommandExecutor executor, @Nullable Predicate condition) implements Predicate { private static final Executor CACHED_EXECUTOR = Executors.newCachedThreadPool(); - - static @NotNull Execution fromCommand(@NotNull Command command) { + @NotNull + static Execution fromCommand(@NotNull Command command) { CommandExecutor defaultExecutor = command.getDefaultExecutor(); Predicate defaultCondition = command.getCondition(); @@ -52,7 +51,8 @@ record Execution(@NotNull Predicate predicate, @Nullable CommandExecutor(source -> defaultCondition == null || defaultCondition.test(source), defaultExecutor, executor, condition); } - static @NotNull Execution fromSyntax(@NotNull CommandSyntax syntax) { + @NotNull + static Execution fromSyntax(@NotNull CommandSyntax syntax) { CommandExecutor executor = syntax.executor(); Predicate condition = syntax.condition(); return new Execution<>(source -> condition == null || condition.test(source), null, executor, condition); @@ -72,10 +72,12 @@ record Execution(@NotNull Predicate predicate, @Nullable CommandExecutor com.mojang.brigadier.@NotNull Command convertExecutor(@NotNull CommandExecutor executor) { + @NotNull + private static com.mojang.brigadier.Command convertExecutor(@NotNull CommandExecutor executor) { return context -> { CACHED_EXECUTOR.execute(() -> executor.execute(context)); return 1; }; } + } diff --git a/common/src/main/java/net/william278/uniform/Graph.java b/common/src/main/java/net/william278/uniform/Graph.java index b5cb8ce..9ad36a9 100644 --- a/common/src/main/java/net/william278/uniform/Graph.java +++ b/common/src/main/java/net/william278/uniform/Graph.java @@ -29,7 +29,6 @@ import org.jetbrains.annotations.NotNull; record Graph(@NotNull Node root) { - static @NotNull Graph create(@NotNull Command command) { return new Graph<>(Node.command(command)); } @@ -38,11 +37,13 @@ record Graph(@NotNull Node root) { return new LiteralElement<>(command.getName()); } - @NotNull LiteralCommandNode build() { + @NotNull + LiteralCommandNode build() { CommandNode node = this.root.build(); if (!(node instanceof LiteralCommandNode literalNode)) { throw new IllegalStateException("Root node is somehow not a literal node. This should be impossible."); } return literalNode; } + } \ No newline at end of file diff --git a/common/src/main/java/net/william278/uniform/Node.java b/common/src/main/java/net/william278/uniform/Node.java index ca0d1ae..2f35c18 100644 --- a/common/src/main/java/net/william278/uniform/Node.java +++ b/common/src/main/java/net/william278/uniform/Node.java @@ -46,4 +46,5 @@ record Node(@NotNull CommandElement element, @Nullable Execution execut return builder.build(); } + } \ No newline at end of file diff --git a/common/src/main/java/net/william278/uniform/element/ArgumentElement.java b/common/src/main/java/net/william278/uniform/element/ArgumentElement.java index a371a39..9365e3a 100644 --- a/common/src/main/java/net/william278/uniform/element/ArgumentElement.java +++ b/common/src/main/java/net/william278/uniform/element/ArgumentElement.java @@ -33,7 +33,8 @@ public record ArgumentElement(@NotNull String name, @NotNull ArgumentType< @Nullable SuggestionProvider suggestionProvider) implements CommandElement { @Override - public @NotNull ArgumentBuilder toBuilder() { + @NotNull + public ArgumentBuilder toBuilder() { var builder = RequiredArgumentBuilder.argument(this.name, this.type); if (this.suggestionProvider != null) builder.suggests(this.suggestionProvider); return builder; diff --git a/common/src/main/java/net/william278/uniform/element/CommandElement.java b/common/src/main/java/net/william278/uniform/element/CommandElement.java index 0284888..33f89e3 100644 --- a/common/src/main/java/net/william278/uniform/element/CommandElement.java +++ b/common/src/main/java/net/william278/uniform/element/CommandElement.java @@ -26,5 +26,7 @@ import org.jetbrains.annotations.NotNull; public interface CommandElement { - @NotNull ArgumentBuilder toBuilder(); + @NotNull + ArgumentBuilder toBuilder(); + } \ No newline at end of file diff --git a/common/src/main/java/net/william278/uniform/element/LiteralElement.java b/common/src/main/java/net/william278/uniform/element/LiteralElement.java index b99ce24..3684658 100644 --- a/common/src/main/java/net/william278/uniform/element/LiteralElement.java +++ b/common/src/main/java/net/william278/uniform/element/LiteralElement.java @@ -28,7 +28,8 @@ import org.jetbrains.annotations.NotNull; public record LiteralElement(@NotNull String name) implements CommandElement { @Override - public @NotNull ArgumentBuilder toBuilder() { + @NotNull + public ArgumentBuilder toBuilder() { return LiteralArgumentBuilder.literal(this.name); } } \ No newline at end of file