From 269aa5dad00bf4a2243010716f2252cc4dd2d14c Mon Sep 17 00:00:00 2001 From: Frank van der Heijden Date: Sun, 23 Jun 2024 16:15:04 +0200 Subject: [PATCH] cloud sender mapper improvement --- CHANGELOG.md | 4 +- .../dev/frankheijden/insights/Insights.java | 33 +++------------ .../commands/util/CommandSenderMapper.java | 42 +++++++++++++++++++ build.gradle.kts | 2 +- 4 files changed, 49 insertions(+), 32 deletions(-) create mode 100644 Insights/src/main/java/dev/frankheijden/insights/commands/util/CommandSenderMapper.java diff --git a/CHANGELOG.md b/CHANGELOG.md index b736ee4..5873850 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,2 @@ # Changelog -- Added support for 1.21 -- Dropped support for 1.20.X -- Bump api version in plugin.yml +- Move \ No newline at end of file diff --git a/Insights/src/main/java/dev/frankheijden/insights/Insights.java b/Insights/src/main/java/dev/frankheijden/insights/Insights.java index 261ca67..5cd57c0 100644 --- a/Insights/src/main/java/dev/frankheijden/insights/Insights.java +++ b/Insights/src/main/java/dev/frankheijden/insights/Insights.java @@ -35,6 +35,7 @@ import dev.frankheijden.insights.commands.parser.ScanHistoryPageParser; import dev.frankheijden.insights.commands.parser.ScanObjectArrayParser; import dev.frankheijden.insights.commands.parser.WorldParser; +import dev.frankheijden.insights.commands.util.CommandSenderMapper; import dev.frankheijden.insights.concurrent.ContainerExecutorService; import dev.frankheijden.insights.listeners.manager.ListenerManager; import dev.frankheijden.insights.nms.core.InsightsNMS; @@ -42,19 +43,14 @@ import dev.frankheijden.insights.tasks.PlayerTrackerTask; import io.leangen.geantyref.TypeToken; import io.papermc.lib.PaperLib; -import io.papermc.paper.command.brigadier.CommandSourceStack; import net.kyori.adventure.platform.bukkit.BukkitAudiences; -import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitTask; import org.incendo.cloud.annotations.AnnotationParser; import org.incendo.cloud.execution.ExecutionCoordinator; import org.incendo.cloud.paper.PaperCommandManager; -import org.incendo.cloud.parser.ParserRegistry; import java.io.File; import java.io.IOException; import java.nio.file.DirectoryStream; @@ -255,12 +251,12 @@ public void reloadLimits() { } private void loadCommands() { - PaperCommandManager commandManager = PaperCommandManager.builder() + var commandManager = PaperCommandManager.builder(new CommandSenderMapper()) .executionCoordinator(ExecutionCoordinator.asyncCoordinator()) .buildOnEnable(this); // Register parsers - ParserRegistry parserRegistry = commandManager.parserRegistry(); + var parserRegistry = commandManager.parserRegistry(); parserRegistry.registerParserSupplier( TypeToken.get(new TypeToken() { }.getType()), @@ -284,7 +280,7 @@ private void loadCommands() { if (commandManager.hasBrigadierManager()) { commandManager.brigadierManager().registerMapping( - new TypeToken>() {}, + new TypeToken>() {}, builder -> { builder.to(argument -> StringArgumentType.greedyString()); builder.cloudSuggestions(); @@ -292,27 +288,8 @@ private void loadCommands() { ); } - commandManager.parameterInjectorRegistry().registerInjector( - CommandSender.class, - (context, annotationAccessor) -> context.sender().getSender() - ); - commandManager.parameterInjectorRegistry().registerInjector( - Player.class, - (context, annotationAccessor) -> { - var sender = context.sender().getSender(); - if (sender instanceof Player player) { - return player; - } - - audiences.sender(sender).sendMessage( - Component.text("This command can only be sent as a player", NamedTextColor.RED) - ); - return null; - } - ); - // Create Annotation Parser - var annotationParser = new AnnotationParser(commandManager, CommandSourceStack.class); + var annotationParser = new AnnotationParser(commandManager, CommandSender.class); // Parse commands annotationParser.parse(new CommandInsights(this)); diff --git a/Insights/src/main/java/dev/frankheijden/insights/commands/util/CommandSenderMapper.java b/Insights/src/main/java/dev/frankheijden/insights/commands/util/CommandSenderMapper.java new file mode 100644 index 0000000..ff78c23 --- /dev/null +++ b/Insights/src/main/java/dev/frankheijden/insights/commands/util/CommandSenderMapper.java @@ -0,0 +1,42 @@ +package dev.frankheijden.insights.commands.util; + +import io.papermc.paper.command.brigadier.CommandSourceStack; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Entity; +import org.incendo.cloud.SenderMapper; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class CommandSenderMapper implements SenderMapper { + @Override + public CommandSender map(CommandSourceStack source) { + return source.getSender(); + } + + @Override + public CommandSourceStack reverse(CommandSender sender) { + return new CommandSourceStack() { + @Override + public @NotNull Location getLocation() { + if (sender instanceof Entity entity) { + return entity.getLocation(); + } + + var worlds = Bukkit.getWorlds(); + return new Location(worlds.isEmpty() ? null : worlds.getFirst(), 0, 0, 0); // Best effort lol + } + + @Override + public @NotNull CommandSender getSender() { + return sender; + } + + @Override + public @Nullable Entity getExecutor() { + return sender instanceof Entity entity ? entity : null; + } + }; + } +} diff --git a/build.gradle.kts b/build.gradle.kts index fa8e004..9c0aba6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -12,7 +12,7 @@ plugins { val name = "Insights" group = "dev.frankheijden.insights" val dependencyDir = "$group.dependencies" -version = "6.19.1" +version = "6.19.2-SNAPSHOT" subprojects { apply(plugin = "java")