Marc Towler
Marc Towler

Reputation: 705

Unable to show players an independent scoreboard

I'm making a player kill counter.

I can record kills and deaths, I can show them to all players but I'm struggling with showing a player just their kills and not those of others.

package uk.co.marctowler.Scores;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.*;

import java.io.File;
import java.io.IOException;


public class core extends JavaPlugin implements Listener {

    File statsFile;
    FileConfiguration stats;
    ScoreboardManager sbm = Bukkit.getScoreboardManager();
    Scoreboard board = sbm.getNewScoreboard();
    Objective obj;



    @Override
    public void onEnable() {
        getLogger().info("Loading Hardcore Score files");

        if((statsFile = new File(getDataFolder(), "scores.yml")).exists()) {
            stats = YamlConfiguration.loadConfiguration(statsFile);
        } else {
            statsFile = new File(getDataFolder(), "scores.yml");
            stats = YamlConfiguration.loadConfiguration(statsFile);
        }

        getServer().getPluginManager().registerEvents(this, this);

        obj = board.registerNewObjective("Kill Stats", "playerKillCount");
        obj.setDisplayName("Kill Count");
        obj.setDisplaySlot(DisplaySlot.SIDEBAR);
        Score score = obj.getScore(Bukkit.getOfflinePlayer(ChatColor.RED + "Kills:"));
        showScoreboard();
    }

    @Override
    public void onDisable() {
        getLogger().info("Saving Scores");

        saveFile();

    }

    public void addKill(String player) {
        getLogger().info("Kills set to: " + (getKills(player) + 1));
        stats.set("Players."+player+".Kills", getKills(player)+1);
        board.getObjective(DisplaySlot.SIDEBAR).getScore(player).setScore(getKills(player));
        saveFile();
    }

    public int getKills(String player) {
        try {
            return stats.getInt("Players."+player+".Kills");
        } catch (Exception e) {
            return 0;
        }
    }

    public void saveFile() {
        try {
            stats.save(statsFile);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public int getDeaths(String player) {
        try {
            return stats.getInt("Players."+player+".Deaths");
        } catch (Exception e) {
            return 0;
        }
    }

    public void addDeath(String player) {
        stats.set("Players."+player+".Deaths", getDeaths(player)+1);
        saveFile();
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e) {
        showScoreboard();
    }

    @EventHandler
    public void onKill(PlayerDeathEvent e){
        if(e.getEntity().getKiller() instanceof Player){
            Player victim = e.getEntity();
            Player killer = victim.getKiller();

            addKill(killer.getName());
            addDeath(victim.getName());
        }
    }

    public void showScoreboard() {
        for(Player current : Bukkit.getOnlinePlayers()) {

            current.setScoreboard(board);

        }

    }
}

Upvotes: 1

Views: 2120

Answers (1)

Unihedron
Unihedron

Reputation: 11051

You need to create a different Scoreboard for each player, as showing them separate values would require creating different Scoreboards.

The changes to make are to map each Player to their own Scoreboard:

HashMap<Player, Scoreboard> boards = new HashMap<>();
HashMap<Player, Objective> objProvider = new HashMap<Player, Objective>() {
    @Override
    get(Player player) {
        if (this.containsKey(player))
            return this.get(player);

        // Lazy initialization
        Scoreboard value = boards.get(player);
        if (value != null)
            return value;
        value = sbm.getNewScoreboard();
        boards.put(player, value);

        // Scoreboard initialization
        Objective obj = board.registerNewObjective("Kill Stats", "playerKillCount");
        obj.setDisplayName("Kill Count");
        obj.setDisplaySlot(DisplaySlot.SIDEBAR);

        this.put(player, obj);

        return value;
    }
};

From there all's left is to hook up the events to handle the player parameter as well, which is done in bold below:

@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
    showScoreboard(e);
}

public void showScoreboard(Player player) {
    player.setScoreboard(objProvider.get(player).getScoreboard());
}

Implementing deaths counter can be done correspondingly by following this model, and completing the save / load process is trivial par following the documentation for Map and inserting and iterating through records.

Upvotes: 2

Related Questions