Solved setPlayerListName on join doesn't work

Discussion in 'Plugin Development' started by Aldrein, Feb 6, 2016.

Thread Status:
Not open for further replies.
  1. I'm making a plugin with grades.
    I've set a file (config.yml) to remember the user's grade (this work)
    But the problem is, in my PlayerJoin class, i try to set the playerListName with his grade but it doesn't show anything, i only see my nickname without my grade
    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            String playerGrade = "default";
            Player player = event.getPlayer();
            if (plugin.getConfig().contains(player.getName())) {
                playerGrade = plugin.getConfig().getString(player.getName() + ".grade");
                player.sendMessage(ChatColor.GOLD + "Votre grade est " + playerGrade); // Translate : You're grade is + playerGrade
            }else {
                plugin.getConfig().createSection(player.getName() + ".grade");
                plugin.getConfig().set(player.getName() + ".grade", "default");
                player.sendMessage(ChatColor.RED + "Vous n'avez pas de grade, vous avez donc le grade par defaut"); //Translate : You don't have any grade, so you have the default grade
            }
            ChangeName.changeDisplayName(player, playerGrade); //I call this function to set the playerListName
        }
    (the messages are in french because i'm french)
    [​IMG]
    I took the screenshot just after login
     
  2. Offline

    Lampades

    Mind posting the config.yml?
     
  3. @Lampades you need the config.yml ?

    My config.yml :
    Code:
    Grade:
      default: 0
    Aldrein:
      grade: dev
    @Lampades

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

    teej107

    Show us the code for this method but first did you register the event?
     
  5. Here is the code on my function setting the player list name
    Code:
    public static void changeDisplayName(Player player, String grade) {
            String playerName = player.getName();
            player.setDisplayName("");
            player.setPlayerListName("");
            if (grade.equals("default")) {
                player.setDisplayName(ChatColor.GRAY + playerName + ChatColor.WHITE);
                player.setPlayerListName(ChatColor.GRAY + playerName);
            }else if (grade.equals("vip")) {
                player.setDisplayName(ChatColor.YELLOW + "[VIP]" + playerName + ChatColor.WHITE);
                player.setPlayerListName(ChatColor.YELLOW + "[VIP]" + playerName);
            }else if (grade.equals("supervip")) {
                player.setDisplayName(ChatColor.LIGHT_PURPLE + "[SuperVIP]" + playerName + ChatColor.WHITE);
                player.setPlayerListName(ChatColor.LIGHT_PURPLE + "[SuperVIP]" + playerName);
            }else if (grade.equals("helper")) {
                player.setDisplayName(ChatColor.DARK_AQUA + "[Helper]" + playerName + ChatColor.WHITE);
                player.setPlayerListName(ChatColor.DARK_AQUA + "[Helper]" + playerName);
            }else if (grade.equals("modo")) {
                player.setDisplayName(ChatColor.RED + "[Moderateur]" + playerName + ChatColor.WHITE);
                player.setPlayerListName(ChatColor.RED + "[Moderateur]" + playerName);
            }else if (grade.equals("builder")) {
                player.setDisplayName(ChatColor.DARK_GREEN + "[Builder]" + playerName + ChatColor.WHITE);
                player.setPlayerListName(ChatColor.DARK_GREEN + "[Builder]" + playerName);
            }else if (grade.equals("dev")) {
                player.setDisplayName(ChatColor.DARK_PURPLE + "[Developpeur]" + playerName + ChatColor.WHITE);
                player.setPlayerListName(ChatColor.DARK_PURPLE + "[Developpeur]" + playerName);
            }else if (grade.equals("admin")) {
                player.setDisplayName(ChatColor.DARK_RED + "[Admin]" + playerName + ChatColor.WHITE);
                player.setPlayerListName(ChatColor.DARK_RED + "[Admin]" + playerName);
            }else if (grade.equals("star")) {
                player.setDisplayName(ChatColor.GOLD + "[Star]" + playerName + ChatColor.WHITE);
                player.setPlayerListName(ChatColor.GOLD + "[Star]" + playerName);
            }else {
                return;
            }
        }
    @teej107 yes of course i registered my events

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 26, 2017
  6. Offline

    teej107

    Well DRY just went out the window on that one. :p Did you register your events?

    EDIT: Try running the code in a task

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 22, 2018
    JoaoBM likes this.
  7. @teej107 how can I do that ? :/

    Ho ! I found the solution ! thanks @teej107 I found how to run the code in a task and it works ! Thank you ! :)
    (final code :)
    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            String playerGrade = "default";
            Player player = event.getPlayer();
            if (plugin.getConfig().contains(player.getName())) {
                playerGrade = plugin.getConfig().getString(player.getName() + ".grade");
                player.sendMessage(ChatColor.GOLD + "Votre grade est " + playerGrade);
            }else {
                plugin.getConfig().createSection(player.getName() + ".grade");
                plugin.getConfig().set(player.getName() + ".grade", "default");
                player.sendMessage(ChatColor.RED + "Vous n'avez pas de grade, vous avez donc le grade par defaut");
            }
            final String finalPlayerGrade = playerGrade;
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    ChangeName.changeDisplayName(player, finalPlayerGrade);
                }
            }, 50);
    
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 6, 2016
  8. Offline

    teej107

    Konato_K likes this.
  9. @teej107 I see there are other solutions but this solution work.... But I'll test the other solutions to see which one is the best
     
  10. Offline

    teej107

    Bukkit's scheduling system is the best for Bukkit. Use Bukkit's because it's made for Bukkit. Using other scheduling systems may cause your code to not perform as expected.
     
Thread Status:
Not open for further replies.

Share This Page