I need help with PlayerExpChangeEvent/setMaxHealth

Discussion in 'Spigot Plugin Development' started by Rucano, Feb 12, 2019.

  1. So I was just messing around with it while I am currently learning and stuff, Then I faced some problem with PlayerExpChangeEvent.

    It works fine for the first time. And I've started finding out there was some problem with the Event. It doesn't Initialize when I've died and Even it doesn't work when I get the exp from 'my' dead entity.
    And the other problem is with Enchant table. Like almost the same as corpse issue, It doesn't initialize when my exp changes from the cost. I'm pretty sure anvil will have the same issue even tho I didn't check for it.

    So basically my question is,
    1. Are there any alternative Events/Solutions that I can use it for ExpChange? Like should I manually check Anvil and EnchantItemEvent for it?
    (Actually, just LevelChange is fine too(But it gotta work with changing with Enchant table and stuff))

    And for just a little curious about the setMaxHealth().
    2. Is there any other way to set the maxhealth? I mean not because it's not working. Because it's Deprecated. And Spigot doc says, "use Attribute.GENERIC_MAX_HEALTH" And I don't know how should I use it.

    Here's my Code for these 'Leveling Events'
    Code (Java):
    package rucano.plugin.lph;

    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.event.player.PlayerExpChangeEvent;

    public class playerLevel implements Listener {
        FileConfiguration config = Main.plugin.getConfig();

        public void onPlayerExpChangeEvent(PlayerExpChangeEvent e) {
            Player p = (Player) e.getPlayer();
            Double cal_maxhp = Main.default_Maxhp + Math.round(p.getLevel()/4)*2;
            config.set(p.getName().toLowerCase(), Double.valueOf(cal_maxhp));
        public void onPlayerDeathEvent(PlayerDeathEvent e) {
            Player p = e.getEntity().getPlayer();
            p.GENERIC_MAX_HEALTH = 0.0;
        public void PlayerLevelChangeEvent(Player player,int oldLevel,int newLevel) {
            config.set(player.getName().toLowerCase(), Double.valueOf(newLevel/4));

  2. As for Question 1, I think that there is no dedicated Event for that, so I think that I must devise some ingenuity.
    As far as Anvil is concerned, this may be helpful.

    I think question 2 is possible in this way.
    Code (Java):
    #2 The_Mad_Pillow, Feb 12, 2019
    Last edited: Feb 12, 2019
    • Like Like x 1
  3. Thanks, man. question 2 is completely solved! And well, I guess I have to manually add the Events. But still, I was really worried about the Anvil. It only has prepare and inventory check events.

    But for the last, Is there any way to Initialize the exp from the player's corpse? When they die and drop exp, For some reason that exp doesn't trigger the onPlayerExpChangeEvent. I think maybe it's because players exp is basically stacked up when they die? So it's not a normal exp from monsters or ores?

    You don't have to answer this one.
    Again, Thanks for the help regardlessly.

Share This Page