From 8af375f4b718a4682de6e06f17e6909225ab56ae Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Sun, 7 Aug 2022 13:49:16 -0700 Subject: [PATCH] Update to 1.19.2 --- build.gradle.kts | 10 ++-- .../testplugin/PluginBrigadierCommand.java | 51 +++++++++++++++++++ .../paperweight/testplugin/TestPlugin.java | 44 ++++++++++++++-- 3 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 src/main/java/io/papermc/paperweight/testplugin/PluginBrigadierCommand.java diff --git a/build.gradle.kts b/build.gradle.kts index 510e144..89cbbab 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -2,7 +2,7 @@ import net.minecrell.pluginyml.bukkit.BukkitPluginDescription plugins { `java-library` - id("io.papermc.paperweight.userdev") version "1.3.7" + id("io.papermc.paperweight.userdev") version "1.3.8" id("xyz.jpenilla.run-paper") version "1.0.6" // Adds runServer and runMojangMappedServer tasks for testing id("net.minecrell.plugin-yml.bukkit") version "0.5.2" // Generates plugin.yml } @@ -17,12 +17,12 @@ java { } dependencies { - paperDevBundle("1.19-R0.1-SNAPSHOT") - // paperweightDevBundle("com.example.paperfork", "1.19-R0.1-SNAPSHOT") + paperDevBundle("1.19.2-R0.1-SNAPSHOT") + // paperweightDevBundle("com.example.paperfork", "1.19.2-R0.1-SNAPSHOT") // You will need to manually specify the full dependency if using the groovy gradle dsl // (paperDevBundle and paperweightDevBundle functions do not work in groovy) - // paperweightDevelopmentBundle("io.papermc.paper:dev-bundle:1.19-R0.1-SNAPSHOT") + // paperweightDevelopmentBundle("io.papermc.paper:dev-bundle:1.19.2-R0.1-SNAPSHOT") } tasks { @@ -58,6 +58,6 @@ tasks { bukkit { load = BukkitPluginDescription.PluginLoadOrder.STARTUP main = "io.papermc.paperweight.testplugin.TestPlugin" - apiVersion = "1.18" + apiVersion = "1.19" authors = listOf("Author") } diff --git a/src/main/java/io/papermc/paperweight/testplugin/PluginBrigadierCommand.java b/src/main/java/io/papermc/paperweight/testplugin/PluginBrigadierCommand.java new file mode 100644 index 0000000..6bd381a --- /dev/null +++ b/src/main/java/io/papermc/paperweight/testplugin/PluginBrigadierCommand.java @@ -0,0 +1,51 @@ +package io.papermc.paperweight.testplugin; + +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import java.util.function.Consumer; +import net.minecraft.commands.CommandSourceStack; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginIdentifiableCommand; +import org.bukkit.craftbukkit.v1_19_R1.CraftServer; +import org.bukkit.craftbukkit.v1_19_R1.command.VanillaCommandWrapper; +import org.bukkit.plugin.Plugin; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.framework.qual.DefaultQualifier; + +@DefaultQualifier(NonNull.class) +final class PluginBrigadierCommand extends Command implements PluginIdentifiableCommand { + private final Consumer> command; + private final Plugin plugin; + + PluginBrigadierCommand( + final Plugin plugin, + final String name, + final Consumer> command + ) { + super(name); + this.plugin = plugin; + this.command = command; + } + + @Override + public boolean execute(final CommandSender sender, final String commandLabel, final String[] args) { + final String joined = String.join(" ", args); + final String argsString = joined.isBlank() ? "" : " " + joined; + ((CraftServer) Bukkit.getServer()).getServer().getCommands().performPrefixedCommand( + VanillaCommandWrapper.getListener(sender), + commandLabel + argsString, + commandLabel + ); + return true; + } + + @Override + public Plugin getPlugin() { + return this.plugin; + } + + Consumer> command() { + return this.command; + } +} diff --git a/src/main/java/io/papermc/paperweight/testplugin/TestPlugin.java b/src/main/java/io/papermc/paperweight/testplugin/TestPlugin.java index 676461a..a65fbb1 100644 --- a/src/main/java/io/papermc/paperweight/testplugin/TestPlugin.java +++ b/src/main/java/io/papermc/paperweight/testplugin/TestPlugin.java @@ -1,27 +1,45 @@ package io.papermc.paperweight.testplugin; +import com.destroystokyo.paper.brigadier.BukkitBrigadierCommandSource; +import com.destroystokyo.paper.event.brigadier.CommandRegisteredEvent; +import com.mojang.brigadier.Command; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.tree.LiteralCommandNode; import java.util.Collection; +import java.util.function.Consumer; import net.minecraft.ChatFormatting; -import net.minecraft.Util; +import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.arguments.EntityArgument; import net.minecraft.network.chat.ClickEvent; import net.minecraft.network.chat.Component; import net.minecraft.server.level.ServerPlayer; import org.bukkit.craftbukkit.v1_19_R1.CraftServer; +import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.framework.qual.DefaultQualifier; +import static net.kyori.adventure.text.Component.text; +import static net.kyori.adventure.text.format.NamedTextColor.BLUE; import static net.minecraft.commands.Commands.argument; import static net.minecraft.commands.Commands.literal; import static net.minecraft.commands.arguments.EntityArgument.players; +@DefaultQualifier(NonNull.class) public final class TestPlugin extends JavaPlugin implements Listener { @Override public void onEnable() { this.getServer().getPluginManager().registerEvents(this, this); - ((CraftServer) this.getServer()).getServer().vanillaCommandDispatcher.getDispatcher().register( - literal("paperweight") - .requires(stack -> stack.hasPermission(stack.getServer().getOperatorUserPermissionLevel())) + + this.registerPluginBrigadierCommand( + "paperweight", + literal -> literal.requires(stack -> stack.getBukkitSender().hasPermission("paperweight")) + .then(literal("hello") + .executes(ctx -> { + ctx.getSource().getBukkitSender().sendMessage(text("Hello!", BLUE)); + return Command.SINGLE_SUCCESS; + })) .then(argument("players", players()) .executes(ctx -> { final Collection players = EntityArgument.getPlayers(ctx, "players"); @@ -36,4 +54,22 @@ public final class TestPlugin extends JavaPlugin implements Listener { })) ); } + + private PluginBrigadierCommand registerPluginBrigadierCommand(final String label, final Consumer> command) { + final PluginBrigadierCommand pluginBrigadierCommand = new PluginBrigadierCommand(this, label, command); + this.getServer().getCommandMap().register(this.getName(), pluginBrigadierCommand); + ((CraftServer) this.getServer()).syncCommands(); + return pluginBrigadierCommand; + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + @EventHandler + public void onCommandRegistered(final CommandRegisteredEvent event) { + if (!(event.getCommand() instanceof PluginBrigadierCommand pluginBrigadierCommand)) { + return; + } + final LiteralArgumentBuilder node = literal(event.getCommandLabel()); + pluginBrigadierCommand.command().accept(node); + event.setLiteral((LiteralCommandNode) node.build()); + } }