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?
your code should look like this Code:java @EventHandlerpublic 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!");} 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.
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?
CookieCoder Change your code to this instead: Code:java @EventHandlerpublic void onBreak(BlockBreakEvent e) {Player p = e.getPlayer();if(!(e.getPlayer().hasPermission("worldprotect.build"))){ // checks if player DOESN'T have the permissione.setCancelled(true); // cancels the BlockBreak evente.getPlayer().sendMessage(ChatColor.GOLD + "[" + ChatColor.AQUA + "You can not break this block!"); // sends message that they can't break the block} return; // allows to break the block when player HAS got the permission} That should fix it.
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
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..
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.
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.
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.