Check if a player has permission to break a block

Discussion in 'Plugin Development' started by MyPasswordIsPassword, Jun 5, 2012.

Thread Status:
Not open for further replies.
  1. I wrote a bit of code to change the wheat drops to bread using BlockBreakEvent, but due to the way it is written (It sets the block to air to prevent dropping wheat as well), it can override plugins like Towny and WorldGuard. I tried decompiling mcMMO to see their method, but I couldn't figure out how they managed to send a "fake" block break event to check if the player had permission. Any help on this is appreciated.

    Edit: Since this is the first result in google for this, I thought I should add what I figured out.

    I re-examined mcMMO, and this is their method:

    When a block break event is called, create a "FakeBlockBreakEvent," a class that extends BlockBreakEvent and has nothing else added. Back in onBlockBreak, check if the Event being called is an instanceof FakeBlockBreakEvent. If so, return without executing any code, in order to prevent an infinite loop. Then, after the FakeBlockBreakEvent is finished, continue with onBlockBreak if FakeBlockBreakEvent.isCancelled() is false, or stop if it is true.

    This may be a bit confusing, so try decompiling mcMMO and looking at the "FakeBlockBreakEvent", "Misc", and "BlockListener" classes for actual code.
     
  2. Offline

    jamietech

    If you're just worried about WorldGuard check it's API to see if you can get the players access to the region they're standing in and then work off that.
     
  3. Offline

    Malikk

    Plugins like Towny and WorldGuard will be listening to the BlockBreakEvent and cancelling it if the player did not have permission. All you need to do is add ignoreCancelled to your event handler.

    Code:java
    1.  
    2. @EventHandler (ignoreCancelled = true)
    3. public void onBlockBreak(BlockBreakEvent event){
    4. //do stuff
    5. }
    6.  


    Now, the code in your plugin won't run if another plugin has cancelled the event.
     
  4. Offline

    jamietech

    Only problem is they would be on priority HIGHEST which is called last AFAIK
     
  5. Offline

    Malikk

    If you're not ever cancelling the event, you should set your priority to MONITOR, that should fix it
     
  6. Offline

    CoffeeOD

    Im doing something similar and had same problem, solved it by first checking cancel status with "event.isCancelled()".
     
  7. Offline

    jamietech

    Easier way to do this is just add "ignoreCancelled = true" to your EventHandler annotation
     
Thread Status:
Not open for further replies.

Share This Page