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

Join event and Quit event don't work as expected for me.

Discussion in 'Development' started by InspectorGadget, Dec 12, 2016.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi, I have asked many Plugin Developers about this issue of mine. I want my plugin to check if the player has that item in his/her Inventory on PlayerJoinEvent and possibly remove the item on PlayerQuitEvent. My code would explain more I guess. Thanks.
    PHP:
    <?php

    namespace ItemDoesIt\RTG;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerItemHeldEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\player\PlayerQuitEvent;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\entity\Effect;
    use 
    pocketmine\event\player\PlayerMoveEvent;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\inventory\Inventory;

    class 
    Main extends PluginBase implements Listener {

        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    //if($this->getServer()->getPluginManager()->getPlugin("ServerCore")) {
                //$this->getLogger()->critical("ServerCore isn't supported atm!");
                //$this->setEnabled(false);
            //}
        
    }
      
        public function 
    onGive($player) {
      
            
    $player->getInventory()->addItem(Item::get(38801)->setCustomName("§eSpawn\nTeleports you to Spawn"));
            
    $player->getInventory()->addItem(Item::get(34701)->setCustomName("§bRandom Effect\nGives you a random Effect"));
      
        }
      
        public function 
    onHeld(PlayerItemHeldEvent $e) {
      
            
    $player $e->getPlayer();
          
            
    $hand $player->getInventory()->getItemInHand();
          
            if(
    $hand->getCustomName() === "§eSpawn\nTeleports you to Spawn") {
                
    $l $this->getServer()->getDefaultLevel()->getSafeSpawn();
                
    $player->teleport($l);
            }
          
            if(
    $hand->getCustomName() === "§bRandom Effect\nGives you a random Effect") {
                switch(
    mt_rand(1,3)) {
              
                case 
    "1":
                    
    $effect Effect::getEffect(3);
                    
    $effect->setDuration(13 20);
                    
    $effect->setAmplifier(1);
                    
    $player->addEffect($effect);
                return;
                case 
    "2":
                    
    $effectt Effect::getEffect(19);
                    
    $effectt->setDuration(13 20);
                    
    $effectt->setAmplifier(1);
                    
    $player->addEffect($effectt);
                return;
                case 
    "3":
                    
    $effecttt Effect::getEffect(20);
                    
    $effecttt->setDuration(13 20);
                    
    $effecttt->setAmplifier(1);
                    
    $player->addEffect($effecttt);
                return;
                }
            }
        }
      
        public function 
    onJoin(PlayerJoinEvent $ev) {
            
    $player $ev->getPlayer();
            foreach(
    $player->getInventory()->getItems() as $inv) {
                if(
    $inv->getId() !== 388) {
                    
    $this->getLogger()->warning("passed");
                    
    $this->onGive($player);
                }
          
            }
          
        }
      
        
    // Temp prior to onjoin verification doesnt work.. this doesnt work either error is from Item::getItem
        
    public function onQuit(PlayerQuitEvent $e) {
      
            
    $p $e->getPlayer();
            
    $i $p->getInventory();
            
    $i Item::getItem(38801);
            
    $ii Item::getItem(34701);
                
    $i->removeItem($i);
                
    $i->removeItem($ii);
                
    $this->getLogger()->warning("passed2");
        }
      
        public function 
    onDisable() {
        }
      
    }
    Or PR here https://github.com/RTGNetworkkk/ItemDoesIt
     
    Last edited: Dec 12, 2016
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    PHP:
    if($player->getInventory()->contains($item) {
      
    //Blablabla...
    }
     
  3. Aviv

    Aviv Baby Zombie

    Messages:
    156
    Doesnt PlayerQuitEvent::getPlayer returns OfflinePlayer?
    i remember i tried removing items from player's inventory when they quit, but i got an error
     
    Primus likes this.
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    What if the player quits the server before joining? This can happen when the player is banned, etc.
     
    Primus likes this.
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    PlayerQuitEvent was added as PlayerJoinEvent doesn't work with my code. I'll remove it once PlayerJoinEvent is fixed.
     
  6. Awzaw

    Awzaw Zombie Pigman Poggit Admin

    Messages:
    726
    GitHub:
    awzaw
    Last edited: Dec 12, 2016
    SOFe likes this.
  7. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
  8. Noobie

    Noobie Silverfish

    Messages:
    16
    PlayerLoginEvent?
     
  9. Noobie

    Noobie Silverfish

    Messages:
    16
    PlayerLoginEvent solves what @SOFe said though
     
  10. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    No. PlayerQuitEvent can be fired even before PlayerPreLoginEvent.
    Just use some sessioning skills to check if the player has been given what you have given him!
     
  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.