Method of recognizing player rightclicking a block with an item?

Discussion in 'Plugin Development' started by TheWayIRoll, Nov 9, 2011.

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

    TheWayIRoll

    I'm having a little trouble working out HashMaps and player handling trying to check to see if the player has rightclicked a block, had his/her hashmap enabled, and is rightclicking with a stick all at once. How can I make this possible?
     
  2. Offline

    Jogy34

    Well first off I'm going to assume that you have the right type of PlayerListener. If you do I think it would be something like this
    Code:
    ItemStack stack = new ItemStack(Material.STICK, player.getItemInHand().getAmount());
    if(player.getItemInHand().equals(stack) && **Check to see if the player is in the hashmap** )
    {
    //Do Stuff
    }
    
     
    halley likes this.
  3. Offline

    TheWayIRoll

    What event would the Block Event call when it's doing something?
     
  4. Offline

    Jogy34

    what do you mean?
     
  5. Offline

    halley

    Code:
    ItemStack stack = new ItemStack(Material.STICK, player.getItemInHand().getAmount());
    if(player.getItemInHand().equals(stack) && **Check to see if the player is in the hashmap** )
    {
    //Do Stuff
    }
    Why would you create a stack of sticks, just to see if he's holding a stick?

    Code:
    ItemStack held = player.getItemInHand();
    if (held.getType() == Material.STICK && ...) { ... }
    
     
  6. Offline

    ZachBora

    Code:
    if (player.getItemInHand().getType() == Material.STICK && ...) { ... }
    ?? profit!
     
    BronzeByte likes this.
  7. Offline

    Jogy34

    I honestly don't know why I did that. I just wasn't thinking
     
  8. Offline

    nisovin

    Also, if you're checking for right click, that would be a PlayerListener, not a BlockListener.
     
  9. And the method is onPlayerInteract(PlayerInteractEvent event)
     
  10. Offline

    TheGodEmperor

    Hmm, question that goes along with this, I'm trying to create snow-ball grenades for the hell of it and so far I have this.

    Code:
    public void onPlayerInteract(PlayerInteractEvent e)
        {
            if (e.getAction() == Action.RIGHT_CLICK_AIR)
            {
                Player p = e.getPlayer();
                if(p.getItemInHand().getType().equals(Material.SNOW_BALL)){
                    World w = p.getWorld();
                    Location s = p.getEyeLocation();
                    s.add(s.getDirection().getX() * 3, s.getDirection().getY() * 3, s.getDirection().getZ() * 3);
    
                    Projectile b = w.spawn(s, Snowball.class);
                    b.setVelocity(p.getEyeLocation().getDirection().multiply(2));
                    b.setShooter(p);
                }
            }
        }
    Now I know without checking for what's in hand it can work, but all right clicking with any object will create the grenade and I want it to just be snowballs (in this instance). Any idea what could be going on? I've tried .equals and == for getItemInHand line.

    Also, I'm trying to change the functionality of the bow and arrow so that when you right click and hold (or right click to activate/deactivate, doesn't matter which to me) you'll just continuously stream arrows instead of the way it works now. I can't think of how to have it continue over time, any ideas?

    Edit:

    Ah nevermind, I've got a problem determining what the entity is onProjectileHit

    Code:
    @Override
        public void onProjectileHit(ProjectileHitEvent e)
        {
            Entity entity = e.getEntity();
            if(e.getType().equals(Material.SNOW_BALL)){
                entity.setFireTicks(100);
                World w = entity.getWorld();
                w.createExplosion(entity.getLocation(), 6);
            }
        }
    Any ideas?

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

    matter123

    you have 1 problem
    1) Type is an different enum from Material so that check would always return false

    improved code here
    Code:
    @Override
        public void onProjectileHit(ProjectileHitEvent e)
        {
            Entity entity = e.getEntity();
            if(e instanceof org.bukkit.entity.Snowball){
                entity.setFireTicks(100);
                World w = entity.getWorld();
                w.createExplosion(entity.getLocation(), 6);
            }
        }
     
  12. Offline

    TheGodEmperor

    hmm, trying that it still doesn't work when I throw a snowball.

    Ah woot, entity isntanceof not e and it works. Thanks for the help :) now to figure out how to make a machine gun-esque bow and arrow instead of how it is.

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

    matter123

    Last edited by a moderator: May 21, 2016
  14. Offline

    TheGodEmperor

    Ya, I'm trying to figure out how to have it called every so often while the right click is down or ya... dunno yet.
     
  15. Offline

    matter123

    ah the Scheduler
    plugin.getServer().getScheduler()
     
Thread Status:
Not open for further replies.

Share This Page