1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved HOW TO MAKe /fly toogle comma

Discussion in 'Development' started by Levi, Dec 18, 2017.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    i want to make a /fly command that toogle /fly it turns on if fly is not enable /fly again it turns off
     
  2. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Well if you want to make it within the same command rather than making two separate ones for toggle on and off, you can make it so when they first enter the command they can be allowed to fly, and to toggle you can do a else statement and check if the player is flying, if they are disable their fly. To let a player fly in code you use setAllowFlight().
     
  3. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    Not necessarily, you can check if the player is already allowed to fly using the getAllowFlight() function
    Here is what you can do (without creating a class dedicated to this command).
    Don't forget to register the command in your plugin.yml :p
    PHP:
    public function onCommand(CommandSender $senderCommand $cmdstring $label, array $args): bool{
            switch (
    strtolower($cmd->getName())){
               case 
    'fly':
                    if(
    $sender->getAllowFlight()){
                        
    $sender->setAllowFlight(false);
                        
    $sender->sendMessage('You cant fly anymore.');
                    }else{
                        
    $sender->setAllowFlight(true);
                        
    $sender->sendMessage('You can now fly.');
                    }
               break;
          }
    }
     
    Last edited: Dec 18, 2017
    NickTehUnicorn likes this.
  4. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    You can do both ways.
     
    Levi likes this.
  5. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    This is the way I did it and it works out as expected and fine.
    PHP:
    public function onCommand(CommandSender $senderCommand $cmd$labels, array $args) :bool{
            if(
    strtolower($cmd->getName()) == "fly"){
                if(
    $sender->hasPermission("fly.command")){
                    if(
    $sender instanceof Player){
                        if(!
    $sender->getAllowFlight()){
                            
    $sender->setAllowFlight(true);
                            
    $sender->sendMessage(c::YELLOW">" .c::GREEN" Fly has now been enabled!");
                            
    $sender->sendTip(c::OBFUSCATED.c::BLUE";;" .c::RESET.c::RED">>" .c::GREEN"Fly enabled" .c::RED"<<" .c::OBFUSCATED.c::BLUE";;");
                            return 
    true;
                            }else{
                                if(
    $sender->getAllowFlight()){
                                    
    $sender->setAllowFlight(false);
                                    
    $sender->sendMessage(c::YELLOW">" .c::GREEN" Fly has now been disabled!!");
                                    
    $sender->sendTip(c::OBFUSCATED.c::BLUE";;" .c::RESET.c::RED">>" .c::GREEN"Fly disabled" .c::RED"<<" .c::OBFUSCATED.c::BLUE";;");
                                    return 
    true;
                                }
                            }
                    }else{
                        
    $sender->sendMessage(c::RED"Use this command in-game");
                        }
                }else{
                    
    $sender->sendMessage(c::RED"You don't have permission to use this command");
                    }
                }
                return 
    true;
            }
    Don't mind the textformat, I just went to my fly plugin and copied pasted the code here. :p
     
    OnTheVerge likes this.
  6. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    thank, now how can i make fly disable in nether?
    i tried
    PHP:
    $target $event->getTarget();
                
    $player $event->getEntity();
                if(
    $target == "nether") {
                    
    $player->sendMessage("ok");
                    
    $player->setAllowFlight(false);
                    }
    on EntityLevelChangeEvent
     
  7. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    It should work, all depend of the name of your world, you should var_dump($target) to be sure of the name of the nether world.
    Oh and you have to check if the entity is a player (is an instanceof Player) because you can't setAllowFlight() or sendMessage() to an entity :p
     
    Levi likes this.
  8. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    I did add if($player instanceof Player){ but it still doesn't send message or disable fly. i also have the nether world loaded
     
  9. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    Player->setFlying(bool) ?
     
    jasonwynn10 likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.