所以这是我的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 当然这不是我想要的,它应该输出John的目标Kill.Which是38
答案 0 :(得分:1)
toString()
的默认值如下:
getClass().getName() + '@' + Integer.toHexString(hashCode())
您必须为Score
的toString()方法提供覆盖,并在那里返回适当的值。
答案 1 :(得分:0)
要获得玩家在bukkit中的分数,您应该在您拥有的分数对象上调用getScore()。这将返回一个包含玩家得分的int。
在你调用getScore()之前,你应该验证你有一个Score对象并且它不是null。
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
或使用您自己的代码从代码中生成目标,确保杀戮目标存在。