Reset XP on death with keepInventory

Discussion in 'Plugin Development' started by Darkuwu, Nov 1, 2022.

Thread Status:
Not open for further replies.
  1. Offline

    Darkuwu

    Hello, I have this server with keepInventory enabled in the nether and end, but not in the overworld. I would like to reset all the XP you have if you die in the nether or the end, but still keep your inventory

    This is what I tried:

    Code:
        @EventHandler
        public void onRespawn(PlayerRespawnEvent event) {
           Player p = event.getPlayer();
             p.setExp(0);
        }
    However, this doesn't seem to work, as I kept my levels upon dying in the nether

    EDIT: Forgot to say I'm a complete noob at making plugins, I'm honestly not aiming to improve, I'm just looking for a solution to this issue
     
  2. Online

    timtower Administrator Administrator Moderator

    Kars likes this.
  3. Offline

    Darkuwu

    I don't know how to check that...
     
  4. Online

    timtower Administrator Administrator Moderator

    System.out.println, just send a message.
     
  5. Offline

    Darkuwu

    I'm sorry, but I still don't get it. Could you help me further? I'm willing to put actual work into it, the way I put it above seemed like I expected somone to do all the work for me and I apologize if you felt that way
     
  6. Online

    timtower Administrator Administrator Moderator

    You put a System.out.println in the method you posted, on the first line, to see if it is getting triggered.
     
  7. Offline

    Strahan

    If you need it explicitly spelled out:
    Code:
    @EventHandler
    public void onRespawn(PlayerRespawnEvent event) {
      System.out.println("respawn event fired");
      Player p = event.getPlayer();
      p.setExp(0);
    }
     
  8. Offline

    Darkuwu

    Thanks for the help, howhever there was no message. the plugin seems to load fine
     
  9. Online

    timtower Administrator Administrator Moderator

    Then you didn't register the event.
     
  10. Offline

    Darkuwu

    I eventually got my ass to work and did this:

    Code:
    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import java.util.logging.Level;
    
    public class Plugin extends JavaPlugin implements Listener {
    
        @Override
        public void onLoad() {
            super.onLoad();
            this.getLogger().log(Level.INFO, "Plugin chargé");
        }
    
        @Override
        public void onEnable() {
            super.onEnable();
            this.getServer().getPluginManager().registerEvents(new XPDeathListener(), this);
        }
    
        @Override
        public void onDisable() {
            super.onDisable();
        }
    
        public class XPDeathListener implements Listener {
    
            @EventHandler
            public void onPlayerDeath(PlayerDeathEvent event) {
                Player player = event.getEntity();
                player.setTotalExperience(0);
                Bukkit.getLogger().info(player.getName() + " lost his XP upon his death");
            }
        }
    }
    There is a message in the console whenever a player dies, however, still no XP removed
     
  11. Online

    timtower Administrator Administrator Moderator

    Clear the xp drop and on playerrespawn set the xp to 0
     
  12. Offline

    Darkuwu

    I don't really need cleaning xp drop since I only want the plugin to remove XP in dimensions with keepInventory enabled (I have it enabled in the nether and the end, but not overworld)

    With that said I finally got this plugin to work properly


    Code:
        @EventHandler
        public void onPlayerRespawn(PlayerRespawnEvent event) {
            Player player = event.getPlayer();
            player.setExp(0);
            player.setLevel(0);
            Bukkit.getLogger().info(player.getName() + " a perdu toute son XP lors de sa réapparition dans le jeu.");
        }
    For some reason, "setTotalExperience" didn't work, so I had to reset levels and xp points separately
     
  13. Offline

    gochi9

    You should get the world the player is in when they died and use getGameRuleValue to check if it has KeepInventory true and if it does remove the exp
     
  14. Offline

    Darkuwu

    So, the plugin works, except it also clears xp when someone takes the end portal back to the overworld, I can't do much right now, anyone willing to edit that bit of code to fix the issue?
     
Thread Status:
Not open for further replies.

Share This Page