1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Scoreboard Kills

Discussion in 'Plugin Help' started by ben12145, Mar 6, 2021.

  1. ben12145

    ben12145 Creeper

    Messages:
    3
    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($players1"§2""SoupFFA");
                
    Scoreboard::setScoreboardEntry($players2"§bKills""SoupFFA");
                
    Scoreboard::setScoreboardEntry($players3"§2" $this->getConfig($name)->get("deaths"0), "SoupFFA");
                
    Scoreboard::setScoreboardEntry($players4"§7>>§f""SoupFFA");
                
    Scoreboard::setScoreboardEntry($players5"§2""SoupFFA");
                
    Scoreboard::setScoreboardEntry($players6"§bKills""SoupFFA");
                
    Scoreboard::setScoreboardEntry($players7"§7>>§f" .  $this->getConfig($name)->get("kills"0), "SoupFFA");
                
    Scoreboard::setScoreboardEntry($players8"§7""SoupFFA");
                
    Scoreboard::setScoreboardEntry($players9"§7""SoupFFA");
            }
        }


    }
     
  2. DavyCraft648

    DavyCraft648 Spider Jockey

    Messages:
    40
    GitHub:
    DavyCraft648
    What is getConfig(Player $player)?
    And what is getConfig(string $name)?
     
    Last edited: Mar 6, 2021
    ben12145 likes this.
  3. DavyCraft648

    DavyCraft648 Spider Jockey

    Messages:
    40
    GitHub:
    DavyCraft648
    I have tried this code and it works
    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($players1"§7+---------------+");
                
    Scoreboard::setLine($players2"§bDeaths");
                
    Scoreboard::setLine($players3"§7>>§c " self::$uConfig[$pName]->get("deaths"0));
                
    Scoreboard::setLine($players4"§7");
                
    Scoreboard::setLine($players5"§bKills");
                
    Scoreboard::setLine($players6"§7>>§a " .  self::$uConfig[$pName]->get("kills"0));
                
    Scoreboard::setLine($players7"§8");
                
    Scoreboard::setLine($players8"§7-----------------");
            }
        }
    }

    PHP:
    class ScoreboardUpdateTask extends Task
    {
       public function 
    onRun(int $currentTick)
       {
          
    Main::updateScoreboard();
       }
    }

    sorry, i not showing the Scoreboard class ;)
     
    ben12145 likes this.
  4. ben12145

    ben12145 Creeper

    Messages:
    3
    Holy Shit! Worked :) You are a fucking legend! Thank you for your big effort
     
    DavyCraft648 likes this.
  5. ben12145

    ben12145 Creeper

    Messages:
    3
    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));
     
  6. DavyCraft648

    DavyCraft648 Spider Jockey

    Messages:
    40
    GitHub:
    DavyCraft648
    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($ratio1); // A formatted version of number.
          
    }
       }
       return 
    "0.0";
    }
     
  7. timistwild

    timistwild Spider Jockey

    Messages:
    33
    Du you also know how to show it now in the scoreboard?
    PHP:
    Scoreboard::setScoreboardEntry($players9"§7>>§f" self::$uConfig[$pName]->get("K/D"0.0), "SoupFFA");
     
    Last edited: Mar 7, 2021
    max1245 likes this.
  8. DavyCraft648

    DavyCraft648 Spider Jockey

    Messages:
    40
    GitHub:
    DavyCraft648
    like I said before
    PHP:
    Scoreboard::setScoreboardEntry($players9"§7>>§f " self::getKillToDeathRatio($pName), "SoupFFA");

    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($ratio1);
          }
       }
       return 
    "0.0";
    }
     
    max1245 likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.