Prevent guests from building on world

Discussion in 'Plugin Development' started by zeoed, Aug 11, 2011.

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

    zeoed

    Hi, sorry if this is the wrong forum but it seemed like it fit best here. In my plugin I have this code which should only let non-op's build on a "guests" world I have:
    Code:
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Server server = player.getServer();
            if (player.isOp() == false) {
                if (player.getLocation().getWorld().getName() != "guests") {
                    event.setCancelled(true);
                } else {
                    event.setCancelled(false);
                }
            }
        }
    This code will prevent any non-op from breaking/placing any blocks, the problem is that even on the guests world, a guest cannot build/delete blocks. To be honest, I see no error in the coding which should prevent them from building/removing blocks on the guests world. Any help is appreciated. Thanks.
     
  2. Offline

    thehutch

    you could use

    if (!player.isOp())
    //do stuff

    which means if player is not Op
     
  3. Offline

    zeoed

    Wouldn't if (player.isOp() == false) { be the same thing?
     
  4. Offline

    thehutch

    Why not use permissions aswell and default as Op?

    if (!player.hasPermission("permission.something.hello"))
    return;

    meaning if the player doesnt have the permission don't do anything

    also that is the bukkitPermissions or SuperPerms

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 18, 2016
  5. Offline

    wwsean08

    get rid of the setCancel = false, theres no reason to say you're not going to cancel it.
     
  6. Offline

    zeoed

    It used to not have setCancel = false, but it still didn't work in that case so I added it in to see if it made a difference. It didn't.
     
  7. Offline

    thehutch

    You should do it like this:


    Code:
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Server server = player.getServer();
            if (player.isOp()  && player.getLocation().getWorld().getName() =="guests") {
                event.setCancelled(true);
    
                } else {
                    event.setCancelled(false);
             }
        }
    I think this is correct

    if player is an Op and the world name = "guests" cancel the event
    else let it be
     
  8. Offline

    Crash

    You don't compare strings using == that compares references.
    Use .equals for exact case or .equalsIgnoreCase for non-case sensitive stuff
     
    thehutch and zeoed like this.
  9. Offline

    zeoed

    The op should be able to build in the guest world as well.
     
  10. Offline

    thehutch

    I thought that was what you wanted?
     
  11. Offline

    zeoed

    Thanks, Crash!
     
  12. Offline

    thehutch

    Code:
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player player = event.getPlayer();
            Server server = player.getServer();
            if (player.isOp()  && player.getLocation().getWorld().getName().equalsIgnoreCase("guests")) {
                event.setCancelled(true);
    
                } else {
                    event.setCancelled(false);
             }
        }
    Corrected
     
  13. Offline

    zeoed

    Sorry hutch, I didn't mean to word it like that >.<
     
  14. Offline

    thehutch

    ok I was working on what you said, if it still wrong maybe I can change it?
     
  15. Offline

    zeoed

    I got it working hutch, thanks for all your help. :)
     
  16. Offline

    thehutch

    Your Welcome. It feels good knowing that I barely started Java a month ago and I can already help someone :)
     
Thread Status:
Not open for further replies.

Share This Page