Bukkit / Spigot - EntityDamageByEntityEvent:投射射手

时间:2017-04-03 20:55:15

标签: java minecraft bukkit eventhandler

我正在创建一个PvP插件,但我无法继续,因为我有一个令人烦恼的演员问题,我无法解决。我正在尝试最后一次伤害伤害,检查伤害者是否使用了箭头,转换了'箭头&# 39;进入射手。但它经常在我的控制台中给我错误。

所以这是我的代码

Player player = (Player)event.getPlayer(); // this variable is in my public void onPlayerMovement event
if(((EntityDamageByEntityEvent) player.getLastDamageCause()).getDamager() instanceof Arrow) 
{
   Arrow arrow = (Arrow) ((EntityDamageByEntityEvent) player.getLastDamageCause()).getDamager();
   ProjectileSource shooter = ((ProjectileSource) ((EntityDamageByEntityEvent) player.getLastDamageCause()).getDamager());

   if(shooter instanceof Player) // checking if the shooter is a player.
   {
      shooter.sendMessage("You hitted someone with a bow right?");
      player.sendMessage("You got hit by a bow..");
   }
}

我想将箭头转换为射手的原因是因为我想向射手显示一条消息,以及其他几件事。

这是错误

[22:39:10 ERROR]: Could not pass event PlayerMoveEvent to FendykPVP v1.8
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:270) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PacketPlayInFlying.a(SourceFile:126) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PacketPlayInFlying$PacketPlayInPosition.a(SourceFile:57) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_121]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_121]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]
Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.entity.CraftArrow cannot be cast to org.bukkit.entity.Player
    at GameCore.playerKill.onPlayerMovement(playerKill.java:102) ~[?:?]
    at sun.reflect.GeneratedMethodAccessor1391.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_121]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_121]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
    ... 15 more

我希望有人可以帮助我

2 个答案:

答案 0 :(得分:1)

这可能会也可能不会起作用,我自己也没有对它进行过测试。但是你需要在投射之前检查它是否是一个箭头。另外,为了获得射击游戏,射弹类中的方法arrow.getShooter()可以被箭头使用。

if(((EntityDamageByEntityEvent) player.getLastDamageCause()).getDamager() instanceof Arrow) {
    Arrow arrow = (Arrow) ((EntityDamageByEntityEvent) player.getLastDamageCause()).getDamager();
    ProjectileSource shooter = arrow.getShooter();

       if(shooter instanceof Player) // checking if the shooter is a player.
       {
          Player shooterPlayer = (Player) shooter;
          shooterPlayer.sendMessage("You hitted someone with a bow right?");
          player.sendMessage("You got hit by a bow..");
       }
}

如果没有帮助,请告诉我第102行是什么。

答案 1 :(得分:1)

以下是经过测试的解决方案的摘录,以便在玩家互相射击时通知他们。这应该可以帮助你。

@EventHandler
public void onEntityDamageByEntity( EntityDamageByEntityEvent evt ) {
    // Is target a player, if not stop
    if ( !(evt.getEntity() instanceof Player) )
        return;

    Player damagee = (Player) evt.getEntity();

    // Is damager an arrow, if not stop
    if ( !(evt.getDamager() instanceof Arrow) )
        return;

    // Is shooter a player, if not stop
    ProjectileSource src = ((Arrow) evt.getDamager()).getShooter();
    if ( !(src instanceof Player) )
        return;

    Player damager = (Player) src;
    damager.sendMessage( "You just shot " + damagee.getName() );
    damagee.sendMessage( "You were shot by " + damager.getName() );
}