p.getExp() Error

Discussion in 'Spigot Plugin Development' started by MaxKrissigo, May 11, 2016.

  1. So I'm trying to use this code, but even when I have 1000 xp, it still gives me the error message. I think there is something wrong with the number which I'm trying to get it to equal, but I am not entirely sure. Here is my code:
    Code (Text):
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
                if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
                if (e.getCurrentItem().getItemMeta() == null) return;
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Basic")) {
                           
                            Player p = (Player) e.getWhoClicked();
                           
                            if (p.getExp() >= (int)1000) {
                               
                                p.getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
                                p.setExp(p.getExp() - 1000);
                               
                            } else {
                               
                                p.sendMessage(ChatColor.RED + "You do not have 1000 xp!");
                               
                            }
                           
                            e.setCancelled(true);
                }
    }
     
  2. MiniDigger

    Supporter

    • Agree Agree x 1
  3. I tried that, but my xp bar doesn't seem to be updating when I remove some of the player's xp.
    Code:
    Code (Text):
        public void onInventoryClick(InventoryClickEvent e) {
                if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
                if (e.getCurrentItem().getItemMeta() == null) return;
                if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Basic")) {
                           
                            Player p = (Player) e.getWhoClicked();
                           
                            if (p.getTotalExperience() >= 1000) {
                               
                                p.getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
                                p.setTotalExperience(p.getTotalExperience() - 1000);
                               
                            } else {
                               
                                p.sendMessage(ChatColor.RED + "You do not have 1000 xp!");
                               
                            }
                           
                            e.setCancelled(true);
                }
    }
     
  4. Use p.getTotalExperience().
     
  5. Try p.getLevel();
     
  6. I don't know if it ever got fixed but when I was modifying XP in one of my old plugins I found that if you enchanted before I saved and restored the XP it would be reset back to the XP level before the enchantment was made. In the end I resorted to working out the XP from get level and getExp and then giving the player the experience rather then setting it.

    See https://github.com/basicmark/EMS/bl...a/io/github/basicmark/config/PlayerState.java for the details :).