i want to make a /fly command that toogle /fly it turns on if fly is not enable /fly again it turns off
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().
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 PHP: public function onCommand(CommandSender $sender, Command $cmd, string $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; }}
This is the way I did it and it works out as expected and fine. PHP: public function onCommand(CommandSender $sender, Command $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.
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
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
I did add if($player instanceof Player){ but it still doesn't send message or disable fly. i also have the nether world loaded