Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-361 Support kotlin builder chain. #361

Merged
merged 1 commit into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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 extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings, B>> 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 extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings, B>> B builder(String fallbackPrefix) {
return builder(fallbackPrefix, JavaPlugin.getProvidingPlugin(LiteBukkitFactory.class), Bukkit.getServer());
}

public static <B extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings, B>> B builder(String fallbackPrefix, Plugin plugin) {
return builder(fallbackPrefix, plugin, Bukkit.getServer());
}

public static <B extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings, B>> 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 extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings, B>> B builder(Plugin plugin, Server server, LiteBukkitSettings settings) {
return (B) LiteCommandsFactory.builder(CommandSender.class, new BukkitPlatform(settings)).selfProcessor((builder, internal) -> {
MessageRegistry<CommandSender> 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());
});
}

}
Original file line number Diff line number Diff line change
@@ -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<CommandSender, LiteBukkitSettings, ?> 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 extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings, B>> B builder() {
return LiteBukkitFactory.builder();
}

public static LiteCommandsBuilder<CommandSender, LiteBukkitSettings, ?> builder(String fallbackPrefix) {
return builder(fallbackPrefix, JavaPlugin.getProvidingPlugin(LiteCommandsBukkit.class), Bukkit.getServer());
/**
* @deprecated Use {@link LiteBukkitFactory#builder(String)} instead
*/
@Deprecated
public static <B extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings, B>> B builder(String fallbackPrefix) {
return LiteBukkitFactory.builder(fallbackPrefix);
}

public static LiteCommandsBuilder<CommandSender, LiteBukkitSettings, ?> builder(String fallbackPrefix, Plugin plugin) {
return builder(fallbackPrefix, plugin, Bukkit.getServer());
/**
* @deprecated Use {@link LiteBukkitFactory#builder(String, Plugin)} instead
*/
@Deprecated
public static <B extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings, B>> B builder(String fallbackPrefix, Plugin plugin) {
return LiteBukkitFactory.builder(fallbackPrefix, plugin);
}

public static LiteCommandsBuilder<CommandSender, LiteBukkitSettings, ?> 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 extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings, B>> B builder(String fallbackPrefix, Plugin plugin, Server server) {
return LiteBukkitFactory.builder(fallbackPrefix, plugin, server);
}

public static LiteCommandsBuilder<CommandSender, LiteBukkitSettings, ?> builder(Plugin plugin, Server server, LiteBukkitSettings settings) {
return LiteCommandsFactory.builder(CommandSender.class, new BukkitPlatform(settings)).selfProcessor((builder, internal) -> {
MessageRegistry<CommandSender> 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 extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings, B>> B builder(Plugin plugin, Server server, LiteBukkitSettings settings) {
return LiteBukkitFactory.builder(plugin, server, settings);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ public final class LiteBungeeFactory {
private LiteBungeeFactory() {
}

public static LiteCommandsBuilder<CommandSender, LiteBungeeSettings, ?> builder(Plugin plugin) {
public static <B extends LiteCommandsBuilder<CommandSender, LiteBungeeSettings, B>> B builder(Plugin plugin) {
return builder(plugin, new LiteBungeeSettings());
}

public static LiteCommandsBuilder<CommandSender, LiteBungeeSettings, ?> builder(Plugin plugin, LiteBungeeSettings liteBungeeSettings) {
return LiteCommandsFactory.builder(CommandSender.class, new BungeePlatform(plugin, liteBungeeSettings))
@SuppressWarnings("unchecked")
public static <B extends LiteCommandsBuilder<CommandSender, LiteBungeeSettings, B>> 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)"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ public final class LiteJDAFactory {
private LiteJDAFactory() {
}

public static LiteCommandsBuilder<User, LiteJDASettings, ?> builder(JDA jda) {
@SuppressWarnings("unchecked")
public static <B extends LiteCommandsBuilder<User, LiteJDASettings, B>> 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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ public final class LiteMinestomFactory {
private LiteMinestomFactory() {
}

public static LiteCommandsBuilder<CommandSender, LiteMinestomSettings, ?> builder() {
public static <B extends LiteCommandsBuilder<CommandSender, LiteMinestomSettings, B>> B builder() {
return builder(MinecraftServer.getServer(), MinecraftServer.getInstanceManager(), MinecraftServer.getConnectionManager(), MinecraftServer.getCommandManager());
}

public static LiteCommandsBuilder<CommandSender, LiteMinestomSettings, ?> builder(
@SuppressWarnings("unchecked")
public static <B extends LiteCommandsBuilder<CommandSender, LiteMinestomSettings, B>> 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<CommandSender> messageRegistry = internal.getMessageRegistry();

builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public final class LiteVelocityFactory {
private LiteVelocityFactory() {
}

public static LiteCommandsBuilder<CommandSource, LiteVelocitySettings, ?> builder(ProxyServer proxy) {
return LiteCommandsFactory.builder(CommandSource.class, new VelocityPlatform(proxy.getCommandManager(), new LiteVelocitySettings()))
@SuppressWarnings("unchecked")
public static <B extends LiteCommandsBuilder<CommandSource, LiteVelocitySettings, B>> B builder(ProxyServer proxy) {
return (B) LiteCommandsFactory.builder(CommandSource.class, new VelocityPlatform(proxy.getCommandManager(), new LiteVelocitySettings()))
.extension(new LiteAdventureExtension<>(), configuration -> configuration
.legacyColor(true)
)
Expand Down