gpt4 book ai didi

java - 无法向玩家展示独立的记分牌

转载 作者:行者123 更新时间:2023-11-30 03:18:48 26 4
gpt4 key购买 nike

我正在制作一个玩家击杀计数器。

我可以记录杀戮和死亡,我可以向所有玩家展示它们,但我很难向玩家只展示他们的杀戮而不是其他人的杀戮。

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);

}

}
}

最佳答案

您需要为每个玩家创建不同的记分板,因为向他们显示单独的值需要创建不同的记分板。

要做的更改是将每个玩家映射到他们自己的记分板:

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;
}
};

接下来剩下的就是连接事件来处理播放器参数,这在下面粗体中完成:

@EventHandlerpublic void onPlayerJoin(PlayerJoinEvent e) {    showScoreboard(e);}public void showScoreboard(Player player) {    player.setScoreboard(objProvider.get(player).getScoreboard());}

可以按照此模型相应地实现死亡计数器,并且完成保存/加载过程是微不足道的,如 the documentation for Map 所示。以及插入和迭代记录。

关于java - 无法向玩家展示独立的记分牌,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31863960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com