Search in sources :

Example 1 with PlayerChatTabCompleteEvent

use of org.bukkit.event.player.PlayerChatTabCompleteEvent in project Glowstone by GlowstoneMC.

the class TabCompleteHandler method handle.

@Override
public void handle(GlowSession session, TabCompleteMessage message) {
    GlowPlayer sender = session.getPlayer();
    String buffer = message.getText();
    List<String> completions = new ArrayList<>();
    // complete command or username
    if (!buffer.isEmpty() && buffer.charAt(0) == '/') {
        List<String> items;
        if (!buffer.isEmpty() && buffer.charAt(0) == '/') {
            items = session.getServer().getCommandMap().tabComplete(sender, buffer.substring(1));
        } else {
            items = session.getServer().getCommandMap().tabComplete(sender, buffer);
        }
        if (items != null) {
            completions.addAll(items);
        }
    } else {
        int space = buffer.lastIndexOf(' ');
        String lastWord;
        if (space == -1) {
            lastWord = buffer;
        } else {
            lastWord = buffer.substring(space + 1);
        }
        // from Command
        for (Player player : session.getServer().getOnlinePlayers()) {
            String name = player.getName();
            if (sender.canSee(player) && StringUtil.startsWithIgnoreCase(name, lastWord)) {
                completions.add(name);
            }
        }
        completions.sort(String.CASE_INSENSITIVE_ORDER);
    }
    // call event and send response
    EventFactory.getInstance().callEvent(new PlayerChatTabCompleteEvent(sender, buffer, completions));
    // TODO: 1.13, properly implement tab-completion
    session.send(new TabCompleteResponseMessage(0, 0, 0, completions.stream().map(str -> new TabCompleteResponseMessage.Completion(str, null)).collect(Collectors.toList())));
}
Also used : List(java.util.List) MessageHandler(com.flowpowered.network.MessageHandler) PlayerChatTabCompleteEvent(org.bukkit.event.player.PlayerChatTabCompleteEvent) EventFactory(net.glowstone.EventFactory) StringUtil(org.bukkit.util.StringUtil) GlowSession(net.glowstone.net.GlowSession) TabCompleteMessage(net.glowstone.net.message.play.player.TabCompleteMessage) TabCompleteResponseMessage(net.glowstone.net.message.play.player.TabCompleteResponseMessage) GlowPlayer(net.glowstone.entity.GlowPlayer) Player(org.bukkit.entity.Player) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) PlayerChatTabCompleteEvent(org.bukkit.event.player.PlayerChatTabCompleteEvent) GlowPlayer(net.glowstone.entity.GlowPlayer) Player(org.bukkit.entity.Player) TabCompleteResponseMessage(net.glowstone.net.message.play.player.TabCompleteResponseMessage) GlowPlayer(net.glowstone.entity.GlowPlayer) ArrayList(java.util.ArrayList)

Aggregations

MessageHandler (com.flowpowered.network.MessageHandler)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 EventFactory (net.glowstone.EventFactory)1 GlowPlayer (net.glowstone.entity.GlowPlayer)1 GlowSession (net.glowstone.net.GlowSession)1 TabCompleteMessage (net.glowstone.net.message.play.player.TabCompleteMessage)1 TabCompleteResponseMessage (net.glowstone.net.message.play.player.TabCompleteResponseMessage)1 Player (org.bukkit.entity.Player)1 PlayerChatTabCompleteEvent (org.bukkit.event.player.PlayerChatTabCompleteEvent)1 StringUtil (org.bukkit.util.StringUtil)1