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

[Solved] Help with PlayerInteractEvent and Double tap

Discussion in 'Development' started by BEcraft, Jan 13, 2017.

  1. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    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 :D
     
  2. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    If you want us to help you, then you will have to create basic code to start off.
     
    OnTheVerge and applqpak like this.
  3. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    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);
    }
     
    OnTheVerge likes this.
  4. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    make sure to cancel the event incase the item is a placeable block
     
    HimbeersaftLP and applqpak like this.
  5. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    Ok I will but do you know how to do it? xD
     
  6. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    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();
        }
    }
     
    BEcraft likes this.
  7. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    When I start the server I get this error:
    "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();
                        }
                        }
                        }
     
  8. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Check line 29. Change
    PHP:
     ($item->getDamage() = 1)//SINGLE EQUAL SIGN
    To
    PHP:
     ($item->getDamage() == 1)//DOUBLE EQUAL SIGN
     
    BEcraft likes this.
  9. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    Ok I did but now I get
    "Notice: undefined index on line 27"
    And when I touch again
    "Notice: undefined index on line 28"
     
  10. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    It's because the array has no names in it: I would use this code (not tested):
    PHP:
    <?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 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.
     
    Last edited: Jan 13, 2017
  11. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    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;
    }
     
  12. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    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
     
  13. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
  14. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    ofcourse? what you expected?
     
  15. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    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];
            }
        }
     
    tybikman likes this.
  16. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    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)
     
  17. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Read OP, he wants to toggle it when tapping the block, not when doubble tapping
     
  18. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    What did you "correct"?
     
  19. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    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;
    
     
  20. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Show the code.
     
  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.