From d78847550c6e9ba6e1196763ed81ef3cc098176a Mon Sep 17 00:00:00 2001 From: William Date: Fri, 14 Jun 2024 15:55:06 +0100 Subject: [PATCH] feat: target Paper 1.20.6, add command descriptions --- README.md | 2 +- build.gradle | 4 +++ .../net/william278/uniform/BaseCommand.java | 14 +++++++--- .../java/net/william278/uniform/Command.java | 5 ++++ example-plugin/build.gradle | 4 +-- .../william278/uniform/ExampleCommand.java | 9 ++++--- .../uniform/ExampleCrossPlatCommand.java | 6 +++++ .../william278/uniform/UniformExample.java | 2 +- .../uniform/fabric/FabricCommand.java | 8 +++++- .../uniform/fabric/FabricCommand.java | 12 ++++++--- paper/build.gradle | 3 +-- .../uniform/paper/PaperCommand.java | 18 ++++++++----- .../uniform/paper/PaperCommandUser.java | 14 +++++----- .../uniform/paper/PaperUniform.java | 26 +++++++++---------- .../uniform/velocity/VelocityCommand.java | 8 +++++- 15 files changed, 91 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index a3355a6..729f02c 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Please note Uniform on Fabric requires [adventure-platform-fabric](https://docs. | Platform | Artifact | Minecraft | Java | |---------------|-------------------------|:----------:|:------:| | Common | `uniform-common` | - | \>`17` | -| Paper | `uniform-paper` | \>`1.20.4` | \>`17` | +| Paper | `uniform-paper` | \>`1.20.6` | \>`21` | | Velocity | `uniform-velocity` | \>`3.3.0` | \>`17` | | Fabric 1.20.1 | `uniform-fabric-1_20_1` | =`1.20.1` | \>`17` | | Fabric 1.20.6 | `uniform-fabric-1_20_6` | =`1.20.6` | \>`21` | diff --git a/build.gradle b/build.gradle index 2da9b06..b828f62 100644 --- a/build.gradle +++ b/build.gradle @@ -99,6 +99,10 @@ subprojects { archiveClassifier.set('') } + if (['example-plugin', 'fabric-1.20.6', 'paper'].contains(project.name)) { + compileJava.options.release.set 21 + } + // Don't include example plugin in publishing if (['example-plugin'].contains(project.name)) return; diff --git a/common/src/main/java/net/william278/uniform/BaseCommand.java b/common/src/main/java/net/william278/uniform/BaseCommand.java index e98dbd6..15698df 100644 --- a/common/src/main/java/net/william278/uniform/BaseCommand.java +++ b/common/src/main/java/net/william278/uniform/BaseCommand.java @@ -42,17 +42,25 @@ public abstract class BaseCommand { @Getter private final String name; @Getter - private final String[] aliases; + private final String description; + @Getter + private final List aliases; public BaseCommand(@NotNull Command command) { this.name = command.getName(); - this.aliases = command.getAliases().toArray(new String[0]); + this.aliases = command.getAliases(); + this.description = command.getDescription(); command.provide(this); } - public BaseCommand(@NotNull String name, @NotNull String... aliases) { + public BaseCommand(@NotNull String name, @NotNull String description, @NotNull List aliases) { this.name = name; this.aliases = aliases; + this.description = description; + } + + public BaseCommand(@NotNull String name, @NotNull List aliases) { + this(name, "", aliases); } @Nullable diff --git a/common/src/main/java/net/william278/uniform/Command.java b/common/src/main/java/net/william278/uniform/Command.java index 0b016dc..77415e3 100644 --- a/common/src/main/java/net/william278/uniform/Command.java +++ b/common/src/main/java/net/william278/uniform/Command.java @@ -30,6 +30,11 @@ public interface Command { @NotNull String getName(); + @NotNull + default String getDescription() { + return ""; + } + @NotNull default List getAliases() { return List.of(); diff --git a/example-plugin/build.gradle b/example-plugin/build.gradle index 24058ad..624e554 100644 --- a/example-plugin/build.gradle +++ b/example-plugin/build.gradle @@ -6,11 +6,11 @@ plugins { dependencies { implementation(project(":paper")) - compileOnly 'io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT' + compileOnly 'io.papermc.paper:paper-api:1.20.6-R0.1-SNAPSHOT' } tasks { runServer { - minecraftVersion("1.20.4") + minecraftVersion("1.20.6") } } \ No newline at end of file diff --git a/example-plugin/src/main/java/net/william278/uniform/ExampleCommand.java b/example-plugin/src/main/java/net/william278/uniform/ExampleCommand.java index 695437d..56a6570 100644 --- a/example-plugin/src/main/java/net/william278/uniform/ExampleCommand.java +++ b/example-plugin/src/main/java/net/william278/uniform/ExampleCommand.java @@ -24,14 +24,17 @@ package net.william278.uniform; import net.kyori.adventure.text.minimessage.MiniMessage; import net.william278.uniform.paper.PaperCommand; +import java.util.List; + +@SuppressWarnings("UnstableApiUsage") public class ExampleCommand extends PaperCommand { public ExampleCommand() { - super("example", "silly-command"); + super("example", "An example command", List.of("silly-command")); addSyntax((context) -> { - context.getSource().getBukkitSender().sendMessage("Woah!!!!"); + context.getSource().getSender().sendMessage("Woah!!!!"); String arg = context.getArgument("message", String.class); - context.getSource().getBukkitSender().sendMessage(MiniMessage.miniMessage().deserialize(arg)); + context.getSource().getSender().sendMessage(MiniMessage.miniMessage().deserialize(arg)); }, stringArg("message")); } diff --git a/example-plugin/src/main/java/net/william278/uniform/ExampleCrossPlatCommand.java b/example-plugin/src/main/java/net/william278/uniform/ExampleCrossPlatCommand.java index 477f819..bafb466 100644 --- a/example-plugin/src/main/java/net/william278/uniform/ExampleCrossPlatCommand.java +++ b/example-plugin/src/main/java/net/william278/uniform/ExampleCrossPlatCommand.java @@ -33,6 +33,12 @@ public class ExampleCrossPlatCommand implements Command { return "example-crossplat"; } + @Override + @NotNull + public String getDescription() { + return "An example cross-platform command"; + } + @Override public void provide(@NotNull BaseCommand command) { command.setCondition(source -> true); diff --git a/example-plugin/src/main/java/net/william278/uniform/UniformExample.java b/example-plugin/src/main/java/net/william278/uniform/UniformExample.java index 5dcdc30..7d80e14 100644 --- a/example-plugin/src/main/java/net/william278/uniform/UniformExample.java +++ b/example-plugin/src/main/java/net/william278/uniform/UniformExample.java @@ -28,7 +28,7 @@ import org.bukkit.plugin.java.JavaPlugin; public class UniformExample extends JavaPlugin { @Override - public void onEnable() { + public void onLoad() { PaperUniform uniform = PaperUniform.getInstance(this); uniform.register(new ExampleCommand()); uniform.register(new ExampleCrossPlatCommand()); diff --git a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java index 610d586..2f6191a 100644 --- a/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java +++ b/fabric-1.20.1/src/main/java/net/william278/uniform/fabric/FabricCommand.java @@ -35,6 +35,8 @@ import net.william278.uniform.CommandUser; import net.william278.uniform.element.ArgumentElement; import org.jetbrains.annotations.NotNull; +import java.util.List; + @SuppressWarnings("unused") public class FabricCommand extends BaseCommand { @@ -42,10 +44,14 @@ public class FabricCommand extends BaseCommand { super(command); } - public FabricCommand(@NotNull String name, @NotNull String... aliases) { + public FabricCommand(@NotNull String name, @NotNull List aliases) { super(name, aliases); } + public FabricCommand(@NotNull String name, @NotNull String description, @NotNull List aliases) { + super(name, description, aliases); + } + protected static ArgumentElement itemArg(String name) { return registryArg(name, Registries.ITEM); } diff --git a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java index cfcd91e..277c728 100644 --- a/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java +++ b/fabric-1.20.6/src/main/java/net/william278/uniform/fabric/FabricCommand.java @@ -35,15 +35,21 @@ import net.william278.uniform.CommandUser; import net.william278.uniform.element.ArgumentElement; import org.jetbrains.annotations.NotNull; +import java.util.List; + @SuppressWarnings("unused") public class FabricCommand extends BaseCommand { - public FabricCommand(@NotNull String name, @NotNull String... aliases) { + public FabricCommand(@NotNull Command command) { + super(command); + } + + public FabricCommand(@NotNull String name, @NotNull List aliases) { super(name, aliases); } - public FabricCommand(@NotNull Command command) { - super(command); + public FabricCommand(@NotNull String name, @NotNull String description, @NotNull List aliases) { + super(name, description, aliases); } protected static ArgumentElement itemArg(String name) { diff --git a/paper/build.gradle b/paper/build.gradle index bbeca3d..0d1ced1 100644 --- a/paper/build.gradle +++ b/paper/build.gradle @@ -6,8 +6,7 @@ plugins { dependencies { api project(path: ':common') - compileOnlyApi 'io.papermc.paper:paper-mojangapi:1.20.4-R0.1-SNAPSHOT' - compileOnlyApi 'io.papermc.paper:paper-api:1.20.4-R0.1-SNAPSHOT' + compileOnlyApi 'io.papermc.paper:paper-api:1.20.6-R0.1-SNAPSHOT' compileOnly 'org.projectlombok:lombok:1.18.32' annotationProcessor 'org.projectlombok:lombok:1.18.32' 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 45d714a..f0ccaff 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java @@ -21,8 +21,8 @@ package net.william278.uniform.paper; -import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import io.papermc.paper.command.brigadier.CommandSourceStack; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; import net.william278.uniform.CommandUser; @@ -35,18 +35,22 @@ import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; -@SuppressWarnings("unused") -public class PaperCommand extends BaseCommand { +@SuppressWarnings({"unused", "UnstableApiUsage"}) +public class PaperCommand extends BaseCommand { public PaperCommand(@NotNull Command command) { super(command); } - public PaperCommand(@NotNull String name, @NotNull String... aliases) { + public PaperCommand(@NotNull String name, @NotNull List aliases) { super(name, aliases); } - protected static ArgumentElement materialArg(String name) { + public PaperCommand(@NotNull String name, @NotNull String description, @NotNull List aliases) { + super(name, description, aliases); + } + + protected static ArgumentElement materialArg(String name) { return new ArgumentElement<>(name, reader -> { String materialName = reader.readString(); Material material = Material.matchMaterial(materialName); @@ -62,7 +66,7 @@ public class PaperCommand extends BaseCommand { }); } - protected static ArgumentElement> playerArg(String name) { + protected static ArgumentElement> playerArg(String name) { return new ArgumentElement<>(name, reader -> { String playerName = reader.readString(); if (playerName.equals("@a")) { @@ -84,7 +88,7 @@ public class PaperCommand extends BaseCommand { @Override @NotNull - protected CommandUser getUser(@NotNull BukkitBrigadierCommandSource user) { + protected CommandUser getUser(@NotNull CommandSourceStack user) { return new PaperCommandUser(user); } 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 d9a418b..462de50 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperCommandUser.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperCommandUser.java @@ -21,7 +21,7 @@ package net.william278.uniform.paper; -import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; +import io.papermc.paper.command.brigadier.CommandSourceStack; import net.kyori.adventure.audience.Audience; import net.william278.uniform.CommandUser; import org.jetbrains.annotations.NotNull; @@ -29,23 +29,25 @@ import org.jetbrains.annotations.Nullable; import java.util.UUID; -public record PaperCommandUser(@NotNull BukkitBrigadierCommandSource source) implements CommandUser { +@SuppressWarnings("UnstableApiUsage") +public record PaperCommandUser(@NotNull CommandSourceStack source) implements CommandUser { @Override - public @NotNull Audience getAudience() { - return source.getBukkitSender(); + @NotNull + public Audience getAudience() { + return source.getSender(); } @Override @Nullable public String getName() { - return source.getBukkitEntity() != null ? source.getBukkitSender().getName() : null; + return source.getExecutor() != null ? source.getExecutor().getName() : null; } @Override @Nullable public UUID getUuid() { - return source.getBukkitEntity() != null ? source.getBukkitEntity().getUniqueId() : null; + return source.getExecutor() != null ? source.getExecutor().getUniqueId() : null; } } 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 d5fe45e..941e830 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java @@ -21,16 +21,15 @@ package net.william278.uniform.paper; -import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent; import com.google.common.collect.Sets; +import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import net.william278.uniform.Command; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.Collections; +import java.util.List; import java.util.Set; /** @@ -39,23 +38,22 @@ import java.util.Set; * @since 1.0 */ @SuppressWarnings("UnstableApiUsage") -public final class PaperUniform implements Listener { +public final class PaperUniform { private static PaperUniform INSTANCE; private final Set commands = Sets.newHashSet(); private PaperUniform(@NotNull JavaPlugin plugin) { - plugin.getServer().getPluginManager().registerEvents(this, plugin); - } - - @EventHandler - @SuppressWarnings({"rawtypes", "unchecked"}) - private void onCommandRegistered(@NotNull CommandRegisteredEvent event) { - for (PaperCommand command : commands) { - event.getRoot().addChild(command.build()); - } - commands.clear(); + plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, (event) -> { + commands.forEach(command -> event.registrar().register( + plugin.getPluginMeta(), + command.build(), + command.getDescription(), + command.getAliases() + )); + commands.clear(); + }); } /** diff --git a/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java b/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java index 3d50e47..9a6c064 100644 --- a/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java +++ b/velocity/src/main/java/net/william278/uniform/velocity/VelocityCommand.java @@ -34,6 +34,8 @@ import net.william278.uniform.CommandUser; import net.william278.uniform.element.ArgumentElement; import org.jetbrains.annotations.NotNull; +import java.util.List; + @SuppressWarnings("unused") public class VelocityCommand extends BaseCommand { @@ -41,10 +43,14 @@ public class VelocityCommand extends BaseCommand { super(command); } - public VelocityCommand(@NotNull String name, @NotNull String... aliases) { + public VelocityCommand(@NotNull String name, @NotNull List aliases) { super(name, aliases); } + public VelocityCommand(@NotNull String name, @NotNull String description, @NotNull List aliases) { + super(name, description, aliases); + } + protected static ArgumentElement serverArg(ProxyServer server, String name, SuggestionProvider suggestionProvider) { ArgumentType argumentType = reader -> {