Help with EntityDamageEvent

Discussion in 'Spigot Plugin Development' started by VinexAx789, May 17, 2016.

  1. Issue: When a player chucks a grenade at there teammates it allows them to take damage. What I wish to do is to cancel this damage created by the grenades for teammates only. If you can please help me/guide me on what to do that would be great. I believe I'm over thinking this a lot.

    Code:
    Code (Java):
    [/COLOR][/FONT]
        @EventHandler
        public void onEntityDamage(EntityDamageEvent e) {
            Player player = (Player) e.getEntity();
            if (plugin.getPlayersTeam(player) != plugin.getPlayersTeam(player)
                    && e.getCause().equals(DamageCause.ENTITY_EXPLOSION)
                    || e.getCause().equals(DamageCause.ENTITY_EXPLOSION)) {
                e.setCancelled(false);
            } else if (plugin.getPlayersTeam(player) == plugin.getPlayersTeam(player)
                    && e.getCause().equals(DamageCause.ENTITY_EXPLOSION)
                    || e.getCause().equals(DamageCause.ENTITY_EXPLOSION)) {
                e.setCancelled(true);
            }
        }
     

    getPlayersTeam Method:

    Code (Java):
        // Returns the team that the player is in
        @SuppressWarnings("deprecation")
        public Team getPlayersTeam(Player player) {
            Team team = null;
            if (blue.hasPlayer(player)) {
                team = blue;
            } else if (red.hasPlayer(player)) {
                team = red;
            }
            return team;
        }
    Again, I believe I'm over thinking this.

    If you can help me that would be fantastic.
     
  2. You are always checking if the player is on the same team as themselves which should (assuming that what getPlayersTeam returns is logical) always be true.
    Code (Text):
    plugin.getPlayersTeam(player) == plugin.getPlayersTeam(player)
    Side note: You generally shouldn't un-cancel events unless you have a good reason to. In this case I don't think your reason for
    Code (Text):
    e.setCancelled(false);
    is a good one. Consider world guard that cancels a damage event for a player in a safe zone and you have just allowed this damage because the player is not on the same team as the person who damaged them essentially building in a bypass to world guard.
     
  3. I cannot use world guard because my minigame is a pvp game there is no safezones. This is going to be difficult making it not damage teammates. But thanks.
     
  4. You could also try and change the e.getEntity(); to e.getDamager(); and e.getDamaged();
     
  5. I think you misunderstood. I used world guard as an example of another plugin that is also handling the same events as you but it applies to any plugins listening for the same event as you. I am not recommending that you use world guard, you have the logic that you need already setup but you are just comparing the damaged player's team to the damaged player's team...
     
  6. I believe that follows under EntityDamageByEntity event.
     
  7. Yeah I already knew that was a mistake from the bat, and yes I misunderstood you big time.
     
  8. You are right, but that is what I mean, you could just change the event to that one if you can't figure it out.
     
  9. I did in fact try that earlier what I did was this though,

    Code (Text):
        @EventHandler
        public void onEntityDamage(EntityDamageByEntityEvent e) {
            Player player = (Player) e.getEntity();
            Player damager = (Player) e.getDamager();
            if (plugin.getPlayersTeam(player) != plugin.getPlayersTeam(damager)
                    && e.getCause().equals(DamageCause.ENTITY_EXPLOSION)
                    || e.getCause().equals(DamageCause.ENTITY_EXPLOSION)) {
                e.setCancelled(false);
            } else if (plugin.getPlayersTeam(player) == plugin.getPlayersTeam(damager)
                    && e.getCause().equals(DamageCause.ENTITY_EXPLOSION)
                    || e.getCause().equals(DamageCause.ENTITY_EXPLOSION)) {
                e.setCancelled(true);
            }
        }
    Then again there's no way to tell if these players are hitting them because I'm creating an explosion.
     
  10. add getType() before the equals.

    I'm not at my programming computer so I am not able to test that, but I think you need to do that...
     
  11. Where at? That won't work. If you're talking about
    e.getCause().equals(DamageCause.ENTITY_EXPLOSION)
    || e.getCause().equals(DamageCause.ENTITY_EXPLOSION)) those for an example
     
  12. Wait a second, I'm thinking of another event... I'm thinking of the InventoryClickEvent; as in defining what type of inventory you are in... LOL, I'll continue to brain storm though
     
  13. Hahaha, no worries, thanks.
     
    • Like Like x 1