OnDeathEvent

Discussion in 'Plugin Development' started by trikxgaming, Mar 31, 2014.

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

    trikxgaming

    Can someone help me with my plugin!


    I need when someone dies the person who killed them to receieve credits.

    Im stuck and I need help!
     
  2. Offline

    coasterman10

    Use PlayerDeathEvent. getPlayer() returns the player killed, and getPlayer().getKiller() returns the person who killed them.
     
  3. Offline

    Arcticmike

    Here's the code
    Code:java
    1. @EventHandler
    2. public void on(PlayerDeathEvent event){
    3. ((Player)event).getKiller().getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
    4. }
     
  4. Offline

    _Belknap_

    trikxgaming
    Ok, basically, you do this:
    Code:java
    1. @EventHandler
    2. public void onDeath(PlayerDeathEvent e) {
    3. if (e.getEntity() instanceof Player && e.getEntity().getKiller() instanceof Player) {
    4. Player killer = (Player)e.getEntity().getKiller();
    5. Bukkit.broadcastMessage(ChatColor.YELLOW + killer.getName() + ChatColor.GREEN + " died at the hands of " + ChatColor.YELLOW + e.getEntity().getName());
    6. }
    7. }
     
  5. Offline

    trikxgaming

    thanks lol i almost had the same code i just had a simple error thanks!

    thanks!

    can you help? should I not be putting economy in the Event handler or what?
    Code:java
    1. @EventHandler
    2. public void playerDeath(PlayerDeathEvent e) {
    3.  
    4. kits.remove(e.getEntity().getName());
    5. if (e.getEntity() instanceof Player && e.getEntity().getKiller() instanceof Player) {
    6. Player killer = (Player)e.getEntity().getKiller();
    7. EconomyResponse r = econ.depositPlayer(killer, 2);
    8.  
    9. }
    10.  
    11.  
    12. }


    Help?

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

    Wolfey

    trikxgaming If there is an error, how about you tell us what's wrong, or give us a stack trace, or something along those lines.
     
  7. Offline

    _Belknap_

    trikxgaming
    Explain to me what this does, because I am confused.
    EDIT: If you are doing an economy based plugin, I believe that a really simple way to do that is you would create a HashMap storing integers through player names like this:
    Code:java
    1. HashMap<String,Integer> economy = new HashMap<String,Integer> ();

    then, when the player joins the game, store a section in the hashmap for them with
    Code:java
    1. economy.put(player.getName(),0);

    Then you would create custom methods such as deposit() or remove() etc.
    Also, on the onDisable(), you would need to save that hashmap into a config, or else all their money will be reset every time you reload the plugin.
    Then, on PlayerDeathEvent, deposit the 2 dollars/coins/currency into that players storage with
    Code:java
    1. @EventHandler
    2. public void playerDeath(PlayerDeathEvent e) {
    3. kits.remove(e.getEntity().getName());
    4. if (e.getEntity() instanceof Player && e.getEntity().getKiller() instanceof Player) {
    5. Player killer = (Player)e.getEntity().getKiller();
    6. int killmoney = economy.get(killer.getName) + 2;
    7. economy.put(killer.getName(), killmoney);
    8.  
    9. }
    10.  
    11.  
    12. }
     
  8. Offline

    trikxgaming

    I need when the killer kills the player the killer gets 2 credits. Ive been trying the past 2 days
     
Thread Status:
Not open for further replies.

Share This Page