当玩家进入服务器时,我希望每秒在记分牌中更新团队的“硬币”和“代币”。当玩家离开服务器时,BukkitRunnable应该被中断。
我做了一些实验,找到了一个解决方案。如果我写得高效实用,我会对你对代码的看法感兴趣。
“updateScoreboard()”方法位于PlayerJoinEvent侦听器中。
你有什么改进的建议吗?
public class ScoreBoard {
public void setScoreBoard(Player player) {
Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
Objective object = board.registerNewObjective("returnscoreboard", "dummy");
object.setDisplayName("§cTest");
object.setDisplaySlot(DisplaySlot.SIDEBAR);
Team satoshis = board.registerNewTeam("satoshis");
Team tokens = board.registerNewTeam("tokens");
object.getScore("§a").setScore(6);
object.getScore("§esatoshis:").setScore(5);
object.getScore("§b").setScore(4);
object.getScore("§c").setScore(3);
object.getScore("§etokens").setScore(2);
object.getScore("§d").setScore(1);
object.getScore("§f").setScore(0);
satoshis.addEntry("§b");
satoshis.setPrefix("≫ §a" + API_Geld.getSatoshis(player.getUniqueId()));
tokens.addEntry("§d");
tokens.setPrefix("≫ §a" + API_Geld.getReturnTokens(player.getUniqueId()));
player.setScoreboard(board);
}
public void updateScoreboard(Player player) {
Scoreboard board = player.getScoreboard();
Team satoshis = board.getTeam("satoshis");
Team tokens = board.getTeam("tokens");
BukkitRunnable tester = new BukkitRunnable() {
@Override
public void run() {
if(player.isOnline()) {
satoshis.setPrefix("≫ §a" + API_Geld.getSatoshis(player.getUniqueId()));
tokens.setPrefix("≫ §a" + API_Geld.getReturnTokens(player.getUniqueId()));
System.out.println("RELOAD");
} else {
System.out.println("OFFLINE");
cancel();
}
}
};
tester.runTaskTimer(Main.getPlugin(), 0, 40);
}
}
我可以向你保证,不会有更多的添加或删除。
public class ScoreBoard {
public void setScoreBoard(Player player) {
Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
Objective object = board.registerNewObjective("returnscoreboard", "dummy");
object.setDisplayName("§cTest");
object.setDisplaySlot(DisplaySlot.SIDEBAR);
Team satoshis = board.registerNewTeam("satoshis");
Team tokens = board.registerNewTeam("tokens");
object.getScore("§a").setScore(6);
object.getScore("§esatoshis:").setScore(5);
object.getScore("§b").setScore(4);
object.getScore("§c").setScore(3);
object.getScore("§etokens").setScore(2);
object.getScore("§d").setScore(1);
object.getScore("§f").setScore(0);
satoshis.addEntry("§b");
satoshis.setPrefix("≫ §a" + API_Geld.getSatoshis(player.getUniqueId()));
tokens.addEntry("§d");
tokens.setPrefix("≫ §a" + API_Geld.getReturnTokens(player.getUniqueId()));
player.setScoreboard(board);
}
public void updateScoreboard(Player player) {
Scoreboard board = player.getScoreboard();
Team satoshis = board.getTeam("satoshis");
Team tokens = board.getTeam("tokens");
BukkitRunnable tester = new BukkitRunnable() {
@Override
public void run() {
if(player.isOnline()) {
satoshis.setPrefix("≫ §a" + API_Geld.getSatoshis(player.getUniqueId()));
tokens.setPrefix("≫ §a" + API_Geld.getReturnTokens(player.getUniqueId()));
System.out.println("RELOAD");
} else {
System.out.println("OFFLINE");
cancel();
}
}
};
tester.runTaskTimer(Main.getPlugin(), 0, 40);
}
}