Can't find the action for right clicking an entity (sheep)

Discussion in 'Plugin Development' started by ZeusAllMighty11, May 26, 2012.

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

    ZeusAllMighty11

    I've found a few similar, but none exact.

    How do I check if a player right clicks a sheep?



    Code:
            if(p.getItemInHand().getType() == Material.STICK){ // checks if the item in hand is a stick
                if(e.getAction() == Action.LEFT_CLICK_AIR){ // if player right clicks sheep
                }
            }
    How can I check if AIR is sheep?
     
  2. Offline

    Acrobot

    EntityDamageByEntityEvent?
    Just check if the damage source is a player and he's holding a stick.
     
  3. Offline

    ZeusAllMighty11



    I tried doing this, excuse me if I'm wrong. I'm still new to this.

    Code:
            if(e.getCause() == (p.getItemInHand() == (Material.STICK))){
    
     
  4. Offline

    Acrobot

    ZeusAllMighty11
    Nope, sorry, but not like that.

    First, check if the event is not cancelled:
    * in your @EventHandler annotation, do that:

    Code:
    @EventHandler(ignoreCancelled = true)
    
    Then, check if your damager is a player and cancel if he's not:

    Code:
    if (!(event.getDamager() instanceof Player)) {
        return;
    }
     
    
    We also need to check if our damaged entity is a sheep:

    Code:
    if (!(event.getEntity() instanceof Sheep)) {
        return;
    } 
    
    Now, we can finally get our Player object and check if he's holding a stick, and as before, cancel if he's not:

    Code:
    Player damager = (Player) event.getDamager();
    ItemStack inHand = damager.getItemInHand();
     
    if (inHand == null || inHand.getType() != Material.STICK) {
       return;
    }
    
    Then, just do what you want with it.
     
  5. Offline

    ZeusAllMighty11

    Acrobot

    Thanks, but what about these errors?

    DescriptionResourcePathLocationType
    Syntax error, insert "enum Identifier" to complete EnumHeadercolorSheep.java/TransformCreature/src/TransformCreatureline 57Java Problem
    Syntax error, insert "EnumBody" to complete EnumDeclarationcolorSheep.java/TransformCreature/src/TransformCreatureline 57Java Problem
     
  6. Offline

    Acrobot

    ZeusAllMighty11
    What...?

    Where did you get those errors?
    I'm pretty sure they're not from my code...
     
  7. Offline

    ZeusAllMighty11

    Acrobot
    The errors are coming from @EventHandler


    Before I had it, no errors. But when I add that in, I get those errors

    I'll leave it out for now...
     
  8. Offline

    Njol

    What about PlayerInteractEntityEvent?
     
  9. Offline

    Acrobot

    Njol
    This would be just a right-click on entity.

    ZeusAllMighty11
    I'm pretty sure you included the wrong @EventHandler annotation. Are you sure it's from Bukkit?
     
  10. Offline

    ZeusAllMighty11


    I only want a right click, not a left click. Because then I have to go and cancel the damage event lol.

    So I think I can use his method?

    And yes, I'm using Bukkit's '@EventHandler'.
     
  11. Offline

    Acrobot

    ZeusAllMighty11
    ...
    Really...
     
  12. Offline

    ZeusAllMighty11


    Well EXCUSE ME, that was my original intent.

    If there's an easier method, I'll go about. Whether it involves right or left, I don't care.
     
  13. Offline

    Acrobot

    ZeusAllMighty11
    Well then, just use PlayerInteractEntityEvent, and instead of checking damager, do event.getPlayer() to get a player, and event.getEntity() (I think, I might be wrong here) to get the clicked entity. Then just check as usual.
     
  14. Offline

    ZeusAllMighty11

    Ok, sorry if I sounded rude above.

    event.getEntity() will not get the entity for PlayerInteractiveEntityEvent, only
    event.getRightClicked ()


    btw that works for now , so we'll see
     
  15. Offline

    Acrobot

    ZeusAllMighty11
    Yeah, getRightClicked() is what I meant :)

    BTW, No problem :p
     
Thread Status:
Not open for further replies.

Share This Page