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

Custom Death Message help

Comments in 'Plugin Development' started by CrazedMiner, May 24, 2015.

  1. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    The code for my Join and Quit Events is working but for some reason my Death Event isn't, instead of displaying my custom message the out put for both public and private is 1 in-game and on Console.
    Code:
    PHP:
    public function onDeath(PlayerDeathEvent $event) {
            
    $player $event->getEntity()->getPlayer();
            
    $killer $event->getEntity()->getLastDamageCause()->getDamager();
            if(
    $killer instanceof Player || $player instanceof Player) {
                
    $publicdm str_replace("@player"$player->getName(), $this->getConfig()->get("Public Death Message")) || str_replace("@killer"$killer->getPlayer()->getName(), $this->getConfig()->get("Private Death Message"));;
                
    $privatedm str_replace("@player"$player->getName(), $this->getConfig()->get("Private Death Message")) || str_replace("@killer"$killer->getPlayer->getName(), $this->getConfig()->get("Private Death Message"));
                
    $event->setDeathMessage($publicdm);
                
    $player->getPlayer()->sendMessage($privatedm);
            }
            if(
    $this->getConfig()->get("No Drops on Death") === true) {
                
    $event->setDrops(array(Item::get(000)));
            }
        }
    I'm pretty sure the problem has something to do with the str_replace()....
    Any help is appreciated, thanks! :)
  2. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    I think the problem is also in your config. You must never put spaces in YAML keys. $this->getConfig()->get("Private Death Message") should be $this->getConfig()->get("Private_Death_Message") and also in your config obviously.
    2. you put two ";" at the first str_replace.
    3. $player = $event->getEntity()->getPlayer(); should be directly $player = $event->getEntity(); because the event is only for Player deaths and so getEntity() already returns a Player instance
    4. $player instance of Player is useless for what I've said before
    PEMapModder and CrazedMiner like this.
  3. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    I changed the things you picked up on and it still just gives 1 as a message in chat...
    PHP:
    public function onDeath(PlayerDeathEvent $event) {
            
    $player $event->getEntity();
            
    $killer $event->getEntity()->getLastDamageCause()->getDamager();
            if(
    $killer instanceof Player) {
                
    $publicdm str_replace("@player"$player->getName(), $this->getConfig()->get("Public-Death-Message")) || str_replace("@killer"$killer->getPlayer()->getName(), $this->getConfig()->get("Private-Death-Message"));
                
    $privatedm str_replace("@player"$player->getName(), $this->getConfig()->get("Private-Death-Message")) || str_replace("@killer"$killer->getPlayer()->getName(), $this->getConfig()->get("Private-Death-Message"));
                
    $event->setDeathMessage($publicdm);
                
    $player->getPlayer()->sendMessage($privatedm);
            }
            if(
    $this->getConfig()->get("No Drops on Death") === true) {
                
    $event->setDrops(array(Item::get(000)));
            }
        }
  4. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    Do str_replace one by one
    PHP:
    $publicdm $this->getConfig()->get("Public-Death-Message");
    $privatedm $this->getConfig()->get("Private-Death-Message");
    //Public
    $publicdm str_replace("@player"$player->getName(), $publicdm);
    $publicdm str_replace("@killer"$killer->getPlayer()->getName(), $publicdm);
    //Private
    $privatedm str_replace("@player"$player->getName(), $privatedm);
    $privatedm str_replace("@killer"$killer->getPlayer()->getName(), $privatedm);
    CrazedMiner likes this.
  5. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    Thanks for the help it works now :D

    I had a feeling the str_replace()'s were the problem but I didn't know how to fix it :p
    EvolSoft likes this.
  6. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    Now when a player does /kill it crashes the server because of it trying to get the damager...
    Code:
    PHP:
    public function onDeath(PlayerDeathEvent $event) {
            
    $player $event->getEntity();
            
    $killer $event->getEntity()->getLastDamageCause()->getDamager();
            if(
    $killer instanceof Player) {
                
    $ipublicdm str_replace("@player"$player->getName(), $this->getConfig()->get("Public-Death-Message"));
                
    $publicdm str_replace("@killer"$killer->getPlayer()->getName(), $ipublicdm);
                
    $iprivatedm str_replace("@player"$player->getName(), $this->getConfig()->get("Private-Death-Message"));
                
    $privatedm str_replace("@killer"$killer->getPlayer()->getName(), $iprivatedm);
                
    $event->setDeathMessage($publicdm);
                
    $player->getPlayer()->sendMessage($privatedm);
            }
            if(
    $this->getConfig()->get("No Drops on Death") === true) {
                
    $event->setDrops(array(Item::get(000)));
            }
        }
    Crash dump:
    Code:
    PocketMine-MP Crash Dump Sun May 24 19:17:40 CHOT 2015
    
    Error: Call to undefined method pocketmine\event\entity\EntityDamageEvent::getDamager()
    File: /Shield_Plugin_1/src/Shield_Plugin_1/Main
    Line: 102
    Type: E_ERROR
    
    THIS CRASH WAS CAUSED BY A PLUGIN
    BAD PLUGIN: Shield_Plugin_1 v1
    
    Code:
    [93]         }
    [94]         if($this->getConfig()->get("Teleport-to-Spawn-on-Quit") === true) {
    [95]             $spawn = $this->getServer()->getDefaultLevel()->getSpawnLocation();
    [96]             $player->teleport($spawn);
    [97]         }
    [98]     }
    [99]    
    [100]     public function onDeath(PlayerDeathEvent $event) {
    [101]         $player = $event->getEntity();
    [102]         $killer = $event->getEntity()->getLastDamageCause()->getDamager();
    [103]         if($killer instanceof Player) {
    [104]             $ipublicdm = str_replace("@player", $player->getName(), $this->getConfig()->get("Public-Death-Message"));
    [105]             $publicdm = str_replace("@killer", $killer->getPlayer()->getName(), $ipublicdm);
    [106]             $iprivatedm = str_replace("@player", $player->getName(), $this->getConfig()->get("Private-Death-Message"));
    [107]             $privatedm = str_replace("@killer", $killer->getPlayer()->getName(), $iprivatedm);
    [108]             $event->setDeathMessage($publicdm);
    [109]             $player->getPlayer()->sendMessage($privatedm);
    [110]         }
    [111]         if($this->getConfig()->get("No Drops on Death") === true) {
    [112]             $event->setDrops(array(Item::get(0, 0, 0)));
    
    Backtrace:
    #0 /src/pocketmine/Server(0): pocketmine\Server->crashDump()
    #1 /src/pocketmine/plugin/MethodEventExecutor(36): Shield_Plugin_1\Main->onDeath()
    #2 /src/pocketmine/plugin/RegisteredListener(96): pocketmine\plugin\MethodEventExecutor->execute()
    #3 /src/pocketmine/plugin/PluginManager(670): pocketmine\plugin\RegisteredListener->callEvent()
    #4 /src/pocketmine/Player(2634): pocketmine\plugin\PluginManager->callEvent()
    #5 /src/pocketmine/entity/Entity(393): pocketmine\Player->kill()
    #6 /src/pocketmine/Player(2652): pocketmine\entity\Entity->setHealth()
    #7 /src/pocketmine/command/defaults/KillCommand(54): pocketmine\Player->setHealth()
    #8 /src/pocketmine/command/SimpleCommandMap(183): pocketmine\command\defaults\KillCommand->execute()
    #9 /src/pocketmine/Server(1826): pocketmine\command\SimpleCommandMap->dispatch()
    #10 /src/pocketmine/Player(2199): pocketmine\Server->dispatchCommand()
    #11 /src/pocketmine/network/RakLibInterface(192): pocketmine\Player->handleDataPacket()
    #12 /src/raklib/server/ServerHandler(92): pocketmine\network\RakLibInterface->handleEncapsulated()
    #13 /src/pocketmine/network/RakLibInterface(137): raklib\server\ServerHandler->handlePacket()
    #14 /src/pocketmine/Server(2215): pocketmine\network\RakLibInterface->process()
    #15 /src/pocketmine/Server(2109): pocketmine\Server->tick()
    #16 /src/pocketmine/Server(1985): pocketmine\Server->tickProcessor()
    #17 /src/pocketmine/Server(1702): pocketmine\Server->start()
    #18 /src/pocketmine/PocketMine(442): pocketmine\Server->__construct()
    #19 (1): ()
    #20 (0): {main}()
    
    PocketMine-MP version: 1.4.1dev #974 [Protocol 20; API 1.11.0]
    Git commit: 0000000000000000000000000000000000000000
    uname -a: Windows NT JACK 6.1 build 7601 (Windows 7 Enterprise Edition Service Pack 1) i586
    PHP Version: 5.6.4
    Zend version: 2.6.0
    OS : WINNT, win
    
    Loaded plugins:
    DevTools 1.10.0 by PocketMine Team for API(s) 1.3.1
    Shield_Plugin_1 1 by CrazedMiner for API(s) 1.8.0
    [/CODE}
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    1. It doesn't have a problem.
    2. Yes it should be fixed, although it only causes a minor performance issue.
    3. :+1: I said that a million times and everyone just ignores me.
    4. Same with 3.
    Why $killer->getPlayer()?
    EvolSoft and CrazedMiner like this.

Share This Page

Advertisement