Skip to content

Commit

Permalink
cloud sender mapper improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankHeijden committed Jun 23, 2024
1 parent 5167fa9 commit 269aa5d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 32 deletions.
4 changes: 1 addition & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# Changelog
- Added support for 1.21
- Dropped support for 1.20.X
- Bump api version in plugin.yml
- Move
33 changes: 5 additions & 28 deletions Insights/src/main/java/dev/frankheijden/insights/Insights.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,22 @@
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;
import dev.frankheijden.insights.placeholders.InsightsPlaceholderExpansion;
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;
Expand Down Expand Up @@ -255,12 +251,12 @@ public void reloadLimits() {
}

private void loadCommands() {
PaperCommandManager<CommandSourceStack> commandManager = PaperCommandManager.builder()
var commandManager = PaperCommandManager.builder(new CommandSenderMapper())
.executionCoordinator(ExecutionCoordinator.asyncCoordinator())
.buildOnEnable(this);

// Register parsers
ParserRegistry<CommandSourceStack> parserRegistry = commandManager.parserRegistry();
var parserRegistry = commandManager.parserRegistry();
parserRegistry.registerParserSupplier(
TypeToken.get(new TypeToken<Limit>() {
}.getType()),
Expand All @@ -284,35 +280,16 @@ private void loadCommands() {

if (commandManager.hasBrigadierManager()) {
commandManager.brigadierManager().registerMapping(
new TypeToken<ScanObjectArrayParser<CommandSourceStack>>() {},
new TypeToken<ScanObjectArrayParser<CommandSender>>() {},
builder -> {
builder.to(argument -> StringArgumentType.greedyString());
builder.cloudSuggestions();
}
);
}

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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CommandSourceStack, CommandSender> {
@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;
}
};
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 269aa5d

Please sign in to comment.