[TUT] Make blocks breakable only by ops.

Discussion in 'Resources' started by sniper_mine, Jan 6, 2014.

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

    sniper_mine

    Hey guys! So I had a few people ask me how they could disable block breaking for everyone but ops.
    It is extremely simple. Here is the code.


    Code:java
    1. @EventHandler(priority = EventPriority.LOWEST)
    2. public void onBlockBreak(BlockBreakEvent event) {
    3. Player player = event.getPlayer();
    4. if(!(player.isOp())) event.setCancelled(true)
    5. }
    6. }
     
  2. Offline

    HelGod

    sniper_mine
    You don't need that else statement you can just do if(!(player.isOp())) event.setCancelled(true)
     
  3. Offline

    sniper_mine

    HelGod
    True!
    Thanks for pointing that out
     
  4. Offline

    Cirno

    Better yet, event.setCancelled(!player.isOp());
     
  5. Offline

    RentAMonkey

    sniper_mine
    Why did you even write an Tutorial for such a simple thing? Just wondering.
     
    ccrama, Jaker232 and sgavster like this.
  6. Offline

    Panjab

    sniper_mine

    By the way, the priority system of bukkit is a little bit confusing. "HIGH -> HIGHEST" doesn't mean it'll used first, you have to use "LOW or LOWEST". In your case the 2nd one. :)
     
  7. Offline

    sniper_mine

    RentAMonkey
    I had a bunch of people asking me.
    Panjab
    Thanks I did not know that
     
  8. Offline

    sgavster

    If you wanna have fun, you can do this:

    Code:java
    1. event.setCancelled(player.isOp() ? false : true);
     
    sniper_mine likes this.
  9. Offline

    Garris0n

    Code:java
    1. @EventHandler
    2. public void onBreak(BlockBreakEvent event){
    3. event.setCancelled(!event.getPlayer().isOp());
    4. }


    Why are we doing this? If you had to come to the resources section for this code, you probably don't know how to code...
     
    kumpelblase2, spoljo666 and Chinwe like this.
  10. Offline

    macguy8

    Cirno Garris0n

    Although that'd work, it'd uncancel the event for any other plugin that cancelled the event. You should do something like...

    Code:
    event.setCancelled(event.getPlayer().isOp() ? event.isCancelled() : true);
     
    Garris0n likes this.
  11. Offline

    SoThatsIt

    or you could just set the event priority to LOWEST
     
  12. Offline

    macguy8

    SoThatsIt True, I suppose, however that'd be relying on the fact any other plugin listening on LOWEST doesn't cancel the event, which although it's uncommon can't be guaranteed.
     
  13. Offline

    BungeeTheCookie

    If it all does the same thing, why bother?
     
    sniper_mine and Garris0n like this.
Thread Status:
Not open for further replies.

Share This Page