作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我编码时,我测试了我的插件,这似乎发生了:基本上,当一名玩家杀死另一名玩家时,他的击杀数就会增加一。然而,当一名玩家完成 5 次击杀时,所有其他玩家也会如此。这是我的代码:
public class KillDeathCounter extends JavaPlugin {
public void onEnable() {
new PlayerListener(this);
getLogger().info("Players will not be able to kit spam.");
this.getConfig().addDefault("playerkills", 0);
this.getConfig().addDefault("playerdeaths", 0);
this.getConfig().options().copyDefaults(true);
saveConfig();
}
public void onDisable() {
getLogger().info("Kill Death Counter has been disabled.");
saveConfig();
}
public boolean onCommand(CommandSender sender, Command cmd, String label,
String[] args) {
if (cmd.getName().equalsIgnoreCase("digipvp")) {
Player p = (Player) sender;
p.sendMessage(ChatColor.AQUA
+ "Our PVP plugin was developed by Pierre Lichtlé, the owner, also known as Master Digi."
+ " If you would like to contact him for a plugin, send him an email at developer.petlamb@gmail.com."
+ " Thanks for playing on our server!");
return true;
}
return false;
}
}
package me.katsunicalis.plugin;
import org.bukkit.ChatColor;
import org.bukkit.Statistic;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
public class PlayerListener implements Listener {
KillDeathCounter kdc;
public PlayerListener(KillDeathCounter plugin) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
kdc = plugin;
}
@EventHandler
public void playerKillsPlayer(EntityDeathEvent e) {
Entity deade = e.getEntity();
Entity killer = e.getEntity().getKiller();
if (killer instanceof Player && deade instanceof Player){
int kills = kdc.getConfig().getInt("playerkills");
int deaths = kdc.getConfig().getInt("playerdeaths");
kdc.getConfig().set("playerkills", kills += 1);
kdc.getConfig().set("playerdeaths", deaths += 1);
killer.sendMessage("You now have " + kills + " kills!");
deade.sendMessage("You now have " + deaths + " deaths!");
}
}
}
非常感谢大家! :)
最佳答案
问题在于,每次一个玩家杀死另一个玩家时,您都会增加相同的变量。要解决此问题,您应该为每个玩家使用不同的变量,例如 "kills.playerUUIDHere"
和 "deaths.playerUUIDHere"
。
获取玩家的UUID
,您可以使用player.getUniqueId()
,然后获取 UUID
的 String
表示形式,您可以使用 uuid.toString()
。因此,您可以执行以下操作来更改玩家的击杀数:
String uuid = player.getUniqueId().toString();
int kills = kdc.getConfig().getInt("playerkills." + uuid);
kdc.getConfig().set("playerkills." + uuid, kills += 1);
相同的逻辑可用于死亡,因此,您可以在 playerKillsPlayer()
方法中使用类似的内容:
@EventHandler
public void playerKillsPlayer(EntityDeathEvent e) {
Entity dead = e.getEntity();
Entity killer = e.getEntity().getKiller();
if(killer instanceof Player && dead instanceof Player){
String killerUUID = ((Player) killer).getUniqueId().toString();
String deadUUID = ((Player) dead).getUniqueId().toString();
int kills = kdc.getConfig().getInt("playerkills." + killerUUID);
int deaths = kdc.getConfig().getInt("playerdeaths." + deadUUID);
kdc.getConfig().set("playerkills." + killerUUID + , kills += 1);
kdc.getConfig().set("playerdeaths." + deadUUID, deaths += 1);
killer.sendMessage("You now have " + kills + " kills!");
dead.sendMessage("You now have " + deaths + " deaths!");
}
}
关于java - 每个玩家的击杀次数始终相同。 (布基特),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30198749/
我是一名优秀的程序员,十分优秀!