Search in sources :

Example 71 with Projectile

use of org.bukkit.entity.Projectile in project Prism-Bukkit by prism.

the class EntitySerializer method serialize.

/**
 * Serialize entity.
 * @param entity Entity.
 */
public final void serialize(Entity entity) {
    entityName = entity.getType().name().toLowerCase();
    // Get custom name
    customName = entity.getCustomName();
    // Get animal age
    if (entity instanceof Ageable) {
        isAdult = ((Ageable) entity).isAdult();
    }
    // Owner
    if (entity instanceof Tameable) {
        final Tameable mob = (Tameable) entity;
        if (mob.getOwner() != null) {
            tamingOwner = mob.getOwner().getUniqueId().toString();
        } else if (mob.isTamed()) {
            tamingOwner = "-none-";
        }
    }
    // Sitting
    if (entity instanceof Sittable) {
        sitting = ((Sittable) entity).isSitting();
    }
    EntityDamageEvent damageEvent = entity.getLastDamageCause();
    // Saves us the null check
    if (damageEvent instanceof EntityDamageByEntityEvent && !damageEvent.isCancelled() && damageEvent.getDamage() > ((LivingEntity) entity).getHealth()) {
        EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) damageEvent;
        if (e.getDamager() instanceof Projectile) {
            customDesc = EntityUtils.getCustomProjectileDescription((Projectile) e.getDamager());
        }
    }
    serializer(entity);
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Sittable(org.bukkit.entity.Sittable) Tameable(org.bukkit.entity.Tameable) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) Ageable(org.bukkit.entity.Ageable) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) Projectile(org.bukkit.entity.Projectile)

Example 72 with Projectile

use of org.bukkit.entity.Projectile in project Prism-Bukkit by prism.

the class PaperListeners method onTargetHitEvent.

/**
 * React to a target hit event.
 * @param event the TargetHitEvent.
 */
@EventHandler(priority = EventPriority.MONITOR)
public void onTargetHitEvent(TargetHitEvent event) {
    Projectile projectile = event.getEntity();
    ProjectileSource shooter = projectile.getShooter();
    if (shooter instanceof Player) {
        if (!Prism.getIgnore().event("target-hit", (Player) shooter)) {
            return;
        }
        Block block = event.getHitBlock();
        RecordingQueue.addToQueue(ActionFactory.createBlock("target-hit", block, (Player) shooter));
    }
}
Also used : Player(org.bukkit.entity.Player) Block(org.bukkit.block.Block) ProjectileSource(org.bukkit.projectiles.ProjectileSource) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 73 with Projectile

use of org.bukkit.entity.Projectile in project Prism-Bukkit by prism.

the class PrismEntityEvents method onEntityDeath.

/**
 * EntityDeathEvent.
 * @param event EntityDeathEvent
 */
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onEntityDeath(final EntityDeathEvent event) {
    final LivingEntity entity = event.getEntity();
    // Mob Death
    if (!(entity instanceof Player)) {
        // Log item drops
        if (Prism.getIgnore().event("item-drop", entity.getWorld())) {
            String name = entity.getType().name().toLowerCase();
            // Inventory
            if (entity instanceof InventoryHolder) {
                final InventoryHolder holder = (InventoryHolder) entity;
                for (final ItemStack i : holder.getInventory().getContents()) {
                    if (checkNotNullorAir(i)) {
                        RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, entity.getLocation(), name));
                    }
                }
            }
            // Equipment
            EntityEquipment equipment = entity.getEquipment();
            if (equipment != null) {
                for (final ItemStack i : equipment.getArmorContents()) {
                    if (checkNotNullorAir(i)) {
                        RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, entity.getLocation(), name));
                    }
                }
            }
            // Hand items not stored in "getArmorContents"
            ItemStack main = entity.getEquipment().getItemInMainHand();
            ItemStack off = entity.getEquipment().getItemInOffHand();
            if (checkNotNullorAir(main)) {
                RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", main, main.getAmount(), -1, null, entity.getLocation(), name));
            }
            if (checkNotNullorAir(off)) {
                RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", off, off.getAmount(), -1, null, entity.getLocation(), name));
            }
        }
        EntityDamageEvent damageEvent = entity.getLastDamageCause();
        Entity entitySource = null;
        Block blockSource = null;
        // Resolve source
        if (damageEvent != null && !damageEvent.isCancelled()) {
            if (damageEvent instanceof EntityDamageByEntityEvent) {
                entitySource = ((EntityDamageByEntityEvent) damageEvent).getDamager();
                if (entitySource instanceof Projectile) {
                    ProjectileSource ps = ((Projectile) entitySource).getShooter();
                    if (ps instanceof BlockProjectileSource) {
                        entitySource = null;
                        blockSource = ((BlockProjectileSource) ps).getBlock();
                    } else {
                        entitySource = (Entity) ps;
                    }
                }
            } else if (damageEvent instanceof EntityDamageByBlockEvent) {
                blockSource = ((EntityDamageByBlockEvent) damageEvent).getDamager();
            }
        }
        // Create handlers
        if (entitySource instanceof Player) {
            Player player = (Player) entitySource;
            if (!Prism.getIgnore().event("player-kill", player)) {
                return;
            }
            RecordingQueue.addToQueue(ActionFactory.createEntity("player-kill", entity, player));
        } else if (entitySource != null) {
            if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) {
                return;
            }
            String name = entitySource.getType().name().toLowerCase(Locale.ENGLISH).replace('_', ' ');
            RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
        } else if (blockSource != null) {
            if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) {
                return;
            }
            String name = "block:" + blockSource.getType().name().toLowerCase(Locale.ENGLISH).replace('_', ' ');
            RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
        } else {
            if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) {
                return;
            }
            String name = "unknown";
            if (damageEvent != null && !damageEvent.isCancelled()) {
                name = damageEvent.getCause().name().toLowerCase(Locale.ENGLISH).replace('_', ' ');
            }
            RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity, name));
        }
    /*
             * if (entity.getLastDamageCause() instanceof EntityDamageByEntityEvent) { final
             * EntityDamageByEntityEvent entityDamageByEntityEvent =
             * (EntityDamageByEntityEvent) entity .getLastDamageCause();
             *
             * // Mob killed by player if (entityDamageByEntityEvent.getDamager() instanceof
             * Player) { final Player player = (Player)
             * entityDamageByEntityEvent.getDamager(); if
             * (!Prism.getIgnore().event("player-kill", player)) return;
             * RecordingQueue.addToQueue(ActionFactory.createEntity("player-kill", entity,
             * player));
             *
             * } // Mob shot by an arrow from a player else if
             * (entityDamageByEntityEvent.getDamager() instanceof Arrow) { final Arrow arrow
             * = (Arrow) entityDamageByEntityEvent.getDamager();
             *
             * if (arrow.getShooter() instanceof Player) { final Player player = (Player)
             * arrow.getShooter(); if (!Prism.getIgnore().event("player-kill", player))
             * return; RecordingQueue.addToQueue(ActionFactory.createEntity("player-kill",
             * entity, player));
             *
             * } else if (arrow.getShooter() instanceof LivingEntity) { final Entity damager
             * = (Entity) arrow.getShooter(); String name = "unknown"; if (damager != null)
             * { name = damager.getType().name().toLowerCase(); }
             *
             * if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
             * RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
             * name)); } else if (arrow.getShooter() instanceof BlockProjectileSource) {
             *
             * final Block damager = (Block)
             * ((BlockProjectileSource)arrow.getShooter()).getBlock();
             *
             * if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
             *
             * String name = "block:" + damager.getType().name().toLowerCase();
             *
             * RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
             * name)); } } else { // Mob died by another mob final Entity damager =
             * entityDamageByEntityEvent.getDamager(); String name = "unknown"; if (damager
             * != null) { name = damager.getType().name().toLowerCase(); }
             *
             * if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
             * RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
             * name)); } } else {
             *
             * if (!Prism.getIgnore().event("entity-kill", entity.getWorld())) return;
             *
             * String killer = "unknown"; final EntityDamageEvent damage =
             * entity.getLastDamageCause(); if (damage != null) { final DamageCause cause =
             * damage.getCause(); if (cause != null) { killer = cause.name().toLowerCase();
             * } }
             *
             * // Record the death as natural
             * RecordingQueue.addToQueue(ActionFactory.createEntity("entity-kill", entity,
             * killer));
             *
             * }
             */
    } else {
        // Determine who died and what the exact cause was
        final Player p = (Player) event.getEntity();
        if (Prism.getIgnore().event("player-death", p)) {
            final String cause = DeathUtils.getCauseNiceName(p);
            String attacker = DeathUtils.getAttackerName(p);
            if (attacker.equals("pvpwolf")) {
                final String owner = DeathUtils.getTameWolfOwner(event);
                attacker = owner + "'s wolf";
            }
            RecordingQueue.addToQueue(ActionFactory.createPlayerDeath("player-death", p, cause, attacker));
        }
        // Log item drops
        if (Prism.getIgnore().event("item-drop", p)) {
            if (!event.getDrops().isEmpty()) {
                for (final ItemStack i : event.getDrops()) {
                    RecordingQueue.addToQueue(ActionFactory.createItemStack("item-drop", i, i.getAmount(), -1, null, p.getLocation(), p));
                }
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) EntityDamageByBlockEvent(org.bukkit.event.entity.EntityDamageByBlockEvent) BlockProjectileSource(org.bukkit.projectiles.BlockProjectileSource) Projectile(org.bukkit.entity.Projectile) LivingEntity(org.bukkit.entity.LivingEntity) EntityDamageByEntityEvent(org.bukkit.event.entity.EntityDamageByEntityEvent) EntityEquipment(org.bukkit.inventory.EntityEquipment) Block(org.bukkit.block.Block) ProjectileSource(org.bukkit.projectiles.ProjectileSource) BlockProjectileSource(org.bukkit.projectiles.BlockProjectileSource) ItemStack(org.bukkit.inventory.ItemStack) InventoryHolder(org.bukkit.inventory.InventoryHolder) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) EventHandler(org.bukkit.event.EventHandler)

Example 74 with Projectile

use of org.bukkit.entity.Projectile in project AuthMeReloaded by AuthMe.

the class EntityListenerTest method shouldRejectProjectileLaunchFromUnauthed.

@Test
public void shouldRejectProjectileLaunchFromUnauthed() {
    // given
    Projectile projectile = mock(Projectile.class);
    Player player = mock(Player.class);
    given(projectile.getShooter()).willReturn(player);
    ProjectileLaunchEvent event = mock(ProjectileLaunchEvent.class);
    given(event.getEntity()).willReturn(projectile);
    given(listenerService.shouldCancelEvent(player)).willReturn(true);
    // when
    listener.onProjectileLaunch(event);
    // then
    verify(listenerService).shouldCancelEvent(player);
    verify(event).setCancelled(true);
}
Also used : Player(org.bukkit.entity.Player) ProjectileLaunchEvent(org.bukkit.event.entity.ProjectileLaunchEvent) Projectile(org.bukkit.entity.Projectile) Test(org.junit.Test)

Example 75 with Projectile

use of org.bukkit.entity.Projectile in project AuthMeReloaded by AuthMe.

the class EntityListenerTest method shouldAllowProjectileLaunchFromAuthedHuman.

@Test
public void shouldAllowProjectileLaunchFromAuthedHuman() {
    // given
    Projectile projectile = mock(Projectile.class);
    Player player = mock(Player.class);
    given(projectile.getShooter()).willReturn(player);
    ProjectileLaunchEvent event = mock(ProjectileLaunchEvent.class);
    given(event.getEntity()).willReturn(projectile);
    given(listenerService.shouldCancelEvent(player)).willReturn(false);
    // when
    listener.onProjectileLaunch(event);
    // then
    verify(listenerService).shouldCancelEvent(player);
    verify(event, never()).setCancelled(anyBoolean());
}
Also used : Player(org.bukkit.entity.Player) ProjectileLaunchEvent(org.bukkit.event.entity.ProjectileLaunchEvent) Projectile(org.bukkit.entity.Projectile) Test(org.junit.Test)

Aggregations

Projectile (org.bukkit.entity.Projectile)86 EventHandler (org.bukkit.event.EventHandler)55 Player (org.bukkit.entity.Player)44 Entity (org.bukkit.entity.Entity)37 LivingEntity (org.bukkit.entity.LivingEntity)28 ProjectileSource (org.bukkit.projectiles.ProjectileSource)18 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)13 Location (org.bukkit.Location)9 Animals (org.bukkit.entity.Animals)9 Monster (org.bukkit.entity.Monster)9 Vector (org.bukkit.util.Vector)9 Villager (org.bukkit.entity.Villager)8 ItemStack (org.bukkit.inventory.ItemStack)8 Hanging (org.bukkit.entity.Hanging)7 EntityDamageEvent (org.bukkit.event.entity.EntityDamageEvent)7 PotionEffect (org.bukkit.potion.PotionEffect)7 Block (org.bukkit.block.Block)6 Arrow (org.bukkit.entity.Arrow)6 Region (br.net.fabiozumbi12.RedProtect.Bukkit.Region)4 NotRegisteredException (com.palmergames.bukkit.towny.exceptions.NotRegisteredException)4