Solved Disable XP drop on death

Discussion in 'Spigot Plugin Development' started by ArKeid0s, Feb 11, 2020.

  1. Hello i wanted to know how i could disable the xp drop on death and xp change on respawn please.
    Because with my plugin, on respawn i set the player xp to 20 but it does not work

    Code (Java):
    @EventHandler
        public void onRespawn(PlayerRespawnEvent e) {
            Player player = e.getPlayer();
            int pThirst = config.getInt("PlayerThirst." + player.getDisplayName());
            player.sendMessage("thirst : " + pThirst);
            main.pThirstHashMap.put(player.getUniqueId(), new ThirstManager(main, pThirst));
            if(main.pThirstHashMap.containsKey(player.getUniqueId())) { player.sendMessage("┬žbi am in !"); }
            ThirstManager pThirstManager = main.pThirstHashMap.get(player.getUniqueId());
            pThirstManager.setPlayerThirst(player, pThirst);
            thirstScheduler(player);
        }
    Code (Text):
        public ThirstManager(MainClass mainClass, int pThirst) {
            main = mainClass;
            pThirst = this.pThirst;
        }
       
        public int getPlayerThirst() { return pThirst; }
        public void setPlayerThirst(Player player, int thirst) {
            player.setLevel(thirst);
            player.sendMessage("┬ždthirst set !");
            pThirst = thirst;
        }
       
    }
     
  2. Listen to EntityDeathEvent and setDroppedXp to 0 to disable xp drop on death.
     
    • Agree Agree x 1
  3. Code (Java):
    @EventHandler
        public void whenPlayerDeath(PlayerDeathEvent e) {
            Player player = e.getEntity();
                    e.setDroppedExp(0);
        }
       
        @EventHandler
        public void whenPlayerRespawn(PlayerRespawnEvent e) {
            Player player = e.getPlayer();
            player.setExp(20);
        }
    I think that with this works, I don't know.
     
  4. That will cast an exception every time an entity dies if it isn't a player. You can't just blindly cast to Player. do instanceof first, to see if the entity actually is a player.
     
    • Agree Agree x 1
  5. It's the PlayerDeathEvent and PlayerRespawnEvent. Event#getEntity always returns a player for these events. There is no type casting happening anyways.
    Should work. If it doesn't, try setting the player's experience a tick after they respawn. You can use the BukkitScheduler for this.
     
  6. Ah my bad, I thought he used EntityDeathEvent, I forgot PlayerDeathEvent existed.

    Damn my IQ is dropping

    EDIT: while we're at it, PlayerDeathEvent has setNewTotalExp, use that instead of listening to two events.
     
    • Informative Informative x 1
  7. Jaja, no problem. I don't think your losing IQ jajaja, I remember that event 'cause I'm using a lot now :v.
     
  8. Btw the setxp on respawn does not work i read the java doc and you should use the setnewlevel instead.
    Thanks to you all for your responses