Question about onEntityDamage/onEntityDamageByProjectile

Discussion in 'Plugin Development' started by nossr50, Mar 28, 2011.

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

    nossr50

    In my plugin I have the following code
    Code:
    /*
                     * COUNTER ATTACK STUFF
                     */
                    if(mcPermissions.getInstance().swords(defender)
                            && mcm.getInstance().isSwords(defender.getItemInHand())){
                        boolean isArrow = false;
                        if(event instanceof EntityDamageByProjectileEvent){
                            if(((EntityDamageByProjectileEvent) event).getProjectile().toString().equals("CraftArrow")){
                                isArrow = true;
                                //DEBUG
                                defender.sendMessage("[event = Damage by projectile] - isArrow = "+isArrow);
                            }
                        }
                        if(isArrow == false){
                            defender.sendMessage("isArrow ="+isArrow);
                            if(mcUsers.getProfile(defender).getSwordsInt() >= 600){
                                if(Math.random() * 2000 <= 600){
                                    mcCombat.getInstance().dealDamage(f, event.getDamage() / 2);
                                    defender.sendMessage(ChatColor.GREEN+"**COUNTER-ATTACKED**");
                                    if(f instanceof Player)
                                        ((Player) f).sendMessage(ChatColor.DARK_RED+"Hit with counterattack!");
                                }
                            } else if (Math.random() * 2000 <= mcUsers.getProfile(defender).getSwordsInt()){
                                mcCombat.getInstance().dealDamage(f, event.getDamage() / 2);
                                defender.sendMessage(ChatColor.GREEN+"**COUNTER-ATTACKED**");
                                if(f instanceof Player)
                                    ((Player) f).sendMessage(ChatColor.DARK_RED+"Hit with counterattack!");
                            }
                        }
    Basically, I want the counter-attacks to only happen when the player is hit by anything other than an arrow. I have set up a boolean isArrow to start at false and be changed to true if the event is an instance of EntityDamageByProjectileEvent. I put in some debug messages as well.

    The problem is, counter attacks still happen when the player is hit by an arrow.

    The following debug messages are received
    "[event = Damage by projectile] - isArrow = true"
    "isArrow = false"

    Both of these debug messages go off when I'm hit with an arrow.

    So obviously I have a few questions

    1) How could both debug messages go off?
    2) Why does it say its not an arrow when the previous message clearly says its an arrow?

    And one other thing I have noticed, if I am hit with anything other than an arrow I receive 2 debug messages telling me isArrow = false. Once again I'm wondering why I'm receiving 2 messages.
     
  2. Offline

    Mixcoatl

    Try this:
    Code:
    boolean isArrow = false;
    if (event instanceof EntityDamageByProjectileEvent) {
      final EntityDamageByProjectileEvent realEvent =
        (EntityDamageByProjectileEvent) event;
      isArrow = (realEvent.getProjectile() instanceof Arrow);
    }
     
  3. Offline

    Sammy

    Try declaring isArrow as a static boolean
     
  4. Offline

    nossr50

    @Mixcoatl seems like half the time it hits it recognizes it as an arrow, and the other half of the time it does not. Very odd...
     
  5. Offline

    Mixcoatl

    You can't declare a local variable as static in Java; that's a C/C++ism. Either way, if you're assigning the variable in each call before using it, and there are no external dependencies on the variable at the class level, there is really no reason for it to be scoped differently.

    Do we know that it's entering the outermost IF block in those other cases?

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

    Sammy

    I didn't saw that the variable was local...
    But if he's using a local variable on OnEntityDamageByProjectile it won't work on onEntityDamage right ?
    I'm new to Java but that what makes sense to me
     
  7. Offline

    Mixcoatl

    Variables are scoped (visible) in the block (read: curly-braces) they're declared inside.
    When you declare a variable as static, you're telling the virtual machine that the variable is accessible from the class rather than the instance. (Think of the class as a blueprint, and the instance as what is constructed using that blueprint.)
    Most of the time, fields have special meaning to the "instance" but are meaningless to the blueprint (imagine if a blueprint of a house were installed with an actual sink, for example.) Fields that are declared static are shared by all the things created using the "blueprint".
     
  8. Offline

    Sammy

    But that's exactly what I'm trying to say :oops:
    I think the code that he's showing is on the OnEntityDamageByProjectile block right?
    and there for the local variable is declared inside that block.
    BUT he want to use that variable on the onEntityDamage block.
    If not, my problem is a language (English) one :p
     
  9. Offline

    Mixcoatl

    Nope. He only wants to use it within the declaring block. At least in the context he posted.
     
  10. Offline

    Sammy

    Ok then! my bad,I misunderstood the context :)
     
Thread Status:
Not open for further replies.

Share This Page