Help with EntityDamageEvent

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

Thread Status:
Not open for further replies.
  1. Offline

    VinexAx789

    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:
    [FONT=Droid Sans][COLOR=rgb(44, 44, 44)]    @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);
            }
        }
    
    [/COLOR][/FONT]


    getPlayersTeam Method:

    Code:
        // 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. Offline

    Zombie_Striker

    I have no idea what you are trying to achieve here. Remember, == is for testing if each object is stored in the same spot in memory. It does not test if the values are the same. Remove the last statement from the if statement because the '||' basically means it will always be true as long as it is an explosion. Also, you are testing if the Team the player is on is the same as the player. This will always be true.
     
  3. Offline

    VinexAx789

    @Zombie_Striker I know I did it weird I already new the player on the same team was true just wasn't too sure. I'm trying to make it so the grenade's explosion doesn't damage teammates.
     
  4. Offline

    Zombie_Striker

    @VinexAx789
    1. Store either the instance of the grenade or the location is blows up in an array or collection.
    2. When a player receives explosion damage, test if the player is within a radius of the grenade.
    3. If so, test who threw the grenade (this can be done by either creating a custom "Grenade" class that stores the team, or you use a HashMap for storing the grenade and having the Value be equal to the team)
    4. If the team that threw the grenade is equal to the player's team.
    5. Cancel the event.
    6. After a few seconds of putting the grenade in the array/collection/hashmap, remove the grenade.
     
  5. Offline

    VinexAx789

    @Zombie_Striker Alright thanks.

    EDIT: I'm not sure if I misinterpreted you but here's what I did in my grenade class.


    Code:

    Code:
    package com.venomsurge.dtp.events.abilities;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.entity.EntityExplodeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.scoreboard.Team;
    
    import com.venomsurge.dtp.DTP;
    
    public class grenadeEvent implements Listener {
    
        public ArrayList<Location> grenadeLoc = new ArrayList<Location>();
    
        public HashMap<Player, Team> getGrenadePlayer = new HashMap<Player, Team>();
    
        public DTP plugin;
    
        public grenadeEvent(DTP plugin) {
            this.plugin = plugin;
        }
    
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            if (event.getAction() == Action.LEFT_CLICK_AIR) {
                if (p.getItemInHand().getType() == Material.TNT) {
                    final Item grenade = event.getPlayer().getWorld().dropItem(p.getEyeLocation(),
                            new ItemStack(Material.TNT));
                    grenade.setVelocity(event.getPlayer().getEyeLocation().getDirection());
                    p.sendMessage(ChatColor.RED + "Tossing frag!");
                    getGrenadePlayer.put(p.getPlayer(), plugin.getPlayersTeam(p));
                    p.getInventory().removeItem(grenade.getItemStack());
                    if (event.getItem().getAmount() > 1) {
                        event.getPlayer().getItemInHand().setAmount(event.getItem().getAmount() - 1);
                    } else {
                        p.setItemInHand(new ItemStack(Material.AIR, 1));
                        event.setCancelled(true);
                    }
                    p.updateInventory();
                    Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    
                        @Override
                        public void run() {
                            grenade.getWorld().createExplosion(grenade.getLocation().getX(), grenade.getLocation().getY(),
                                    grenade.getLocation().getZ(), 4.8F, false, false);
                            grenadeLoc.add(grenade.getLocation());
                            grenade.remove();
                        }
                    }, 60L);
                    event.setCancelled(true);
                }
            }
        }
    
        @EventHandler
        public void onGrenadeExplode(EntityExplodeEvent event) {
            Entity entity = event.getEntity();
            if (entity == null) {
                event.blockList().clear();
            }
        }
    
    }
    
    My Event:
    Code:
        @EventHandler
        public void onEntityDamage(EntityDamageEvent e) {
            Player player = (Player) e.getEntity();
            if (grenadeEvent.getGrenadePlayer.get(player) == plugin.getPlayersTeam(player)
                    && e.getCause().equals(DamageCause.ENTITY_EXPLOSION)
                    || e.getCause().equals(DamageCause.ENTITY_EXPLOSION)) {
                e.setCancelled(true);
            } else if (grenadeEvent.getGrenadePlayer.get(player) != plugin.getPlayersTeam(player)
                    && e.getCause().equals(DamageCause.ENTITY_EXPLOSION)
                    || e.getCause().equals(DamageCause.ENTITY_EXPLOSION)) {
                e.setCancelled(false);
            }
        }
     
    Last edited: May 16, 2016
Thread Status:
Not open for further replies.

Share This Page