What is wrong with this code?

Discussion in 'Archived: Plugin Requests' started by Mrchasez, Oct 1, 2011.

  1. Offline

    Mrchasez

  2. Offline

    Kaikz

    I have xAuth and CommandBook, and they both block these characters.
     
  3. Offline

    Mrchasez

    xAuth does not...
    I have it, It dont block anything.
     
  4. Offline

    Kaikz

    eh idk, I thought I remember seeing a verify-names option.
     
  5. Offline

    Mrchasez

    Its a simple plugin
    Ill just do one tonight, based on the old one >_<

    Anyone know ?
    Someone help me

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  6. Offline

    Bush

  7. Offline

    Mrchasez

    It loads up, But when someone joins with a character (that isnt allowed)
    nothing happens.
     
  8. Offline

    bergerkiller

    @Mrchasez Instead of blacklisting you should whitelist. It works best. For example, loop through all characters in the name and only allow names:
    - longer than 3 chars
    - at least two a-z, A-Z
    - abcdefghijklmnopqrstuv, capitals, 012345689, /\-=() allowed (more is possible)

    Blacklisting is generally not a good idea, better come up with an allowed format and build upon that.

    EDIT

    Also, you can't get the address from within the onPlayerLogin event. Use onPlayerJoin and kick.
     
  9. Offline

    feildmaster

    else if (username.matches(".*[^A-Za-z0-9_].*"))

    that's what you should be using.

    or rather:

     
  10. Offline

    cyberdude

    I am suspecting you are wrong with your regex:
    $^[A-Za-z0-9_]+$

    The way I'm seeing it, this check will always fail, because $ = end of string. So you have a regex checking if the first character is the end of a string, which it will never be. And I find you negation to be wrong. You can do one of these two:

    Code:
    else if (username.matches(".*[^A-Za-z0-9_].*"))
    {
        return true;
    }
    
    OR

    Code:
    else if (!username.matches("^[A-Za-z0-9_]+$"))
    {
        return true;
    }
    
    Notice the first one, you are checking for a negation, so in your case you shouldn't check for string start/end (which you were trying with the $ sign). Also the negation must be inside the square brackets, and you don't need to specify the repetition plus sign.
    The other example adds an boolean not infront of the check, and then checks whether the user matches, and here I'm using the ^ and $ as start/end of string notation.

    Both of these should get you going...

    You might want to debug using some log.info og console.println thrown into your code. If you had debugged this else if statement, you'd probably had noticed that this never translates to true, and thus never blocks anybody.

    Edit:
    Go away fieldmaster ;);)... Beat me to it...

    Edit2:
    I think me and fieldmaster is saying two different things, but we are pointing out the flaw in the same place

    Edit3:
    Edited my first example, as fieldmasters example is correct in this case. (Didn't really put much into it that he wasn't using the Regex object but rather String.matches.)

    Edit4:
    Going further with fieldmasters suggestion of shortening you could write your entire illigal check as follow:

    Code:
    public boolean isIlligal(String username)
    {
    	return !username.matches("^[A-Za-z0-9_]{1,16}$");
    }
    
     
  11. Offline

    Mrchasez

  12. Offline

    bergerkiller

    @Mrchasez nope. See this:
    Code:
                  if (plugin.checkpermissions(Event.getPlayer(), "illegalname.see"))
                  {
                    player.sendMessage("§c"+Event.getPlayer().getName()+" ("+Event.getPlayer().getAddress().getHostName().toString()+") is kicked for il
    Code:
    Event.getPlayer().getAddress().getHostName().toString()
    Event.getPlayer().getAddress() returns null, causing a nullreferenceexception to occur. This is why I mentioned using onPlayerJoin instead of onPlayerLogin.
     
  13. Offline

    Mrchasez

    OK thanks!
    I fixed it got it working :>

    Thank you so much, Ill mention you guys in the thread
     
  14. Offline

    feildmaster

    Address shouldn't do that in login. Especially since the address is detected in prelogin. It should pass that through to Login.
     
  15. Offline

    Mrchasez

Share This Page