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

Need help with my PlayerStats plugin.

Comments in 'Plugin Development' started by FuryTacticz, Sep 5, 2015.

  1. FuryTacticz
    Offline

    FuryTacticz New Member

    Joined:
    Aug 31, 2015
    Posts:
    23
    Minecraft User:
    FuryTacticz
    Hi guys its LeNewbFuryTacticz again failing to create yet another plugin :D

    Anyway guys im trying to make a plugin where a popup displays your kills, deaths and kill/death ratio.
    This is what I have so far and yes I know I cannot put two classes in one file as its not allowed for plugin submission, anyways heres the code:
    PHP:
    <?php

    namespace Stats;

    use 
    pocketmine\Server;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandExecutor;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\PluginCommand;
    use 
    pocketmine\Player;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\event\player\PlayerChatEvent;
    use 
    pocketmine\event\player\PlayerInteractEvent;
    use 
    pocketmine\event\player\PlayerDeathEvent;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\event\entity\EntityDamageByEntityEvent;
    use 
    pocketmine\event\entity\EntityLevelChangeEvent;
    use 
    pocketmine\event\block\BlockPlaceEvent;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\event\EventExecutor;
    use 
    pocketmine\event\EventPriority;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\tile\Sign;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\Player\getName;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\event\player\PlayerQuitEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\block\Block;
    use 
    pocketmine\event\player\PlayerMoveEvent;
    use 
    pocketmine\pocketmine\Level;
    use 
    pocketmine\entity\Effect;
    use 
    pocketmine\event\player\PlayerPreLoginEvent;



    class 
    Stats extends PluginBase implements Listener{
      
      
      
      
                   
     
        public function 
    onLoad(){
            
    $this->getLogger()->info(TextFormat::YELLOW."Loaded.");
        }

        public function 
    onEnable(){
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
      
        
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new AutoTask($this), 10);
      
        @
    mkdir($this->getDataFolder() . "PlayerStats/");
        }
      
        public function 
    onPlayerLogin(PlayerPreLoginEvent $event){
            
    $ign $event->getPlayer()->getName();
            
    $player $event->getPlayer();
            
    $file = ($this->getDataFolder()."PlayerStats/".$ign."_Stats.yml");  
                if(!
    file_exists($file)){
                    
    $this->PlayerFile = new Config($this->getDataFolder()."PlayerStats/".$ign."_Stats.yml"Config::YAML);
                    
    $this->PlayerFile->set("Kills"0);
                    
    $this->PlayerFile->set("Deaths"0);
                    
    $this->PlayerFile->save();
                }
            }
              
      
      
        public function 
    onDeath(PlayerDeathEvent $event){
            
    $victim $event->getEntity();
            
    $this->PlayerFile = new Config($this->getDataFolder()."PlayerStats/".$victim->getName()."_Stats.yml"Config::YAML);
            
    $i $this->PlayerFile->get("Deaths");
            
    $n $i+1;
            
    $this->PlayerFile->set("Deaths"$n);
            
    $this->PlayerFile->save();
        }
      
        public function 
    onKill(PlayerDeathEvent $event){
            
    $victim $event->getEntity();
            
    $killer $event->getEntity()->getLastDamageCause()->getDamager();
            if(
    $event->getEntity()->getLastDamageCause()->getDamager() instanceof Player){
                
    $this->PlayerFile = new Config($this->getDataFolder()."PlayerStats/".$killer->getName()."_Stats.yml"Config::YAML);
                
    $i $this->PlayerFile->get("Deaths");
                
    $n $i+1;
                
    $this->PlayerFile->set("Kills"$n);
                
    $this->PlayerFile->save();
            }
                  
        }
          
          
      
        public function 
    AutoPopup(){

          foreach(
    $this->getServer()->getLevelByName("world")->getPlayers () as $player){
                
    $this->PlayerFile = new Config($this->getDataFolder()."PlayerStats/".$player->getName()."_Stats.yml"Config::YAML);
                
    $kills $this->PlayerFile->get("Kills");
                
    $deaths $this->PlayerFile->get("Deaths");
                 if(
    $kills != and $deaths != 0){
                     
    $kdr $kills/$deaths;
                     
    $player->sendPopup(TextFormat::GOLD."Kills: ".TextFormat::WHITE.$kills.TextFormat::GOLD." Deaths: ".TextFormat::WHITE.$deaths.TextFormat::GOLD." KDR: ".TextFormat::WHITE.$kdr);
                 }
                 if(
    $kills === and $deaths === or $kills === and $deaths === 1){
                     
    $player->sendPopup(TextFormat::GOLD."Kills: ".TextFormat::WHITE.$kills.TextFormat::GOLD." Deaths: ".TextFormat::WHITE.$deaths);
                 }
                 if(
    $kills and $deaths 0){
                      
    $kdr $kills/$deaths;
                      
    $player->sendPopup(TextFormat::GOLD."Kills: ".TextFormat::WHITE.$kills.TextFormat::GOLD." Deaths: ".TextFormat::WHITE.$deaths.TextFormat::GOLD." KDR: ".TextFormat::WHITE.$kdr);
                 }
                 if(
    $kills === and $deaths === 0){
                     
    $player->sendPopup(TextFormat::GOLD."Kills: ".TextFormat::WHITE.$kills.TextFormat::GOLD." Deaths: ".TextFormat::WHITE.$deaths);
                 }
               
          }
    }
      

    }

    class 
    AutoTask extends PluginTask
    {
        public function 
    __construct(Plugin $owner) {
            
    parent::__construct($owner);
        }
        public function 
    onRun($currentTick){
            
    $this->getOwner()->AutoPopup();
        }
         
    }
    The issue is that the kills dont save properly HOWEVER deaths save perfectly o.0 this is the issue I need help to resolve.
    My next issue is with:
    PHP:
    $event->getEntity()->getLastDamageCause()->getDamager()
    If I do /kill <ign> it crashes the server (I havent tried but I suspect that fall damage will also crash the server, so how can I fix this?

    Many thanks in advance :) :) :)
    Last edited: Sep 5, 2015
  2. FuryTacticz
    Offline

    FuryTacticz New Member

    Joined:
    Aug 31, 2015
    Posts:
    23
    Minecraft User:
    FuryTacticz
  3. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    PHP:
    $i $this->PlayerFile->get("Deaths");
    $n $i+1;
    $this->PlayerFile->set("Kills"$n);
    $this->PlayerFile->save();
    Error is on the first line. You made $i = deaths.
    It should = kills
    Plus you have two PlayerDeathEvents. Just combine them into one event.
    applqpak and FuryTacticz like this.
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You need to check. getLastDamageCause may return null. Or return an EntityDamageEvent that isn't an EntityDamageByEntityEvent.
    applqpak and FuryTacticz like this.
  5. FuryTacticz
    Offline

    FuryTacticz New Member

    Joined:
    Aug 31, 2015
    Posts:
    23
    Minecraft User:
    FuryTacticz
    I currently cant test the plugin, but like this:
    PHP:
    if($event->getEntity()->getLastDamageCause() != null){
            
    //blah runs other code
    }
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    no
    PHP:
    if($event instanceof EntityDamageByEntityEvent){
    $attacker $event->getDamager();
    }
    applqpak and PEMapModder like this.
  7. FuryTacticz
    Offline

    FuryTacticz New Member

    Joined:
    Aug 31, 2015
    Posts:
    23
    Minecraft User:
    FuryTacticz
    PHP:
    public function onKill(PlayerDeathEvent $event){
            
    $victim $event->getEntity();
            if(
    $event->getEntity()->getLastDamageCause() != null){
                
    $killer $event->getEntity()->getLastDamageCause()->getDamager();
                
    $this->PlayerFile = new Config($this->getDataFolder()."PlayerStats/".$killer->getName()."_Stats.yml"Config::YAML);
                
    $i $this->PlayerFile->get("Kills");
                
    $n $i+1;
                
    $this->PlayerFile->set("Kills"$n);
                
    $this->PlayerFile->save();
            }
                 
        }
    Or like this:

    PHP:
    public function onKill(PlayerDeathEvent $event){
            
    $victim $event->getEntity();
            if(
    $event->getEntity()->getLastDamageCause() instanceof Player){
                
    $killer $event->getEntity()->getLastDamageCause()->getDamager();
                
    $this->PlayerFile = new Config($this->getDataFolder()."PlayerStats/".$killer->getName()."_Stats.yml"Config::YAML);
                
    $i $this->PlayerFile->get("Kills");
                
    $n $i+1;
                
    $this->PlayerFile->set("Kills"$n);
                
    $this->PlayerFile->save();
            }
                 
        }
  8. FuryTacticz
    Offline

    FuryTacticz New Member

    Joined:
    Aug 31, 2015
    Posts:
    23
    Minecraft User:
    FuryTacticz
    ohh okay thanks :)
  9. FuryTacticz
    Offline

    FuryTacticz New Member

    Joined:
    Aug 31, 2015
    Posts:
    23
    Minecraft User:
    FuryTacticz
    That didnt work for me so I did this and it worked :)
    PHP:
    public function onKill(PlayerDeathEvent $event){
            
    $victim $event->getEntity();
            if(
    $event->getEntity()->getLastDamageCause() instanceof EntityDamageByEntityEvent){
                
    $killer $event->getEntity()->getLastDamageCause()->getDamager();
                
    $this->PlayerFile = new Config($this->getDataFolder()."PlayerStats/".$killer->getName()."_Stats.yml"Config::YAML);
                
    $i $this->PlayerFile->get("Kills");
                
    $n $i+1;
                
    $this->PlayerFile->set("Kills"$n);
                
    $this->PlayerFile->save();
            }
                 
        }
  10. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Ow yeah, forgot ur using PlayerDeathEvent.
    applqpak likes this.
  11. FuryTacticz
    Offline

    FuryTacticz New Member

    Joined:
    Aug 31, 2015
    Posts:
    23
    Minecraft User:
    FuryTacticz
    Guys I require more help Basically my plugin works fine on my windows pc but when I use it on my server on my linux dedicated server i get this error and yes it 100% works on windows.

    [​IMG]
  12. xpyctum
    Offline

    xpyctum Active Member Plugin Developer

    Joined:
    Jan 13, 2014
    Posts:
    292
    Plugins:
    4
    Minecraft User:
    tylerdisney
    At least come up with another name, because I had already written a plugin
  13. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    on Enable() add
    PHP:
    @mkdir($this->getDataFolder());
    Before creating Config
    Last edited: Mar 28, 2016
    applqpak and I_Is_Payton_ like this.
  14. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    Use MySQL local datas are after a few weeks laagy
  15. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Not everyone has a MySQL database, or maybe he doesnt want to use one. I've been saving my data locally for as long as i can remember and can still have 120 players online with liitle to no lag.
    P.S: This thread is old
    applqpak and Primus like this.
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Or use SQLite3.
    Primus and applqpak like this.
  17. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
  18. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    But for what PocketMine supports by default, only SQLite3 and MySQL

Share This Page

Advertisement