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); } }
according to the java doc getExp returns the exp the player has in on level. so if you just reached level 11 for example you have 0 exp. what you want is https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Player.html#getTotalExperience() and https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Player.html#setTotalExperience(int)
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); } }
You're not using it right, read this: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/Player.html#setExp(float) . THe amount of exp is a float, so if you want to show half a bar of exp, you'd use p.setExp(0.5F)
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 .