Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to get the player that died on PlayerDeathEvent

Comments in 'Plugin Development' started by Hittmana, Apr 21, 2016.

  1. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    I am making a new plugin to teleport players to the spawn of the world they died in instead of the "Hub" world. Here is my most current code:
    PHP:
    <?php

    namespace Hittmana\SpawnPro;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\Listener;

    class 
    MainClass extends PluginBase implements Listener
    {
      public function 
    onEnable()
            {
            
    $this->getLogger()->info("SpawnPro enabled v1.0.0");
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            }
        public function 
    onDisable()
            {
            
    $this->getLogger()->info("SpawnPro disabled v1.0.0");
            }  
        public function 
    onDeath(PlayerDeathEvent $event)
            {
            
    $cfg $this->getConfig()->getAll();
            
    $main $cfg["Home-World"]; 
            
    $enable $cfg["Enable-Respawn-in-death-world"];
            
    $player $event->getPlayer();
            
    $level $player->getLevel()->getName();
                if (
    $enable == "yes")
                    {
                        
    /*
                        * @param Player $player
                        */
                        
    $level $this->getServer()->getLevelByName($level);
                        
    $player->teleport($this->getServer()->getLevelByName($level)->getSafeSpawn());
                    }
                    else
                    {
                        
    /*
                        * @param Player $player
                        */
                        
    $player->teleport($this->getServer()->getLevelByName($main)->getSafeSpawn());
                    }
            }
            function 
    onJoin(PlayerJoinEvent $event)
            {
                
    /*
                * @param Player $player
                */
                
    $cfg $this->getConfig()->getAll();
                
    $main $cfg["Home-World"]; 
                
    $player $event->getPlayer();
                
    $player->teleport($this->getServer()->getLevelByName($main)->getSafeSpawn());
            }
    }
    Whenever the player dies it crashes the server with this crashlog:
    Code:
    ClearSky Crash Dump Wed Apr 20 23:09:35 ACT 2016
    
    Error: Call to undefined method pocketmine\event\player\PlayerDeathEvent::getPlayer()
    File: /phar_SpawnPro 2_uN6oxt183PBeF0P.phar/src/HittmanA/SpawnPro/MainClass
    Line: 29
    Type: E_ERROR
    
    THIS CRASH WAS CAUSED BY A PLUGIN
    BAD PLUGIN: SpawnPro v1.0.0
    
    Code:
    [20] }
    [21] public function onDeath(PlayerDeathEvent $event)
    [22] {
    [23] $cfg = $this->getConfig()->getAll();
    [24] $main = $cfg["Home-World"];
    [25] $enable = $cfg["Enable-Respawn-in-death-world"];
    [26] $player = $event->getPlayer();
    [27] $level = $player->getLevel()->getName();
    [28] if ($enable == "yes")
    [29] {
    [30] /*
    [31] * @param Player $player
    [32] */
    [33] $level = $this->getServer()->getLevelByName($level);
    [34] $player->teleport($this->getServer()->getLevelByName($level)->getSafeSpawn());
    [35] }
    [36] else
    
    Backtrace:
    #0 (): pocketmine\Server->crashDump(boolean)
    
    ClearSky version: 1.1 #371 [Protocol 40; API 1.13.1]
    uname -a: Windows NT US-BUF-014 6.1 build 7601 (Windows Server 2008 R2 Standard Edition Service Pack 1) i586
    PHP Version: 5.6.17
    Zend version: 2.6.0
    OS : WINNT, win
    
    Loaded plugins:
    1vs1 0.0.5 by for API(s) 1.0.0, 1.1.0
    AdminFun 1.0.0 by hoyinm14mc for API(s) 1.12.0, 1.13.0, 2.0.0
    AdvancedKits 4.0.1 by luca28pet for API(s) 1.12.0
    AdvertisingKick 1.0.0 by for API(s) 1.0.0
    Alias 1.2.0 by ZacHack for API(s) 1.12.0
    AlwaysSpawn 2.2.2 by Philip Shilling for API(s) 1.12.0
    BanItem 2.2 by LDX for API(s) 1.0.0, 2.0.0
    BasicHUD 1.0.6 by aliuly for API(s) 1.12.0
    Broadcaster 1.16 by EvolSoft for API(s) 1.12.0
    ChallengerPE 1.0.0 by for API(s) 1.4.0
    ChatCensor 1.4 by EvolSoft for API(s) 1.9.0
    PocketGuard 2.1.1 by MinecrafterJPN for API(s) 1.10.0
    ClearLagg 1.1.0 by LegendOfMCPE for API(s) 1.0.0
    CombatLogger 1.2.0 by Lambo for API(s) 1.12.0
    CommandTracker 1.1 by Scott Handley for API(s) 1.10.0
    CrateKeys 1.0.5 by CDFalcon, CDFalcon for API(s) 1.13.0
    CustomAreas 1 by luca28pet for API(s) 1.12.0
    DevTools 1.10.0 by PocketMine Team for API(s) 1.3.1
    EasyMOTD 1.0a by LDX for API(s) 1.12.0
    EconomyAPI 2.0.9 by onebone for API(s) 1.0.0, 1.1.0, 1.2.0, 1.2.1, 1.3.0, 1.3.1, 1.4.0, 1.4.1
    EconomyCasino 2.0.2 by onebone for API(s) 1.0.0, 1.1.0, 1.2.0, 1.2.1, 1.3.0, 1.3.1, 1.4.0, 1.4.1
    EconomyGamble 2.0-alpha by hmm for API(s) 1.4.1
    EconomyPotionDispenser 0.0.2 by Khinenw for API(s) 1.12.0
    EconomyPShop 2.0.2 by onebone for API(s) 1.12.0
    EconomySell 2.0.7 by onebone for API(s) 1.12.0
    EconomyTax 2.0.3 by onebone for API(s) 1.0.0, 1.1.0, 1.2.0, 1.2.1, 1.3.0, 1.3.1, 1.4.0, 1.4.1
    EmptyWorld 1.0.0 by aliuly for API(s) 1.12.0
    essentialsTP 1.1.0 by for API(s) 1.12.0
    GoogleAnalytics 1.0.0 by for API(s) 1.0.0
    HelpOp 1.0.4 by hoyinm for API(s) 1.9.0
    HereAuth 1.0-Dev#44 by PEMapModder for API(s) 1.12.0
    iProtector 3.1a by LDX for API(s) 1.9.0
    ItemCasePE 1.0.8 by aliuly for API(s) 1.10.0, 1.13.0
    JoinCommands 1.4.1 by PixelGuy75 for API(s) 1.0.0
    KillBounty 1.0.1.5 by for API(s) 1.0.1.5
    Lightning_Strike 2 by MichaelM04 for API(s) 1.0.0
    LimitedCreative 1.0.0 by basprohop for API(s) 1.10.0
    LiveSigns 1.2.1 by aliuly for API(s) 1.12.0
    ManyWorlds 2.0.3 by aliuly for API(s) 1.10.0
    MineReset 2.1 by Falk for API(s) 1.0.0
    Nick 1.0.1 by AryToNeX for API(s) 1.12.0
    PeacefulSpawn 2.5 by LDX for API(s) 1.0.0, 2.0.0
    SpawnPro 1.0.0 by for API(s) 1.4.0
    PocketDockConsole 0.0.14 by humerusj for API(s) 1.0.0
    PopupAlerts 1.3 by EvolSoft for API(s) 1.12.0
    PurePerms 1.2.2 by 64FF00 for API(s) 1.12.0
    ServerLoveMCPE 3.1 by TheDeibo for API(s) 1.10.0
    ServerMail 0.0.2 by tschrock ([email protected]) for API(s) 1.0.0
    SignShop 1.1.3 by xionbig, xionbig for API(s) 1.11.0
    Slapper 1.2.9 by jojoe77777 for API(s) 1.12.0
    SpawnWithItems 2.1 by LDX for API(s) 1.1.0, 2.0.0
    SummonMCPE 1.0.0 by for API(s) 1.4.0
    TimeCommander 2 by LDX for API(s) 1.0.0, 1.1.0, 1.2.0, 1.3.0, 1.4.0
    TradePro 0.3.3 by for API(s) 1.4.0
    VipGate 1.0.0 by for API(s) 1.2.0
    VoteReward 2.1 by LDX for API(s) 1.0.0, 1.1.0, 1.2.0, 1.3.0
    WarnPlayer 1.0.0 by applqpak for API(s) 1.0.0
    Wolfbit-HG beta_1.7 by xBeastMode for API(s) 1.13.0
    WorldEditArt 1.0.0_beta_dev_#048 by PEMapModder for API(s) 1.0.0
    WorldProtect 2.2.0 by aliuly for API(s) 1.10.0, 1.12.0
    Jail 2.0.3 by hoyinm14mc for API(s) 1.13.0, 1.13.1
    KillRate 2.1.1 by aliuly for API(s) 1.12.0
    MyPlot 1 by Wies and Exxarion for API(s) 1.0.0
    PureChat 1.3.3 by 64FF00 for API(s) 1.11.0
    SignWarp 1.5.1 by aliuly for API(s) 1.10.0, 1.12.0
    SimpleWarp 2.1.0 by Falkirks for API(s) 1.12.0
    SimplePortals 0.0.3 by Falkirks for API(s) 1.10.0
    I see it says "Error: Call to undefined method pocketmine\event\player\PlayerDeathEvent::getPlayer()" so how can I get the name of the player that died so I can teleport them to the worlds spawn?
  2. Kvetinac97
    Offline

    Kvetinac97 Active Member Plugin Developer

    Joined:
    Nov 17, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    Kvetinac97
    PHP:
    $event->getEntity();
    Hittmana likes this.
  3. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Thanks. What do I have to
    PHP:
    use
    with it?
  4. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    No
  5. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    What do you mean? I don't have to include anything for it?
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    No
    PHP:
    $player->teleport($this->getServer()->getLevelByName($main)->getSafeSpawn());
    Have you checked if the world $main really exists before calling methods on it?

    TIP: Load or check given levels in config on plugin enable and if there is something wrong - disable plugin with error message.

    I see that you are using wrong doc comment
    PHP:
    /*
     * @param Player $player
     */
    But it should look like this
    PHP:
     /**
      * @var Player $player
      */
    @param - Parameter of function
    @var - Describes variable and shows what it holds
    Last edited: Apr 21, 2016
  7. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Teleporting on PlayerDeathEvent doesn't make sense.. Do you really want to teleport a dead player who respawns somewhere else? Either use EntityDamageEvent(if the damage is bigger than the players health) and teleport there(also cancel so he doesn't really die) or use Player::setSpawn()
  8. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    I didn't think of that :confused:. That makes much more sense. Im going to change that now.
  9. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    How do I cancel an event? (First time I needed to cancel an event)
  10. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    $event->setCancelled(), while $event is your event and it implements Cancellable
    applqpak and Hittmana like this.
  11. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    setCancelled(bool) [I think true is the standard]
    Hittmana likes this.
  12. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Thank you! If I release it I will give you credit for helping
  13. Nawaf_Craft1b
    Offline

    Nawaf_Craft1b Active Member Plugin Developer

    Joined:
    May 8, 2015
    Posts:
    232
    Plugins:
    1
    Minecraft User:
    Nawaf_Craft1b
    Just Lean PM-API
    applqpak likes this.
  14. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Here is the new code:
    PHP:
    <?php

    namespace Hittmana\SpawnPro;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Player;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\Cancellable;

    class 
    MainClass extends PluginBase implements Listener
    {
      public function 
    onEnable()
            {
            
    $this->getLogger()->info("SpawnPro enabled v1.0.0");
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            }
        public function 
    onDisable()
            {
            
    $this->getLogger()->info("SpawnPro disabled v1.0.0");
            }  
        public function 
    onEntityDamage(EntityDamageEvent $event)
            {
            
    $cfg $this->getConfig()->getAll();
            
    $main $cfg["Home-World"]; 
            
    $enable $cfg["Enable-Respawn-in-death-world"];
            
    $player $event->getEntity();
            
    $level $player->getLevel()->getName();
            if(
    $event->getEntity() instanceof Player){
                if (
    $enable == "yes")
                    {
                        
    /**
                        * @var Player $player
                        */
                        
    $event->setCancelled();
                        
    $level $this->getServer()->getLevelByName($level);
                        
    $player->teleport($this->getServer()->getLevelByName($level)->getSafeSpawn());
                    }
                    else
                    {
                        
    /**
                        * @var Player $player
                        */
                        
    $event->setCancelled();
                        
    $player->teleport($this->getServer()->getLevelByName($main)->getSafeSpawn());
                    }
            }
            }
            function 
    onJoin(PlayerJoinEvent $event)
            {
                
    /**
                * @var Player $player
                */
                
    $cfg $this->getConfig()->getAll();
                
    $main $cfg["Home-World"]; 
                
    $player $event->getPlayer();
                
    $player->teleport($this->getServer()->getLevelByName($main)->getSafeSpawn());
            }
    }
    This will work correctly, right? BTW I know I haven't checked whether or not $main is actually a world. I will add this later once I know that this code will actually work.
  15. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    This will always teleport the player when he gets damage. Check if the getDamage() of the event is bigger than getHealth() of the player.
    applqpak likes this.
  16. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Ohh Ok. Other than that will this work?
  17. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Ok here is my code for getting health and damage:
    PHP:
    if ($event->getDamage() > $player->getHealth())
    This is correct, right?
  18. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
    if(($player->getHealth() - $event->getDamage()) <= 0)
    applqpak and Vaivez66 like this.
  19. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Actually, both is correct. But it should be
    PHP:
    if ($event->getDamage() >= $player->getHealth())
    applqpak likes this.

Share This Page

Advertisement