好的,所以如果玩家被僵尸杀死(例子)我试图获取暴徒名称,但我总是得到一个NullPointerException。
我确信我在代码中搞砸了一些东西,但我现在真的很头疼,我无法找到问题所在。
以下是代码:
package me.parat26.listeners;
import me.parat26.utils.SettingsManager;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
public class PlayerDeathCauses implements Listener
{
SettingsManager config = SettingsManager.getInstance();
@EventHandler
public void checkDeathCauses(EntityDeathEvent e)
{
Player p = (Player) e.getEntity();
String victim = p.getDisplayName();
EntityDamageEvent entityDamageCause = p.getLastDamageCause();
DamageCause damage = entityDamageCause.getCause();
String mob = e.getEntity().getType().getName();
if (damage == DamageCause.DROWNING)
{
String death = config.parse("death.drowned").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.ENTITY_ATTACK)
{
String death = config.parse("death.mob").replace("<player>", victim).replace("<mob>", mob);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.ENTITY_EXPLOSION)
{
String death = config.parse("death.mob-explosion").replace("<player>", victim).replace("<mob>", mob);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.FALL)
{
String death = config.parse("death.fall").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.FALLING_BLOCK)
{
String death = config.parse("death.falling-block").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.FIRE)
{
String death = config.parse("death.fire").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.LAVA)
{
String death = config.parse("death.lava").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.LIGHTNING)
{
String death = config.parse("death.lightning").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.MAGIC)
{
String death = config.parse("death.magic").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.POISON)
{
String death = config.parse("death.poison").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.PROJECTILE)
{
String death = config.parse("death.projectile").replace("<player>", victim).replace("<mob>", mob);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.STARVATION)
{
String death = config.parse("death.starved").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.SUFFOCATION)
{
String death = config.parse("death.suffocated").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.SUICIDE)
{
String death = config.parse("death.suicide").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.VOID)
{
String death = config.parse("death.void").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
else if (damage == DamageCause.WITHER)
{
String death = config.parse("death.wither").replace("<player>", victim);
((PlayerDeathEvent) e).setDeathMessage(death);
}
}
}
答案 0 :(得分:3)
您的错误是将e.getEntity()
类型转换为(Player)
类型,而实体不是播放器。在将e.getEntity()
转换为(Player)
之前,您需要进行检查以确保Entity
属于Player
类型。
if(e.getEntity() instanceof Player){
Player p = (Player)e.getEntity();
}
否则每当羊,牛或其他东西死亡时,它会尝试将Cow
实体强制转换为Player
实体,这显然不会起作用。
另外,我不知道它实际上是否是Cow
类型,这只是一个例子。请务必遵循API!
希望这有帮助!
答案 1 :(得分:2)
基于此堆栈跟踪,您将获得ClassCastException。这意味着您正在尝试转换无效的类。您正试图将e.getEntity()强制转换为播放器
Player p = (Player) e.getEntity();
但是这个调用返回的实例不是Player类,而是CraftBat类。如果您尝试
,您可以看到返回的内容 System.out.println(e.getEntity.getClass());
或在IDE中尝试调试,看看它在Debug窗口中的类。所以这适用于这种情况:
CraftBat craftBat = (CraftBat)e.getEntity();
但它很可能不是你要找的东西。