Function to Expand Partial Names

Discussion in 'Resources' started by Frigid, Jan 4, 2011.

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

    Frigid

    I coded this up when I was doing my own custom mods to SMP and I thought I might as well contribute something to this here fine-looking community.

    Code:
        public String expandName(String Name) {
            int m = 0;
            String Result = "";
            for (int n = 0; n < getServer().getOnlinePlayers().length; n++) {
                String str = getServer().getOnlinePlayers()[n].getName();
                if (str.matches("(?i).*" + Name + ".*")) {
                    m++;
                    Result = str;
                }
                if (str.equalsIgnoreCase(Name))
                    return str;
            }
            if (m == 1)
                return Result;
            if (m > 1) {
                return null;
            }
            if (m < 1) {
                return Name;
            }
            return Name;
        }
    I'm totally new to the whole "plugin" thing so feel free to correct any errors in interaction with Bukkit you see.
     
  2. Offline

    Raphfrk

    You could make it slightly faster by having it return null as soon as m==2.

    Code:
        public String expandName(String Name) {
            int m = 0;
            String Result = "";
            for (int n = 0; n < getServer().getOnlinePlayers().length; n++) {
                String str = getServer().getOnlinePlayers()[n].getName();
                if (str.matches("(?i).*" + Name + ".*")) {
                    m++;
                    Result = str;
                    if(m==2) {
                        return null;
                    }
                }
                if (str.equalsIgnoreCase(Name))
                    return str;
            }
            if (m == 1)
                return Result;
            if (m > 1) {
                return null;
            }
            if (m < 1) {
                return Name;
            }
            return Name;
        }
    
    
    Ofc, if there are rarely ever double matches, then the cost of the check to see if m==2 might cost more.
     
  3. Offline

    TwistedPriest

    This is an excellent little function and very useful, but could someone explain
    Code:
    if (str.matches("(?i).*" + Name + ".*"))
    I am completely lost on that.

    EDIT: Particularly the "(?i).*" part, that's what's got me.
     
  4. Offline

    petteyg359

    That's redundant. The if (m < 1) block needs to go away.
     
  5. It seems like it checks if str contain whatever Name is.

    I am not good at regex, but as far I can tell the "(?i)" should be unnecessary.
     
  6. place the
    if (str.equalsIgnoreCase(Name))
    return str;part inside the previcus loop, because if the expression not matcher, how could the strings be equals to each other, this saves some little time
     
  7. Offline

    TnT

    An over 1 year old bump. ferrybig - I'm pretty sure this was either resolved already, or the OP simply doesn't care anymore. I doubt he was waiting, holding his breath for a year for that answer.

    Locked.
     
    TfT_02 likes this.
Thread Status:
Not open for further replies.

Share This Page