From b59b376eb6648c2efafdee0ede9089a72747bd04 Mon Sep 17 00:00:00 2001 From: William Date: Sat, 15 Jun 2024 22:48:36 +0100 Subject: [PATCH] feat: support 1.17.1+ brigadier on Paper --- README.md | 2 +- .../java/net/william278/uniform/Graph.java | 1 - example-plugin/build.gradle | 3 +- .../william278/uniform/UniformExample.java | 6 +- .../src/main/resources/paper-plugin.yml | 2 +- .../uniform/paper/LegacyPaperCommand.java | 86 +++++++++++++++++++ .../uniform/paper/LegacyPaperCommandUser.java | 53 ++++++++++++ .../uniform/paper/PaperCommand.java | 15 ++++ .../uniform/paper/PaperUniform.java | 52 +++++++---- 9 files changed, 194 insertions(+), 26 deletions(-) create mode 100644 paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java create mode 100644 paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommandUser.java diff --git a/README.md b/README.md index 3ba204d..03bf05e 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ Uniform _currently_ targets the following platforms: | Platform | Artifact | Minecraft | Java | |---------------|--------------------|:----------:|:------:| | Common | `uniform-common` | - | \>`17` | -| Paper | `uniform-paper` | \>`1.20.6` | \>`21` | +| Paper | `uniform-paper` | \>`1.17.1` | \>`21` | | Velocity | `uniform-velocity` | \>`3.3.0` | \>`17` | | Fabric 1.20.1 | `uniform-fabric` | =`1.20.1` | \>`17` | | Fabric 1.20.6 | `uniform-fabric` | =`1.20.6` | \>`21` | diff --git a/common/src/main/java/net/william278/uniform/Graph.java b/common/src/main/java/net/william278/uniform/Graph.java index 8a01e08..599ce0a 100644 --- a/common/src/main/java/net/william278/uniform/Graph.java +++ b/common/src/main/java/net/william278/uniform/Graph.java @@ -21,7 +21,6 @@ package net.william278.uniform; -import com.mojang.brigadier.Command; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.tree.CommandNode; import com.mojang.brigadier.tree.LiteralCommandNode; diff --git a/example-plugin/build.gradle b/example-plugin/build.gradle index 3ab567c..3f32d2e 100644 --- a/example-plugin/build.gradle +++ b/example-plugin/build.gradle @@ -4,8 +4,7 @@ plugins { } dependencies { - // implementation(project(":paper")) - implementation(project(":bukkit")) + implementation(project(":paper")) compileOnly 'io.papermc.paper:paper-api:1.20.6-R0.1-SNAPSHOT' } 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 2f41020..269a43d 100644 --- a/example-plugin/src/main/java/net/william278/uniform/UniformExample.java +++ b/example-plugin/src/main/java/net/william278/uniform/UniformExample.java @@ -21,15 +21,15 @@ package net.william278.uniform; -import net.william278.uniform.bukkit.BukkitUniform; +import net.william278.uniform.paper.PaperUniform; import org.bukkit.plugin.java.JavaPlugin; @SuppressWarnings("unused") public class UniformExample extends JavaPlugin { @Override - public void onLoad() { - BukkitUniform uniform = BukkitUniform.getInstance(this); + public void onEnable() { + PaperUniform uniform = PaperUniform.getInstance(this); uniform.register(new ExampleCommand()); } diff --git a/example-plugin/src/main/resources/paper-plugin.yml b/example-plugin/src/main/resources/paper-plugin.yml index c4956ec..bf8e80c 100644 --- a/example-plugin/src/main/resources/paper-plugin.yml +++ b/example-plugin/src/main/resources/paper-plugin.yml @@ -1,4 +1,4 @@ name: UniformExample version: 1.0 -api-version: "1.20" +api-version: "1.19" main: net.william278.uniform.UniformExample \ No newline at end of file diff --git a/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java b/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java new file mode 100644 index 0000000..42910fc --- /dev/null +++ b/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommand.java @@ -0,0 +1,86 @@ +/* + * 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.paper; + +import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; +import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.tree.LiteralCommandNode; +import lombok.AllArgsConstructor; +import net.william278.uniform.BaseCommand; +import net.william278.uniform.Command; +import net.william278.uniform.CommandUser; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.Set; + +@SuppressWarnings({"removal", "deprecation", "UnstableApiUsage"}) +public class LegacyPaperCommand extends BaseCommand { + + public LegacyPaperCommand(@NotNull Command command) { + super(command); + } + + public LegacyPaperCommand(@NotNull String name, @NotNull List aliases) { + super(name, aliases); + } + + public LegacyPaperCommand(@NotNull String name, @NotNull String description, @NotNull List aliases) { + super(name, description, aliases); + } + + @Override + @NotNull + protected CommandUser getUser(@NotNull Object user) { + return new LegacyPaperCommandUser((BukkitBrigadierCommandSource) user); + } + + @Override + public void addSubCommand(@NotNull Command command) { + addSubCommand(new LegacyPaperCommand(command)); + } + + @AllArgsConstructor + static class Registrar implements Listener { + @NotNull + private final JavaPlugin plugin; + @NotNull + private final Set commands; + + @EventHandler + public void commandRegisterEvent(CommandRegisteredEvent event) { + commands.forEach(command -> { + final LiteralCommandNode built = command.build(); + event.getRoot().addChild(built); + command.getAliases().forEach(alias -> event.getRoot().addChild( + LiteralArgumentBuilder.literal(alias).redirect(built).build() + )); + }); + commands.clear(); + } + } + +} diff --git a/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommandUser.java b/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommandUser.java new file mode 100644 index 0000000..d947895 --- /dev/null +++ b/paper/src/main/java/net/william278/uniform/paper/LegacyPaperCommandUser.java @@ -0,0 +1,53 @@ +/* + * 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.paper; + +import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; +import net.kyori.adventure.audience.Audience; +import net.william278.uniform.CommandUser; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.UUID; + +@SuppressWarnings("removal") +public record LegacyPaperCommandUser(@NotNull BukkitBrigadierCommandSource source) implements CommandUser { + + @Override + @NotNull + public Audience getAudience() { + return source.getBukkitSender(); + } + + @Override + @Nullable + public String getName() { + return source.getBukkitEntity() != null ? source.getBukkitEntity().getName() : null; + } + + @Override + @Nullable + public UUID getUuid() { + return source.getBukkitEntity() != null ? source.getBukkitEntity().getUniqueId() : null; + } + +} 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 dc09e89..8d86bbd 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperCommand.java @@ -23,6 +23,7 @@ package net.william278.uniform.paper; import com.mojang.brigadier.exceptions.CommandSyntaxException; import io.papermc.paper.command.brigadier.CommandSourceStack; +import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import net.william278.uniform.BaseCommand; import net.william278.uniform.Command; import net.william278.uniform.CommandUser; @@ -30,10 +31,12 @@ import net.william278.uniform.element.ArgumentElement; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; +import java.util.Set; @SuppressWarnings({"unused", "UnstableApiUsage"}) public class PaperCommand extends BaseCommand { @@ -50,6 +53,18 @@ public class PaperCommand extends BaseCommand { super(name, description, aliases); } + static void register(@NotNull JavaPlugin plugin, @NotNull Set commands) { + plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, (event) -> { + commands.forEach(command -> event.registrar().register( + plugin.getPluginMeta(), + command.build(), + command.getDescription(), + command.getAliases() + )); + commands.clear(); + }); + } + public static ArgumentElement material(String name) { return new ArgumentElement<>(name, reader -> { String materialName = reader.readString(); 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 78b0170..e494fcc 100644 --- a/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java +++ b/paper/src/main/java/net/william278/uniform/paper/PaperUniform.java @@ -22,10 +22,7 @@ package net.william278.uniform.paper; import com.google.common.collect.Sets; -import io.papermc.paper.command.brigadier.CommandSourceStack; -import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents; import net.william278.uniform.Command; -import net.william278.uniform.Uniform; import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; @@ -38,23 +35,30 @@ import java.util.Set; * * @since 1.0 */ -@SuppressWarnings("UnstableApiUsage") -public final class PaperUniform implements Uniform { +public final class PaperUniform { private static PaperUniform INSTANCE; + private final Set legacyCommands = Sets.newHashSet(); private final Set commands = Sets.newHashSet(); private PaperUniform(@NotNull JavaPlugin plugin) { - plugin.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, (event) -> { - commands.forEach(command -> event.registrar().register( - plugin.getPluginMeta(), - command.build(), - command.getDescription(), - command.getAliases() - )); - commands.clear(); - }); + if (isUseModernApi()) { + PaperCommand.register(plugin, commands); + } else { + plugin.getServer().getPluginManager().registerEvents( + new LegacyPaperCommand.Registrar(plugin, legacyCommands), plugin + ); + } + } + + private static boolean isUseModernApi() { + try { + Class.forName("io.papermc.paper.command.brigadier.CommandSourceStack"); + return true; + } catch (ClassNotFoundException e) { + return false; + } } /** @@ -75,20 +79,32 @@ public final class PaperUniform implements Uniform