getLevel() not working properly

Discussion in 'Plugin Development' started by KerchooK, Jul 2, 2018.

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

    KerchooK

    When I use .getLevel(); to get the player's experience level it works improperly, but only in some cases. In the events PlayerJoinEvent and EnchantItemEvent, it works properly and gets the players experience level. In other methods it returns the total experience the player has gained since last death as if I had used .getTotalExperience();

    In this plugin, I am creating a scoreboard that will display the player's experience level to them, here are my event handlers.

    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
    
            Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
    
            Objective objective = board.registerNewObjective("experience", "xp");
    
            objective.getScoreboard().resetScores("experience");
    
            objective.setDisplaySlot(DisplaySlot.SIDEBAR);
    
            objective.setDisplayName("Experience Leaderboard");
    
            e.getPlayer().setScoreboard(board);
    
            e.getPlayer().getScoreboard().getObjective("experience").getScore(e.getPlayer().getDisplayName())
                    .setScore(e.getPlayer().getLevel());
    
        }
    
        @EventHandler
        public void onXPChange(PlayerExpChangeEvent e) {
    
            e.getPlayer().getScoreboard().getObjective("experience").getScore(e.getPlayer().getDisplayName())
                    .setScore(e.getPlayer().getLevel());
    
            e.getPlayer().sendMessage("XP Changed");
    
        }
    
        @EventHandler
        public void onDeath(PlayerDeathEvent e) {
    
            if (e.getEntity() instanceof Player) {
    
                e.getEntity().getScoreboard().getObjective("experience").getScore(e.getEntity().getDisplayName())
                        .setScore(e.getEntity().getLevel());
    
                e.getEntity().sendMessage("Death Occured");
    
            }
    
        }
    
        @EventHandler
        public void onEnchant(EnchantItemEvent e) {
    
            e.getEnchanter().getScoreboard().getObjective("experience").getScore(e.getEnchanter().getDisplayName())
                    .setScore((int) e.getEnchanter().getLevel());
          
            e.getEnchanter().sendMessage("Item Enchanted");
    
        }
    EDIT:
    The scoreboard works properly for a few seconds when loaded, but then randomly updates and stops working, I have not gotten any of the debug messages I placed in the file, so I am not sure what is causing that to update.
     
    Last edited: Jul 2, 2018
  2. Offline

    MightyOne

    try putting debug messages everywhere until you find out
     
Thread Status:
Not open for further replies.

Share This Page