From 4166af1d3aec67c88e34dd442d38f8208e099c52 Mon Sep 17 00:00:00 2001 From: Wesley Wolfe Date: Wed, 17 Oct 2012 04:31:36 -0500 Subject: [PATCH] Implement chat tab completion API. Fixes BUKKIT-2608. Adds BUKKIT-2607. The chat tab completion implementation also includes a sanity check to assure type-safety in the list. --- .../org/bukkit/craftbukkit/CraftServer.java | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/bukkit/craftbukkit/CraftServer.java b/src/main/java/org/bukkit/craftbukkit/CraftServer.java index 56ae1420cc..853c240bad 100644 --- a/src/main/java/org/bukkit/craftbukkit/CraftServer.java +++ b/src/main/java/org/bukkit/craftbukkit/CraftServer.java @@ -6,6 +6,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; @@ -86,6 +87,7 @@ import org.bukkit.craftbukkit.util.DatFileFilter; import org.bukkit.craftbukkit.util.Versioning; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.player.PlayerChatTabCompleteEvent; import org.bukkit.event.world.WorldInitEvent; import org.bukkit.event.world.WorldLoadEvent; import org.bukkit.event.world.WorldSaveEvent; @@ -113,6 +115,7 @@ import org.bukkit.potion.Potion; import org.bukkit.potion.PotionEffectType; import org.bukkit.plugin.messaging.StandardMessenger; import org.bukkit.scheduler.BukkitWorker; +import org.bukkit.util.StringUtil; import org.bukkit.util.permissions.DefaultPermissions; import org.yaml.snakeyaml.Yaml; @@ -1257,19 +1260,31 @@ public final class CraftServer implements Server { player.sendMessage(ChatColor.RED + "An internal error occurred while attempting to tab-complete this command"); getLogger().log(Level.SEVERE, "Exception when " + player.getName() + " attempted to tab complete " + message, ex); } - + return completions == null ? ImmutableList.of() : completions; } public List tabCompleteChat(Player player, String message) { Player[] players = getOnlinePlayers(); - List completions = new ArrayList(players.length); + List completions = new ArrayList(); + PlayerChatTabCompleteEvent event = new PlayerChatTabCompleteEvent(player, message, completions); + String token = event.getLastToken(); for (Player p : players) { - if (player.canSee(p)) { + if (player.canSee(p) && StringUtil.startsWithIgnoreCase(p.getName(), token)) { completions.add(p.getName()); } } + pluginManager.callEvent(event); + Iterator it = completions.iterator(); + while (it.hasNext()) { + Object current = it.next(); + if (!(current instanceof String)) { + // Sanity + it.remove(); + } + } + Collections.sort(completions, String.CASE_INSENSITIVE_ORDER); return completions; } }