.getLocation() of a dead entity

Discussion in 'Plugin Development' started by Robin Bi, Mar 21, 2014.

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

    Robin Bi

    Hey there, it's me again...


    So i'd like to drop custom items when the player kills an entity.

    This is my Code:
    Code:java
    1. public class EventListener implements Listener {
    2.  
    3. public EventListener(Main plugin) {
    4. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    5. }
    6.  
    7.  
    8. @EventHandler
    9. public void onMobKill(EntityDeathEvent entityKill) {
    10. Entity entity = entityKill.getEntity();
    11. Location deathloc = entityKill.?
    12. }
    13.  
    14.  
    15. }



    My Problem is, that getLocation() doesn't work with a dead entity.
    Is there any way to get the Location of a dead animal without saving the Locations of every single mob?




    Yours sincerely,
    Robin Bi
     
  2. Offline

    Noxyro

    Why is it not working?!
    You even get a LivingEntity from the EntityDeathEvent... to that implies that it's still living when the event is triggered, isn't it?

    Error log maybe?

    Edit 1: Oh and for your problem, you can simply get the drops with event.getDrops() and then modify that collection.
     
  3. Offline

    Robin Bi

    Noxyro You mean
    Code:java
    1. Location deathloc = entityKill.getLocation();
    should work? Well Eclipse tells me that getLocation() is undefined for EntityDeathEvents.
    Propably it's something easy, don't blame me, i'm new to bukkit ^^



    Yours sincerely,
    Robin Brix
     
  4. Offline

    drtshock

    Robin Bi just modify the getDrops collection like Noxyro said :) No need for getting the entity's location then.
     
  5. Offline

    Robin Bi

    Oh I didn't really get that Noxyro 's code would change the drop, just thought it would "cacel" the death...
    I'm gonna try it and tell you whether it worked or not :)


    EDIT: Oh dear god... I saw his signature and thought it'd be a part of the post.... Another evidence of a bukkit(.org)-noob's noobyness.


    Yours sincerely,
    Robin Bi
     
  6. Offline

    GameplayJDK

    Robin Bi
    Noxyro Doesn't change the drops. As you said, he is cancelling the entitys death if it was cause by "suicide".
     
  7. Offline

    Robin Bi

    GameplayJDK Yeah and it's only his signature... He wanted me to do .getDrops(); Anyways, I'll have to find out how to set the drops then ^^ Whatever, that should be possible via Google :)


    Yours sincerely,
    Robin Bi
     
  8. Offline

    GameplayJDK

    Robin Bi
    To remove all drops do getDrops().clear();
    and then you can add drops getDrops().add(new ItemStack(Material.DIAMOND, 64));
     
  9. Offline

    Robin Bi

    GameplayJDK Oh, not having to google it gives me more time to programm :) Thanks! Do you also know how to have custom item names?




    Yours sincerely,
    Robin Bi
     
  10. Offline

    NonameSL

    Ahh, I see.
    You are trying to do e.getLocation() to get the location of the event.
    What you should do is entityKill.getEntity().getLocation(); to get the location of the entity.

    Although, it will be much simpler to just add to the drops the itemstacks you want.
     
  11. Offline

    GameplayJDK

    Robin Bi
    You need to manipulate the ItemStack ItemMeata for that:
    Code:java
    1.  
    2. getDrops().clear();
    3. getDrops().add(new ItemStack(Material.DIAMOND, 64).getItemMeta().setDisplayName(ChatColor.RED + "These diamonds are evil!"));
    4.  
     
    Robin Bi and mazentheamazin like this.
  12. Offline

    Robin Bi

    Yeah, seems legit. :) Concerning to the cheerful helpers above, I'm currently adding the ItemDrops. I'm looking for a way for custom item names though.

    EDIT: GamePlayJDK was faster :D

    Yours sincerely,
    Robin Bi

    GameplayJDK, a HUGE thank you! :)


    Yours sincerely,
    Robin Bi

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

    GameplayJDK

  14. Offline

    Robin Bi

    GameplayJDK I'm afraid i didn't really get your solution :confused:
    Eclipse underlines .add at Event-Name-Given-By-Me.getDrops().add(new ItemStack(Material.ANDSOON); and tells me that the method add(ItemStack) in the type List<ItemStack> is not applicable for the arguments (void).

    What argument is necessary then?



    Yours sincerely,
    Robin Bi
     
  15. Offline

    GameplayJDK

    Robin Bi
    Seems that it does not accept the inline modification of the ItemStack ItemMeta..
    So do this:
    Code:java
    1.  
    2. ItemStack evildiamonds = new ItemStack(Material.DIAMOND, 64);
    3. evildiamonds.getItemMeta().setDisplayName(ChatColor.RED+"Evil diamonds");
    4. getDrops().add(evildiamonds);
    5.  
     
    Robin Bi likes this.
  16. Offline

    Robin Bi

    GameplayJDK Unfortunately that didn't work as well. The mob i wanted to drop that special item drops that special item, but without my custom name... Also, the mob always drops only one item, even though i've given the int "dropcount" as item count and iniciated that int before.

    Code:java
    1. int dropcount;
    2. dropcount = 1 + (int)(Math.random() * ((3 - 5) + 1));



    Do you know what i did wrong again? ^^

    (If you want you could join me on TeamSpeak so we can discuss that easier...)



    EDIT: Solved the random thingy myself, 3 - 5 should've been 3 - 1. Name's still not working.





    Yours sincerely,
    Robin Bi
     
  17. Offline

    GameplayJDK

    Robin Bi
    I prefer to use "new Random().nextInt()" instead of Math.random(). I don't like math ^^;
    There is also a method to drop items naturally at a certain location.. I think it was Location.dropItemNaturally(...) or something like that..
    The thing with the name....
    Code:
    ItemStack blah = new ItemStack(Material.IRON_PICKAXE, 64);
    ItemMeta blahmeta = blah.getItemMeta();
    blahmeta.setDisplayName("blahblah");
    blah.setItemMeta(blahmeta);
    
     
    Robin Bi likes this.
  18. Offline

    Robin Bi

    GameplayJDK the name works now, too :) Thanks for your time.



    Yours sincerely,
    Robin Bi
     
  19. Offline

    GameplayJDK

Thread Status:
Not open for further replies.

Share This Page