PlayerInteractEvent not firing when a player hits air

Discussion in 'Plugin Development' started by tommycake50, Dec 13, 2012.

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

    tommycake50

    so i have this plugin that im developing and part of it needs to strike lightenning at a block if a player clicks a block or if a player clicks air get their target block,
    However i have come accross said problem in the title,
    Heres my listener code(Please note i did try adding debug messages so i know it didnt fire also it works when a player hits a block)
    Code:
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
        public void onPlayerInteractEvent(PlayerInteractEvent event){
            if(event.getAction().equals(Action.LEFT_CLICK_BLOCK) || event.getAction().equals(Action.LEFT_CLICK_AIR)){
                if(ModeLists.ThorList.contains(event.getPlayer()) && event.getPlayer().getItemInHand().getTypeId() == 258){
                Location loc = event.getPlayer().getTargetBlock(null, 20).getLocation();
                World w = event.getClickedBlock().getWorld();
                w.strikeLightning(loc);
                }
            }
        }
    im confused xD and i havnt found a way to fix it. :/
     
  2. Offline

    repsor

    U use

    Code:
    World w = event.getClickedBlock().getWorld();
    but when you click air there is no clickedblock. use the block at the location the player is looking at, the player.getTargetBlock()


    btw.
    I assume you have 1. item nmr. 258 in your hand and 2. you left click air, not right click it? Maybe it's because the maximumrange of the targetblock is 20?
     
  3. Offline

    tommycake50

    yeah ik im not stupid xD but getTargetBlock works for clicked blocks too its just the one you are looking at at the time,
    and the max i was aiming was 10,
    and i was holding an iron axe,
    and like i said i put in debug messages and it definitely didnt fire and left click block does work.
     
  4. Offline

    repsor

    here is the problem:

    [repeat[

    U use
    Code:
    World w = event.getClickedBlock().getWorld();
    but when you click air there is no .getClickedBlock(): that will return null.
    Instead of .getClickedBlock() use the block at the location the player is looking at, the player.getTargetBlock()

    TargetBlock and Clickedblock are two different things: the clickedblock will only work when you click a block. the targetblock works always !
     
  5. Offline

    tommycake50

    oh yeah but that doesnt solve the fact that the event doesnt even fire when its clicking air :/ like i said i put debug messages xD.
     
  6. Offline

    repsor

    where did you put the debug message?
     
  7. Offline

    tommycake50

    ill put the code with the debug message,
    Code:
    @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
        public void onPlayerInteractEvent(PlayerInteractEvent event){
            event.getPlayer().sendMessage("the event fired");
            if(event.getAction().equals(Action.LEFT_CLICK_BLOCK) || event.getAction().equals(Action.LEFT_CLICK_AIR)){
                if(ModeLists.ThorList.contains(event.getPlayer()) && event.getPlayer().getItemInHand().getTypeId() == 258){
                Location loc = event.getPlayer().getTargetBlock(null, 20).getLocation();
                World w = event.getClickedBlock().getWorld();
                w.strikeLightning(loc);
                }
            }
        }
     
  8. Offline

    repsor

    o.o "this is pretty freaky" (TOS, blender mango project). The thing I don't understand is that the event does trigger when you right click....
     
  9. Offline

    tommycake50

    hmm wierd well i dont 100% need this it would be nice to get it fixed tho...
     
  10. Offline

    repsor

    Wait so you're saying your server doesn't recognize left click air as an interactevent at all?
     
  11. Offline

    fireblast709

    afaik, do not use the ignoreCancelled = true
     
  12. Offline

    tommycake50

    oh lol it was false when i was testing it i put it to true after that for testing purposes xD.
    (ik those click air events always get passed as cancelled)
     
  13. Offline

    ZeusAllMighty11



    I don't even see why people do that, but I am a nooby. I never check if it was cancelled, and I don't have to bother ignoring if it's cancelled?

    I told another guy over skype to remove it from his code, and oddly it worked after that! :p
     
  14. Offline

    fireblast709

    Same, I just ignore it (never had a real use of checking)
     
  15. Offline

    tommycake50

    it shouldnt affect anything(apart from still getting the event even if it is cancelled)
     
Thread Status:
Not open for further replies.

Share This Page