Right Click with item event?

Discussion in 'Plugin Development' started by AstramG, May 1, 2012.

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

    AstramG

    Hello, I was wondering if anyone knows how to make it so that if you right click somewhere with a special item something will happen? For example blaze powder you can make shoot fireballs, blaze rods can make lightning, flint and steel + TNT = different reaction. So please, tell me how to use this event. And keep in mind I am fairly new to the Bukkit API so keep it simple. Thank you!
     
  2. Offline

    Sabersamus

    PlayerInteractEvent and check what the item in the players hand is, if(event.getAction() == Action."something"){
     
  3. Offline

    Mr Burkes

    I think what you could do is check for a PlayerInteractEvent and do:
    Code:
    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
        Player p = event.getPlayer();
     
        if(p.getItemInHand() == Material.BLAZE_POWDER){
            Fireball fire = p.getWorld().spawn(event.getPlayer().getLocation(), Fireball.class);
            fire.setShooter(p);
        }
        else if(p.getItemInHand() == Material.BLAZE_ROD){
            //Do whatever
        }
    }
    
    Realistically, I don't know if this would work, because I don't think a PlayerInteractEvent is thrown if a player only right clicks on air, so test it out for me :3
     
  4. Offline

    AstramG

    Can you please show me an example @ Sabersamus, I'm pretty confused on how to use this still.

    Mr Burkes I've tried your way but on the material and p.getItemInHand() lines this error occurs: "Incompatible operand types ItemStack and Material"

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

    Mr Burkes

    Ah, my bad. Try this:
    Code:
    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
        Player p = event.getPlayer();
     
        if(p.getItemInHand().getType() == Material.BLAZE_POWDER){
            Fireball fire = p.getWorld().spawn(event.getPlayer().getLocation(), Fireball.class);
            fire.setShooter(p);
        }
        else if(p.getItemInHand().getType() == Material.BLAZE_ROD){
            //Do whatever
        }
    }
    
     
  6. Offline

    AstramG

    Almost perfect Mr Burkes, just that if you shoot it while on the ground the impact hits the ground is there a way to make it shoot a bit higher? And yes you can click in the air for the PlayerInteract event. Also, just wondering is there a way to make the fireball not create and explosion or fire? Just the bullet?

    Oh, one last very important thing, is there a way to check what block you right click on, for example, using flint and steel on TNT.

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

    Mr Burkes

    This should fix the explosion on the ground and make it not have fire....
    Code:
    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
        Player p = event.getPlayer();
     
        if(p.getItemInHand().getType() == Material.BLAZE_POWDER){
            Fireball fire = p.getWorld().spawn(event.getPlayer().getLocation().add(new Vector(0.0D, 1.0D, 0.0D)), Fireball.class);
            fire.setFireTicks(0);
            fire.setShooter(p);
        }
        else if(p.getItemInHand().getType() == Material.BLAZE_ROD){
            //Do whatever
        }
    }
    
    And there is a way to cancel the explosion via EntityExplodeEvent...

    Code:
    @EventHandler(priority=EventPriority.HIGH)
    public void onCreeperExplode(EntityExplodeEvent event) {
        Entity e = event.getEntity();
     
        if (e instanceof Fireball){
            Fireball fb = (Fireball) e;
         
            if(fb.getShooter() instanceof Player){
                fb.setYield(0);
            }
        }
    }
    However, I do believe the setYield(0) call will negate any damage at all, but someone else can help :p

    To check what block is clicked on, I think you can do:

    Code:
    event.getTargetBlock()
     
    and then check if
     
    == Material.TNT
    == Material.WOOD
     
    or something like that
     
  8. Offline

    AstramG

    Gosh, your'e a great coder, mind if I refer to you for other plugins? Btw Im following you.

    How would you make it so that it is only when you right click with an item instead of in both cases?

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

    Mr Burkes

    This should only allow them to use if they right click in the AIR.
    Code:
    @EventHandler(priority=EventPriority.HIGH)
    public void onPlayerUse(PlayerInteractEvent event){
        Player p = event.getPlayer();
     
        if(event.getAction().equals(Action.RIGHT_CLICK_AIR)){
            if(p.getItemInHand().getType() == Material.BLAZE_POWDER){
                Fireball fire = p.getWorld().spawn(event.getPlayer().getLocation().add(new Vector(0.0D, 1.0D, 0.0D)), Fireball.class);
                fire.setFireTicks(0);
                fire.setShooter(p);
            }
            else if(p.getItemInHand().getType() == Material.BLAZE_ROD){
                //Do whatever
            }
        }
    }
     
  10. Offline

    AstramG

    I may have said that wrong, I meant that both, left AND right clicking trigger the action.

    event.getTargetBlock() doesn't work.

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

    Mr Burkes

    Try event.getClickedBlock() instead. :3
     
  12. Offline

    AstramG

    if(p.getItemInHand().getType() == Material.FLINT_AND_STEEL && event.getClickedBlock() == Material.TNT){

    This line results the error: " Incompatible operand types Block and Material"

    The explosion + fire canceling also doesn't work. Im playing around with it atm.

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

    Mr Burkes

    Try:
    Code:
    if(p.getItemInHand().getType() == Material.FLINT_AND_STEEL && event.getClickedBlock().getType() == Material.TNT){
     
  14. Offline

    AstramG

  15. Offline

    Brian_Entei

    I was about to ask a question similar to the clicking events, but this thread answered all my questions! Thanks, original poster and helper!

    -Brian_Entei

    P.S. Sorry to bump an old thread, but it's useful!
     
    DrkMatr1984 likes this.
  16. Offline

    Curtis3321

    Hello, sorry for bumping.

    But i need some help.

    How can i make it so, when a player LEFT clicks with blazerod in there hand, it will shoot a fireball, but if they dont have the permission it will cancel?

    Help ;)
     
  17. Offline

    IonMame

    Again, sorry for bumping.

    I would really be interested in how you block the right click event if the player doesn't have permission?
     
  18. Offline

    SpaceManiac

    Add a condition to the if: && player.hasPermission("x.y.z")
     
  19. Offline

    Jocke155

    This is exactly what i'm looking for. But i have a little bit different needs, i want a command that looks like this:
    /abshield [level] [duration] [cooldown] This gives the item that i'm holding in hand ability to give me absorption shield when i right click with the item i'm holding. It gives the [level] aborption potion effect, and over [duration] in seconds, and has a [cooldown] before you get the effect again when rightclick. How can i do this? :/
     
  20. Offline

    Sabersamus

    I'm not quite sure what exactly you need, what exactly do you need help with?
     
  21. Offline

    ChunkMe

    Try this
    Code:java
    1. @EventHandler
    2. public void ballFiring(PlayerInteractEvent e){
    3. Player p = e.getPlayer();
    4. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    5. if (!(e.getItem().getType() == Material.BLAZE_ROD)) return;
    6. if(!p.hasPermission("example.example")) return;
    7. Fireball s = e.getPlayer().launchProjectile(Fireball.class);
    8. }
     
  22. Offline

    AstramG

    This was my old thread from 2 years ago. I have solved it a very long time ago, and got much better at developing. :p
     
  23. Offline

    ChunkMe

Thread Status:
Not open for further replies.

Share This Page