0

I need to know how to declare a projectile inside the if statement using the getCause() conditional.

Then, I need to know how to cancel the damage from that projectile if the players are in the same gang.

Please give me an explanation on how to do with along with some example code please!

Here is my event.

@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);
            }
        }
    }
}
MrLore
  • 3,759
  • 2
  • 28
  • 36
Dev Sock
  • 171
  • 1
  • 2
  • 12

3 Answers3

0

EntityDamageByEntityEvent has a method public Entity getDamager(), and there's an interface Projectile extends Entity, I should imagine that calling getDamager() when the cause is DamageCause.PROJECTILE will be the Projectile Entity.

As for cancelling it, I'm not certain but there are some methods which sound promising; Entity#remove(), EntityDamageEvent#setDamage(double), or event.setCancelled(true); as you use later in your code.

MrLore
  • 3,759
  • 2
  • 28
  • 36
0

When you fire the projectile, you should save the UUID of it in a HashMap.
As an example, we'll use a SnowBall as our projectile:

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
}

Then on entity damage event, check if the shooter & the damaged player are in the same gang:

@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
      }
    }
  }
}

You could always change the snowball to something else like an arrow... Just make sure to change all of the SnowBall related code to be whatever projectile you're using.

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
0

Not sure how much has changed, but if you want the player, you should be able to get it from the entity object.

I.e.

@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
    }
  }
}
R4zorax
  • 504
  • 3
  • 10