Bukkit Plugin - Get the closest matching player?

Discussion in 'Bukkit Help' 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. This is what I did for my EasyPM plugin.

    Code:
        private Player findPlayer(String playerName) {
            Player[] onlinePlayers = Bukkit.getOnlinePlayers();
            Player playerFound = null;
            int foundPlayers = 0;
    
            if (onlinePlayers.length > 0) {
                for (Player op : onlinePlayers) {
                    if (op != null) {
                        if (op.getName().toLowerCase().startsWith(playerName.toLowerCase())) {
                            foundPlayers++;
                            playerFound = op;
                        }
                    }
                }
    
                if (foundPlayers != 1) {
                    playerFound = null;
                }
            }
            return playerFound;
        }
    
    And there I go again with wrong code...

    The Bukkit.getPlayer method already has this implemented. So just try getting the player with Bukkit.getPlayer("playername") and it will return null if it didn't found anything.

    This is what Bukkit.getPlayer does:
    Code:
        public Player getPlayer(final String name) {
            Player[] players = getOnlinePlayers();
    
            Player found = null;
            String lowerName = name.toLowerCase();
            int delta = Integer.MAX_VALUE;
            for (Player player : players) {
                if (player.getName().toLowerCase().startsWith(lowerName)) {
                    int curDelta = player.getName().length() - lowerName.length();
                    if (curDelta < delta) {
                        found = player;
                        delta = curDelta;
                    }
                    if (curDelta == 0) break;
                }
            }
            return found;
        }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
Thread Status:
Not open for further replies.

Share This Page