Hi guys its LeNewbFuryTacticz again failing to create yet another plugin 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: <?phpnamespace 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 != 0 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 === 1 and $deaths === 0 or $kills === 0 and $deaths === 1){ $player->sendPopup(TextFormat::GOLD."Kills: ".TextFormat::WHITE.$kills.TextFormat::GOLD." Deaths: ".TextFormat::WHITE.$deaths); } if($kills > 0 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 === 0 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
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.
You need to check. getLastDamageCause may return null. Or return an EntityDamageEvent that isn't an EntityDamageByEntityEvent.
I currently cant test the plugin, but like this: PHP: if($event->getEntity()->getLastDamageCause() != null){ //blah runs other code}
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(); } }
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(); } }
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.
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