Easy kill reward code (Need help)!

Discussion in 'Plugin Development' started by FirecatHD, Nov 14, 2013.

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

    FirecatHD

    I do know a little about this like
    Code:java
    1. @EventHandler
    2. player.deathEvent get.killer


    And that you have to check if it is a player and stuff like that (the code is invalid, i know that) i want it to give a player £50 for killing + Sending the killer a message + playing a tone/sound to the killer.

    Remember, i dont ask if someone can code the plugin for me, i just want to know how, because it will be merged into an another plugin i am making.

    So i just need a little code to start with or something like that, so if you know how to do this, please post an answer!

    Thanks.

    Tags: sgavster L33m4n123 SkillSam
     
  2. Offline

    sgavster

    Code:java
    1. @EventHandler
    2. public void onDeath(PlayerDeathEvent e) {
    3. Player p = e.getEntity();
    4. if(p.getKiller() instanceof Player) {
    5. Player d = p.getKiller();
    6. //do stuff with d
    7. }
    8. }
     
    FirecatHD likes this.
  3. Offline

    FirecatHD

    sgavster

    Thanks man, i will have a go at it :D
     
  4. Offline

    drtshock Retired Staff

    You'll want to check if the killer is null otherwise you'll get an NPE every time someone commits suicide. Also check if it's a player :)
     
    FirecatHD likes this.
  5. Offline

    FirecatHD

    sgavster

    Ok i have it working, but sometimes the console prints out some nasty errors :confused:
    It does not affect the plugin, and it sill works but i would like to fix the errors before moving on with the plugin.

    Source code:
    Code:java
    1. package me.FirecatHD.TBS_PvP;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Sound;
    5. import org.bukkit.entity.LivingEntity;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.EntityDeathEvent;
    10.  
    11. public class DeathListener implements Listener {
    12.  
    13. @EventHandler
    14. public void onDeath(EntityDeathEvent e) {
    15. LivingEntity p = e.getEntity();
    16. //if(p.getKiller() instanceof Player) {
    17. Player d = p.getKiller();
    18. {
    19. d.playSound(p.getKiller().getLocation(), Sound.EXPLODE, 5, 1);
    20. d.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "TBS PvP" + ChatColor.BLACK + "]: " + ChatColor.GREEN + "You " + ChatColor.RED + "killed " + ChatColor.GREEN + "a Player!");
    21. }
    22. }
    23. }
    24.  


    And yes, i have registrered the event in the main class.
    So could the problem be what drtshock said, or something else?

    If someone can help me, please do it!

    Thanks.
     
  6. Offline

    L33m4n123

    you have to check if p.getKiller is instanceof Player as you commented out. Because if a Skelleton kills someone it gets an error there
     
    FirecatHD likes this.
  7. Offline

    FirecatHD

    L33m4n123

    Yes i have modified the code a little for debugging so therefor i commented it out, but i think i know the error so i am gonna try what i can imagine it is.

    Il report as soon as i find something out ;)

    Ok it works thanks everyone!

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

    sgavster

    Why did you comment out if the player is a killer, thats why you got errors..
     
    FirecatHD and L33m4n123 like this.
  9. Offline

    FirecatHD

    sgavster

    It was because iwas testing the code on entitys instead of players, but it works now.

    -----------------------------------------------------------------------------------------------------------------------

    But here is my next question: I want to play a exploding RED firework on the deathEvent (You know at Quake you kill a player an the player explodes) thats how i want it.

    Here is my code:
    Code:java
    1. package me.FirecatHD.TBS_PvP;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Sound;
    5. import org.bukkit.entity.LivingEntity;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.EntityDeathEvent;
    10.  
    11. public class DeathListener implements Listener {
    12.  
    13. @EventHandler
    14. public void onDeath(PlayerDeathEvent e) {
    15. LivingEntity p = e.getEntity();
    16. if(p.getKiller() instanceof Player) {
    17. Player d = p.getKiller();
    18. {
    19. d.playSound(p.getKiller().getLocation(), Sound.EXPLODE, 5, 1);
    20. d.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "TBS PvP" + ChatColor.BLACK + "]: " + ChatColor.GREEN + "You " + ChatColor.RED + "killed " + ChatColor.GREEN + "a Player!");
    21. }
    22.  
    23. }
    24. }
    25. }


    So i hope someone can help me and thanks in advance!

    Tags: L33m4n123 SkillSam
     
  10. Offline

    superjesse07

    FirecatHD do this
    Code:java
    1. package me.FirecatHD.TBS_PvP;
    2.  
    3. import org.bukkit.ChatColor;
    4. import org.bukkit.Sound;
    5. import org.bukkit.entity.LivingEntity;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.EntityDeathEvent;
    10.  
    11. public class DeathListener implements Listener {
    12.  
    13. @EventHandler
    14. public void onDeath(PlayerDeathEvent e) {
    15. LivingEntity p = e.getEntity();
    16. if(p.getKiller() instanceof Player) {
    17. Player d = p.getKiller();
    18. {
    19. d.playSound(p.getKiller().getLocation(), Sound.EXPLODE, 5, 1);
    20. d.sendMessage(ChatColor.BLACK + "[" + ChatColor.DARK_RED + "TBS PvP" + ChatColor.BLACK + "]: " + ChatColor.GREEN + "You " + ChatColor.RED + "killed " + ChatColor.GREEN + "a Player!");
    21. Firework fw = (Firework) p.getWorld().spawn(p.getLocation(), Firework.class);
    22. FireworkEffect effect = FireworkEffect.builder().trail(true).flicker(true).withColor(Color.RED).with(Type.BURST).build();
    23. FireworkMeta fwm = fw.getFireworkMeta();
    24. fwm.clearEffects();
    25. fwm.addEffect(effect);
    26. Field f;
    27. try {
    28. f = fwm.getClass().getDeclaredField("power");
    29. f.setAccessible(true);
    30. f.set(fwm, -2);
    31. } catch (NoSuchFieldException e) {
    32. // TODO Auto-generated catch block
    33. e.printStackTrace();
    34. } catch (SecurityException e) {
    35. // TODO Auto-generated catch block
    36. e.printStackTrace();
    37. // TODO Auto-generated catch block
    38. e.printStackTrace();
    39. } catch (IllegalAccessException e) {
    40. // TODO Auto-generated catch block
    41. e.printStackTrace();
    42. }
    43. }
    44.  
    45. }
    46. }
    47. }
    it should work
     
  11. Offline

    FirecatHD

    superjesse07

    Hmm it fires of a firework, but it does not explode :oops:
    Also i want it to explode right away, dont shoot up in the air first.

    Anyways thanks for the help, and if you know how to solve this i would be very grateful :)
     
  12. Offline

    Garris0n

    Did you really just spoonfeed a new coder reflection? facepalm

    Also, why the reflection? Does setting the power to 0 not work?
     
  13. Offline

    superjesse07

    garrison no it doesn't work it shoots it up in the air but FirecatHD for me its working fine whats the error or do you not get a error
     
  14. Offline

    FirecatHD

    superjesse07 and@Garris0n

    Nope i dont get any errors, but i think this is a bit advanced for me right now (I have only been coding for 4 days...)
    So i found out i could use a lightning strike instead, so i will use that until i learn this :D

    Anyways, thanks to you both for helping!
     
  15. Offline

    superjesse07

    no problem
     
Thread Status:
Not open for further replies.

Share This Page