Event help.

Discussion in 'Plugin Development' started by CookieCoder, Jul 9, 2014.

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

    CookieCoder

    I have this event:
    Code:
        @EventHandler
        public void onBreak(BlockBreakEvent e) {
            Player p = e.getPlayer();
            if(e.getPlayer().hasPermission("worldprotect.build"));
            p.setCanceled(true);
            e.getPlayer().sendMessage(ChatColor.GOLD + "[" + ChatColor.AQUA + "You can not break this block!");
        }
    i have this code but i have the permissions but i can't break blocks?
     
  2. Offline

    kunhunjon

    your code should look like this
    Code:java
    1. @EventHandler
    2. public void onBreak(BlockBreakEvent e)
    3. {
    4. Player p = e.getPlayer();
    5.  
    6. if(e.getPlayer().hasPermission("worldprotect.build"))
    7. {
    8. p.setCanceled(true);
    9. }
    10.  
    11. e.getPlayer().sendMessage(ChatColor.GOLD + "[" + ChatColor.AQUA + "You can not break this block!");
    12. }


    Im not sure if using .setCanceled on a player will stop the block break. You might have to use e.{Something to stop the block break} or use onPlayerInteract or something similar then cancel it to stop block breaking. For that consult the JavaDoc. Also, make sure your testing your plugin on a clean bukkit server, don't have any conflicting permission plugins, or any other plugins at all.

    Also you're trying to cancel when they have the permission to build, and either way a player would get the message saying that they can't break the block.
     
  3. Offline

    teej107

    CookieCoder This is your problem
    You don't put semi-colons after if statements.
     
    Asgernohns likes this.
  4. Offline

    Necrodoom

    kunhunjon that code still doesn't do the proper logic. It would fire the cannot break message no matter if user can break or not, and you say that if user has permission to break, deny breaking.

    CookieCoder you seem to have a start, but forgot to actually do anything with the if check. Did you mean to return from the method if permission is given?
     
  5. Offline

    indyetoile

    CookieCoder
    Change your code to this instead:
    Code:java
    1. @EventHandler
    2. public void onBreak(BlockBreakEvent e) {
    3. Player p = e.getPlayer();
    4. if(!(e.getPlayer().hasPermission("worldprotect.build"))){ // checks if player DOESN'T have the permission
    5. e.setCancelled(true); // cancels the BlockBreak event
    6. e.getPlayer().sendMessage(ChatColor.GOLD + "[" + ChatColor.AQUA + "You can not break this block!"); // sends message that they can't break the block
    7. } return; // allows to break the block when player HAS got the permission
    8. }

    That should fix it.
     
  6. Offline

    Necrodoom

    Why is everyone's first reaction is to spoonfeed without explaining at all? It isn't helpful.
     
    MooshViolet and AoH_Ruthless like this.
  7. Offline

    indyetoile

  8. Offline

    CookieCoder

    Doenst work
     
  9. Offline

    indyetoile

    CookieCoder
    Please post your full class here.
    Are you registering your events?
     
  10. Offline

    teej107

    indyetoile
    What? return; statements just end the method. Since return; is already at the end of the method, it is pretty useless to put one there.
    @CookieCoder
     
  11. Offline

    indyetoile

    teej107

    It doesn't matter- it's just to clarify it.
     
  12. Offline

    AoH_Ruthless

    indyetoile
    Returning at the end of a method is bad practice. Only amateurs, imo, do that. It's bad practice and any good programmer reading your code knows when the end of the method has arrived, provided your code is indented properly. While it doesn't technically matter, it is not a good coding practice..
     
    MooshViolet and teej107 like this.
  13. Offline

    kunhunjon


    Necrodoom Maybe if you actually took the time to read my post you would have seen this "Also you're trying to cancel when they have the permission to build, and either way a player would get the message saying that they can't break the block." Read posts before trying to correct them please.
     
  14. Offline

    Necrodoom

    kunhunjon so you copy paste the same logic error?
     
    xTigerRebornx likes this.
  15. Offline

    kunhunjon


    Yes because as you said
    I fixed the basic errors in his code and purposely didn't spoon feed him the right logic, just explained what he was doing wrong.
     
  16. Offline

    MooshViolet

    How about we help this poor man instead of arguing about spoonfeeding. Like honestly, I am against it, but let people help the way they want to help.
     
Thread Status:
Not open for further replies.

Share This Page