所以我正在制作杀戮计数器插件,我让听众听取死亡并杀死它看起来像这样:
@EventHandler
public void killPlayer(EntityDeathEvent e) {
Entity deadEntity = e.getEntity();
Entity killer = e.getEntity().getKiller();
if (killer instanceof Player && deadEntity instanceof Player) {
Player player = (Player) killer;
Player dead = (Player) deadEntity;
int killcount = 0;
int deathcount = 0;
String killpath = "playerkills." + player.getName().toLowerCase();
String deathpath = "playerdeaths." + dead.getName().toLowerCase();
if (config.contains(killpath)) {
killcount = config.getInt(killpath);
}
if (config.contains(deathpath)) {
deathcount = config.getInt(deathpath);
}
config.set(killpath, killcount +1);
config.set(deathpath, deathcount +1);
plugin.saveConfig();
}
我已经尝试制作“预听众”,以防止错误发生,但它无法正常工作。这是我做的一段代码:
@EventHandler
public void playerDeath(PlayerDeathEvent e){
Entity entity = e.getEntity();
EntityDamageEvent entityDamageEvent = entity.getLastDamageCause();
if(entityDamageEvent.getCause() == EntityDamageEvent.DamageCause.VOID){
//Player died from void
}
}
但我仍然得到同样的错误。这是错误:
答案 0 :(得分:2)
事情是:
Void不是Bukkit中的实体。
因此,你的行
Entity killer = e.getEntity().getKiller();
由于虚空不是实体而引发错误。
如果它不是实体,the getKiller() method returns null!
对于常规修复,请在使用之前检查你的杀手是否为空。
if (killer != null)//Continue