From ad25f16f4d787ead70dea3f3c620f7750aae7161 Mon Sep 17 00:00:00 2001 From: InkerBot Date: Thu, 22 Aug 2024 21:59:59 +0800 Subject: [PATCH] use java8 to build --- build.gradle | 48 ++++++------------- .../uniform/bukkit/BukkitCommand.java | 21 +++++--- .../uniform/bukkit/BukkitCommandUser.java | 14 ++++-- .../uniform/bukkit/BukkitUniform.java | 4 +- common/build.gradle | 1 + .../net/william278/uniform/BaseCommand.java | 3 +- .../java/net/william278/uniform/Command.java | 38 ++++++++++----- .../william278/uniform/CommandExecutor.java | 6 +-- .../net/william278/uniform/CommandSyntax.java | 9 ++-- .../william278/uniform/ConversionNode.java | 18 +++++-- .../net/william278/uniform/Execution.java | 20 +++++--- .../java/net/william278/uniform/Graph.java | 11 +++-- .../java/net/william278/uniform/Node.java | 7 ++- .../net/william278/uniform/Permission.java | 23 ++++++--- .../java/net/william278/uniform/Uniform.java | 2 +- .../uniform/element/ArgumentElement.java | 2 +- .../uniform/element/LiteralElement.java | 5 +- .../william278/uniform/util/StringUtil.java | 40 ++++++++++++++++ .../william278/uniform/ExtendedCommand.java | 21 ++++++-- .../uniform/paper/LegacyPaperCommand.java | 17 +++++-- .../uniform/paper/LegacyPaperCommandUser.java | 14 ++++-- .../uniform/paper/PaperCommand.java | 16 ++++--- .../uniform/paper/PaperCommandUser.java | 12 +++-- .../uniform/paper/PaperUniform.java | 4 +- settings.gradle | 6 +-- .../uniform/velocity/VelocityCommandUser.java | 12 +++-- 26 files changed, 250 insertions(+), 124 deletions(-) create mode 100644 common/src/main/java/net/william278/uniform/util/StringUtil.java diff --git a/build.gradle b/build.gradle index 760987d..1cf7d0a 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ plugins { id 'java' } -group 'net.william278' +group 'org.inksnow.husk' version "$ext.library_version${versionMetadata()}" description "$ext.library_description" defaultTasks 'licenseFormat', 'build' @@ -21,30 +21,12 @@ ext { publishing { repositories { - if (System.getenv("RELEASES_MAVEN_USERNAME") != null) { - maven { - name = "william278-releases" - url = "https://repo.william278.net/releases" - credentials { - username = System.getenv("RELEASES_MAVEN_USERNAME") - password = System.getenv("RELEASES_MAVEN_PASSWORD") - } - authentication { - basic(BasicAuthentication) - } - } - } - if (System.getenv("SNAPSHOTS_MAVEN_USERNAME") != null) { - maven { - name = "william278-snapshots" - url = "https://repo.william278.net/snapshots" - credentials { - username = System.getenv("SNAPSHOTS_MAVEN_USERNAME") - password = System.getenv("SNAPSHOTS_MAVEN_PASSWORD") - } - authentication { - basic(BasicAuthentication) - } + maven { + name = 'husk-release' + url = findProperty("repository.huskrelease.url") + credentials { + username = findProperty("repository.huskrelease.username") + password = findProperty("repository.huskrelease.password") } } } @@ -56,7 +38,7 @@ allprojects { apply plugin: 'java' compileJava.options.encoding = 'UTF-8' - compileJava.options.release.set 17 + compileJava.options.release.set 8 javadoc.options.encoding = 'UTF-8' javadoc.options.addStringOption('Xdoclint:none', '-quiet') @@ -134,7 +116,7 @@ subprojects { if (['common'].contains(project.name)) { publications { mavenJavaCommon(MavenPublication) { - groupId = 'net.william278.uniform' + groupId = 'org.inksnow.husk.uniform' artifactId = 'uniform-common' version = "$rootProject.version" artifact shadowJar @@ -147,7 +129,7 @@ subprojects { if (['paper'].contains(project.name)) { publications { mavenJavaPaper(MavenPublication) { - groupId = 'net.william278.uniform' + groupId = 'org.inksnow.husk.uniform' artifactId = 'uniform-paper' version = "$rootProject.version" artifact shadowJar @@ -160,7 +142,7 @@ subprojects { if (['bukkit'].contains(project.name)) { publications { mavenJavaBukkit(MavenPublication) { - groupId = 'net.william278.uniform' + groupId = 'org.inksnow.husk.uniform' artifactId = 'uniform-bukkit' version = "$rootProject.version" artifact shadowJar @@ -173,7 +155,7 @@ subprojects { if (['velocity'].contains(project.name)) { publications { mavenJavaVelocity(MavenPublication) { - groupId = 'net.william278.uniform' + groupId = 'org.inksnow.husk.uniform' artifactId = 'uniform-velocity' version = "$rootProject.version" artifact shadowJar @@ -186,7 +168,7 @@ subprojects { if (['fabric-1.20.1'].contains(project.name)) { publications { mavenJavaFabric1_20_1(MavenPublication) { - groupId = 'net.william278.uniform' + groupId = 'org.inksnow.husk.uniform' artifactId = 'uniform-fabric' version = "${rootProject.version}+1.20.1" artifact remapJar @@ -199,7 +181,7 @@ subprojects { if (['fabric-1.21'].contains(project.name)) { publications { mavenJavaFabric1_21(MavenPublication) { - groupId = 'net.william278.uniform' + groupId = 'org.inksnow.husk.uniform' artifactId = 'uniform-fabric' version = "${rootProject.version}+1.21" artifact remapJar @@ -212,7 +194,7 @@ subprojects { if (['sponge-11'].contains(project.name)) { publications { mavenSponge11(MavenPublication) { - groupId = 'net.william278.uniform' + groupId = 'org.inksnow.husk.uniform' artifactId = 'uniform-sponge' version = "${rootProject.version}+11" artifact shadowJar diff --git a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java index 7ebbd0d..af48919 100644 --- a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommand.java @@ -32,6 +32,7 @@ import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; import net.william278.uniform.Permission; import net.william278.uniform.Uniform; +import net.william278.uniform.util.StringUtil; import org.bukkit.Bukkit; import org.bukkit.command.CommandException; import org.bukkit.command.CommandSender; @@ -82,14 +83,17 @@ public class BukkitCommand extends BaseCommand { this.setAliases(command.getAliases()); this.setUsage(getUsageText()); if (permission != null) { - this.setPermission(permission.node()); + this.setPermission(permission.getNode()); } } @NotNull private String getUsageText() { - return dispatcher.getSmartUsage(dispatcher.getRoot(), Bukkit.getConsoleSender()).values().stream() - .map("/%s"::formatted).collect(Collectors.joining("\n")); + return dispatcher.getSmartUsage(dispatcher.getRoot(), Bukkit.getConsoleSender()) + .values() + .stream() + .map("/"::concat) + .collect(Collectors.joining("\n")); } @SuppressWarnings("deprecation") @@ -121,13 +125,15 @@ public class BukkitCommand extends BaseCommand { dispatcher.parse(passed, sender), passed.length() // Spigot API limitation - we can only TAB complete the full text length :( ) - .thenApply(suggestions -> suggestions.getList().stream().map(Suggestion::getText).toList()) + .thenApply(suggestions -> suggestions.getList().stream() + .map(Suggestion::getText) + .collect(Collectors.toList())) .join(); } @Override public boolean testPermissionSilent(@NotNull CommandSender target) { - if (permission == null || permission.node().isBlank()) { + if (permission == null || StringUtil.isBlank(permission.getNode())) { return true; } return new BukkitCommandUser(target).checkPermission(permission); @@ -140,7 +146,10 @@ public class BukkitCommand extends BaseCommand { @NotNull private String getInput(@NotNull String[] args) { - return args.length == 0 ? getName() : "%s %s".formatted(getName(), String.join(" ", args)); + if (args.length == 0) { + return getName(); + } + return getName() + " " + String.join(" ", args); } } diff --git a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommandUser.java b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommandUser.java index 5673ac0..866de98 100644 --- a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommandUser.java +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitCommandUser.java @@ -21,6 +21,7 @@ package net.william278.uniform.bukkit; +import lombok.Value; import net.kyori.adventure.audience.Audience; import net.william278.uniform.CommandUser; import net.william278.uniform.Permission; @@ -32,7 +33,10 @@ import org.jetbrains.annotations.Nullable; import java.util.UUID; -public record BukkitCommandUser(@NotNull CommandSender sender) implements CommandUser { +@Value +public class BukkitCommandUser implements CommandUser { + + @NotNull CommandSender sender; @Override @NotNull @@ -48,15 +52,15 @@ public record BukkitCommandUser(@NotNull CommandSender sender) implements Comman @Override @Nullable public UUID getUuid() { - return sender instanceof Player player ? player.getUniqueId() : null; + return sender instanceof Player ? ((Player) sender).getUniqueId() : null; } @Override public boolean checkPermission(@NotNull Permission permission) { - if (sender.isPermissionSet(permission.node())) { - return sender.hasPermission(permission.node()); + if (sender.isPermissionSet(permission.getNode())) { + return sender.hasPermission(permission.getNode()); } - return permission.defaultValue().check( + return permission.getDefaultValue().check( sender.isOp() || sender instanceof ConsoleCommandSender ); } diff --git a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java index 9343a02..67f8d8a 100644 --- a/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java +++ b/bukkit/src/main/java/net/william278/uniform/bukkit/BukkitUniform.java @@ -37,6 +37,7 @@ import space.arim.morepaperlib.commands.CommandRegistration; import java.util.Arrays; import java.util.Locale; import java.util.function.Function; +import java.util.stream.Collectors; /** * A class for registering commands with the Bukkit server's CommandMap @@ -91,7 +92,8 @@ public final class BukkitUniform implements Uniform { registrar.getServerCommandMap().registerAll( PLUGIN.getName().toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9_]", ""), Arrays.stream(commands).map(c -> (BukkitCommand) c) - .map(c -> (org.bukkit.command.Command) c.getImpl(this)).toList() + .map(c -> (org.bukkit.command.Command) c.getImpl(this)) + .collect(Collectors.toList()) ); } diff --git a/common/build.gradle b/common/build.gradle index bb42f35..e1d4bff 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -6,6 +6,7 @@ plugins { dependencies { compileOnlyApi 'com.mojang:brigadier:1.1.8' compileOnlyApi 'net.kyori:adventure-api:4.17.0' + compileOnlyApi 'com.google.guava:guava:21.0' compileOnly 'org.jetbrains:annotations:24.1.0' compileOnly 'org.projectlombok:lombok:1.18.32' diff --git a/common/src/main/java/net/william278/uniform/BaseCommand.java b/common/src/main/java/net/william278/uniform/BaseCommand.java index 7c5a83c..71ea227 100644 --- a/common/src/main/java/net/william278/uniform/BaseCommand.java +++ b/common/src/main/java/net/william278/uniform/BaseCommand.java @@ -21,6 +21,7 @@ package net.william278.uniform; +import com.google.common.collect.ImmutableList; import com.mojang.brigadier.arguments.*; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.suggestion.SuggestionProvider; @@ -114,7 +115,7 @@ public abstract class BaseCommand { @SafeVarargs public final void addConditionalSyntax(@Nullable Predicate condition, @NotNull CommandExecutor executor, @NotNull CommandElement... elements) { - var syntax = new CommandSyntax<>(condition, executor, List.of(elements)); + CommandSyntax syntax = new CommandSyntax<>(condition, executor, ImmutableList.copyOf(elements)); this.syntaxes.add(syntax); } diff --git a/common/src/main/java/net/william278/uniform/Command.java b/common/src/main/java/net/william278/uniform/Command.java index 81e4243..28cba8b 100644 --- a/common/src/main/java/net/william278/uniform/Command.java +++ b/common/src/main/java/net/william278/uniform/Command.java @@ -21,10 +21,13 @@ package net.william278.uniform; +import com.google.common.collect.ImmutableList; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.RequiredArgsConstructor; import lombok.Setter; +import lombok.Value; import net.william278.uniform.annotations.Argument; import net.william278.uniform.annotations.CommandDescription; import net.william278.uniform.annotations.CommandNode; @@ -39,6 +42,7 @@ import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.function.Predicate; @@ -51,7 +55,7 @@ import static net.william278.uniform.CommandExecutor.methodToExecutor; public abstract class Command implements CommandProvider { private final String name; - private List aliases = List.of(); + private List aliases = Collections.emptyList(); private String description = ""; @Getter(AccessLevel.NONE) private @Nullable Permission permission = null; @@ -70,7 +74,7 @@ public abstract class Command implements CommandProvider { throw new IllegalArgumentException("@CommandNode annotation is required on annotated command/sub-commands"); } this.name = node.value(); - this.aliases = List.of(node.aliases()); + this.aliases = ImmutableList.copyOf(node.aliases()); this.description = node.description(); this.scope = node.scope(); Permission.annotated(node.permission()).ifPresent(this::setPermission); @@ -87,11 +91,16 @@ public abstract class Command implements CommandProvider { } public boolean contains(@NotNull CommandUser user) { - return switch (this) { - case IN_GAME -> !user.isConsole(); - case CONSOLE -> user.isConsole(); - case ALL -> true; - }; + switch (this) { + case IN_GAME: + return !user.isConsole(); + case CONSOLE: + return user.isConsole(); + case ALL: + return true; + default: + throw new IllegalArgumentException("Unknown execution scope: " + this); + } } } @@ -194,8 +203,15 @@ public abstract class Command implements CommandProvider { } } - public record SubCommand(@NotNull String name, @NotNull List aliases, @Nullable Permission permission, - @NotNull ExecutionScope scope, @NotNull CommandProvider provider) { + @Value + @RequiredArgsConstructor + public static class SubCommand { + @NotNull String name; + @NotNull List aliases; + @Nullable Permission permission; + @NotNull ExecutionScope scope; + @NotNull CommandProvider provider; + public SubCommand(@NotNull String name, @NotNull List aliases, @Nullable Permission permission, @NotNull CommandProvider provider) { this(name, aliases, permission, ExecutionScope.ALL, provider); @@ -206,11 +222,11 @@ public abstract class Command implements CommandProvider { } public SubCommand(@NotNull String name, @Nullable Permission permission, @NotNull CommandProvider provider) { - this(name, List.of(), permission, ExecutionScope.ALL, provider); + this(name, Collections.emptyList(), permission, ExecutionScope.ALL, provider); } public SubCommand(@NotNull String name, @NotNull CommandProvider provider) { - this(name, List.of(), null, ExecutionScope.ALL, provider); + this(name, Collections.emptyList(), null, ExecutionScope.ALL, provider); } diff --git a/common/src/main/java/net/william278/uniform/CommandExecutor.java b/common/src/main/java/net/william278/uniform/CommandExecutor.java index 706861e..6f46102 100644 --- a/common/src/main/java/net/william278/uniform/CommandExecutor.java +++ b/common/src/main/java/net/william278/uniform/CommandExecutor.java @@ -39,7 +39,7 @@ public interface CommandExecutor { @NotNull BaseCommand cmd) { return (context) -> { try { - method.invoke(instance, injectParams(method, context, cmd)); + method.invoke(instance, private$injectParams(method, context, cmd)); } catch (IllegalAccessException | InvocationTargetException e) { throw new IllegalStateException("Failed to invoke command executor from annotated method", e); } @@ -47,8 +47,8 @@ public interface CommandExecutor { } @Nullable - private static Object @NotNull [] injectParams(@NotNull Method method, @NotNull CommandContext context, - @NotNull BaseCommand cmd) { + static Object @NotNull [] private$injectParams(@NotNull Method method, @NotNull CommandContext context, + @NotNull BaseCommand cmd) { final Object[] params = new Object[method.getParameterCount()]; for (int i = 0; i < method.getParameterCount(); i++) { final Parameter param = method.getParameters()[i]; diff --git a/common/src/main/java/net/william278/uniform/CommandSyntax.java b/common/src/main/java/net/william278/uniform/CommandSyntax.java index 4ee5126..7648833 100644 --- a/common/src/main/java/net/william278/uniform/CommandSyntax.java +++ b/common/src/main/java/net/william278/uniform/CommandSyntax.java @@ -21,6 +21,7 @@ package net.william278.uniform; +import lombok.Value; import net.william278.uniform.element.CommandElement; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -28,7 +29,9 @@ 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) { - +@Value +public class 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 450069c..58ab162 100644 --- a/common/src/main/java/net/william278/uniform/ConversionNode.java +++ b/common/src/main/java/net/william278/uniform/ConversionNode.java @@ -21,6 +21,9 @@ package net.william278.uniform; +import com.google.common.collect.ImmutableList; +import lombok.RequiredArgsConstructor; +import lombok.Value; import net.william278.uniform.element.CommandElement; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -31,8 +34,13 @@ import java.util.Map; import static net.william278.uniform.Graph.commandToElement; -record ConversionNode(@NotNull CommandElement element, @Nullable Execution execution, - @NotNull Map, ConversionNode> nextMap) { +@Value +@RequiredArgsConstructor +public class ConversionNode { + + @NotNull CommandElement element; + @Nullable Execution execution; + @NotNull Map, ConversionNode> nextMap; static @NotNull ConversionNode fromCommand(@NotNull BaseCommand command) { ConversionNode root = new ConversionNode<>(commandToElement(command), Execution.fromCommand(command)); @@ -40,8 +48,8 @@ record ConversionNode(@NotNull CommandElement element, @Nullable Execution for (CommandSyntax syntax : command.getSyntaxes()) { ConversionNode syntaxNode = root; - for (CommandElement element : syntax.elements()) { - boolean last = element == syntax.elements().get(syntax.elements().size() - 1); + for (CommandElement element : syntax.getElements()) { + boolean last = element == syntax.getElements().get(syntax.getElements().size() - 1); syntaxNode = syntaxNode.nextMap.computeIfAbsent(element, e -> { Execution execution = last ? Execution.fromSyntax(syntax) : null; return new ConversionNode<>(e, execution); @@ -69,7 +77,7 @@ record ConversionNode(@NotNull CommandElement element, @Nullable Execution nodes[i++] = entry.toNode(); } - return new Node<>(this.element, this.execution, List.of(nodes)); + return new Node<>(this.element, this.execution, ImmutableList.copyOf(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 29d9547..ef24f88 100644 --- a/common/src/main/java/net/william278/uniform/Execution.java +++ b/common/src/main/java/net/william278/uniform/Execution.java @@ -22,6 +22,7 @@ package net.william278.uniform; import com.mojang.brigadier.builder.ArgumentBuilder; +import lombok.Value; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -29,11 +30,16 @@ 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 { +@Value +public class Execution implements Predicate { private static final Executor CACHED_EXECUTOR = Executors.newCachedThreadPool(); + @NotNull Predicate predicate; + @Nullable CommandExecutor defaultExecutor; + @Nullable CommandExecutor executor; + @Nullable Predicate condition; + @NotNull static Execution fromCommand(@NotNull BaseCommand command) { CommandExecutor defaultExecutor = command.getDefaultExecutor(); @@ -42,9 +48,9 @@ record Execution(@NotNull Predicate predicate, @Nullable CommandExecutor executor = defaultExecutor; Predicate condition = defaultCondition; for (CommandSyntax syntax : command.getSyntaxes()) { - if (!syntax.elements().isEmpty()) continue; - executor = syntax.executor(); - condition = syntax.condition(); + if (!syntax.getElements().isEmpty()) continue; + executor = syntax.getExecutor(); + condition = syntax.getCondition(); break; } @@ -53,8 +59,8 @@ record Execution(@NotNull Predicate predicate, @Nullable CommandExecutor Execution fromSyntax(@NotNull CommandSyntax syntax) { - CommandExecutor executor = syntax.executor(); - Predicate condition = syntax.condition(); + CommandExecutor executor = syntax.getExecutor(); + Predicate condition = syntax.getCondition(); return new Execution<>(source -> condition == null || condition.test(source), null, executor, condition); } diff --git a/common/src/main/java/net/william278/uniform/Graph.java b/common/src/main/java/net/william278/uniform/Graph.java index 599ce0a..d90ad50 100644 --- a/common/src/main/java/net/william278/uniform/Graph.java +++ b/common/src/main/java/net/william278/uniform/Graph.java @@ -24,11 +24,14 @@ package net.william278.uniform; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.LiteralCommandNode; +import lombok.Value; import net.william278.uniform.element.CommandElement; import net.william278.uniform.element.LiteralElement; import org.jetbrains.annotations.NotNull; -record Graph(@NotNull Node root) { +@Value +public class Graph { + @NotNull Node root; static @NotNull Graph create(@NotNull BaseCommand command) { return new Graph<>(Node.command(command)); @@ -41,10 +44,10 @@ record Graph(@NotNull Node root) { @NotNull LiteralCommandNode build() { CommandNode node = this.root.build(); - if (!(node instanceof LiteralCommandNode literalNode)) { + if (!(node instanceof LiteralCommandNode)) { throw new IllegalStateException("Root node is somehow not a literal node. This should be impossible."); } - return literalNode; + return (LiteralCommandNode) node; } @NotNull @@ -52,7 +55,7 @@ record Graph(@NotNull Node root) { final LiteralArgumentBuilder builder = LiteralArgumentBuilder.literal(name); final CommandNode command = this.root.build(); builder.executes(command.getCommand()); - this.root.children().forEach(child -> builder.then(child.build())); + this.root.getChildren().forEach(child -> builder.then(child.build())); return builder; } diff --git a/common/src/main/java/net/william278/uniform/Node.java b/common/src/main/java/net/william278/uniform/Node.java index fab6284..8b7a1dd 100644 --- a/common/src/main/java/net/william278/uniform/Node.java +++ b/common/src/main/java/net/william278/uniform/Node.java @@ -23,13 +23,18 @@ package net.william278.uniform; import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.tree.CommandNode; +import lombok.Value; import net.william278.uniform.element.CommandElement; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; -record Node(@NotNull CommandElement element, @Nullable Execution execution, @NotNull List> children) { +@Value +public class Node { + @NotNull CommandElement element; + @Nullable Execution execution; + @NotNull List> children; static @NotNull Node command(@NotNull BaseCommand command) { return ConversionNode.fromCommand(command).toNode(); diff --git a/common/src/main/java/net/william278/uniform/Permission.java b/common/src/main/java/net/william278/uniform/Permission.java index 6c9f8df..ed6429a 100644 --- a/common/src/main/java/net/william278/uniform/Permission.java +++ b/common/src/main/java/net/william278/uniform/Permission.java @@ -21,14 +21,22 @@ package net.william278.uniform; +import lombok.RequiredArgsConstructor; +import lombok.Value; import net.william278.uniform.annotations.PermissionNode; +import net.william278.uniform.util.StringUtil; import org.jetbrains.annotations.NotNull; import java.util.Optional; import java.util.function.Predicate; @SuppressWarnings("unused") -public record Permission(@NotNull String node, @NotNull Default defaultValue) { +@Value +@RequiredArgsConstructor +public class Permission { + + @NotNull String node; + @NotNull Default defaultValue; Permission(@NotNull String node) { this(node, Default.FALSE); @@ -51,7 +59,7 @@ public record Permission(@NotNull String node, @NotNull Default defaultValue) { @NotNull static Optional annotated(@NotNull PermissionNode annotation) { - if (annotation.value().isBlank()) { + if (StringUtil.isBlank(annotation.value())) { return Optional.empty(); } return Optional.of(new Permission(annotation.value(), annotation.defaultValue())); @@ -63,11 +71,12 @@ public record Permission(@NotNull String node, @NotNull Default defaultValue) { FALSE; public boolean check(boolean op) { - return switch (this) { - case IF_OP -> op; - case TRUE -> true; - case FALSE -> false; - }; + switch (this) { + case IF_OP: return op; + case TRUE: return true; + case FALSE: return false; + default: throw new IllegalStateException("Unexpected value: " + this); + } } } diff --git a/common/src/main/java/net/william278/uniform/Uniform.java b/common/src/main/java/net/william278/uniform/Uniform.java index 9f01df2..5135a5d 100644 --- a/common/src/main/java/net/william278/uniform/Uniform.java +++ b/common/src/main/java/net/william278/uniform/Uniform.java @@ -32,7 +32,7 @@ public interface Uniform { default void register(@NotNull Object... annotated) { register(Arrays.stream(annotated) - .map(c -> c instanceof Command cmd ? cmd : new Command.AnnotatedCommand(c)) + .map(c -> c instanceof Command ? (Command) c : new Command.AnnotatedCommand(c)) .toArray(Command[]::new)); } 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 6899e4a..8180719 100644 --- a/common/src/main/java/net/william278/uniform/element/ArgumentElement.java +++ b/common/src/main/java/net/william278/uniform/element/ArgumentElement.java @@ -50,7 +50,7 @@ public final class ArgumentElement implements CommandElement { @Override @NotNull public ArgumentBuilder toBuilder() { - var builder = RequiredArgumentBuilder.argument(this.name, this.type); + RequiredArgumentBuilder 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/LiteralElement.java b/common/src/main/java/net/william278/uniform/element/LiteralElement.java index c18c1b8..b760f31 100644 --- a/common/src/main/java/net/william278/uniform/element/LiteralElement.java +++ b/common/src/main/java/net/william278/uniform/element/LiteralElement.java @@ -23,9 +23,12 @@ package net.william278.uniform.element; import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import lombok.Value; import org.jetbrains.annotations.NotNull; -public record LiteralElement(@NotNull String name) implements CommandElement { +@Value +public class LiteralElement implements CommandElement { + @NotNull String name; @Override @NotNull diff --git a/common/src/main/java/net/william278/uniform/util/StringUtil.java b/common/src/main/java/net/william278/uniform/util/StringUtil.java new file mode 100644 index 0000000..c881fe0 --- /dev/null +++ b/common/src/main/java/net/william278/uniform/util/StringUtil.java @@ -0,0 +1,40 @@ +/* + * This file is part of Uniform, licensed under the GNU General Public License v3.0. + * + * Copyright (c) Tofaa2 + * Copyright (c) William278 + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.william278.uniform.util; + +import lombok.experimental.UtilityClass; + +@UtilityClass +public class StringUtil { + public static boolean isBlank(String $this) { + final int strLen = $this.length(); + if (strLen == 0) { + return true; + } + for (int i = 0; i < strLen; i++) { + if (!Character.isWhitespace($this.charAt(i))) { + return false; + } + } + return true; + } +} diff --git a/example-plugin/src/main/java/net/william278/uniform/ExtendedCommand.java b/example-plugin/src/main/java/net/william278/uniform/ExtendedCommand.java index c4632e8..0325790 100644 --- a/example-plugin/src/main/java/net/william278/uniform/ExtendedCommand.java +++ b/example-plugin/src/main/java/net/william278/uniform/ExtendedCommand.java @@ -21,6 +21,7 @@ package net.william278.uniform; +import com.google.common.collect.ImmutableList; import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.kyori.adventure.text.Component; import net.william278.uniform.element.ArgumentElement; @@ -38,7 +39,7 @@ public class ExtendedCommand extends Command { public ExtendedCommand() { super("example"); setDescription("An example command for Uniform"); - setAliases(List.of("helloworld")); + setAliases(ImmutableList.of("helloworld")); setPermission(Permission.defaultIfOp("uniform.example")); } @@ -56,9 +57,21 @@ public class ExtendedCommand extends Command { final CommandUser user = sub.getUser(ctx.getSource()); final IceCreamFlavor flavor = ctx.getArgument("flavor", IceCreamFlavor.class); switch (flavor) { - case VANILLA -> user.getAudience().sendMessage(Component.text("Vanilla ice cream is fine!")); - case CHOCOLATE -> user.getAudience().sendMessage(Component.text("Chocolate ice cream is kino!")); - case STRAWBERRY -> user.getAudience().sendMessage(Component.text("Strawberry ice cream is ok...")); + case VANILLA: { + user.getAudience().sendMessage(Component.text("Vanilla ice cream is fine!")); + break; + } + case CHOCOLATE: { + user.getAudience().sendMessage(Component.text("Chocolate ice cream is kino!")); + break; + } + case STRAWBERRY: { + user.getAudience().sendMessage(Component.text("Strawberry ice cream is ok...")); + break; + } + default: { + // do nothing + } } }, exampleCustomArg())); } diff --git a/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java b/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java index 2b11994..d4319e0 100644 --- a/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java +++ b/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java @@ -27,6 +27,7 @@ import com.mojang.brigadier.suggestion.Suggestion; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.william278.uniform.*; +import net.william278.uniform.util.StringUtil; import org.bukkit.Bukkit; import org.bukkit.command.CommandException; import org.bukkit.command.CommandSender; @@ -87,14 +88,14 @@ public class LegacyPaperCommand extends BaseCommand { this.setAliases(command.getAliases()); this.setUsage(getUsageText()); if (permission != null) { - this.setPermission(permission.node()); + this.setPermission(permission.getNode()); } } @NotNull private String getUsageText() { return dispatcher.getSmartUsage(dispatcher.getRoot(), Bukkit.getConsoleSender()).values().stream() - .map("/%s"::formatted).collect(Collectors.joining("\n")); + .map("/"::concat).collect(Collectors.joining("\n")); } @SuppressWarnings("deprecation") @@ -126,13 +127,16 @@ public class LegacyPaperCommand extends BaseCommand { dispatcher.parse(passed, sender), passed.length() // Spigot API limitation - we can only TAB complete the full text length :( ) - .thenApply(suggestions -> suggestions.getList().stream().map(Suggestion::getText).toList()) + .thenApply(suggestions -> suggestions.getList() + .stream() + .map(Suggestion::getText) + .collect(Collectors.toList())) .join(); } @Override public boolean testPermissionSilent(@NotNull CommandSender target) { - if (permission == null || permission.node().isBlank()) { + if (permission == null || StringUtil.isBlank(permission.getNode())) { return true; } return new LegacyPaperCommandUser(target).checkPermission(permission); @@ -140,7 +144,10 @@ public class LegacyPaperCommand extends BaseCommand { @NotNull private String getInput(@NotNull String[] args) { - return args.length == 0 ? getName() : "%s %s".formatted(getName(), String.join(" ", args)); + if (args.length == 0) { + return getName(); + } + return getName() + " " + String.join(" ", args); } } diff --git a/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommandUser.java b/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommandUser.java index 3ebe5f8..d425d1c 100644 --- a/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommandUser.java +++ b/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommandUser.java @@ -21,6 +21,7 @@ package net.william278.uniform.paper; +import lombok.Value; import net.kyori.adventure.audience.Audience; import net.william278.uniform.CommandUser; import net.william278.uniform.Permission; @@ -32,7 +33,10 @@ import org.jetbrains.annotations.Nullable; import java.util.UUID; -public record LegacyPaperCommandUser(@NotNull CommandSender sender) implements CommandUser { +@Value +public class LegacyPaperCommandUser implements CommandUser { + + @NotNull CommandSender sender; @Override @NotNull @@ -48,15 +52,15 @@ public record LegacyPaperCommandUser(@NotNull CommandSender sender) implements C @Override @Nullable public UUID getUuid() { - return sender instanceof Player player ? player.getUniqueId() : null; + return sender instanceof Player ? ((Player) sender).getUniqueId() : null; } @Override public boolean checkPermission(@NotNull Permission permission) { - if (sender.isPermissionSet(permission.node())) { - return sender.hasPermission(permission.node()); + if (sender.isPermissionSet(permission.getNode())) { + return sender.hasPermission(permission.getNode()); } - return permission.defaultValue().check( + return permission.getDefaultValue().check( sender.isOp() || sender instanceof ConsoleCommandSender ); } diff --git a/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java b/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java index ce3e0fd..ff948b1 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java @@ -21,6 +21,7 @@ package net.william278.uniform.paper; +import com.google.common.collect.ImmutableList; import com.mojang.brigadier.exceptions.CommandSyntaxException; import io.papermc.paper.command.brigadier.CommandSourceStack; import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; @@ -38,6 +39,7 @@ import java.util.Collection; import java.util.List; import java.util.Set; import java.util.function.Function; +import java.util.stream.Collectors; @SuppressWarnings({"unused", "UnstableApiUsage"}) public class PaperCommand extends BaseCommand { @@ -92,11 +94,11 @@ public class PaperCommand extends BaseCommand { if (playerName.equals("@a")) { return Bukkit.getOnlinePlayers(); } - var player = Bukkit.getPlayer(playerName); + Player player = Bukkit.getPlayer(playerName); if (player == null) { throw CommandSyntaxException.BUILT_IN_EXCEPTIONS.dispatcherUnknownArgument().createWithContext(reader); } - return List.of(player); + return ImmutableList.of(player); }, (context, builder) -> { builder.suggest("@a"); for (Player player : Bukkit.getOnlinePlayers()) { @@ -112,9 +114,9 @@ public class PaperCommand extends BaseCommand { public List> getSyntaxes() { return super.getSyntaxes().stream().map( syntax -> new CommandSyntax<>( - syntax.condition(), - syntax.executor(), - syntax.elements().stream() + syntax.getCondition(), + syntax.getExecutor(), + syntax.getElements().stream() .filter(e -> e instanceof ArgumentElement) .map(e -> (ArgumentElement) e) .map(e -> e.custom() ? new ArgumentElement<>( @@ -123,9 +125,9 @@ public class PaperCommand extends BaseCommand { e.suggestionProvider() ) : e) .map(e -> (CommandElement) e) - .toList() + .collect(Collectors.toList()) ) - ).toList(); + ).collect(Collectors.toList()); } @Override diff --git a/paper/src/main/java/net/william278/uniform/paper/PaperCommandUser.java b/paper/src/main/java/net/william278/uniform/paper/PaperCommandUser.java index 9b4e646..c52da7b 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperCommandUser.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperCommandUser.java @@ -22,6 +22,7 @@ package net.william278.uniform.paper; import io.papermc.paper.command.brigadier.CommandSourceStack; +import lombok.Value; import net.kyori.adventure.audience.Audience; import net.william278.uniform.CommandUser; import net.william278.uniform.Permission; @@ -32,8 +33,9 @@ import org.jetbrains.annotations.Nullable; import java.util.UUID; @SuppressWarnings("UnstableApiUsage") -public record PaperCommandUser(@NotNull CommandSourceStack source) implements CommandUser { - +@Value +public class PaperCommandUser implements CommandUser { + @NotNull CommandSourceStack source; @Override @NotNull @@ -55,10 +57,10 @@ public record PaperCommandUser(@NotNull CommandSourceStack source) implements Co @Override public boolean checkPermission(@NotNull Permission permission) { - if (source.getSender().isPermissionSet(permission.node())) { - return source.getSender().hasPermission(permission.node()); + if (source.getSender().isPermissionSet(permission.getNode())) { + return source.getSender().hasPermission(permission.getNode()); } - return permission.defaultValue().check( + return permission.getDefaultValue().check( source.getSender().isOp() || source.getSender() instanceof ConsoleCommandSender ); } diff --git a/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java b/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java index 8d7fd26..18c64d4 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java @@ -35,6 +35,7 @@ import java.util.Arrays; import java.util.Locale; import java.util.Set; import java.util.function.Function; +import java.util.stream.Collectors; import java.util.stream.Stream; /** @@ -108,7 +109,8 @@ public final class PaperUniform implements Uniform { // Register with the legacy API plugin.getServer().getCommandMap().registerAll( plugin.getName().toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9_]", ""), - s.map(c -> (LegacyPaperCommand) c).map(c -> (org.bukkit.command.Command) c.getImpl(this)).toList() + s.map(c -> (LegacyPaperCommand) c).map(c -> (org.bukkit.command.Command) c.getImpl(this)) + .collect(Collectors.toList()) ); } diff --git a/settings.gradle b/settings.gradle index 6196c78..96150fb 100644 --- a/settings.gradle +++ b/settings.gradle @@ -12,14 +12,14 @@ include( // Server Plugins 'paper', 'bukkit', - 'sponge-11', + // 'sponge-11', // Proxy Plugins 'velocity', // Fabric Server-Side Mods - 'fabric-1.20.1', - 'fabric-1.21', + // 'fabric-1.20.1', + // 'fabric-1.21', // Example plugin 'example-plugin' diff --git a/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommandUser.java b/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommandUser.java index d154194..e76e83f 100644 --- a/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommandUser.java +++ b/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommandUser.java @@ -24,6 +24,7 @@ package net.william278.uniform.velocity; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.permission.Tristate; import com.velocitypowered.api.proxy.Player; +import lombok.Value; import net.kyori.adventure.audience.Audience; import net.william278.uniform.CommandUser; import net.william278.uniform.Permission; @@ -32,7 +33,10 @@ import org.jetbrains.annotations.Nullable; import java.util.UUID; -public record VelocityCommandUser(CommandSource source) implements CommandUser { +@Value +public class VelocityCommandUser implements CommandUser { + + CommandSource source; @Override @NotNull @@ -54,10 +58,10 @@ public record VelocityCommandUser(CommandSource source) implements CommandUser { @Override public boolean checkPermission(@NotNull Permission permission) { - if (source.getPermissionValue(permission.node()) != Tristate.UNDEFINED) { - return source.getPermissionValue(permission.node()) == Tristate.TRUE; + if (source.getPermissionValue(permission.getNode()) != Tristate.UNDEFINED) { + return source.getPermissionValue(permission.getNode()) == Tristate.TRUE; } - return permission.defaultValue().check(source.hasPermission(permission.node())); + return permission.getDefaultValue().check(source.hasPermission(permission.getNode())); } }