Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Trying To Stop Non-Builders From Placing/Breaking Blocks

Comments in 'Plugin Development' started by Calrizer, Jun 9, 2015.

  1. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Hello again people of PocketMine,

    I'm creating a plugin that only allows players with a 'Builder' or 'Owner' rank to place/break blocks.
    Here is the code so far...
    PHP:
    public function onBlockPlace(BlockPlaceEvent $event){
          
            
    $player $event->getPlayer();
          
            if (
    $this->getRank($player) !== "Builder" || "Owner") {
                
    $event->setCancelled();
                
    $player->sendMessage("[CR] Only [Builders] can place blocks!");
            }
        }
      
        public function 
    onBlockBreak(BlockBreakEvent $event){
          
            
    $player $event->getPlayer();
          
            if (
    $this->getRank($player) !== "Builder" || "Owner") {
                
    $event->setCancelled();
                
    $player->sendMessage("[CR] Only [Builders] can break blocks!");
            }
        }         
    So I thought this would work, and it does but it applies to everyone even Builders and Owners

    How can I make it so only the ranks that are not Builders or Owners have this function applied to them.

    -Calrizer
  2. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    BTW the second function isn't intended to be indented. Formatting XD
  3. MyAJVideo
    Offline

    MyAJVideo Active Member

    Joined:
    May 16, 2015
    Posts:
    148
    Minecraft User:
    mrAJV2010
    Why do you do it with rank and not op status ?
  4. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    This wouldn't work because you are writing PHP (which is a computer language and not a natural human language). The compute will evaluate your expression by first looking at $this->getRank($player) !== "Builder" and come with a boolean, and then evaluate "Owner" which evaluates to a boolean true. Then it does the "||" which takes whatever was on the left with the true from the "Owner", which always results in true.

    What you want is:

    PHP:
            if ($this->getRank($player) !== "Builder" && $this->getRank($player) !== "Owner") {
    Creeperface and Calrizer like this.
  5. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Cheers! Works perfectly!
  6. MyAJVideo
    Offline

    MyAJVideo Active Member

    Joined:
    May 16, 2015
    Posts:
    148
    Minecraft User:
    mrAJV2010
    How do you add ranks and make ranks and stuff like that
  7. aliuly
    Offline

    aliuly Notable Member Plugin Developer

    Joined:
    Feb 8, 2014
    Posts:
    1,086
    Plugins:
    17
    But do you understand why this works? Why it's && and not ||?
    PEMapModder likes this.
  8. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Yeah it was a simple mistake || is the logical operator for 'OR' when in fact I needed to use the 'AND' operator which is && in PHP.
    Cheers for all the help!
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    `and`, `or, `&&` and `||` all exist in PHP, with different priority.
    Creeperface likes this.

Share This Page

Advertisement