Custom Kill Messages + Sound

Discussion in 'Plugin Development' started by 4non, Jul 7, 2013.

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

    4non

    This thread should explain everything I'm trying to do for my server: http://forums.bukkit.org/threads/br...en-specific-player-joins.157528/#post-1738095


    Anyways, when my friend (Skeleton_o_Dhuum) kills someone, I want a custom death message to appear. I want the vanilla "Player was slain by Player" to be removed, and I want my own custom message to appear, along with a sound.

    I know how to play the sound, I know about PlayerDeathEvent, but I'm kind of confused on how to make a specific message play when a specific player kills another player.
    Here's an example (I know it's not going to work)
    Code:java
    1. if (event.getPlayer().getname().equalsIgnoreCase("Skeleton_o_Dhuum")){
    2. if (event.PlayerDeathEvent)
    3. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "[Player] has been taken...");
    4. for(Player p : Bukkit.getOnlinePlayers()){
    5. p.playSound(p.getLocation(), Sound.CAVE_7, 1, 0);

    The sound I want is Cave 7 found on http://www.minecraftwiki.net/wiki/Ambience
     
  2. Offline

    iFamasssxD

    You need to get the cause of the death and get the current death message that will be sent. Cancel that then send out a new one.
     
  3. Offline

    4non

    So pretty much this? (minus the death message cancelling.)
    Code:java
    1. package net.malicepvp.malicepvp;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Sound;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.event.Listener;
    9. import org.bukkit.event.entity.PlayerDeathEvent;
    10.  
    11. public class reap
    12. implements Listener
    13. {
    14. public static MalicePvP plugin;
    15.  
    16. public reap(MalicePvP instance)
    17. {
    18. plugin = instance;
    19. }
    20.  
    21. @EventHandler
    22. public void onPlayerDeath(PlayerDeathEvent event)
    23. {
    24. String message = event.getDeathMessage();
    25. if (message.contains("was slain by Skeleton_o_Dhuum")) {
    26. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "Someone has been taken...");
    27. for(Player p : Bukkit.getOnlinePlayers()){
    28. p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 0);
    29. }
    30.  
    31. if (message.contains("was shot by Skeleton_o_Dhuum")) {
    32. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "Someone has been taken...");
    33. for(Player p : Bukkit.getOnlinePlayers()){
    34. p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 0);
    35. }
    36.  
    37. if (message.contains("was killed by Skeleton_o_Dhuum")) {
    38. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "Someone has been taken...");
    39. for(Player p : Bukkit.getOnlinePlayers()){
    40. p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 0);
    41. }
    42. }
    43. }
    44. }
    45. }
    46. }

    Oh, and how do you add the victim's name? (ex: %player%)
     
  4. Offline

    iFamasssxD

    Player victim = event.getEntity();
    String vitcimsname = victim.getName()

    EDIT: A good source to look at for this type of thing is to look into the source of HeroicDeath.
     
    4non likes this.
  5. Offline

    4non

    I was looking at Kivi, I though HeroicDeath was discontinued..
    Anyways, since the sound and message are only supposed to play if he kills, am I supposed to stick this:
    Code:
    if(event.getPlayer().getName().equalsIgnoreCase("Skeleton_o_Dhuum")){
    in somewhere or am I supposed to do this:
    Code:
     if (message.contains("was shot by Skeleton_o_Dhuum")) {
    *Edit: Oh and there's a red line under "event" in event.getEntity();
     
  6. Offline

    iFamasssxD

    Well getPlayer gets who is killed not who is killing.
    In order to get the killer you need to check for it:
    Code:
    event.getEntity().getKiller();
    As for the messages I am not too sure.
     
  7. Offline

    4non

    You have no idea how much that helps xD
    Anyways, I'm going to be afk for a few hours.
    Before I go, one more thing, do you have a fix for "event cannot be resolved"?
     
  8. Offline

    iFamasssxD

    You need to set killer as a player.
    Code:
    Player killer = event.getEntity().getKiller();
     
  9. Offline

    4non

    I already did that, event still cannot be resolved.

    Code =
    Code:java
    1. package net.malicepvp.malicepvp;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Sound;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.Event;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.PlayerDeathEvent;
    11.  
    12. public class reap
    13. implements Listener
    14. {
    15. public static MalicePvP plugin;
    16.  
    17. public reap(MalicePvP instance)
    18. {
    19. plugin = instance;
    20. }
    21. Player killer = event.getEntity().getKiller();
    22. Player victim = event.getEntity();
    23. String vitcimsname = victim.getName();
    24. @EventHandler
    25. public void onPlayerDeath(PlayerDeathEvent event)
    26. {
    27. String message = event.getDeathMessage();
    28. if (message.contains("was slain by Skeleton_o_Dhuum")) {
    29. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "Someone has been taken...");
    30. for(Player p : Bukkit.getOnlinePlayers()){
    31. p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 0);
    32. }
    33.  
    34. if (message.contains("was shot by Skeleton_o_Dhuum")) {
    35. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "Someone has been taken...");
    36. for(Player p : Bukkit.getOnlinePlayers()){
    37. p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 0);
    38. }
    39.  
    40. if (message.contains("was killed by Skeleton_o_Dhuum")) {
    41. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "Someone has been taken...");
    42. for(Player p : Bukkit.getOnlinePlayers()){
    43. p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 0);
    44. }
    45. }
    46. }
    47. }
    48. }
    49. }

    iFamasssxD

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

    iFamasssxD

    I copied the code you have and I dont get any errors so im not sure what the problem is.
     
  11. Offline

    macguy8

    iFamasssxD

    Er.... what? Some of the variables in this class (for example, event in the first class variable) have not been initalized.

    @OP You need to create the variables in the method, not in the class. Here's some tips: You have to cancel out the death message and replace it with yours, not just broadcast yours. Instead of broadcasting the new death message, change the old one (event.setDeathMessage("Someone died")) Normally, I hate giving people code, but you seem like you need it.

    Code:
    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent event) {
        if (event.getEntity().getName().equalsIgnoreCase("PersonYouWantToDoStuffWith")) { // If the person is the person you want to do something special with.
            event.setDeathMessage("PersonYouWantToDoStuffWith just died!"); // Change the death message to what you want.
     
            // Play your death noise here.
        }
    }
     
  12. Offline

    4non

    That code would work great for something that I've already figured out.. My friend isn't supposed to have a death message in what I've already made, but a custom message and noise that displays when he kills someone, and only when he kills someone. When other players kill each other, well, I have other plugins dealing with that.
     
  13. Offline

    macguy8

    4non
    So this thread is solved or there's still an issue at hand?
     
  14. Offline

    4non

    It's not working, but the code I made earlier that iFamasssxD helped me with works. So I guess half of the plugin is working.

    In case anyone was wondering, it's still not working...
    iFamasssxD the only error is that event cannot be resolved.

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

    xTrollxDudex

    4non
    Ugh put Player killer = ... INSIDE the event method
     
  16. Offline

    4non

    Would sticking
    Code:
    private PlayerDeathEvent event;
    work?
     
  17. Offline

    xTrollxDudex

    4non
    Just let me do it for you:
    Code:java
    1. package net.malicepvp.malicepvp;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.Sound;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.Event;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.PlayerDeathEvent;
    11.  
    12. public class reap
    13. implements Listener
    14. {
    15. public static MalicePvP plugin;
    16.  
    17. public reap(MalicePvP instance)
    18. {
    19. plugin = instance;
    20. }
    21.  
    22. @EventHandler
    23. public void onPlayerDeath(PlayerDeathEvent event)
    24. {
    25. //I MOVED THESE FOR YOU
    26. Player killer = event.getEntity().getKiller();
    27. Player victim = event.getEntity();
    28. String vitcimsname = victim.getName();
    29. //don't declare fields dealing with the event before it, put it inside
    30.  
    31. String message = event.getDeathMessage();
    32. if (message.contains("was slain by Skeleton_o_Dhuum")) {
    33. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "Someone has been taken...");
    34. for(Player p : Bukkit.getOnlinePlayers()){
    35. p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 0);
    36. }
    37.  
    38. if (message.contains("was shot by Skeleton_o_Dhuum")) {
    39. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "Someone has been taken...");
    40. for(Player p : Bukkit.getOnlinePlayers()){
    41. p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 0);
    42. }
    43.  
    44. if (message.contains("was killed by Skeleton_o_Dhuum")) {
    45. Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "Someone has been taken...");
    46. for(Player p : Bukkit.getOnlinePlayers()){
    47. p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 0);
    48. }
    49. }
    50. }
    51. }
    52. }
    53. }
     
  18. Offline

    4non

    Code:java
    1.  
    2.  
    3. [CODE]These aren't being used
    4. VVVVVVVVVVVVVVVVVVVVVVVVVVV
    5. Player killer = event.getEntity().getKiller();
    6. Player victim = event.getEntity();
    7. String vitcimsname = victim.getName();[/CODE]
    8.  
    9. Oh and the code doesn't work :p
    10.  
    11.  
    12.  
    13. ^WHY WAS THAT A PART OF THE CODE.
     
  19. Offline

    Techy4198

    Code:
    package net.malicepvp.malicepvp;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Sound;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.PlayerDeathEvent;
     
    public class reap implements Listener {
      public static MalicePvP plugin;
     
      public reap(MalicePvP instance) {
        plugin = instance;
      }
     
      @EventHandler
      public void onPlayerDeath(PlayerDeathEvent event) {
        Player killer = event.getEntity().getKiller();
        String killersName = killer.getName();
     
        String message = event.getDeathMessage();
        if (killersName == "Skeleton_o_Dhuum")) {
          Bukkit.getServer().broadcastMessage(ChatColor.AQUA + "[" + ChatColor.GRAY + "???" + ChatColor.AQUA + "] " + ChatColor.BLACK + "Someone has been taken...");
          for(Player p : Bukkit.getOnlinePlayers()){
            p.playSound(p.getLocation(), Sound.WITHER_DEATH, 1, 0);
          }
        }
      }
    }
     
  20. Offline

    WhatAaCow

    @Techy4198
    Why getting death message? I think you want so disable deathMessage so use:
    Code:java
    1. event.setDeathMessage("");
     
  21. Offline

    4non

    Error: Type mismatch: cannot convert from void to String
     
  22. Offline

    Bart

    event.setDeathMessage(null); will stop it from displaying a death message, if you do an empty string then Bukkit will still broadcast an empty message.
     
  23. Offline

    Technius

    Why don't you just change the death message instead of broadcasting a new one?
     
  24. Offline

    4non

    Type mismatch: cannot convert from void to String
     
  25. Offline

    Bart

    That must be because of something else.. I do event.setDeathMessage(null) all the time. You could also do event.setDeathMessage("your custom message")
     
  26. Offline

    4non

    So instead of broadcasting the message I should set his death message?
     
  27. Offline

    cheer60823

    4non setting his death message would make it so if he died at all, by lava or you, it would say the same thing.
     
  28. Offline

    4non

    oh.
    the point of this is so that whenever he kills someone a message plays.. not when he dies.
     
  29. Offline

    cheer60823

    4non When I said 'he' I meant the victimized player.
     
  30. Offline

    4non

    oh.
     
Thread Status:
Not open for further replies.

Share This Page