无法向玩家显示独立记分牌
Unable to show players an independent scoreboard
我正在制作玩家击杀计数器。
我可以记录杀戮和死亡,我可以向所有玩家展示它们,但我很难向玩家展示他们的杀戮,而不是其他人的。
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);
}
}
}
您需要为每个玩家创建不同的记分板,因为向他们显示不同的值需要创建不同的记分板。
要做的更改是将每个 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;
}
};
剩下的就是连接事件以处理玩家参数,这在下面的 bold 中完成:
@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
showScoreboard(e);
}
public void showScoreboard(Player player) {
player.setScoreboard(objProvider.get(player).getScoreboard());
}
可以通过遵循此模型相应地实现死亡计数器,并且完成保存/加载过程是微不足道的,遵循 the documentation for Map 并插入和遍历记录。
我正在制作玩家击杀计数器。
我可以记录杀戮和死亡,我可以向所有玩家展示它们,但我很难向玩家展示他们的杀戮,而不是其他人的。
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);
}
}
}
您需要为每个玩家创建不同的记分板,因为向他们显示不同的值需要创建不同的记分板。
要做的更改是将每个 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;
}
};
剩下的就是连接事件以处理玩家参数,这在下面的 bold 中完成:
@EventHandler public void onPlayerJoin(PlayerJoinEvent e) { showScoreboard(e); } public void showScoreboard(Player player) { player.setScoreboard(objProvider.get(player).getScoreboard()); }
可以通过遵循此模型相应地实现死亡计数器,并且完成保存/加载过程是微不足道的,遵循 the documentation for Map 并插入和遍历记录。