Work-Around for lack of EntityDamageByProjectileEvent

Discussion in 'Plugin Development' started by Amaroq, Aug 15, 2011.

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

    Amaroq

    So I used this quite often in my code, for checking if entities were being damaged by arrows. However, since build 1040-ish, it stopped working, since it became depreciated.

    Now a lot of my code doesn't work pertaining to this, and I cannot seem to figure out a workaround. I know it now uses EntityDamageEvent, but have no idea how to use it. Anyone know?

    If you can, just fix up this current batch of code:

    Code:
    if(event instanceof EntityDamageByProjectileEvent)
     
  2. Offline

    escape

    Not tested, but it'd be something like:
    Code:JAVA
    1.  
    2. onEntityDamage(EntityDamageEvent event) {
    3. if(event instanceof EntityDamageByEntityEvent && event.getDamager() instanceof Arrow) {
    4. // code
    5. }
    6. }
    7.  
     
  3. Offline

    Amaroq

    Interesting. Do you know how to use the EntityDamageEvent.getCause() function? Or anyone, really. It'd be extremely helpful to know.
     
  4. Offline

    DrBowe

    I mean, it tells you what caused the damage. Unless you're referring to the syntax, in which case it would be :
    Code:java
    1.  
    2. //Let's say you're checking to see if they were damaged by a fire tick
    3. if(event.getCause() == DamageCause.FIRE_TICK){
    4. //do stuff
    5. }
    6.  


    Out of curiosity, have you ever worked with enumerations?
     
  5. Offline

    Amaroq

    Sorry, I was unspecific. And uh.. No, I have not used Enumerations before, and, sadly, never heard of them until now. Never needed them, until now, apparently. But thank you for explaining the syntax there. I'm relatively new to Java, my current knowledge of programming extends to Lua, PHP, and a bit of Javascript.

    Unless there is more about them I should know?
     
  6. Offline

    DrBowe

    @Amaroq
    I was just curious, because I had the exact same issue when I first started coding plugins. I had no idea what an enumeration was, and could not figure out what the syntax was to retrieve it. :p

    They're definitely a useful asset...sort of like a class that *isn't* a class.

    I'd recommend looking up a few tutorials on their uses, as they can help tremendously if used right.
     
  7. Offline

    Amaroq

    Alright. One last question. How would I go about retrieving the one who caused the EntityDamageEvent? I need to be able to retrieve the one who shot the arrow, as well as the arrow itself. Originally I used .getDamager() and .getProjectile(), but again, these no longer work.
     
  8. Offline

    DrBowe

    @Amaroq
    Code:java
    1.  
    2. public void onEntityDamage(EntityDamageEvent event){
    3. //check for damage by entity (and arrow)
    4. if(event instanceof EntityDamageByEntityEvent){
    5. EntityDamageByEntityEvent nEvent = (EntityDamageByEntityEvent) event;
    6. if(!(nEvent.getDamager() instanceof Arrow)){
    7. return;
    8. }
    9. //This will retrieve the arrow object
    10. Arrow a = (Arrow) nEvent.getDamager();
    11. //This will retrieve the person who shot the arrow
    12. LivingEntity le = a.getShooter();
    13. //do stuff
    14. }
    15. }
    16.  

    Of course, there are probably errors in this...but I believe this should work

    EDIT:
    Also, @escape
    I don't think that code snippet would work, since you never cast EntityDamageByEntityEvent onto the event, therefor it doesn't have a getDamager() method available to it.

    EDIT 2:
    Holy hell, what happened to my post.
    Fix'd
     
  9. Offline

    Amaroq

    AH. So I have to Cast it as an EntityDamageByEntityEvent. I was not aware.

    Goddamn, Bukkit API is complex. -laughs- Thanks again.
     
  10. Offline

    desht

    The Oracle (Sun) Java docs have a good page on enums:

    http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

    Given that I came to Java from C/C++, I was surprised at how much extra you can do with enums in Java. They're pretty powerful.
     
  11. Offline

    Shamebot

    Enums are classes. The enum{} syntax is just some compiler candy to create a class with static fields.
    Code:java
    1. enum material
    2. {
    3. STONE(1),WOOD(5);
    4. private final int id;
    5. material(int id)
    6. {
    7. this.id = id;
    8. }
    9. public int getId()
    10. {
    11. return id;
    12. }
    13. }

    Is the roughly same as
    Code:java
    1. class material extends Enum
    2. {
    3. public static final material STONE = new material("STONE",0,1);
    4. public static final material WOOD = new material("WOOD",1,5);
    5.  
    6. private final int id;
    7. private material(String s,int i, int id)
    8. {
    9. super(s,i);
    10. this.id = id;
    11. }
    12. public int getId()
    13. {
    14. return id;
    15. }
    16. }
     
  12. Offline

    shadowdemonx

    How cute. XD
     
  13. Offline

    IDragonfire

  14. this is the code i use for when i'm doing my snowball stuff, the instance can be an arrow aswell:
    Code:
    if(event.getCause() == DamageCause.PROJECTILE && event instanceof EntityDamageByEntityEvent){
        if(event.getDamager() instanceof Snowball){
                //code
            }
    }
     
Thread Status:
Not open for further replies.

Share This Page