Why not just use getAllowFlight() like I did? What is the fly array for, isn't getAllowFlight enough?
But: When I am in survival, press the button I can fly, value is true Then I switch to creative and back to survival again I can't fly, but the value is still true, so the player has to double tap the block which could confue some players
well then that is your own job to handle it, a very simple check like "only do that if player is in adventure OR survival)" will do the trick
@xBeastMode here its: PHP: <?phpnamespace Prueba;use pocketmine\Server;use pocketmine\Player;use pocketmine\plugin\PluginBase;use pocketmine\event\Listener;use pocketmine\item\Item;use pocketmine\block\Block;use pocketmine\event\player\PlayerInteractEvent;class Main extends PluginBase implements Listener{ public $LastTimeTouched = []; public $TimesClicked = []; public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); } public function onTouch(PlayerInteractEvent $event){ $player = $event->getPlayer(); $name = $player->getName(); $item = $event->getItem(); if(!isset($this->LastTimeTouched[$name]) && !isset($this->TimeClicked[$name])){ $this->TimesClicked[$name] = 0; $this->LastTimeClicked[$name] = time(); return;} $clicks = $this->TimesClicked[$name]; $last = $this->LastTimeTouched[$name]; if(((time() - $last) <= 1) && ($clicks == 1) && ($item->getId() == 322) && ($item->getDamage() == 1)){ $player->setAllowFlight(true); $this->TimesClicked[$name] = 0; $this->LastTimeTouched[$name] = time(); }elseif(((time() - $last) <= 1) && ($clicks == 0) && ($item->getId() == 322) && ($item->getDamage() == 1)){ $player->setAllowFlight(false); $this->TimesClicked[$name] = 0; $this->LastTimeClicked[$name] = time(); } } } And @HimbeersaftLP here: PHP: if(!isset($this->fly[$player->getName())) { $this->fly[$player->getName() = false; }//and here($item->getDamage() = 1) To: PHP: if(!isset($this->fly[$player->getName()])) { $this->fly[$player->getName()] = false; }//($item->getDamage() == 1)
Still dont show any console error or allow the player to fly PHP: <?phpnamespace Prueba;use pocketmine\Server;use pocketmine\Player;use pocketmine\plugin\PluginBase;use pocketmine\event\Listener;use pocketmine\item\Item;use pocketmine\block\Block;use pocketmine\event\player\PlayerInteractEvent;class Main extends PluginBase implements Listener{ public $LastTimeTouched = []; public $TimesClicked = []; public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); } public function onTouch(PlayerInteractEvent $event){ $player = $event->getPlayer(); $name = $player->getName(); $item = $event->getItem(); if(!isset($this->LastTimeTouched[$name]) && !isset($this->TimeClicked[$name])){ $this->TimesClicked[$name] = 0; $this->LastTimeClicked[$name] = time(); return; } $clicks = $this->TimesClicked[$name]; $last = $this->LastTimeTouched[$name]; if(((time() - $last) <= 1) && ($clicks == 1) && ($item->getId() == 322) && ($item->getDamage() == 1)){ $player->setAllowFlight(true); $player->sendMessage("§aYou can fly"); $this->TimesClicked[$name] = 0; $this->LastTimeTouched[$name] = time(); }elseif(($clicks == 0) && ($item->getId() == 322) && ($item->getDamage() == 1)){ $player->setAllowFlight(false); $player->sendMessage("§cYou cant fly"); $this->TimesClicked[$name] = 0; $this->LastTimeClicked[$name] = time(); } } }
Clean method. PHP: //Item id of the item the player would tap a block with.const ITEM_ID = 20;public function onInteract(PlayerInteractEvent $event){ if(($item = $event->getItem())->getId() === self::ITEM_ID){ if($event->getBlock()->getId() !== 0){ $player = $event->getPlayer(); $player->setAllowFlight(!$player->getAllowFlight()); } }}
Double tap method: PLEASE define each and every global variable before writing a function. PHP: //Item id of the item the player would tap a block with.const ITEM_ID = 20;private $taps = [];public function onInteract(PlayerInteractEvent $event){ if(($item = $event->getItem())->getId() === self::ITEM_ID){ if($event->getBlock()->getId() !== 0){ $player = $event->getPlayer(); if(!isset($this->taps[$player->getId()])){ $this->taps[$player->getId()] = time(); //Player has tapped once. }elseif($this->taps[$player->getId()] - time() >= -2){ //Player has tapped twice. unset($this->taps[$player->getId()]); $player->setAllowFlight(!$player->getAllowFlight()); }else $this->taps[$player->getId()] = time(); } }} You can change -2 to an even lesser number if you want to extend the double click time. You can increase the number to decrease the time.
That's not poor code, just because it looks messy or long and you can't understand it. Try this one: PHP: <?phpnamespace Prueba;use pocketmine\Server;use pocketmine\Player;use pocketmine\plugin\PluginBase;use pocketmine\event\Listener;use pocketmine\item\Item;use pocketmine\block\Block;use pocketmine\event\player\PlayerInteractEvent;class Main extends PluginBase implements Listener{ public $LastTimeTouched = []; public $TimesClicked = []; public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); } public function onTouch(PlayerInteractEvent $event){ $player = $event->getPlayer(); $name = $player->getName(); $item = $event->getItem(); if(!isset($this->LastTimeTouched[$name]) && !isset($this->TimeClicked[$name])){ $this->TimesClicked[$name] = 0; $this->LastTimeClicked[$name] = time(); return; } $clicks = $this->TimesClicked[$name]; $last = $this->LastTimeTouched[$name]; if(((time() - $last) <= 1) && ($clicks == 1) && ($item->getId() == 322) && ($item->getDamage() == 1)){ $player->setAllowFlight(true); $player->sendMessage("§aYou can fly"); $this->TimesClicked[$name] = 0; $this->LastTimeTouched[$name] = time(); return; }if(($clicks == 0) && ($item->getId() == 322) && ($item->getDamage() == 1)){ $player->setAllowFlight(false); $player->sendMessage("§cYou cant fly"); $this->TimesClicked[$name] = 0; $this->LastTimeClicked[$name] = time(); return; } } } If not just use my short method: PHP: if(($item->getDamage() == 1) && ($item->getId() == 322)){$player->getAllowFlight() ? $player->setAllowFlight(0) : $player->setAllowFlight(1);}
Thanks @jasonwynn10, @xBeastMode, @Thunder33345, @HimbeersaftLP, @Muqsit for take time to help me @Muqsit I tested your code and it works for me and @xBeastMode I added what you said but still it don't show anything but thank you for all the effort!
If I were you, I will use microtime(true) .Using a variable to save last time, and next tap, calculate the delay time.