Having trouble trimming names.

Discussion in 'Plugin Development' started by Sabersamus, Oct 31, 2012.

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

    Sabersamus

    What I'm trying to accomplish is simple, if a players name + color, exceeds 16, I want to trim it to fit as a PlayerListName. So, naturally I use String.substring(0, x); where x is (probably) the length of their name - 2.

    Here's what I have for code:

    Code:java
    1. //PlayerJoinEvent
    2. String name = realPlayer.getDisplayName(); //realPlayer = event.getPlayer();
    3. //All players display names have been set by this point, so color has already been added.
    4. if(name.length() > 16){
    5. realPlayer.setPlayerListName(name.substring(0, 14)); //If the name is longer than 16, trim it down 2, taking into account that ChatColor adds on 2 chars.
    6. }else{ //Leave it be
    7. realPlayer.setPlayerListName(name);
    8. }


    Looking at this, it should work. However that is not the case. When someone logs in, with a DisplayName longer than 16 chars, it throws the exception "Player List Name may not exceed 16 characters"

    Can anyone help?
     
  2. Offline

    Technius

    Hm, could you direct me to which line?

    Also, substring should be used like this:
    Code:
    name.substring(0,16)
    
    because the second number is exclusive.
    Running "test".substring(1,3) would result in "es".
     
  3. Offline

    Sabersamus

    The line the error is on is this
    Code:java
    1.  
    2. realPlayer.setPlayerListName(name.substring(0, 14));
    3.  


    Honestly, at this point I don't care how much of their name is showing, as long as the players name is colored, and is recognizable. for example, i dont want it to be sab, while my name is sabersamus, and 3 other players names start with sab
     
  4. Offline

    Technius

    Try adding debug code like this:
    Code:
    String new = name.substring(0,17);
    System.out.println(name + "," + name.length  +";" + new + "," + new.length);
    
    and see what happens!
     
  5. Offline

    Sabersamus

    Alright, now the problem is getting someone who can throw the error to join my test server :p
     
  6. Offline

    fireblast709

    put your test server in offline-mode, join with a name like abcdefghijklmnopqrstuvwxyz (just put it in the username, and go play offline) and test it ;). (I usually use this trick to get test subjects)
     
  7. Offline

    Sabersamus

    That results in the name "Player"
     
Thread Status:
Not open for further replies.

Share This Page