Hi, Im making a plugin for my server and I would like to know how to do that when a player tap any block with a specific item it turn on flight mode and when player tap again on any block it turn off flight mode
Here its an example: PHP: public function Ontouch(PlayerInteractEvent $event){$player = $event->getPlayer();$item = $event->getItem();if($item->getId() == 322 && $item->getDamage() == 1){//and here too if player tap the first time$player->setAllowFlight(true);}//maybe here if player touch second time$player->setAllowFlight(false);}
Try this code, never tried it but there's a chance it could work: PHP: /**@var int**/public $lastTimeTouched = [];/**@var int**/public $timesClicked = [];public function Ontouch(PlayerInteractEvent $event){ $player = $event->getPlayer(); $item = $event->getItem(); $ltt = $this->lastTimeTouched[$player->getName()]; $tc = $this->timesClicked[$player->getName()]; if(((time() - $ltt) <= 1) && ($tc == 1) && ($item->getId() == 322) && ($item->getDamage() == 1)){ //tapped twice //your code here $this->timesClicked[$player->getName()] = 0; $this->lastTimeTouched[$player->getName()] = time(); }elseif(($tc == 0) && ($item->getId() == 322) && ($item->getDamage() == 1)){ //tapped once //your code here ++$this->timesClicked[$player->getName()]; $this->lastTimeTouched[$player->getName()] = time(); }}
When I start the server I get this error: Spoiler "Fatal error: Can't use method return value in write context" And then kill the server. Here its the code: Code: <?php namespace 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(); $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(); } } }
Check line 29. Change PHP: ($item->getDamage() = 1)//SINGLE EQUAL SIGN To PHP: ($item->getDamage() == 1)//DOUBLE EQUAL SIGN
Ok I did but now I get Spoiler "Notice: undefined index on line 27" And when I touch again Spoiler "Notice: undefined index on line 28"
It's because the array has no names in it: I would use this code (not tested): 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 function onEnable(){ $this->getServer()->getPluginManager()->registerEvents($this, $this); } public function onTouch(PlayerInteractEvent $event){ $player = $event->getPlayer(); $item = $event->getItem(); if(($item->getId() == 322) && ($item->getDamage() = 1)){ if($player->getAllowFlight() == false){ $player->setAllowFlight(true); }else{ $player->setAllowFlight(false); } } }} Edit: What it does: It checks if the player is already in flight mode and if he is, disable it, and if not, enable it.
Before the $ltt variable add this: PHP: if(!isset($this->LastTimeTouched[$name]) && !Isset($this->TimeClicked[$name])){ $this->TimesClicked[$name] = 0; $this->LastTimeClicked[$name] = time(); return;}
to double click, you just register the last clicked time = current time, if it is same/X time delay(should be needed due to network lat) that means it is a double click
then why not just use this: (still untested) PHP: public function onTouch(PlayerInteractEvent $event){ $player = $event->getPlayer(); $item = $event->getItem(); if(!isset($this->fly[$player->getName())) { $this->fly[$player->getName() = false; } if(($item->getId() == 322) && ($item->getDamage() = 1)){ if($this->fly[$player->getName()]) if($player->getAllowFlight() == false){ $player->setAllowFlight(true); }else{ $player->setAllowFlight(false); } $this->fly[$player->getName] = !$this->fly[$player->getName]; } }
By the way I added it and when I touch it it don't show any error in console but neither allow to flight xD And @HimbeersaftLP I tested your code same as @jasonwynn10 code's and still not Woking (I corrected some things on codes)
thanks for pointing it out! i got carried away by reading other codes and forget about OP like on tap should toggle a array that defines if it is enable/not Example: (DO NOT COPY, just use it as a reference) Code: $enable = []; ontap: if !isset $enable[$player] or $enable[$player]== false set flight true $enable[$player] = true; else set flight false $enable[$player] = false;