Hi! I made a code where the kills and deaths of an player are saved in a config. (Every player has his own) Now i want to show the Kills and Deaths in a scoreboard(the scoreboard is working fine). There is just the problem, that the kills and deaths are summarized. (Player 1 has two Kills. Player two has 3. The scoreboard shows 5 kills) Is there another way to get the id of the "owner" of the scoreboard? PHP: class Main extends PluginBase implements Listener { public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getScheduler()->scheduleRepeatingTask(new ScoreboardTask($this), 20); @mkdir($this->getDataFolder() . "Database/"); } public function onJoin(PlayerJoinEvent $event) { $player = $event->getPlayer(); $this->config = new Config($this->getDataFolder() . "Database/" . strtolower($player->getName()), Config::YAML, array( "kills" => 0, "deaths" => 0 )); $this->config; } public function onDeath(PlayerDeathEvent $event) { $player = $event->getPlayer(); $this->getConfig($player)->set("deaths", $this->config->get("deaths") + 1); $player = $event->getPlayer(); $killer = $player->getLastDamageCause(); if ($killer instanceof EntityDamageByEntityEvent) { $killer = $killer->getDamager(); if ($killer instanceof Player) { $this->getConfig($killer)->set("kills", $this->config->get("kills") + 1); } } $this->config->save(); } #Scoreboard public function scoreboard(): void { foreach ($this->getServer()->getOnlinePlayers() as $players) { $name = $players->getName(); //Create Scoreboard::removeScoreboard($players, "SoupFFA"); Scoreboard::createScoreboard($players, "§l§6SoupFFA", "SoupFFA"); //Set Entrys Scoreboard::setScoreboardEntry($players, 1, "§2", "SoupFFA"); Scoreboard::setScoreboardEntry($players, 2, "§bKills", "SoupFFA"); Scoreboard::setScoreboardEntry($players, 3, "§2" . $this->getConfig($name)->get("deaths", 0), "SoupFFA"); Scoreboard::setScoreboardEntry($players, 4, "§7>>§f", "SoupFFA"); Scoreboard::setScoreboardEntry($players, 5, "§2", "SoupFFA"); Scoreboard::setScoreboardEntry($players, 6, "§bKills", "SoupFFA"); Scoreboard::setScoreboardEntry($players, 7, "§7>>§f" . $this->getConfig($name)->get("kills", 0), "SoupFFA"); Scoreboard::setScoreboardEntry($players, 8, "§7", "SoupFFA"); Scoreboard::setScoreboardEntry($players, 9, "§7", "SoupFFA"); } }}
I have tried this code and it works Spoiler: Main PHP: class Main extends PluginBase implements Listener{ /** @var Config[] */ private static array $uConfig = []; public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getScheduler()->scheduleRepeatingTask(new ScoreboardUpdateTask(), 20 * 3); } public function onJoin(PlayerJoinEvent $event) { $player = $event->getPlayer(); $pName = $player->getName(); if (!is_dir($this->getDataFolder() . "Database/")) mkdir($this->getDataFolder() . "Database/"); self::$uConfig[$pName] = new Config($this->getDataFolder() . "Database/$pName.yml", Config::YAML, [ "kills" => 0, "deaths" => 0]); } public function onDeath(PlayerDeathEvent $event) { $player = $event->getPlayer(); $pName = $player->getName(); self::$uConfig[$pName]->set("deaths", self::$uConfig[$pName]->get("deaths", 0) + 1); $lastDam = $player->getLastDamageCause(); if ($lastDam instanceof EntityDamageByEntityEvent) { $killer = $lastDam->getDamager(); if ($killer instanceof Player) { self::$uConfig[$pName]->set("kills", self::$uConfig[$pName]->get("kills", 0) + 1); } } self::$uConfig[$pName]->save(); } public static function updateScoreboard(): void { foreach (Server::getInstance()->getOnlinePlayers() as $players) { $pName = $players->getName(); if (!isset(self::$uConfig[$pName])) return; Scoreboard::new($players, "SoupFFA", "§l§6SoupFFA"); Scoreboard::setLine($players, 1, "§7+---------------+"); Scoreboard::setLine($players, 2, "§bDeaths"); Scoreboard::setLine($players, 3, "§7>>§c " . self::$uConfig[$pName]->get("deaths", 0)); Scoreboard::setLine($players, 4, "§7"); Scoreboard::setLine($players, 5, "§bKills"); Scoreboard::setLine($players, 6, "§7>>§a " . self::$uConfig[$pName]->get("kills", 0)); Scoreboard::setLine($players, 7, "§8"); Scoreboard::setLine($players, 8, "§7-----------------"); } }} Spoiler: ScoreboardUpdateTask PHP: class ScoreboardUpdateTask extends Task{ public function onRun(int $currentTick) { Main::updateScoreboard(); }} sorry, i not showing the Scoreboard class
When i now want to divide Kills by Deaths. How would you do it? my try(isnt working xD): PHP: self::$uConfig[$pName]->set("K/D",self::$uConfig[$pName]->get("kills",0) / self::$uConfig[$pName]->get("deaths",0));
Spoiler: Code PHP: /*** Handle player death* @param PlayerDeathEvent $event*/public function onDeath(PlayerDeathEvent $event){ $player = $event->getPlayer(); $pName = $player->getName(); //self::$uConfig[$pName]->set("deaths", self::$uConfig[$pName]->get("deaths", 0) + 1); $lastDam = $player->getLastDamageCause(); if ($lastDam instanceof EntityDamageByEntityEvent) { $killer = $lastDam->getDamager(); if ($killer instanceof Player) { //self::$uConfig[$pName]->set("kills", self::$uConfig[$pName]->get("kills", 0) + 1); $kName = $killer->getName(); self::$uConfig[$pName]->set("deaths", self::$uConfig[$pName]->get("deaths", 0) + 1); // If you want to check if player death is because of pvp self::$uConfig[$pName]->set("K/D", $this->getKillToDeathRatio($pName)); self::$uConfig[$pName]->save(); // Don't forget to save the config! self::$uConfig[$kName]->set("kills", self::$uConfig[$kName]->get("kills", 0) + 1); // Sorry, i forgot self::$uConfig[$kName]->set("K/D", $this->getKillToDeathRatio($kName)); self::$uConfig[$kName]->save(); } }}/*** https://github.com/Ifera/KDR/blob/bc646159291bcc608d4f50cb4337dd9de8399d0a/src/JackMD/KDR/provider/YamlProvider.php#L97-L111* @param string $pName Player name* @return string*/public function getKillToDeathRatio(string $pName): string{ $kills = self::$uConfig[$pName]->get("kills", 0); $deaths = self::$uConfig[$pName]->get("deaths", 0); if($deaths !== 0) { $ratio = $kills / $deaths; if($ratio !== 0) { return number_format($ratio, 1); // A formatted version of number. } } return "0.0";}
Du you also know how to show it now in the scoreboard? PHP: Scoreboard::setScoreboardEntry($players, 9, "§7>>§f" . self::$uConfig[$pName]->get("K/D", 0.0), "SoupFFA");
like I said before Spoiler: code PHP: Scoreboard::setScoreboardEntry($players, 9, "§7>>§f " . self::getKillToDeathRatio($pName), "SoupFFA"); Spoiler: code PHP: public static function getKillToDeathRatio(string $pName): string // I'll make this static{ $kills = self::$uConfig[$pName]->get("kills", 0); $deaths = self::$uConfig[$pName]->get("deaths", 0); if($deaths !== 0) { $ratio = $kills / $deaths; if($ratio !== 0) { return number_format($ratio, 1); } } return "0.0";}