A simple problem

Discussion in 'Spigot Plugin Development' started by dadus33, May 29, 2015.

  1. I've got an issue with my plugin... I know it must be something simple, but I just can't figure out what's wrong.
    Here's my code:
    Code (Text):
       
    @EventHandler(priority = EventPriority.HIGH)
    public void onNameTag(AsyncPlayerReceiveNameTagEvent e) {
            Player p = e.getNamedPlayer();
            String name = "";
            TagAPI.refreshPlayer(p);
            int i;
            int j = e.getNamedPlayer().getDisplayName().lastIndexOf('&');
            if(j>=0){
            for(i=j; i<=e.getNamedPlayer().getDisplayName().length()-1; ++i){
                name = name+p.getDisplayName().charAt(i);
            }
            }
            e.setTag(ChatColor.translateAlternateColorCodes('&', name));
            p.setPlayerListName(ChatColor.translateAlternateColorCodes('&', name));
            name = null;
            p = null;
        }
    The problem is, my name is empty, both in the tablist and over my head (I'm using @md_5 's iTag replacement for TagAPI for this). Any ideas?
    Thanks!
     
  2. Is there any error log?
     
  3. Is there any error logs? Also you should try using the Scoreboard API, wich can do the same thing without an external plugin and allows you to set up to 48 character length names.
     
  4. No, there aren't any error logs. I don't want to use the scoreboard api because it interfeers with other pluging that use scoreboards.
     
  5. Strung#lastIndexOf() returns -1 if the given char or substring is not found in the string. Returning -1 would cause you to skip the for loop and name would remain pointing to "".

    Chances are the string is already translated and doesn't contain & but the actual color symbol (ChatColor.COLOR_CHAR I think it is). Never the less you should handle this non existant color case anyways.
     
  6. Thank you! Then, would this code do the work?
    Code (Text):

    @EventHandler
    public void onNameTag(AsyncPlayerReceiveNameTagEvent e) {
            Player p = e.getNamedPlayer();
            String name = "";
            TagAPI.refreshPlayer(p);
            int i;
            int j = e.getNamedPlayer().getDisplayName().lastIndexOf(ChatColor.COLOR_CHAR);
            if(j>=0){
            for(i=j; i<=e.getNamedPlayer().getDisplayName().length()-1; ++i){
                name = name+p.getDisplayName().charAt(i);
            }
            }
            e.setTag(name);
            p.setPlayerListName(name);
            name = null;
            p = null;
    }
     
  7. Oh... Just compiled it and it looks like it doesn't... The exact same thing happens. It's just as you said, like the name remains an empty string... But I just can't figgure out why... As you see, I tried finding the last position of the color char, just in case the names were already translated, as you said, but it doesn't seem to work...
     
  8. Look at these names:
    §bMrBlobman which is MrBlobman
    MrBlobman which is MrBlobman


    It makes sense that the first one would work. Your cases seem to have that covered. But look at the second case, is there a § anywhere? Nope and so the String#indexOf() would return -1. We then skip the for loop because
    Code (Text):
    if(j>=0)
    tells us to jump to
    Code (Text):
    e.setTag(name);
    and because we didn't enter the for loop name is still what is has been initialized to, "". You can just add an else branch or initialize the name to the players direct name. If you are trying to get rid of the colors in the name you can accomplish this with ChatColor#stripColor(String).
     
  9. What I am acctually trying to do is getting the last color used, then set the name string to all that comes after the last color, including the color.
     
  10. Then you should make a check for whether j is -1 (the name does not contain any color characters) and set the name directly if that is the case.
     
  11. Well, that's the issue. J is always -1, even when I am sure the name contains color characters.
     
  12. TagAPI is outdated m8. That is your problem.
     
  13. No. Read this before posting dumb things.