[Unsolved] How to create death message

Discussion in 'Plugin Development' started by ASHninja1997, Aug 3, 2013.

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

    ASHninja1997

    I am trying to create a death message and I looked up several pages on the forums for this but nothing I found worked. So this is the code I have so far and it doesn't work
    Code:java
    1. @EventHandler
    2. public void onPlayerDeathEvent(PlayerDeathEvent event) {
    3. shootFireworks();
    4. EntityDamageEvent dc = event.getEntity().getLastDamageCause();
    5. if(dc instanceof EntityDamageByEntityEvent) {
    6. if(((EntityDamageByEntityEvent)dc).getDamager() instanceof Arrow) {
    7. Arrow arrow = (Arrow) ((EntityDamageByEntityEvent)dc).getDamager();
    8. if(arrow.getShooter() instanceof Player) {
    9. Player shooter = (Player) arrow.getShooter();
    10. event.setDeathMessage(ChatColor.GOLD + event.getEntity().getDisplayName() + ChatColor.YELLOW + " Sniped " + shooter.getDisplayName());
    11. }}}}

    It gives me a error in the console but it doesn't actually give me the error to the Death Event instead it gives me a error of one of my other plugins. The error it gives is not problem and is not related to this..I am telling you so you don't have to ask for the error because I don't know.
     
  2. Offline

    Pew446

    This is untested, but try:

    Code:
    @EventHandler
        public void onPlayerDeathEvent(PlayerDeathEvent event) {
            shootFireworks();
            EntityDamageEvent dc = event.getEntity().getLastDamageCause();
            if(dc.getEntity() instanceof Arrow)
            {
                Arrow arrow = (Arrow)dc.getEntity();
                if(arrow.getShooter() instanceof Player)
                {
                   
                    Player shooter = (Player) arrow.getShooter();
                    event.setDeathMessage(ChatColor.GOLD + event.getEntity().getDisplayName() + ChatColor.YELLOW  + " Sniped " + shooter.getDisplayName());
                }
            }
        }
     
  3. Offline

    ASHninja1997

    Pew446
    Thank you I will try this

    Pew446
    No errors popped up but it doesn't broadcast the message all it says is "ASHninja1997 was shoot by Arrow"

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

    rippin

    Did you register the event? You might of forgotten.
     
  5. Offline

    ASHninja1997

    rippin
    I was looking at other post's on this but I always get confused when registering the event.....can you show me how to?
     
  6. Offline

    rippin

    Something like this in your onEnable

    Code:java
    1. PluginManager pm = this.getServer().getPluginManager();
    2.  
    3. pm.registerEvents(new YourListener(this), this);
    4.  

    (YourListener is the name of your listener class)
    If you don't have an event class and it's in your main class just replace the class (YourListener(this)) with "this"
     
  7. Offline

    ASHninja1997

    rippin
    I have this instead of that will it work.
    Code:
    getServer().getPluginManager().registerEvents(this, this);
     
  8. Offline

    Pew446


    Does "this" implement Listener? Make sure the first argument is the Listener and the second is the Plugin. Why don't you test your events by adding a System.out.println("Hello World"), or even sending the player a message. Try something very simple to make sure it works.
     
  9. Offline

    rippin

    Yes you need to implement listener as well.

    Code:java
    1. public class YOURCLASS extends JavaPlugin implements Listener
     
  10. Offline

    JPG2000

  11. What you can try,

    Code:java
    1.  
    2. if(event.getEntity().getKiller() instanceof Player && event.getEntity().getKiller().getItemInHand().equals(Material.BOW){
    3. event.setDeathMessage("SHOT! :D");
    4. }
    5. }
    6.  

    EDIT2: New idea!
    Untested, but it should work. The only reason it shouldn't is if they switch items right or before the event is called(Which should be impossible).
     
  12. Offline

    phips99

    Code:java
    1.  
    2. }}}}


    WTF?! Are you using Eclipse as your IDE?
     
Thread Status:
Not open for further replies.

Share This Page