Solved Custom DamageCause.

Discussion in 'Spigot Plugin Development' started by Marido, Jun 14, 2018.

  1. Hello.

    I have been expirimenting with DamageCause in the Spigot API.

    There is a caster that casts a trail towards an another player with damage, and if the player dies by it's damage a death message will be output:
    "Player died."

    The thing I want to do is to have this:
    "Player was killed by Caster using Trail"

    I've been trying to create a custom EntityDamageByEntityEvent and call the event when the player gets damaged, but that didn't work.

    Any suggestions or help?

    Damage is being applied to the entity with:
    Code (Text):
  2. What I do in my own plugins is use the player#damage(10,damager) to indicate the caster damaged that player.

    Then, I attatch to the hurt entity some metadata using the built in bukkit methods in order to save the name of the item that attacked them and the system time in milliseconds.

    Then, in the player death event, I check if they have that special metadata and if they do, compare the system time I captured then to what I have now. If the gap is small enough, I can conclude they were killed by that item. So I edit the death message using getPlayer#getKiller for the player name and the metadata you stored earlier for the name of the item.

    Let me know if you need any help implementing this. I use this technique myself and it works great!
  3. Smart, but wouldn't it be useless to apply a metadata to a player since you could try to get the item in the hand from the damager (and check if its not null ofc).
  4. NathanWolf


    I don't think you can truly make custom DamageCause.

    Do players use an item to cast spells? If so you can call damage(amount, entity), passing in the caster as the damaging entity. Then the vanilla death message would say "Player was killed by Caster using Item"

    Otherwise I think if you want truly custom .... do something like what the poster above said (ninja'd) :)

    Basically just track it yourself and make a custom death message.
  5. In my case, my "spells" didn't need items sometimes. And also, some do damage over a period of time giving a chance they will not still be holding the item. This is the safest approach to guarantee the correctness of the item used compared to that approach.
  6. Thats a really nice one, all this time haven't thought about damage(amount, entity) but also never had to make something like that but i will in future plugins that i must.
    I guess this works for custom killing types like particles and spells, in something that it wouldn't catch the killer,am i correct?
  7. Thanks for the awesome quick help, for everyone browsing about the same issue, just use:
    Code (Text):
    player#damage(amount, damager);
    It will automatically add the item in the death message if the damager holds an item.
    • Friendly Friendly x 1
  8. Thats a really cool one.