Solved Is there a way to just check if a player is rightclicking

Discussion in 'Plugin Development' started by kasszz, Apr 29, 2013.

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

    kasszz

    Is there a way to just check if a player is rightclicking. So in general, not on a animal or mob or block? just rightclicking.

    thanks :)
     
  2. Offline

    tamajpm

    I take a very long time Bukkit Plugins and I have not encountered that you can. You can do:

    getItemInHand
    GetInventory
    getItemOnCursor

    There are still a number of others.
     
  3. Offline

    kreashenz

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e){
    3. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICL_BLOCK){
    4. // do stuff
    5. }
     
  4. Offline

    zeddio

    just listen on PlayerInteractEvent and do
    event.getAction == Action.rigth_click || event.getAction == Action.right _click_block
    :)
     
  5. Offline

    kasszz

    thanks, but it looks like it's not working on entities and not even when I click in the air :S

    bump, no one ?

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

    Jake0oo0

    kasszz Did you register events, and use @EventHandler
     
  7. Offline

    kasszz

    This is my code:

    Code:
    @EventHandler(ignoreCancelled = true)
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(player.getInventory().getHelmet().getType() == Material.IRON_HELMET){
                    if(player.getItemInHand().getType() == Material.STICK){
                        getLogger().info("Vuurbal VUURBAL");
                    }
                }
            }
        }
     
  8. Offline

    Hoolean

    kasszz

    May we see all of your code? Sometimes it can be the simplest of things :)
     
  9. Offline

    beastman3226

    kasszz
    That doesn't answer his question though. Did you register your event in the
     
  10. Offline

    kasszz

    uuhhmm, I thought he meant the @EventHandler. but here is my whole code:

    Code:
    package me.kasszz.magicmadness;
     
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.CraftItemEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class MagicMadness extends JavaPlugin implements Listener {
       
        public void onEnable(){
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this, this);
            ShapedRecipe LightningWand = new ShapedRecipe(new ItemStack(Material.STICK, 1));
            LightningWand.shape(" W ", " W ", " W ");
            LightningWand.setIngredient('W', Material.STICK);
            getServer().addRecipe(LightningWand);
        }
       
        public void onDisable(){
            getServer().clearRecipes();
        }
       
        @EventHandler(ignoreCancelled = true)
        public void onCraftItem (CraftItemEvent event){
            if(event.getRecipe().equals("LightningWand") && event.getRecipe().getResult().equals(Material.STICK)){
                getLogger().info("doet het het?");
            }
        }
       
        @EventHandler(ignoreCancelled = true)
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
                if(player.getInventory().getHelmet().getType() == Material.IRON_HELMET){
                    if(player.getItemInHand().getType() == Material.STICK){
                        getLogger().info("Vuurbal VUURBAL");
                    }
                }
            }
        }
    }
    I know that the other parts don't work, I'm still working on it ^^

    is there no one who knows this ?

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

    SWISA

    Code:
    @EventHandler
        public void onPlayerInteract(PlayerInteractEvent e){
            if(e.getAction().equals(Action.RIGHT_CLICK_AIR)||e.getAction().equals(Action.RIGHT_CLICK_BLOCK)){
                if(e.getPlayer().getInventory().getHelmet().getType().equals(Material.IRON_HELMET)){
                    if(e.getPlayer().getItemInHand().getType().equals(Material.STICK)){
                        e.getPlayer().sendMessage("Vuurbal VUURBAL");
                    }
                }
            }
           
        }
    This worked for me.
     
  12. Offline

    kasszz

    is there a difference between == and .equals ?
     
  13. Offline

    Hoolean

    I believe with enums (Class.VALUE) there is no difference, however with Objects such as strings where the value not the instance is to be compared, .equals(Object obj) should be used :)
     
  14. Offline

    kasszz

    It still only works when I press the right mouse button when looking at the ground :(
     
  15. Offline

    SWISA

    Do you wear a iron helmet?
     
  16. Offline

    kasszz

    But is there not a event or function for it ?
     
  17. I belive right-click-air with nothing in-hand doesn't trigger at all, not sure... you should debug your event by printing messages with the values you're checking before the actual conditions.
     
  18. Offline

    Hoolean

    I can confirm this :)
     
  19. Offline

    kasszz

    But I have a stick in my hand :O
     
  20. kasszz
    Ok but the suggestion still stands with debugging... or do you like guessing what the code does ? :p

    Also, you might want to check getHelmet() against null before using getType() on it because when you don't have a helmet that will return and you'll get a NPE.
     
  21. Offline

    desht

    When right-clicking air, the event will arrive cancelled. So make sure you don't have ignoreCancelled = true in your @EventHandler annotation.
     
  22. Offline

    kasszz

    desht, I don't get it, why is it in there then ? :S
     
  23. Offline

    desht

    Why is what in where? It's just the way air-click events are handled in Bukkit. Counter-intuitive but there you have it.
     
  24. Offline

    kasszz

    Thanks :) it's working :)
     
Thread Status:
Not open for further replies.

Share This Page