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

[Solved] PlayerToggleSneakEvent doesn't work?

Discussion in 'Facepalm' started by jasonwynn10, Mar 14, 2017.

  1. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Using the following function with a registered listener, I noticed that the PlayerToggleSneakEvent is never called.
    PHP:
    public function onSneakToggle(PlayerToggleSneakEvent $event) {
        
    $this->getLogger()->info("Sneak Triggered!");
    }
    Can someone help me understand why this is occurring?
     
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
  3. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
  4. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    sorry to only answer with questions
    did you register your listener? did you check if other events work? did you check if your plugin is loaded?
     
  5. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    Code:
    public function onSneak(\pocketmine\event\player\PlayerToggleSneakEvent $ev){
        var_dump($ev);
    }
    
    works for me. Make sure you imported it correctly? idk
     
  6. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    If it's working properly for you, then I don't know what could be wrong. I know that I registered the class as a Listener and move events are called....
     
  7. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    PHP:
    <?php
    namespace jasonwynn10;

    /*
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU Lesser General Public License as published by
     * the Free Software Foundation, either version 3 of the License, or
     * (at your option) any later version.
     *
     * @author jasonwynn10
     * @link https://github.com/jasonwynn10/AirSneakBoots
     */

    use pocketmine\block\Air;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerMoveEvent;
    use 
    pocketmine\event\player\PlayerToggleSneakEvent;
    use 
    pocketmine\item\Armor;
    use 
    pocketmine\item\DiamondBoots;
    use 
    pocketmine\item\enchantment\Enchantment;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;

    class 
    Main extends PluginBase implements Listener {
        public function 
    onCommand(CommandSender $senderCommand $command$label, array $args) {
            if(
    strtolower($command) == "boots") {
                if(
    $sender instanceof Player) {
                    
    $item Item::get(Armor::DIAMOND_BOOTS);
                    
    $item->setCustomName("Diamond Boots of Flight");
                    
    $ench Enchantment::getEnchantment(Enchantment::TYPE_ARMOR_PROTECTION);
                    
    $ench->setLevel(3);
                    
    $item->addEnchantment($ench);
                    
    #$sender->getInventory()->addItem($item);
                    
    return $sender->getInventory()->setBoots($item);
                }
                return 
    false;
            }
            return 
    true;
        }
        public function 
    onMove(PlayerMoveEvent $event) {
            
    $to $event->getTo();
            
    $from $event->getFrom();
            
    $p $event->getPlayer();
            if(
    $p->getInventory()->getBoots() instanceof DiamondBoots and $p->getInventory()->getBoots()->getCustomName() == "Diamond Boots of Flight") {
                
    $this->getLogger()->debug("Named Item Found (Movement)");
                if(
    $p->isSneaking()) {
                    for(
    $x = -1$x <= 1$x++) {
                        for(
    $z = -1$z <= 1$z++) {
                            
    $newVector = new Vector3($to->getX()+$x$to->getY()-1$to->getZ()+$z);
                            
    $oldVector = new Vector3($from->getX()+$x$from->getY()-1$from->getZ()+$z);
                            
    $block $p->getLevel()->getBlock($newVector);
                            if(
    $block instanceof Air) {
                                
    $p->getLevel()->setBlock($oldVectorBlock::get(Block::AIR), falsefalse);
                                
    $this->getLogger()->debug("Placed air under {$p->getName()}'s last position");
                                
    $p->getLevel()->setBlock($newVectorBlock::get(Block::DIRT), falsefalse); // TODO set back to invisible bedrock
                                
    $this->getLogger()->debug("Placed bedrock under {$p->getName()}'s next position");
                            }
                        }
                    }
                }
            }
        }
        public function 
    onSneakToggle(PlayerToggleSneakEvent $event) {
            
    var_dump($event);
            if(!
    $event->getPlayer()->isSneaking() or $event->getPlayer()->getInventory()->getBoots() instanceof DiamondBoots) {
                if(
    $event->getPlayer()->getInventory()->getBoots()->getCustomName() != "Diamond Boots of Flight") {
                    return;
                }
                
    $this->getLogger()->debug("Named Item Found (Sneak Toggle)");
                
    $p $event->getPlayer();
                for(
    $x $p->getX()-1;$x <= 1$x++) {
                    for(
    $z $p->getZ()-1;$z <= 1$z++) {
                        
    $vector = new Vector3($p->getX()+$x$p->getY()-1$p->getZ()+$z);
                        
    $p->getLevel()->setBlock($vectorBlock::get(Block::AIR), falsefalse);
                        
    $this->getLogger()->debug("Placed air under {$p->getName()}'s last position");
                    }
                }
            }
        }
    }
    Maybe someone can pick out an error I may have missed?
     
  8. Jonas

    Jonas Baby Zombie

    Messages:
    192
    I think you must register the events with the function onEnable()
     
    corytortoise and jasonwynn10 like this.
  9. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Wow, I made the biggest noob's mistake...
    Can this thread be moved to the facepalm section?
     
    dktapps likes this.
  10. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    Done :D
     
  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.