Skip to content

Commit

Permalink
GH-438 Bukkit OfflinePlayer argument (#438)
Browse files Browse the repository at this point in the history
* Improve LiteBukkitFactory

* Implement OfflinePlayerArgument

* Use TreeSet for OfflinePlayerArgument's suggestions. Add example.

* Move createFallbackPrefix method to BukkitFallbackPrefixUtil.

---------

Co-authored-by: Rollczi <[email protected]>
  • Loading branch information
BlackBaroness and Rollczi authored Oct 1, 2024
1 parent 0f4af80 commit 1c7f418
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.rollczi.example.bukkit.command.GameModeCommand;
import dev.rollczi.example.bukkit.command.KickCommand;
import dev.rollczi.example.bukkit.command.NumberCommand;
import dev.rollczi.example.bukkit.command.OfflineInfoCommand;
import dev.rollczi.example.bukkit.command.RandomItemCommand;
import dev.rollczi.example.bukkit.command.TeleportCommand;
import dev.rollczi.example.bukkit.command.currency.CurrencyBalanceCommand;
Expand Down Expand Up @@ -49,6 +50,7 @@ public void onEnable() {

// Commands
.commands(
new OfflineInfoCommand(),
new ConvertCommand(),
new GameModeCommand(),
new KickCommand(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dev.rollczi.example.bukkit.command;

import dev.rollczi.example.bukkit.util.ChatUtil;
import dev.rollczi.litecommands.annotations.argument.Arg;
import dev.rollczi.litecommands.annotations.async.Async;
import dev.rollczi.litecommands.annotations.command.Command;
import dev.rollczi.litecommands.annotations.context.Context;
import dev.rollczi.litecommands.annotations.execute.Execute;
import java.time.Duration;
import java.time.Instant;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;

@Command(name = "offline-info")
public class OfflineInfoCommand {

@Execute
void executeOfflineInfo(
@Context CommandSender sender,
@Arg OfflinePlayer offlinePlayer
) {
String online = "&7(" + (offlinePlayer.isOnline() ? "&aonline" : "&coffline") + "&7)";
Instant lastPlayed = Instant.ofEpochMilli(offlinePlayer.getLastPlayed());
String lastPlayedFormatted = Duration.between(lastPlayed, Instant.now()).toHours() + "h";

sender.sendMessage(ChatUtil.color("&7Player: &e" + offlinePlayer.getName() + " " + online));
sender.sendMessage(ChatUtil.color("&7uuid: " + offlinePlayer.getUniqueId()));
sender.sendMessage(ChatUtil.color("&7Last played: &e" + lastPlayedFormatted));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
import dev.rollczi.litecommands.LiteCommandsBuilder;
import dev.rollczi.litecommands.LiteCommandsFactory;
import dev.rollczi.litecommands.bukkit.argument.LocationArgument;
import dev.rollczi.litecommands.bukkit.argument.OfflinePlayerArgument;
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.bukkit.util.BukkitFallbackPrefixUtil;
import dev.rollczi.litecommands.message.MessageRegistry;
import java.util.Locale;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
Expand All @@ -26,20 +27,21 @@ 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());
Plugin plugin = JavaPlugin.getProvidingPlugin(LiteBukkitFactory.class);
return builder(BukkitFallbackPrefixUtil.create(plugin), plugin, plugin.getServer());
}

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

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

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

public static <B extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings, B>> B builder(String fallbackPrefix, Plugin plugin, Server server) {
Expand All @@ -55,6 +57,7 @@ public static <B extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings,
MessageRegistry<CommandSender> messageRegistry = internal.getMessageRegistry();

builder
.bind(Plugin.class, () -> plugin)
.bind(Server.class, () -> server)
.bind(BukkitScheduler.class, () -> server.getScheduler())

Expand All @@ -65,6 +68,7 @@ public static <B extends LiteCommandsBuilder<CommandSender, LiteBukkitSettings,
.argument(Player.class, new PlayerArgument(server, messageRegistry))
.argument(World.class, new WorldArgument(server, messageRegistry))
.argument(Location.class, new LocationArgument(messageRegistry))
.argument(OfflinePlayer.class, new OfflinePlayerArgument(server, plugin, true))

.context(Player.class, new PlayerOnlyContextProvider(messageRegistry))
.context(World.class, new WorldContext(messageRegistry))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package dev.rollczi.litecommands.bukkit.argument;

import dev.rollczi.litecommands.argument.Argument;
import dev.rollczi.litecommands.argument.parser.ParseResult;
import dev.rollczi.litecommands.argument.resolver.ArgumentResolver;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.suggestion.SuggestionContext;
import dev.rollczi.litecommands.suggestion.SuggestionResult;
import java.util.TreeSet;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;

import java.time.Instant;

public class OfflinePlayerArgument extends ArgumentResolver<CommandSender, OfflinePlayer> {

private static final int SUGGESTION_LIMIT = 256;

private final Server server;
private final Plugin plugin;
private final boolean enableThreadCheck;

private final TreeSet<String> nicknames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
private Instant nextWarningDate = Instant.EPOCH;

public OfflinePlayerArgument(Server server, Plugin plugin, boolean enableThreadCheck) {
this.server = server;
this.plugin = plugin;
this.enableThreadCheck = enableThreadCheck;

// Server#getOfflinePlayers() can be blocking, so we don't want to call it every time
for (OfflinePlayer offlinePlayer : server.getOfflinePlayers()) {
this.nicknames.add(offlinePlayer.getName());
}

// Save new joining player names so our suggestions are more wide
// TODO: Unregister this listener on Platform#unregister()
server.getPluginManager().registerEvents(new Listener() {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
nicknames.add(event.getPlayer().getName());
}
}, plugin);
}

@SuppressWarnings("deprecation")
@Override
protected ParseResult<OfflinePlayer> parse(Invocation<CommandSender> invocation, Argument<OfflinePlayer> context, String argument) {
// TODO: Use async argument parsing: https://github.com/Rollczi/LiteCommands/pull/435
if (enableThreadCheck && server.isPrimaryThread() && Instant.now().isAfter(nextWarningDate)) {
plugin.getLogger().warning("LiteCommands | OfflinePlayer argument resolved synchronously! This might cause server freeze! Add @Async to '" + context.getName() + "' argument. (command /" + invocation.name() + ")");
nextWarningDate = Instant.now().plusSeconds(60);
}

return ParseResult.success(server.getOfflinePlayer(argument));
}

@Override
public SuggestionResult suggest(Invocation<CommandSender> invocation, Argument<OfflinePlayer> argument, SuggestionContext context) {
if (nicknames.size() < SUGGESTION_LIMIT) {
return SuggestionResult.of(nicknames);
}

String input = context.getCurrent().multilevel();

if (input.isEmpty()) {
return nicknames.stream()
.limit(SUGGESTION_LIMIT)
.collect(SuggestionResult.collector());
}

return nicknames.subSet(input, input + Character.MAX_VALUE).stream()
.limit(SUGGESTION_LIMIT)
.collect(SuggestionResult.collector());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.rollczi.litecommands.bukkit.util;

import java.util.Locale;
import org.bukkit.plugin.Plugin;

public final class BukkitFallbackPrefixUtil {

private BukkitFallbackPrefixUtil() {
}

public static String create(Plugin plugin) {
return create(plugin.getName());
}

public static String create(String name) {
return name.toLowerCase(Locale.ROOT)
.replace(" ", "-");
}

}

0 comments on commit 1c7f418

Please sign in to comment.