onPlayerDeath, but more detailed

Discussion in 'Plugin Development' started by mattmcmullen1, Jul 27, 2013.

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


    Is there a way to have an event when a player gets killed by a mob, does /suicide, drowns, falls to their death, etc.? Basically, the onPlayerDeath event is not as detailed as I would like it to be, and I want different things to happen when a player dies certain ways. Is there a way I could do this?
  2. Offline


    I suppose you could parse the death message from the event.

    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onPlayerDeath(PlayerDeathEvent e) {
    3. String msg = e.getDeathMessage();
    4. msg = msg.substring(msg.indexOf(" "));
    5. if (msg.equals("fell to their death")) {
    6. //code
    7. } else if (msg.equals("drowned")) {
    8. //code
    9. }
    10. ....etc

    Of course those aren't the actual strings, but you get the idea.
  3. Offline


    Just trying to change my code, I realized that my event is PlayerRespawn. Any way I could do it as that event?

    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    2. Player player1 = (Player) sender;
    3. if(commandLabel.equalsIgnoreCase("nwiversion")){
    4. player1.sendMessage(ChatColor.RED + "NotWorthIt Version: 1.1");
    5. }
    6. if(commandLabel.equalsIgnoreCase("motd")){
    7. player1.sendMessage(getConfig().getString("themotd"));
    8. }
    9. return false;
    10. }
    11. @EventHandler
    12. public void onPlayerRespawn(PlayerRespawnEvent event){
    13. final Player player = event.getPlayer();
    14. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new BukkitRunnable(){
    15. public void run(){
    16. player.setHealth(10.0);
    17. player.setFoodLevel(10);
    18. player.sendMessage(ChatColor.RED + "You have died and respawned with half health.");
    19. }
    20. }, 18);
    21. }
    22. }

    So, here's my plugin code. What do I have to do if a player falls to their death, and I want:
    Health set to 10
    Food level set to 10

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
    Last edited by a moderator: Jun 3, 2016
  4. Offline


    How does a player have 10 hearts at death? Elaborate please
  5. Offline


    xTrollxDudex when coding, health set to 10 means 5 hearts in game? Same with food level

    EDIT: realized what you actually asked, but earlier I said --when editing my code, I realized the event is onPlayerRespawn, not onPlayerDeath--
  6. Offline


    xTrollxDudex mattmcmullen1 would a check for if player instance of work? Ex if player istanceof player if player instanceof player && player instance of arrow
Thread Status:
Not open for further replies.

Share This Page