我当时是根据游戏制作一个名为“选择室”的插件,当我想创建一个记分板时,我将变量放入了计时器中。因为有一个带有变量的字符串,所以我不想使用记分板的纯红色文本。因此,我决定使用分数来显示计时器,但是我很快遇到了问题。我不知道如何更新分数,因此显示正确的时间。现在,我已打印分数以聊天以调试问题。谢谢!
这是我的代码:
package main;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Score;
import org.bukkit.scoreboard.Scoreboard;
public class MainClass extends JavaPlugin implements Listener {
int secondTimer = 30;
int minuteTimer = 0;
boolean running = false;
public void onEnable() {
getLogger().info("***********************");
getLogger().info("Choice Chamber Enabled");
getLogger().info("Choice Chamber V. " + getServer().getPluginManager().getPlugin("Choice Chamber").getDescription().getVersion());
getLogger().info("Written By @McMattGames");
getLogger().info("***********************");
}
public void onDisable() {
getLogger().info("***********************");
getLogger().info("Choice Chamber Disabled");
getLogger().info("***********************");
}
//commands
@EventHandler
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
secondTimer = 30;
minuteTimer = 0;
if (cmd.getName().equalsIgnoreCase("choice") || cmd.getName().equalsIgnoreCase("c")) {
Scoreboard board = getServer().getScoreboardManager().getNewScoreboard();
Objective objective = board.registerNewObjective("timer", "dummy");
Score score = objective.getScore(ChatColor.GREEN + "Timer: ");
Score score2 = objective.getScore(ChatColor.GREEN + "" + minuteTimer + ":" + secondTimer);
objective.setDisplayName(ChatColor.GOLD + "" + ChatColor.BOLD + "Choice Chamber");
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
score.setScore(-1);
score2.setScore(-2);
if (sender instanceof Player) {
if (args.length > 0) {
Player p = (Player) sender;
if (args[0].equalsIgnoreCase("start")) {
if (running == !true) {
Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] Round Starting!");
running = true;
p.setScoreboard(board);
Bukkit.getScheduler().runTaskTimer(this, new Runnable() {
public void run() {
secondTimer = secondTimer - 1;
if (secondTimer <= 0) {
minuteTimer = minuteTimer - 1;
secondTimer = 59;
}
if (minuteTimer < 0) {
Bukkit.getServer().getScheduler().cancelAllTasks();
Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] Round Over!");
running = false;
}
if (minuteTimer >= 0) {
if (secondTimer > 9) {
getServer().broadcastMessage(minuteTimer + ":" + secondTimer);
} else {
getServer().broadcastMessage(minuteTimer + ":0" + secondTimer);
}
}
}
}, 0, 20);
} else {
Bukkit.getServer().broadcastMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + ChatColor.RED + "Round Already Started");
}
} else {
if (args[0].equalsIgnoreCase("join")) {
if (running == true) {
p.setScoreboard(board);
p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + "Joining");
} else {
p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + ChatColor.RED + "No Game Started!");
}
} else {
p.sendMessage(ChatColor.WHITE + "[" + ChatColor.GOLD + "Choice Chamber" + ChatColor.WHITE + "] " + ChatColor.RED + "Invalad Command!");
}
}
}
}
}
return true;
}
}
最佳答案
如果更新Bukkit的默认记分板系统,该系统将闪烁,因此应使用SpigotLibs ScoreboardAPI对记分板系统进行编码。
https://www.spigotmc.org/resources/spigotlib.5925/
如果您需要其他帮助,可以通过Skype与我联系:gyurixdev