Search in sources :

Example 56 with Projectile

use of org.bukkit.entity.Projectile in project MagicPlugin by elBukkit.

the class Mage method damagedBy.

@Override
public void damagedBy(@Nonnull Entity damager, double damage) {
    if (damagedBy == null)
        return;
    if (damager instanceof Projectile) {
        ProjectileSource source = ((Projectile) damager).getShooter();
        if (!(source instanceof Entity))
            return;
        damager = (Entity) source;
    }
    // Only tracking players for now.
    if (!(damager instanceof Player))
        return;
    Player damagingPlayer = (Player) damager;
    lastDamager = damagedBy.get(damagingPlayer.getUniqueId());
    if (lastDamager == null) {
        lastDamager = new DamagedBy(damagingPlayer, damage);
        damagedBy.put(damagingPlayer.getUniqueId(), lastDamager);
    } else {
        lastDamager.damage += damage;
    }
    if (topDamager != null) {
        if (topDamager.getEntity() == null || topDamager.damage < lastDamager.damage || !withinRange(topDamager.getEntity())) {
            topDamager = lastDamager;
            setTarget(damagingPlayer);
        }
    } else {
        topDamager = lastDamager;
        setTarget(damagingPlayer);
    }
    if (entityData != null) {
        entityData.onDamage(this, damage);
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) HumanEntity(org.bukkit.entity.HumanEntity) Player(org.bukkit.entity.Player) ProjectileSource(org.bukkit.projectiles.ProjectileSource) Projectile(org.bukkit.entity.Projectile)

Example 57 with Projectile

use of org.bukkit.entity.Projectile in project MagicPlugin by elBukkit.

the class ProjectileSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    checkReflection();
    getTarget();
    int count = parameters.getInt("count", 1);
    int size = parameters.getInt("size", defaultSize);
    int radius = parameters.getInt("radius", 0);
    double damage = parameters.getDouble("damage", 0);
    float speed = (float) parameters.getDouble("speed", 0.6f);
    float spread = (float) parameters.getDouble("spread", 12);
    Collection<PotionEffect> effects = null;
    if (radius > 0) {
        effects = getPotionEffects(parameters);
        radius = (int) (mage.getRadiusMultiplier() * radius);
    }
    // Modify with wand power
    // Turned some of this off for now
    // count *= mage.getRadiusMultiplier();
    size = (int) (mage.getRadiusMultiplier() * size);
    float damageMultiplier = mage.getDamageMultiplier();
    // speed *= damageMultiplier;
    damage *= damageMultiplier;
    spread /= damageMultiplier;
    boolean useFire = parameters.getBoolean("fire", true);
    int tickIncrease = parameters.getInt("tick_increase", 1180);
    String projectileTypeName = parameters.getString("projectile", "Arrow");
    if (projectileClass == null || worldClass == null || fireballClass == null || arrowClass == null || craftArrowClass == null) {
        controller.getLogger().warning("Can't find NMS classess");
        return SpellResult.FAIL;
    }
    Class<?> projectileType = NMSUtils.getBukkitClass("net.minecraft.server.Entity" + projectileTypeName);
    if (projectileType == null || (!arrowClass.isAssignableFrom(projectileType) && !projectileClass.isAssignableFrom(projectileType) && !fireballClass.isAssignableFrom(projectileType))) {
        controller.getLogger().warning("Bad projectile class: " + projectileTypeName);
        return SpellResult.FAIL;
    }
    Constructor<? extends Object> constructor = null;
    Method shootMethod = null;
    Method setPositionRotationMethod = null;
    Field dirXField = null;
    Field dirYField = null;
    Field dirZField = null;
    Method addEntityMethod = null;
    try {
        constructor = projectileType.getConstructor(worldClass);
        if (fireballClass.isAssignableFrom(projectileType)) {
            dirXField = projectileType.getField("dirX");
            dirYField = projectileType.getField("dirY");
            dirZField = projectileType.getField("dirZ");
        }
        if (projectileClass.isAssignableFrom(projectileType) || arrowClass.isAssignableFrom(projectileType)) {
            shootMethod = projectileType.getMethod("shoot", Double.TYPE, Double.TYPE, Double.TYPE, Float.TYPE, Float.TYPE);
        }
        setPositionRotationMethod = projectileType.getMethod("setPositionRotation", Double.TYPE, Double.TYPE, Double.TYPE, Float.TYPE, Float.TYPE);
        addEntityMethod = worldClass.getMethod("addEntity", entityClass);
    } catch (Throwable ex) {
        ex.printStackTrace();
        return SpellResult.FAIL;
    }
    // Prepare parameters
    Location location = getEyeLocation();
    Vector direction = getDirection().normalize();
    // Track projectiles to remove them after some time.
    List<Projectile> projectiles = new ArrayList<>();
    // Spawn projectiles
    Object nmsWorld = NMSUtils.getHandle(location.getWorld());
    LivingEntity player = mage.getLivingEntity();
    for (int i = 0; i < count; i++) {
        try {
            // Spawn a new projectile
            Object nmsProjectile = null;
            nmsProjectile = constructor.newInstance(nmsWorld);
            if (nmsProjectile == null) {
                throw new Exception("Failed to spawn projectile of class " + projectileTypeName);
            }
            // Velocity must be set manually- EntityFireball.setDirection applies a crazy-wide gaussian distribution!
            if (dirXField != null && dirYField != null && dirZField != null) {
                // Taken from EntityArrow
                double spreadWeight = Math.min(0.4f, spread * 0.007499999832361937D);
                double dx = speed * (direction.getX() + (random.nextGaussian() * spreadWeight));
                double dy = speed * (direction.getY() + (random.nextGaussian() * spreadWeight));
                double dz = speed * (direction.getZ() + (random.nextGaussian() * spreadWeight));
                dirXField.set(nmsProjectile, dx * 0.1D);
                dirYField.set(nmsProjectile, dy * 0.1D);
                dirZField.set(nmsProjectile, dz * 0.1D);
            }
            Vector modifiedLocation = location.toVector().clone();
            if (i > 0 && fireballClass.isAssignableFrom(projectileType) && spread > 0) {
                modifiedLocation.setX(modifiedLocation.getX() + direction.getX() + (random.nextGaussian() * spread / 5));
                modifiedLocation.setY(modifiedLocation.getY() + direction.getY() + (random.nextGaussian() * spread / 5));
                modifiedLocation.setZ(modifiedLocation.getZ() + direction.getZ() + (random.nextGaussian() * spread / 5));
            }
            setPositionRotationMethod.invoke(nmsProjectile, modifiedLocation.getX(), modifiedLocation.getY(), modifiedLocation.getZ(), location.getYaw(), location.getPitch());
            if (shootMethod != null) {
                shootMethod.invoke(nmsProjectile, direction.getX(), direction.getY(), direction.getZ(), speed, spread);
            }
            Entity entity = NMSUtils.getBukkitEntity(nmsProjectile);
            if (entity == null || !(entity instanceof Projectile)) {
                throw new Exception("Got invalid bukkit entity from projectile of class " + projectileTypeName);
            }
            Projectile projectile = (Projectile) entity;
            if (player != null) {
                projectile.setShooter(player);
            }
            projectiles.add(projectile);
            addEntityMethod.invoke(nmsWorld, nmsProjectile);
            if (projectile instanceof Fireball) {
                Fireball fireball = (Fireball) projectile;
                fireball.setIsIncendiary(useFire);
                fireball.setYield(size);
            }
            if (projectile instanceof Arrow) {
                Arrow arrow = (Arrow) projectile;
                if (useFire) {
                    arrow.setFireTicks(300);
                }
                // Hackily make this an infinite arrow and set damage
                try {
                    if (arrowClass == null || craftArrowClass == null) {
                        controller.getLogger().warning("Can not access NMS EntityArrow class");
                    } else {
                        Method getHandleMethod = arrow.getClass().getMethod("getHandle");
                        Object handle = getHandleMethod.invoke(arrow);
                        Field fromPlayerField = arrowClass.getField("fromPlayer");
                        fromPlayerField.setInt(handle, 2);
                        if (damage > 0) {
                            Field damageField = arrowClass.getDeclaredField("damage");
                            damageField.setAccessible(true);
                            damageField.set(handle, damage);
                        }
                    }
                } catch (Throwable ex) {
                    ex.printStackTrace();
                }
            }
            Collection<EffectPlayer> projectileEffects = getEffects("projectile");
            for (EffectPlayer effectPlayer : projectileEffects) {
                effectPlayer.start(projectile.getLocation(), projectile, null, null);
            }
            registerForUndo(projectile);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    if (tickIncrease > 0 && projectiles.size() > 0 && arrowClass != null) {
        scheduleProjectileCheck(projectiles, tickIncrease, effects, radius, 5);
    }
    registerForUndo();
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) PotionEffect(org.bukkit.potion.PotionEffect) ArrayList(java.util.ArrayList) LivingEntity(org.bukkit.entity.LivingEntity) Field(java.lang.reflect.Field) Fireball(org.bukkit.entity.Fireball) Vector(org.bukkit.util.Vector) Arrow(org.bukkit.entity.Arrow) Method(java.lang.reflect.Method) Projectile(org.bukkit.entity.Projectile) EffectPlayer(com.elmakers.mine.bukkit.api.effect.EffectPlayer) Location(org.bukkit.Location)

Example 58 with Projectile

use of org.bukkit.entity.Projectile in project MagicPlugin by elBukkit.

the class PlayerController method onProjectileLaunch.

@EventHandler
public void onProjectileLaunch(ProjectileLaunchEvent event) {
    if (launching || event.isCancelled())
        return;
    Projectile projectile = event.getEntity();
    ProjectileSource shooter = projectile.getShooter();
    // Not really handling magic mobs with magic bows...
    if (!(shooter instanceof Player))
        return;
    Player player = (Player) shooter;
    Mage mage = controller.getRegisteredMage(player);
    if (mage == null)
        return;
    Wand wand = mage.getActiveWand();
    if (wand == null)
        return;
    if (wand.getIcon().getMaterial() != Material.BOW)
        return;
    double minPull = wand.getDouble("cast_min_bowpull");
    double pull = Math.min(1.0, projectile.getVelocity().length() / MAX_ARROW_SPEED);
    if (minPull > 0 && pull < minPull) {
        if (wand.isInventoryOpen())
            event.setCancelled(true);
        return;
    }
    Spell spell = wand.getActiveSpell();
    if (spell == null) {
        if (wand.isInventoryOpen())
            event.setCancelled(true);
        return;
    }
    event.setCancelled(true);
    String[] parameters = { "bowpull", Double.toString(pull) };
    // prevent recursion!
    launching = true;
    try {
        wand.cast(parameters);
    } catch (Exception ex) {
        controller.getLogger().log(Level.SEVERE, "Error casting bow spell", ex);
    }
    launching = false;
}
Also used : Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.magic.Mage) Wand(com.elmakers.mine.bukkit.wand.Wand) ProjectileSource(org.bukkit.projectiles.ProjectileSource) Spell(com.elmakers.mine.bukkit.api.spell.Spell) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 59 with Projectile

use of org.bukkit.entity.Projectile in project MagicPlugin by elBukkit.

the class EntityController method onEntityDamageByEntity.

@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    Entity entity = event.getEntity();
    if (entity instanceof Projectile || entity instanceof TNTPrimed)
        return;
    Entity damager = event.getDamager();
    /*
        if (damager instanceof Projectile) {
            Projectile projectile = (Projectile)damager;
            ProjectileSource source = projectile.getShooter();
            if (source instanceof LivingEntity) {
                damager = (Entity)source;
            }
        }
        if (entity instanceof Creature && damager instanceof LivingEntity) {
            Creature creature = (Creature)entity;
            creature.setTarget((LivingEntity)damager);
        }
        */
    UndoList undoList = controller.getEntityUndo(damager);
    if (undoList != null) {
        // Prevent dropping items from frames,
        if (event.getCause() != EntityDamageEvent.DamageCause.ENTITY_ATTACK || undoList.isScheduled()) {
            undoList.damage(entity);
            if (!entity.isValid()) {
                event.setCancelled(true);
            }
        } else {
            undoList.modify(entity);
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) TNTPrimed(org.bukkit.entity.TNTPrimed) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

Example 60 with Projectile

use of org.bukkit.entity.Projectile in project askyblock by tastybento.

the class IslandGuard1_9 method onLingeringPotionSplash.

@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onLingeringPotionSplash(final LingeringPotionSplashEvent e) {
    if (!IslandGuard.inWorld(e.getEntity().getLocation())) {
        return;
    }
    // Try to get the shooter
    Projectile projectile = (Projectile) e.getEntity();
    if (projectile.getShooter() != null && projectile.getShooter() instanceof Player) {
        UUID uuid = ((Player) projectile.getShooter()).getUniqueId();
        // Store it and remove it when the effect is gone
        thrownPotions.put(e.getAreaEffectCloud().getEntityId(), uuid);
        plugin.getServer().getScheduler().runTaskLater(plugin, new Runnable() {

            @Override
            public void run() {
                thrownPotions.remove(e.getAreaEffectCloud().getEntityId());
            }
        }, e.getAreaEffectCloud().getDuration());
    }
}
Also used : Player(org.bukkit.entity.Player) UUID(java.util.UUID) Projectile(org.bukkit.entity.Projectile) EventHandler(org.bukkit.event.EventHandler)

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