Getting the exp from a player.

Discussion in 'Spigot Plugin Development' started by nov4e, Jul 12, 2019.

  1. Hello guys. I'm here to ask how can i get some things related to experience.
    How can i get the player's exp level? doing p.getExpToLevel() it returns 9 but actually the player's level is 1.
    After few levels it returns 64 i dont know why.
    How can i get the player's exp and the exp required to reach the next level? I tried to get the player's exp with p.getExp() but it returns 0,0ecc instead of a int.

    Thanks for help. I hope i was clear.
     
  2. Hello,

    I just recommand you to check the Player spigot javadoc;

    To get the player exp level: Player#getLevel()
    To get the exp required to reach the next level: Player#getExpToLevel()
     
    • Like Like x 1
  3. How can i get the exp amount as a int?
     
  4. Player#getTotalExperience if you want the overall total xp
     
  5. Seems not working.
    [​IMG]
    [​IMG]
    Code (Text):
    @EventHandler
        public void onPlayerExpChangeEvent(PlayerExpChangeEvent e) {
            Player p = e.getPlayer();
            int i = (int) (p.getExp() + p.getExpToLevel());
            String message = "ยงa+ " + e.getAmount() + "XP (" + p.getExp() + "/" + i + ")";
            PacketPlayOutChat packet = new PacketPlayOutChat(ChatSerializer.a("{\"text\":\"" + message.replace("&", "ยง") + "\"}"), (byte) 2);
             ((CraftPlayer) p).getHandle().playerConnection.sendPacket(packet);
        }
     
  6. Use 'getTotalExperience' instead of 'getExp'
     
  7. That's the total amount of experience gained, not current total experience.
    I wrote a utility for this a few years back, you're welcome to use it.
     
    • Informative Informative x 1
  8. Code (Text):

        public int getAllExp(int level, float b) {
            if (level >= 0 && level <= 16) {
                return level * level + 6 * level + getLevelExp(level, b);
            } else if (level >= 17 && level <= 31) {
                return (int) (level * level * 2.5 - 40.5 * level + 360) + getLevelExp(level, b);
            } else {
                return (int) (level * level * 4.5 - 162.5 * level + 2220) + getLevelExp(level, b);
            }
        }

        public int getLevelExp(int level, float b) {
            return (int) (getLevelAllExp(level) * b);
        }

        public int getLevelAllExp(int level) {
            if (level >= 0 && level <= 15) {
                return 2 * level + 7;
            } else if (level >= 16 && level <= 30) {
                return 5 * level - 38;
            } else {
                return 9 * level - 158;
            }
        }
    The "b" is Player.getExp()
    The "level" is Player.getLevel()