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. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Why not just use getAllowFlight() like I did?

    What is the fly array for, isn't getAllowFlight enough?
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    no, so it works on every possible use cases
     
  3. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    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
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member

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

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    @xBeastMode here its:
    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 
    $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)
     
  6. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    you cant do that!!
    you could try item set damage (IF it even existed)
     
  7. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    No, both of his corrections were fine, this was used in an if-statement
     
  8. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    On the elseif statement remove this:
    PHP:
    ((time() - $last) <= 1) &&
     
  9. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    Still dont show any console error or allow the player to fly
    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 
    $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();                   
                     }
                     }
                     }
     
  10. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    poor code 6/10
     
    Sandertv likes this.
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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());
            }
        }
    }
     
  12. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    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.
     
  13. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    That's not poor code, just because it looks messy or long and you can't understand it.

    Try this one:

    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 
    $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);
    }
     
    BEcraft likes this.
  14. BEcraft

    BEcraft Slime

    Messages:
    79
    GitHub:
    BEcraft
    Muqsit, HimbeersaftLP and jasonwynn10 like this.
  15. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    BEcraft likes this.
  16. Jesse2061

    Jesse2061 Silverfish

    Messages:
    19
    If I were you, I will use microtime(true) .Using a variable to save last time, and next tap, calculate the delay time.
     
  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.