无法向玩家展示独立记分牌

时间:2015-08-06 19:10:43

标签: java bukkit

我正在制作一名玩家杀戮计数器。

我可以记录杀戮和死亡,我可以向所有玩家展示,但我正在努力向玩家展示他们的杀戮而不是其他玩家。

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);

        }

    }
}

1 个答案:

答案 0 :(得分:2)

您需要为每位玩家创建一个不同的记分板,因为要显示不同的记分牌,需要创建不同的记分板。

要做的更改是将每个Player映射到他们自己的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;
    }
};

从那里开始,剩下的就是连接事件来处理玩家参数,这可以在下面的粗体中完成:

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

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

执行死亡计数器可以通过遵循此模型相应地完成,并且完成保存/加载过程在the documentation for Map之后并且插入和迭代记录是微不足道的。