Solved Adding a DeathMessage to my Plugin

Discussion in 'Plugin Development' started by Matthu699, Oct 5, 2013.

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

    Matthu699

    So I’m curious how I would add some custom Death Messages to my plugin so when someone is killed with the /killPlayer It would show like <Player> was exploded in style (Random) and have 5 of these death messages and have them go off in random
    If anyone could help that would be awesome
     
  2. Offline

    MrSparkzz

    Matthu699
    Well essentially there are two ways to go about doing this.

    1. You could create an event with a random number generator and a switch statement.
    2. You could create an onCommand() method and just have it set the player's health to zero then display a random message.
    I would suggest going with the onCommand() method, just because it's easier.

    Code:java
    1.  
    2. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    3. cmd = command.getName();
    4.  
    5. if (cmd.ignoreCase("killplayer") {
    6. if (args.length < 1 || args.length > 1) return true;
    7.  
    8. Player target = Bukkit.getPlayer(args[0]);
    9.  
    10. if (target == null) return true;
    11.  
    12. target.setHealth(0);
    13. Bukkit.broadcastMessage(genRandom());
    14. }
    15. }
    16.  
    17. public String genRandom() {
    18. Random random = new Random();
    19.  
    20. int i = random.netInt(5);
    21.  
    22. switch (i) {
    23. case 0:
    24. return "Death Message 1";
    25. case 1:
    26. return "Death Message 2";
    27. case 2:
    28. return "Death Message 3";
    29. case 3:
    30. return "Death Message 4";
    31. case 4:
    32. return "Death Message 5";
    33. }
    34. }
    35.  

    note: this was not created using an IDE, so there may be a few errors about.
     
  3. Offline

    Matthu699

    Alright I will add this to my existing code. Thanks :)
     
  4. Offline

    MrSparkzz

    Set this thread to Solved.

    Also, something that both you and I should've done from the start was add "@username" to the beginning of our responses. I had posted the response a good 15 minutes before I realized I forgot to push a notification to you.
     
  5. Offline

    Matthu699

    @MrSparkzz
    Okay So this is the code I have so far

    Code:java
    1. package com.hybrah;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public final class KillPlayer extends JavaPlugin {
    11.  
    12. public void onEnable(){
    13. getLogger().info("Has been Enabled!");
    14. }
    15. public void onDisable() {
    16. getLogger().info("Has been Disbaled");
    17. }
    18. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    19. if(cmd.getName().equalsIgnoreCase(“KillPlayer")){
    20. Player target = sender.getServer().getPlayer(args[0]);
    21.  
    22. if (target == null) {
    23. sender.sendMessage(args[0] + " is not online, Try to kill a player that is online.");
    24. return true;
    25. }
    26. float explosionPower = 5F;
    27. target.getWorld().createExplosion(target.getLocation(), explosionPower);
    28. target.setHealth(0);
    29. }
    30.  
    31. public String genRandom() {
    32. Random random = new Random();
    33.  
    34. int i = random.nextInt(5);
    35.  
    36. switch (i) {
    37. case 0:
    38. return "Death Message 1";
    39. case 1:
    40. return "Death Message 2";
    41. case 2:
    42. return "Death Message 3";
    43. case 3:
    44. return "Death Message 4";
    45. case 4:
    46. return "Death Message 5";
    47. }
    48.  
    49. }
    50.  
    51.  
    52.  
    53. }
    54.  


    Does that look sorta correct?
     
  6. Offline

    Henzz

    Matthu699
    Set your custom death messages using the PlayerDeathEvent's setDeathMessage method.
     
  7. Offline

    MrSparkzz

    Matthu699
    On lines 31-49; you need to remove that whole block from your 'onCommand' method. Put it right outside of the line 53 bracket and you should be all set.

    Also, you're missing a bracket, (I think you need another one at the end).

    Let's not get too far ahead of ourselves. We're doing basic stuff right now. Just making a command, kill someone, then give a pre-defined, randomly generated string.

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

    Henzz

    MrSparkzz
    Relax mate, he wanted something to set his custom death messages. So that is another alternative.
     
  9. Offline

    MrSparkzz

    I am relaxed? I was just saying that I was teaching him the easy way.
     
  10. Offline

    Henzz

    MrSparkzz
    I know, I was suggesting a way of setting a custom death message which both works for natural deaths and kill commands.
     
  11. Offline

    MrSparkzz

    Henzz
    I see what you're saying, and I had that in my original response. The only reason I didn't make it for him was because I haven't done much with events in forever, and they might've changed since the last time I used them, as well as the fact that I'm too lazy to figure it out ;)
     
  12. Offline

    Matthu699

    MrSparkzz
    Would their be any return statement?

    I figured it out it would just be Return null;​
    But thank you so much learned a lot to day ​

    @MrSparkzz

    Just curious if it is possible to add to my death messages so it would say <KilledPlayer>DeathMessage

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

    MrSparkzz

    Matthu699
    sorry I didn't get any notifications from you for some reason. Post your updated code, I'll modify it and put comments on the things I changed.
     
  14. Offline

    Matthu699

    @MrSparkzz

    Here you go.

    Code:java
    1. package com.hybrah;
    2.  
    3. import java.util.Random;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public final class KillPlayer extends JavaPlugin {
    12.  
    13. public void onEnable(){
    14. getLogger().info("Has been Enabled!");
    15. }
    16. public void onDisable() {
    17. getLogger().info("Has been Disbaled");
    18. }
    19. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    20. if(cmd.getName().equalsIgnoreCase("KillPlayer")){
    21. Player target = sender.getServer().getPlayer(args[0]);
    22.  
    23. if (target == null) {
    24. sender.sendMessage(args[0] + " is not online, Try to kill a player that is online.");
    25. return true;
    26. }
    27. float explosionPower = 5F;
    28. target.getWorld().createExplosion(target.getLocation(), explosionPower);
    29. target.setHealth(0);
    30. Bukkit.broadcastMessage(genRandom());
    31. }
    32. return false;
    33. }
    34. public String genRandom() {
    35. Random random = new Random();
    36.  
    37. int i = random.nextInt(5);
    38.  
    39. switch (i) {
    40. case 0:
    41. return "Got killed with a Explosive force";
    42. case 1:
    43. return "Randomly Blew Up";
    44. case 2:
    45. return "Decided to go to Heaven";
    46. case 3:
    47. return "Didnt care just did it";
    48. case 4:
    49. return "Mistakes were made";
    50. }
    51. return null;

     
  15. Offline

    MrSparkzz

    Matthu699
    just replace everything in the class with this, UNLESS you have stuff below the genRandom() method, then just add that to the bottom.
    Code:java
    1.  
    2. package com.hybrah;
    3.  
    4. import java.util.Random;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public final class KillPlayer extends JavaPlugin implements CommandExecutor {
    13.  
    14. public void onEnable() {
    15. getCommand("KillPlayer").setExecutor(this); // creates the KillPlayer command
    16.  
    17. getLogger().info("Has been Enabled!");
    18. }
    19. public void onDisable() {
    20. getLogger().info("Has been Disbaled");
    21. }
    22.  
    23. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    24. if(cmd.getName().equalsIgnoreCase("KillPlayer")){
    25. if (args.length != 1) { // checks to see if there's only one argument
    26. sender.sendMessage("You need to enter a playername!") // explains that the code requires just one argument
    27. return true; // stops the code from going any further
    28. }
    29.  
    30. Player target = Bukkit.getPlayer(args[0]);
    31.  
    32. if (target == null) {
    33. sender.sendMessage(args[0] + " is not online, Try to kill a player that is online.");
    34. return true;
    35. }
    36.  
    37. float explosionPower = 5F;
    38. target.getWorld().createExplosion(target.getLocation(), explosionPower);
    39. target.setHealth(0);
    40. Bukkit.broadcastMessage(genRandom());
    41. return true; // tells bukkit that the command worked
    42. }
    43. return false; // tells bukkit that the command didn't work
    44. }
    45. public String genRandom() {
    46. Random random = new Random();
    47.  
    48. int i = random.nextInt(5);
    49.  
    50. switch (i) {
    51. case 0:
    52. return "Got killed with a Explosive force";
    53. case 1:
    54. return "Randomly Blew Up";
    55. case 2:
    56. return "Decided to go to Heaven";
    57. case 3:
    58. return "Didn't care just did it";
    59. case 4:
    60. return "Mistakes were made";
    61. default: // if for whatever reason it returns something other than 0-4
    62. return; // stop the code
    63. }
    64. }
    65.  
    66. /*
    67.   * if you have any other code
    68.   * post it in here.
    69.   */
    70. }
    71.  

    this should work just fine
     
  16. Offline

    Matthu699

    MrSparkzz

    Alright Thank you, Everything works perfectly
     
  17. Offline

    MrSparkzz

    Matthu699
    please set this thread to solved.
     
Thread Status:
Not open for further replies.

Share This Page