0

我需要知道如何使用 getCause() 条件在 if 语句中声明弹丸。

然后,如果玩家在同一个帮派中,我需要知道如何取消该弹丸的伤害。

请给我一个关于如何处理的解释以及一些示例代码!

这是我的活动。

@EventHandler
public void onEntityDamage(EntityDamageByEntityEvent event){
    if(event.getEntity() instanceof Player){
        Player damaged = (Player) event.getEntity();
        String DUUID = damaged.getUniqueId().toString().toLowerCase();

        if(event.getCause() == DamageCause.PROJECTILE){

            //I NEED THE CODE TO PUT IN HERE

        }

        if(event.getDamager() instanceof Player){
            Player damager = (Player) event.getDamager();
            String DRUUID = damager.getUniqueId().toString().toLowerCase();

            if(MinecraftGTA.config.getString(DRUUID + ".Gang").equalsIgnoreCase(MinecraftGTA.config.getString(DUUID + ".Gang"))){
                event.setCancelled(true);
            }
        }
    }
}
4

3 回答 3

0

不确定有多少变化,但如果你想要播放器,你应该能够从实体对象中获取它。

IE

@EventHandler
public void entityDamage(EntityDamageByEntityEvent e){
  if(e.getDamager() instanceof Projectile && e.getEntity() instanceof Player){
    //make sure the damager is a snowball & the damaged entity is a Player
    ProjectileSource shooter = ((Projectile) event.getDamager()).getShooter();
    if (!(shooter instanceof Player)) {
      return;
    }
    // compare shooter and entity here...
    if (isSameGang((Player) shooter, (Player) e.getEntity()) {
      e.setCancelled(true); // cancel damage
    }
  }
}
于 2015-01-08T17:42:26.817 回答
0

EntityDamageByEntityEvent有一个方法public Entity getDamager(),有一个interface Projectile extends Entity,我应该想象getDamager()当原因是DamageCause.PROJECTILE实体Projectile时调用。

至于取消它,我不确定,但有些方法听起来很有希望;Entity#remove(), EntityDamageEvent#setDamage(double), 或event.setCancelled(true);稍后在代码中使用。

于 2014-04-13T11:56:39.473 回答
0

发射弹丸时,应将其 UUID 保存在HashMap.
例如,我们将使用 aSnowBall作为我们的射弹:

public Map<String, String> projectileUUID = new HashMap<String, String>

public void launch(Player p){//call this to launch the projectile
  String uuid = p.launchProjectile(Snowball.class).getUniqueId().toString(); //launch a snowball & get the UUID

  projectileUUID.put(uuid, p.getName());//put the UUID of the projectile in the map
}

然后在实体损坏事件中,检查射手和损坏的玩家是否在同一个帮派中:

@EventHandler
public void entityDamage(EntityDamageByEntityEvent e){
  if(e.getDamager() instanceof SnowBall && e.getEntity() instanceof Player){//make sure the damager is a snowball & the damaged entity is a Player
    String uuid = ((SnowBall) e.getDamager()).getUniqueId().toString(); //get the UUID of the snowball
    if(projectileUUID.contains(uuid)){//check if our HashMap contains the UUID
      String damaged = ((Player) e.getEntity()).getName(); //get the damaged player's name
      String shooter = projectileUUID.get(uuid); //get the shooter's name
      if(getGang(shooter).equals(getGang(damaged)){//check if the two player's gangs are the same here
        e.setCancelled(true); //cancel the event
      }
    }
  }
}

您可以随时将其更改snowball为其他内容,例如arrow...只需确保将所有SnowBall相关代码更改为您正在使用的任何射弹。

于 2014-04-13T15:53:09.850 回答