在编写代码时,我测试了插件,这似乎发生了:
基本上,当一个玩家杀死另一个玩家时,他的杀死人数将增加一个。但是,例如当一个玩家杀死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 [email protected]."
+ " 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()
,然后可以使用String
来获取UUID
的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 - 每个玩家的击杀数始终相同。 (Bukkit),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30198749/