-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
0f4af80
commit 1c7f418
Showing
5 changed files
with
149 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
examples/bukkit/src/main/java/dev/rollczi/example/bukkit/command/OfflineInfoCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/argument/OfflinePlayerArgument.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
litecommands-bukkit/src/dev/rollczi/litecommands/bukkit/util/BukkitFallbackPrefixUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(" ", "-"); | ||
} | ||
|
||
} |