From ab64836fc058de0ab6294f1b14f34d7a05f4df91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 17 Jan 2023 09:29:56 +0100 Subject: [PATCH] create the bindings --- .../bukkit/inject/BukkitModule.java | 33 +++++++++++ .../commands/PlotSquaredCommandManager.java | 55 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandManager.java diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java b/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java index 0b6810785..c1fca5b50 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/inject/BukkitModule.java @@ -18,12 +18,16 @@ */ package com.plotsquared.bukkit.inject; +import cloud.commandframework.CommandManager; +import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator; +import cloud.commandframework.paper.PaperCommandManager; import com.google.inject.AbstractModule; import com.google.inject.Provides; import com.google.inject.Singleton; import com.google.inject.assistedinject.FactoryModuleBuilder; import com.plotsquared.bukkit.BukkitPlatform; import com.plotsquared.bukkit.listener.SingleWorldListener; +import com.plotsquared.bukkit.player.BukkitPlayer; import com.plotsquared.bukkit.player.BukkitPlayerManager; import com.plotsquared.bukkit.queue.BukkitChunkCoordinator; import com.plotsquared.bukkit.queue.BukkitQueueCoordinator; @@ -47,6 +51,8 @@ import com.plotsquared.core.inject.factory.ChunkCoordinatorBuilderFactory; import com.plotsquared.core.inject.factory.ChunkCoordinatorFactory; import com.plotsquared.core.inject.factory.HybridPlotWorldFactory; import com.plotsquared.core.inject.factory.ProgressSubscriberFactory; +import com.plotsquared.core.player.ConsolePlayer; +import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.world.DefaultPlotAreaManager; import com.plotsquared.core.plot.world.PlotAreaManager; import com.plotsquared.core.plot.world.SinglePlotAreaManager; @@ -68,10 +74,14 @@ import com.sk89q.worldedit.extension.platform.Actor; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import org.checkerframework.checker.nullness.qual.NonNull; +import java.util.function.Function; + public class BukkitModule extends AbstractModule { private static final Logger LOGGER = LogManager.getLogger("PlotSquared/" + BukkitModule.class.getSimpleName()); @@ -145,4 +155,27 @@ public class BukkitModule extends AbstractModule { return EconHandler.nullEconHandler(); } + @Provides + @Singleton + @NonNull CommandManager> provideCommandManager() throws Exception { + final Function, CommandSender> plotToPlatform = plotPlayer -> { + if (plotPlayer instanceof BukkitPlayer bukkitPlayer) { + return bukkitPlayer.getPlatformPlayer(); + } + return Bukkit.getConsoleSender(); + }; + final Function> platformToPlot = commandSender -> { + if (commandSender instanceof Player player) { + return BukkitUtil.adapt(player); + } + return ConsolePlayer.getConsole(); + }; + + return new PaperCommandManager<>( + this.bukkitPlatform, + AsynchronousCommandExecutionCoordinator.>builder().withSynchronousParsing().build(), + platformToPlot, + plotToPlatform + ); + } } diff --git a/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandManager.java b/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandManager.java new file mode 100644 index 000000000..c7d877641 --- /dev/null +++ b/Core/src/main/java/com/plotsquared/core/commands/PlotSquaredCommandManager.java @@ -0,0 +1,55 @@ +/* + * PlotSquared, a land and world management plugin for Minecraft. + * Copyright (C) IntellectualSites + * Copyright (C) IntellectualSites team and 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 com.plotsquared.core.commands; + +import cloud.commandframework.CommandManager; +import cloud.commandframework.annotations.AnnotationParser; +import cloud.commandframework.meta.SimpleCommandMeta; +import com.google.inject.Inject; +import com.plotsquared.core.player.PlotPlayer; +import io.leangen.geantyref.TypeToken; +import org.checkerframework.checker.nullness.qual.NonNull; + +public class PlotSquaredCommandManager { + + private final CommandManager> commandManager; + private final AnnotationParser> annotationParser; + + @Inject + public PlotSquaredCommandManager( + final @NonNull CommandManager> commandManager + ) { + this.commandManager = commandManager; + this.annotationParser = new AnnotationParser>( + this.commandManager, + new TypeToken>() { + }, + parameters -> SimpleCommandMeta.empty() + ); + } + + /** + * Scans the given {@link Class class} for commands, and registers them. + * + * @param clazz the class to scan. + */ + public void scanClass(final @NonNull Class clazz) { + this.annotationParser.parse(clazz); + } +}