How to get the closest matching player?

Discussion in 'Plugin Development' started by Mr Burkes, Dec 1, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Mr Burkes

    Sorry, I'm new to Bukkit (I just started about an hour ago). I've gotten a simple /tell command going here, and I want to make it so that if a player doesn't type in the player's full name, that it will send the message to the player whose name is closest to that string.

    For example, if a player does:

    /tell Bo <message>

    If it can't find Bo and Bob is online, it will send to Bob.

    --------------------------------------------------

    Here's my code for my main plugin class:

    Code:
    import org.bukkit.command.*;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.*;
    import org.bukkit.*;
    
    import java.awt.Color;
    import java.util.logging.*;
    
    public class KapipPack extends JavaPlugin{
    
        public static KapipPack plugin;
        public final Logger logger = Logger.getLogger("Minecraft");
        public final ChatColor RED = ChatColor.RED;
        public final ChatColor GRAY = ChatColor.GRAY;
        public final ChatColor WHITE = ChatColor.WHITE;
    
    
        public void onDisable(){        //When plugin disables
            PluginDescriptionFile pdf = this.getDescription();
            this.logger.info(pdf.getName()+" is now disabled!");
        }
    
        public void onEnable(){        //When plugin enables
            PluginDescriptionFile pdf = this.getDescription();
            this.logger.info(pdf.getName()+" version "+pdf.getVersion()+" is enabled!");
    
            TellExecutor TE = new TellExecutor(this);
            getCommand("tell").setExecutor(TE);
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            PluginDescriptionFile pdf = this.getDescription();
    
            if(cmd.getName().equalsIgnoreCase("info")){
                sender.sendMessage(RED+"-----------INFO-----------");
                sender.sendMessage("KapipPack Version "+pdf.getVersion());
                sender.sendMessage("Created by: Mr_Burkes");
                sender.sendMessage(RED+"--------------------------");
                return true;
            }
    
            else{
                return false;
            }
        }
    
    }
    
    And for my TellExecutor.class:

    Code:
    import org.bukkit.ChatColor;
    import org.bukkit.OfflinePlayer;
    import org.bukkit.command.*;
    import org.bukkit.entity.Player;
    
    public class TellExecutor implements CommandExecutor {
    
        private KapipPack plugin;
        private ChatColor WHITE = ChatColor.WHITE;
        private ChatColor GRAY = ChatColor.GRAY;
        private ChatColor RED = ChatColor.RED;
     
        public TellExecutor(KapipPack plugin) {
            this.plugin = plugin;
        }
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            try{
                String message = args[1];
                String playerRaw = args[0];
    
                plugin.logger.info(sender.getName()+" did command /tell");
     
                OfflinePlayer OP = plugin.getServer().getOfflinePlayer(playerRaw);
    
                if(OP.isOnline()){
                    Player player = plugin.getServer().getPlayer(playerRaw);
                    player.sendMessage(GRAY+"["+sender.getName()+" -> Me] "+WHITE+message);
                    sender.sendMessage(GRAY+"[Me -> "+OP.getName()+"] "+WHITE+message);
                }
                else{
                    sender.sendMessage(RED+"Can't find player!");
                }
    
                return true;
            }
            catch(Exception e){
                sender.sendMessage(GRAY+"Try this: "+RED+"/tell <message>");
                return false;
            }
        }
    }
     
  2. Offline

    Jogy34

    One simple tedious way to do this is to get an array a char's for each name of every player on the server and compare them one by one and whichever names has the most matches you send the message to that player.
     
  3. Offline

    Daniel Heppner

    WHAT? There's a built in Bukkit method of this.
    Take a server object and call the getPlayer() method on it. It will return a player object if you put in the first few letters of a username. Works like a charm.
     
  4. Offline

    rasb

    Daniel is doing it right. IIRC it also picks up display names?
     
  5. Offline

    Mr Burkes

    Yeah, it works. Thanks!
     
Thread Status:
Not open for further replies.

Share This Page