无法将 Bukkit 记分牌分数转换为字符串

Cannot convert Bukkit Scoreboard score to String

这是我的 Bukkit 插件代码

import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.scoreboard.*;
import org.bukkit.Bukkit;
//import org.bukkit.*;

public class GetScore extends JavaPlugin{


@Override
public void onEnable() {
    // TODO Insert logic to be performed when the plugin is enabled
}

@Override
public void onDisable() {
    // TODO Insert logic to be performed when the plugin is disabled
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("getscore")) { // If the player typed /basic then do the following...
        // doSomething
        ScoreboardManager manager = Bukkit.getScoreboardManager();
        Scoreboard board = manager.getMainScoreboard();
        //Scoreboard sb = new Scoreboard();
        Objective objective = board.getObjective("Kill");
        Score score = objective.getScore("John");
        //sender.sendMessage(score.toString());
        sender.sendMessage(score.toString());
        return true;
    } //If this has happened the function will return true. 
        // If this hasn't happened the value of false will be returned.
    return false; 
}
}

问题是插件编译没有错误,但是当我输入 /getscore 时,我得到了这个: org.bukkit.craftbukkit.v1_7_R4.scoreboard.CraftScore@16f690c 当然这不是我想要的,它应该输出约翰在 objective Kill.Which 上的分数是 38

toString()的默认值如下: getClass().getName() + '@' + Integer.toHexString(hashCode())

您必须为 Score 的 toString() 方法提供覆盖,并在其中 return 适当的值。

要在 bukkit 中获得玩家得分,您应该对您拥有的 Score 对象调用 getScore()。这将 return 一个包含玩家得分的整数。

在调用 getScore() 之前,您应该验证您有一个 Score 对象并且它不为空。

ScoreboardManager manager = Bukkit.getScoreboardManager();
Scoreboard board = manager.getMainScoreboard();
Objective objective = board.getObjective("Kill");
Score score = objective.getScore("John");
int playerKills;
if(score == null) { // John didn't make any kills, and hasn't entered the scoeboard yet
    playerKills = 0;
} else {
    playerKills = score.getScore();
}
sender.sendMessage("Your kills: " + String.valueOf(score));

上面大部分代码是从你的问题中复制过来的,只调整了最后几行。在 运行 代码之前,通过 运行 /scoreboard objectives add Kills totalKillCount 或使用您自己的代码从代码中创建 objective 确保 kills objective 存在。