Search in sources :

Example 31 with Projectile

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

the class EntityListenerTest method shouldHandleOldShooterMethod.

@Test
public void shouldHandleOldShooterMethod() {
    // given
    Projectile projectile = mock(Projectile.class);
    Player shooter = mock(Player.class);
    given(projectile.getShooter()).willReturn(shooter);
    ProjectileLaunchEvent event = new ProjectileLaunchEvent(projectile);
    given(listenerService.shouldCancelEvent(shooter)).willReturn(true);
    // when
    listener.onProjectileLaunch(event);
    // then
    verify(listenerService).shouldCancelEvent(shooter);
    assertThat(event.isCancelled(), equalTo(true));
}
Also used : Player(org.bukkit.entity.Player) ProjectileLaunchEvent(org.bukkit.event.entity.ProjectileLaunchEvent) Projectile(org.bukkit.entity.Projectile) Test(org.junit.Test)

Example 32 with Projectile

use of org.bukkit.entity.Projectile in project EliteMobs by MagmaGuy.

the class PlayerDamagedByEliteMobHandler method eliteMobDamageHandler.

/**
 * EliteMobs -> player damage handler.
 *
 * @param event
 */
@EventHandler(priority = EventPriority.HIGHEST)
public void eliteMobDamageHandler(PlayerDamagedByEliteMobEvent event) {
    if (!event.getEliteMobEntity().isValid()) {
        event.setCancelled(true);
        return;
    }
    if (event.getEntityDamageByEntityEvent().isCancelled() && !bypass)
        return;
    // From this point on, the damage event is fully altered by Elite Mobs
    Player player = (Player) event.getEntity();
    if (ElitePlayerInventory.playerInventories.get(player.getUniqueId()) == null)
        return;
    if (player.isBlocking()) {
        if (player.getInventory().getItemInOffHand().getType().equals(Material.SHIELD)) {
            ItemMeta itemMeta = player.getInventory().getItemInOffHand().getItemMeta();
            Damageable damageable = (Damageable) itemMeta;
            if (player.getInventory().getItemInOffHand().getItemMeta().hasEnchant(Enchantment.DURABILITY))
                if (player.getInventory().getItemInOffHand().getItemMeta().getEnchantLevel(Enchantment.DURABILITY) / 20D > ThreadLocalRandom.current().nextDouble())
                    damageable.setDamage(damageable.getDamage() + 5);
            player.getInventory().getItemInOffHand().setItemMeta(itemMeta);
            if (Material.SHIELD.getMaxDurability() < damageable.getDamage())
                player.getInventory().setItemInOffHand(null);
        }
        if (event.getEntityDamageByEntityEvent().getDamager() instanceof Projectile)
            event.getEntityDamageByEntityEvent().getDamager().remove();
        return;
    }
    // if the damage source is custom , the damage is final
    if (bypass) {
        bypass = false;
        // Deal with the player getting killed
        if (player.getHealth() - event.getEntityDamageByEntityEvent().getDamage() <= 0)
            PlayerDeathMessageByEliteMob.addDeadPlayer(player, PlayerDeathMessageByEliteMob.initializeDeathMessage(player, event.getEliteMobEntity().getLivingEntity()));
        return;
    }
    // if the projectile deals custom damage
    if (event.getProjectile() != null && ProjectileTagger.projectileHasCustomDamage(event.getProjectile())) {
        double damage = ProjectileTagger.getProjectileCustomDamage(event.getProjectile());
        if (damage < 0)
            return;
        // Set the final damage value
        event.getEntityDamageByEntityEvent().setDamage(EntityDamageEvent.DamageModifier.BASE, damage);
        // Deal with the player getting killed
        if (player.getHealth() - event.getEntityDamageByEntityEvent().getDamage() <= 0)
            PlayerDeathMessageByEliteMob.addDeadPlayer(player, PlayerDeathMessageByEliteMob.initializeDeathMessage(player, event.getEliteMobEntity().getLivingEntity()));
        return;
    }
    // Determine tiers
    double eliteTier = MobTierCalculator.findMobTier(event.getEliteMobEntity());
    double playerTier = ElitePlayerInventory.playerInventories.get(player.getUniqueId()).getArmorTier(true);
    double newDamage = eliteToPlayerDamageFormula(eliteTier, playerTier, player, event.getEliteMobEntity(), event.getEntityDamageByEntityEvent());
    // nullify vanilla reductions
    for (EntityDamageEvent.DamageModifier modifier : EntityDamageByEntityEvent.DamageModifier.values()) if (event.getEntityDamageByEntityEvent().isApplicable(modifier))
        event.getEntityDamageByEntityEvent().setDamage(modifier, 0);
    // Set the final damage value
    event.getEntityDamageByEntityEvent().setDamage(EntityDamageEvent.DamageModifier.BASE, newDamage);
    // Deal with the player getting killed
    if (player.getHealth() - event.getEntityDamageByEntityEvent().getDamage() <= 0)
        PlayerDeathMessageByEliteMob.addDeadPlayer(player, PlayerDeathMessageByEliteMob.initializeDeathMessage(player, event.getEliteMobEntity().getLivingEntity()));
}
Also used : Player(org.bukkit.entity.Player) Damageable(org.bukkit.inventory.meta.Damageable) ItemMeta(org.bukkit.inventory.meta.ItemMeta) Projectile(org.bukkit.entity.Projectile) EntityDamageEvent(org.bukkit.event.entity.EntityDamageEvent) EventHandler(org.bukkit.event.EventHandler)

Example 33 with Projectile

use of org.bukkit.entity.Projectile in project Glowstone by GlowstoneMC.

the class ItemBow method endUse.

@Override
public void endUse(GlowPlayer player, ItemStack bow) {
    // Check arrows again, since plugins may have changed the inventory while the bow was drawn
    Optional<GlowInventorySlot> maybeArrow = findArrow(player);
    GlowInventorySlot slot = null;
    ItemStack arrow = null;
    final Material arrowType;
    Projectile launchedProjectile = null;
    boolean consumeArrow = false;
    if (maybeArrow.isPresent()) {
        slot = maybeArrow.get();
        arrow = slot.getItem();
        arrowType = arrow.getType();
        consumeArrow = (player.getGameMode() != GameMode.CREATIVE);
    } else if (player.getGameMode() == GameMode.CREATIVE) {
        // Can fire without arrows in Creative
        arrowType = Material.ARROW;
        consumeArrow = false;
    } else {
        arrowType = Material.AIR;
    }
    switch(arrowType) {
        case ARROW:
            if (bow.containsEnchantment(Enchantment.ARROW_INFINITE)) {
                consumeArrow = false;
            }
            launchedProjectile = player.launchProjectile(Arrow.class);
            break;
        case TIPPED_ARROW:
            launchedProjectile = player.launchProjectile(TippedArrow.class);
            GlowTippedArrow launchedTippedArrow = (GlowTippedArrow) launchedProjectile;
            launchedTippedArrow.copyFrom((PotionMeta) arrow.getItemMeta());
            break;
        case SPECTRAL_ARROW:
            launchedProjectile = player.launchProjectile(SpectralArrow.class);
            break;
        case AIR:
            // Not in creative mode and have no arrow
            break;
        default:
            GlowServer.logger.log(Level.SEVERE, () -> String.format("Attempt to fire %s from a bow", arrowType));
    }
    if (launchedProjectile != null) {
        float chargeFraction = (TICKS_TO_FULLY_CHARGE - Floats.constrainToRange(player.getUsageTime(), 0.0f, TICKS_TO_FULLY_CHARGE)) / TICKS_TO_FULLY_CHARGE;
        EntityShootBowEvent event = new EntityShootBowEvent(player, bow, arrow, launchedProjectile, chargeFraction);
        event.setConsumeArrow(consumeArrow);
        event = EventFactory.getInstance().callEvent(event);
        consumeArrow = event.getConsumeArrow();
        // TODO: Call for Skeleton firing too when implemented
        if (event.isCancelled()) {
            launchedProjectile.remove();
        } else {
            chargeFraction = event.getForce();
            launchedProjectile = (Projectile) event.getProjectile();
            if (consumeArrow) {
                int amount = arrow.getAmount();
                if (amount <= 1) {
                    arrow = InventoryUtil.createEmptyStack();
                } else {
                    arrow.setAmount(amount - 1);
                }
                slot.setItem(arrow);
            }
            double damage = Math.max(1.0, MAX_BASE_DAMAGE + (chargeFraction == 1.0 && ThreadLocalRandom.current().nextFloat() >= 0.8 ? 1 : 0) * chargeFraction * (1 + 0.25 * bow.getEnchantmentLevel(Enchantment.ARROW_DAMAGE)));
            launchedProjectile.setVelocity(player.getEyeLocation().getDirection().multiply(Math.max(5, chargeFraction * MAX_SPEED)));
            if (bow.containsEnchantment(Enchantment.ARROW_FIRE)) {
                // Arrow will burn as long as it's in flight, unless extinguished by water
                launchedProjectile.setFireTicks(Integer.MAX_VALUE);
            }
            // Plugin may change projectile to non arrow.
            if (launchedProjectile instanceof Arrow) {
                Arrow launchedArrow = (Arrow) launchedProjectile;
                launchedArrow.setDamage(damage);
                launchedArrow.setKnockbackStrength(bow.getEnchantmentLevel(Enchantment.ARROW_KNOCKBACK));
                // 20% crit chance
                if (ThreadLocalRandom.current().nextDouble() < 0.2) {
                    launchedArrow.setCritical(true);
                }
            }
            if (player.getInventory().getItemInMainHand().getType() == Material.BOW) {
                player.getInventory().setItemInMainHand(InventoryUtil.damageItem(player, bow));
            } else {
                player.getInventory().setItemInOffHand(InventoryUtil.damageItem(player, bow));
            }
        }
    }
    player.setUsageItem(null);
    player.setUsageTime(0);
}
Also used : Arrow(org.bukkit.entity.Arrow) SpectralArrow(org.bukkit.entity.SpectralArrow) TippedArrow(org.bukkit.entity.TippedArrow) GlowTippedArrow(net.glowstone.entity.projectile.GlowTippedArrow) EntityShootBowEvent(org.bukkit.event.entity.EntityShootBowEvent) Material(org.bukkit.Material) SpectralArrow(org.bukkit.entity.SpectralArrow) Projectile(org.bukkit.entity.Projectile) TippedArrow(org.bukkit.entity.TippedArrow) GlowTippedArrow(net.glowstone.entity.projectile.GlowTippedArrow) GlowTippedArrow(net.glowstone.entity.projectile.GlowTippedArrow) GlowInventorySlot(net.glowstone.inventory.GlowInventorySlot) ItemStack(org.bukkit.inventory.ItemStack)

Example 34 with Projectile

use of org.bukkit.entity.Projectile in project Glowstone by GlowstoneMC.

the class ItemProjectileTest method testUse.

@SuppressWarnings("unchecked")
@Test
public void testUse() {
    ItemStack itemStack = new ItemStack(type, 1);
    inventory.addItem(itemStack);
    Projectile projectile = Mockito.mock(projectileClass);
    when(player.launchProjectile(projectileClass)).thenReturn((T) projectile);
    item.use(player, itemStack);
    checkProjectile((T) projectile);
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) Projectile(org.bukkit.entity.Projectile) Test(org.junit.jupiter.api.Test)

Example 35 with Projectile

use of org.bukkit.entity.Projectile in project EliteMobs by MagmaGuy.

the class Explosion method generateExplosion.

private static void generateExplosion(List<Block> blockList, Entity entity, ElitePower elitePower, Location explosionSource) {
    if (!DefaultConfig.isDoExplosionRegen())
        return;
    if (EliteMobs.worldGuardIsEnabled && explosionSource != null && !WorldGuardFlagChecker.doExplosionRegenFlag(explosionSource))
        return;
    ArrayList<BlockState> blockStates = new ArrayList<>();
    for (Block block : blockList) {
        if (block.getType().isAir() || block.getType().equals(Material.FIRE) || block.isLiquid() || EntityTracker.isTemporaryBlock(block))
            continue;
        nearbyBlockScan(blockStates, block.getState());
    }
    Entity shooter = EntityFinder.filterRangedDamagers(entity);
    EliteEntity eliteEntity = null;
    if (shooter != null)
        eliteEntity = EntityTracker.getEliteMobEntity(shooter);
    EliteExplosionEvent eliteExplosionEvent = null;
    // for projectiles
    if (entity instanceof Projectile) {
        eliteExplosionEvent = new EliteExplosionEvent(eliteEntity, elitePower = ElitePower.getElitePower(EliteProjectile.readExplosivePower((Projectile) entity)), entity.getLocation(), blockStates);
    } else {
        eliteExplosionEvent = new EliteExplosionEvent(eliteEntity, elitePower, entity.getLocation(), blockStates);
    }
    if (eliteExplosionEvent.isCancelled())
        return;
    if (explosionSource != null)
        eliteExplosionEvent.setExplosionSourceLocation(explosionSource);
    eliteExplosionEvent.visualExplosionEffect(elitePower);
    for (BlockState blockState : blockStates) {
        blockState.getBlock().setType(Material.AIR);
        blockState.getBlock().getState().update(true);
    }
    new Explosion(blockStates);
}
Also used : EliteEntity(com.magmaguy.elitemobs.mobconstructor.EliteEntity) Entity(org.bukkit.entity.Entity) EliteEntity(com.magmaguy.elitemobs.mobconstructor.EliteEntity) EliteExplosionEvent(com.magmaguy.elitemobs.api.EliteExplosionEvent) Projectile(org.bukkit.entity.Projectile) EliteProjectile(com.magmaguy.elitemobs.combatsystem.EliteProjectile)

Aggregations

Projectile (org.bukkit.entity.Projectile)87 EventHandler (org.bukkit.event.EventHandler)56 Player (org.bukkit.entity.Player)45 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