From b82bcb90a1b31d2a089d9166cc7f9cc4ec179ecd Mon Sep 17 00:00:00 2001 From: Rollczi Date: Mon, 22 Jan 2024 00:37:07 +0100 Subject: [PATCH] Support kotlin builder chain. --- .../bukkit/LiteBukkitFactory.java | 77 +++++++++++++++++ .../bukkit/LiteCommandsBukkit.java | 82 +++++++------------ .../bungee/LiteBungeeFactory.java | 7 +- .../litecommands/jda/LiteJDAFactory.java | 5 +- .../minestom/LiteMinestomFactory.java | 7 +- .../velocity/LiteVelocityFactory.java | 5 +- 6 files changed, 121 insertions(+), 62 deletions(-) create mode 100644 litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteBukkitFactory.java diff --git a/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteBukkitFactory.java b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteBukkitFactory.java new file mode 100644 index 000000000..c54ff1681 --- /dev/null +++ b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteBukkitFactory.java @@ -0,0 +1,77 @@ +package dev.rollczi.litecommands.bukkit; + +import dev.rollczi.litecommands.LiteCommandsBuilder; +import dev.rollczi.litecommands.LiteCommandsFactory; +import dev.rollczi.litecommands.bukkit.argument.LocationArgument; +import dev.rollczi.litecommands.bukkit.argument.PlayerArgument; +import dev.rollczi.litecommands.bukkit.argument.WorldArgument; +import dev.rollczi.litecommands.bukkit.context.LocationContext; +import dev.rollczi.litecommands.bukkit.context.PlayerOnlyContextProvider; +import dev.rollczi.litecommands.bukkit.context.WorldContext; +import dev.rollczi.litecommands.message.MessageRegistry; +import java.util.Locale; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitScheduler; + +public final class LiteBukkitFactory { + + private LiteBukkitFactory() { + } + + public static > B builder() { + JavaPlugin plugin = JavaPlugin.getProvidingPlugin(LiteBukkitFactory.class); + String name = plugin.getDescription().getName() + .toLowerCase(Locale.ROOT) + .replace(" ", "-"); + + return builder(name, plugin, plugin.getServer()); + } + + public static > B builder(String fallbackPrefix) { + return builder(fallbackPrefix, JavaPlugin.getProvidingPlugin(LiteBukkitFactory.class), Bukkit.getServer()); + } + + public static > B builder(String fallbackPrefix, Plugin plugin) { + return builder(fallbackPrefix, plugin, Bukkit.getServer()); + } + + public static > B builder(String fallbackPrefix, Plugin plugin, Server server) { + LiteBukkitSettings settings = new LiteBukkitSettings(server) + .fallbackPrefix(fallbackPrefix); + + return builder(plugin, server, settings); + } + + @SuppressWarnings("unchecked") + public static > B builder(Plugin plugin, Server server, LiteBukkitSettings settings) { + return (B) LiteCommandsFactory.builder(CommandSender.class, new BukkitPlatform(settings)).selfProcessor((builder, internal) -> { + MessageRegistry messageRegistry = internal.getMessageRegistry(); + + builder + .bind(Server.class, () -> server) + .bind(BukkitScheduler.class, () -> server.getScheduler()) + + .scheduler(new BukkitSchedulerImpl(server.getScheduler(), plugin)) + + .settings(bukkitSettings -> bukkitSettings.tabCompleter(TabComplete.create(internal.getScheduler(), plugin))) + + .argument(Player.class, new PlayerArgument(server, messageRegistry)) + .argument(World.class, new WorldArgument(server, messageRegistry)) + .argument(Location.class, new LocationArgument(messageRegistry)) + + .context(Player.class, new PlayerOnlyContextProvider(messageRegistry)) + .context(World.class, new WorldContext(messageRegistry)) + .context(Location.class, new LocationContext(messageRegistry)) + + .result(String.class, new StringHandler()); + }); + } + +} diff --git a/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteCommandsBukkit.java b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteCommandsBukkit.java index 6eeca4b09..09005495b 100644 --- a/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteCommandsBukkit.java +++ b/litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/LiteCommandsBukkit.java @@ -1,75 +1,53 @@ package dev.rollczi.litecommands.bukkit; -import dev.rollczi.litecommands.LiteCommandsFactory; import dev.rollczi.litecommands.LiteCommandsBuilder; -import dev.rollczi.litecommands.bukkit.argument.LocationArgument; -import dev.rollczi.litecommands.bukkit.context.LocationContext; -import dev.rollczi.litecommands.bukkit.argument.WorldArgument; -import dev.rollczi.litecommands.bukkit.context.WorldContext; -import dev.rollczi.litecommands.bukkit.context.PlayerOnlyContextProvider; -import dev.rollczi.litecommands.bukkit.argument.PlayerArgument; -import dev.rollczi.litecommands.message.MessageRegistry; -import dev.rollczi.litecommands.util.StringUtil; -import java.util.Locale; -import org.bukkit.Bukkit; -import org.bukkit.Location; import org.bukkit.Server; -import org.bukkit.World; import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.scheduler.BukkitScheduler; public final class LiteCommandsBukkit { private LiteCommandsBukkit() { } - public static LiteCommandsBuilder builder() { - JavaPlugin plugin = JavaPlugin.getProvidingPlugin(LiteCommandsBukkit.class); - String name = plugin.getDescription().getName(); - - return builder(name.toLowerCase(Locale.ROOT).replace(" ", "-"), plugin, plugin.getServer()); + /** + * @deprecated Use {@link LiteBukkitFactory#builder(String)} instead + */ + @Deprecated + public static > B builder() { + return LiteBukkitFactory.builder(); } - public static LiteCommandsBuilder builder(String fallbackPrefix) { - return builder(fallbackPrefix, JavaPlugin.getProvidingPlugin(LiteCommandsBukkit.class), Bukkit.getServer()); + /** + * @deprecated Use {@link LiteBukkitFactory#builder(String)} instead + */ + @Deprecated + public static > B builder(String fallbackPrefix) { + return LiteBukkitFactory.builder(fallbackPrefix); } - public static LiteCommandsBuilder builder(String fallbackPrefix, Plugin plugin) { - return builder(fallbackPrefix, plugin, Bukkit.getServer()); + /** + * @deprecated Use {@link LiteBukkitFactory#builder(String, Plugin)} instead + */ + @Deprecated + public static > B builder(String fallbackPrefix, Plugin plugin) { + return LiteBukkitFactory.builder(fallbackPrefix, plugin); } - public static LiteCommandsBuilder builder(String fallbackPrefix, Plugin plugin, Server server) { - LiteBukkitSettings settings = new LiteBukkitSettings(server) - .fallbackPrefix(fallbackPrefix); - - return builder(plugin, server, settings); + /** + * @deprecated Use {@link LiteBukkitFactory#builder(String, Plugin, Server)} instead + */ + @Deprecated + public static > B builder(String fallbackPrefix, Plugin plugin, Server server) { + return LiteBukkitFactory.builder(fallbackPrefix, plugin, server); } - public static LiteCommandsBuilder builder(Plugin plugin, Server server, LiteBukkitSettings settings) { - return LiteCommandsFactory.builder(CommandSender.class, new BukkitPlatform(settings)).selfProcessor((builder, internal) -> { - MessageRegistry messageRegistry = internal.getMessageRegistry(); - - builder - .bind(Server.class, () -> server) - .bind(BukkitScheduler.class, () -> server.getScheduler()) - - .scheduler(new BukkitSchedulerImpl(server.getScheduler(), plugin)) - - .settings(bukkitSettings -> bukkitSettings.tabCompleter(TabComplete.create(internal.getScheduler(), plugin))) - - .argument(Player.class, new PlayerArgument(server, messageRegistry)) - .argument(World.class, new WorldArgument(server, messageRegistry)) - .argument(Location.class, new LocationArgument(messageRegistry)) - - .context(Player.class, new PlayerOnlyContextProvider(messageRegistry)) - .context(World.class, new WorldContext(messageRegistry)) - .context(Location.class, new LocationContext(messageRegistry)) - - .result(String.class, new StringHandler()); - }); + /** + * @deprecated Use {@link LiteBukkitFactory#builder(Plugin, Server, LiteBukkitSettings)} instead + */ + @Deprecated + public static > B builder(Plugin plugin, Server server, LiteBukkitSettings settings) { + return LiteBukkitFactory.builder(plugin, server, settings); } } diff --git a/litecommands-bungee/src/dev/rollczi/litecommands/bungee/LiteBungeeFactory.java b/litecommands-bungee/src/dev/rollczi/litecommands/bungee/LiteBungeeFactory.java index 80dd577fd..46227eb96 100644 --- a/litecommands-bungee/src/dev/rollczi/litecommands/bungee/LiteBungeeFactory.java +++ b/litecommands-bungee/src/dev/rollczi/litecommands/bungee/LiteBungeeFactory.java @@ -14,12 +14,13 @@ public final class LiteBungeeFactory { private LiteBungeeFactory() { } - public static LiteCommandsBuilder builder(Plugin plugin) { + public static > B builder(Plugin plugin) { return builder(plugin, new LiteBungeeSettings()); } - public static LiteCommandsBuilder builder(Plugin plugin, LiteBungeeSettings liteBungeeSettings) { - return LiteCommandsFactory.builder(CommandSender.class, new BungeePlatform(plugin, liteBungeeSettings)) + @SuppressWarnings("unchecked") + public static > B builder(Plugin plugin, LiteBungeeSettings liteBungeeSettings) { + return (B) LiteCommandsFactory.builder(CommandSender.class, new BungeePlatform(plugin, liteBungeeSettings)) .bind(ProxyServer.class, plugin::getProxy) .context(ProxiedPlayer.class, new BungeeOnlyPlayerContextual<>("Only players can use this command! (Set this message in LiteBungeeFactory)")) diff --git a/litecommands-jda/src/dev/rollczi/litecommands/jda/LiteJDAFactory.java b/litecommands-jda/src/dev/rollczi/litecommands/jda/LiteJDAFactory.java index 7c8bbc9a3..afe6cd184 100644 --- a/litecommands-jda/src/dev/rollczi/litecommands/jda/LiteJDAFactory.java +++ b/litecommands-jda/src/dev/rollczi/litecommands/jda/LiteJDAFactory.java @@ -30,10 +30,11 @@ public final class LiteJDAFactory { private LiteJDAFactory() { } - public static LiteCommandsBuilder builder(JDA jda) { + @SuppressWarnings("unchecked") + public static > B builder(JDA jda) { JDAPlatform platform = new JDAPlatform(new LiteJDASettings(), jda); - return LiteCommandsFactory.builder(User.class, platform).selfProcessor((builder, internal) -> builder + return (B) LiteCommandsFactory.builder(User.class, platform).selfProcessor((builder, internal) -> builder .settings(settings -> settings.translator(createTranslator(internal.getWrapperRegistry()))) .bind(JDA.class, () -> jda) .result(String.class, new StringHandler()) diff --git a/litecommands-minestom/src/dev/rollczi/litecommands/minestom/LiteMinestomFactory.java b/litecommands-minestom/src/dev/rollczi/litecommands/minestom/LiteMinestomFactory.java index 9cb05972b..ba90406a0 100644 --- a/litecommands-minestom/src/dev/rollczi/litecommands/minestom/LiteMinestomFactory.java +++ b/litecommands-minestom/src/dev/rollczi/litecommands/minestom/LiteMinestomFactory.java @@ -22,17 +22,18 @@ public final class LiteMinestomFactory { private LiteMinestomFactory() { } - public static LiteCommandsBuilder builder() { + public static > B builder() { return builder(MinecraftServer.getServer(), MinecraftServer.getInstanceManager(), MinecraftServer.getConnectionManager(), MinecraftServer.getCommandManager()); } - public static LiteCommandsBuilder builder( + @SuppressWarnings("unchecked") + public static > B builder( Server server, InstanceManager instanceManager, ConnectionManager connectionManager, CommandManager commandManager ) { - return LiteCommandsFactory.builder(CommandSender.class, new MinestomPlatform(commandManager)).selfProcessor((builder, internal) -> { + return (B) LiteCommandsFactory.builder(CommandSender.class, new MinestomPlatform(commandManager)).selfProcessor((builder, internal) -> { MessageRegistry messageRegistry = internal.getMessageRegistry(); builder diff --git a/litecommands-velocity/src/dev/rollczi/litecommands/velocity/LiteVelocityFactory.java b/litecommands-velocity/src/dev/rollczi/litecommands/velocity/LiteVelocityFactory.java index 4c68bb65a..27fc7b045 100644 --- a/litecommands-velocity/src/dev/rollczi/litecommands/velocity/LiteVelocityFactory.java +++ b/litecommands-velocity/src/dev/rollczi/litecommands/velocity/LiteVelocityFactory.java @@ -12,8 +12,9 @@ public final class LiteVelocityFactory { private LiteVelocityFactory() { } - public static LiteCommandsBuilder builder(ProxyServer proxy) { - return LiteCommandsFactory.builder(CommandSource.class, new VelocityPlatform(proxy.getCommandManager(), new LiteVelocitySettings())) + @SuppressWarnings("unchecked") + public static > B builder(ProxyServer proxy) { + return (B) LiteCommandsFactory.builder(CommandSource.class, new VelocityPlatform(proxy.getCommandManager(), new LiteVelocitySettings())) .extension(new LiteAdventureExtension<>(), configuration -> configuration .legacyColor(true) )