Why doesn't the "EntityDamageByEntityEvent" get activated?

Ariix :

So, I'm trying to do a teams plugin, where each team has a player1 and player2. Now I wanted to add friendyfire using the EntityDamageByEntityEvent, but either my code doesn't work, or the event doesn't get activated. I'm pretty new to Plugins, so it could be that my code is a little bit stupid. Btw, "teams2" is a ArrayList with "Team" Objects in it, each contains player1, player2 and the teamname.

This is the function:

public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {

        if (((event.getEntity() instanceof Player)) && ((event.getDamager() instanceof Player))) {
            Player player = (Player)event.getEntity();
            Player damager = (Player)event.getDamager();
            for(int i = 0; i < teams2.size(); i++){
                if((teams2.get(i).player1 == player || teams2.get(i).player2 == player) && (teams2.get(i).player1 == damager || teams2.get(i).player2 == damager)){
                    event.setCancelled(true);
                }
            }
        }
        else if (((event.getEntity() instanceof Arrow)) && ((event.getDamager() instanceof Player))) {
            Entity arrow = event.getEntity();
            if ((((Projectile)arrow).getShooter() instanceof Player)) {
                Player player = (Player)arrow;
                Player damager = (Player)event.getDamager();
                for(int i = 0; i < teams2.size(); i++){
                    if((teams2.get(i).player1 == player || teams2.get(i).player2 == player) && (teams2.get(i).player1 == damager || teams2.get(i).player2 == damager)){
                        event.setCancelled(true);
                    }
                }
            }
        }
    }

It should cancel the event, so that players from the same team can't attack each others, but thats not the case. Why? You can also suggest other ways to create a FriendlyFire function.

Nicola Uetz :

Since you did not post your whole sourcecode I will start with one simple question that many people new to Bukkit programming get done wrong (even happens to me some times): Did you register your events?

public class YourPlugin extends JavaPlugin {

    @Override
    public void onEnable() {
        super.onEnable();
        Bukkit.getServer().getPluginManager().registerEvents(listener, this);
    }

}

listener is an instance of your Listener class. So if you have this code you posted in your question in your MainClass it should look like this:

public class YourPlugin extends JavaPlugin implements Listener {

    @Override
    public void onEnable() {
        super.onEnable();
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }

    @EventHandler
    public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
        //your event
    }

}

and if you have a seperate class it should look like this:

public class YourPlugin extends JavaPlugin {

    @Override
    public void onEnable() {
        super.onEnable();
        Bukkit.getServer().getPluginManager().registerEvents(new YourListener(), this);
    }

}
public class YourListener implements Listener {

    @EventHandler
    public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
        //your event
    }

}

It is very important that you put this @EventHandler one line above your EntityDamageByEntntyEvent. This might also be a problem why your event doesn't work.

I looked through your Listener and it should work totally fine. I just took the time to tighten it up a bit:

@EventHandler
public void onEntityDamageByEntity(EntityDamageByEntityEvent e) {
    if (!(e.getEntity() instanceof Player)) return;
    Player player = (Player) e.getEntity();
    Player damager;
    if (e.getDamager() instanceof Arrow) {
        Arrow a = (Arrow) e.getDamager();
        if (!(a.getShooter() instanceof Player)) return;
        damager = (Player) a.getShooter();
    } else {
        if (!(e.getDamager() instanceof Player)) return;
        damager = (Player) e.getDamager();
    }
    for (Team t : teams2) {
        if ((t.player1 == player || t.player2 == player) && (t.player1 == damager || t.player2 == damager)) {
            e.setCancelled(true);
        }
    }
}

I don't know what class is stored in teams2 but I took a wild guess it would be Team so you might have to change that if you took some other class-name as you would like to try out my code.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=104869&siteId=1