Issue with updating PlayerListName on PlayerRespawnEvent

Discussion in 'Plugin Development' started by icicl, Jul 13, 2021.

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

    icicl

    In my plugin, I have a listener that displays the player's current health in the tab display. It works fine, except that whenever a player respawns, the display does not update until they take damage.

    Here is the associated code:
    Code:
        @EventHandler
        public void onRespawn(PlayerRespawnEvent e) {
            updatePlayer(e.getPlayer(),20);
            if (plugin.game!=null && plugin.game.get_participant(e.getPlayer())!=null){
                e.setRespawnLocation(plugin.game.get_participant(e.getPlayer()).getSpawn());
            }
        }
    
        public void updatePlayer(Player player, int health){
            String health_str = "§4";
            for (int i = 0; i < health / 2; i++) {
                health_str += "❤";
            }
            if (health % 2 == 1) {
                health_str += "§c❤";
            }
            health_str += "§0";
            for (int i = health; i < 20-1; i += 2) {
                health_str += "❤";
            }
            if (plugin.game==null){
                player.setPlayerListName("§a"+player.getName() + " " + health_str);
                return;
            }
            player.setPlayerListName("§a"+player.getName() + " " + health_str + plugin.game.get_player_score(player));
        }
    
    I have checked, and updatePlayer is being called on respawn, with the health parameter of 20, so I am not sure why it is not working. Additionally, the display successfully updates on taking damage, healing, or relogging, so I know that the listener is running successfully and that the updatePlayer function should work. If anyone could help me figure out what the issue is, it would be much appreciated.

    Here is the other relevant code from the class:
    Code:
    public class TabAgent implements Listener {
        private Main plugin;
    
        public TabAgent(Main plugin) {
            this.plugin = plugin;
            for (Player player:Bukkit.getOnlinePlayers()){
                updatePlayer(player);
            }
        }
    
        @EventHandler
        public void onDamage(EntityDamageEvent e) {
            if ((e.getEntityType() == EntityType.PLAYER) && (e.getCause() != EntityDamageEvent.DamageCause.VOID)) {
                Player player = (Player) e.getEntity();
                updatePlayer(player,(int) (player.getHealth() - e.getFinalDamage()));
            }
        }
        @EventHandler
        public void onHeal(EntityRegainHealthEvent e) {
            if (e.getEntityType() == EntityType.PLAYER) {
                Player player = (Player) e.getEntity();
                updatePlayer(player,(int) (player.getHealth() + e.getAmount()));
            }
        }
        @EventHandler
        public void onJoinServer(PlayerJoinEvent e) {
            if (plugin.game!=null && plugin.game.get_participant(e.getPlayer())!=null){
                plugin.game.get_participant(e.getPlayer()).player=e.getPlayer();
            }
            updatePlayer(e.getPlayer());
        }
        @EventHandler
        public void onRespawn(PlayerRespawnEvent e) {
            updatePlayer(e.getPlayer(),20);
            if (plugin.game!=null && plugin.game.get_participant(e.getPlayer())!=null){
                e.setRespawnLocation(plugin.game.get_participant(e.getPlayer()).getSpawn());
            }
        }
    
        public void updatePlayer(Player player){
            updatePlayer(player,(int)player.getHealth());
        }
        public void updatePlayer(Player player, int health){
            String health_str = "§4";
            for (int i = 0; i < health / 2; i++) {
                health_str += "❤";
            }
            if (health % 2 == 1) {
                health_str += "§c❤";
            }
            health_str += "§0";
            for (int i = health; i < 20-1; i += 2) {
                health_str += "❤";
            }
            if (plugin.game==null){
                player.setPlayerListName("§a"+player.getName() + " " + health_str);
                return;
            }
            player.setPlayerListName("§a"+player.getName() + " " + health_str + plugin.game.get_player_score(player));
        }
    }
     
  2. Offline

    Tim_M

    A wild guess but see what happens if you delay the update method by like half a second after a player respawns. Also are you SURE that the method gets called upon respawning?
     
  3. Offline

    davidclue

    @icicl Use a string builder to create the string instead of '+=' in your for loops, in the respawn event update the player with a delayed task of 1 second because for some reason calling some player methods on respawn doesn't work (I believe the event is fired before the player actually fully respawns). Also, don't update the player with 20 health 0n respawn just use player.getHealth() because for some reason if a player uses an end portal it triggers it as a player respawn event not a portal event, it's a weird bug.
     
Thread Status:
Not open for further replies.

Share This Page