Search in sources :

Example 1 with EffectPlayer

use of com.elmakers.mine.bukkit.api.effect.EffectPlayer in project MagicPlugin by elBukkit.

the class SpawnEntityAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Block targetBlock = context.getTargetBlock();
    targetBlock = targetBlock.getRelative(BlockFace.UP);
    Location spawnLocation = targetBlock.getLocation();
    Location sourceLocation = context.getLocation();
    spawnLocation.setPitch(sourceLocation.getPitch());
    spawnLocation.setYaw(sourceLocation.getYaw());
    MageController controller = context.getController();
    if (entityTypeProbability != null && !entityTypeProbability.isEmpty()) {
        if (repeatRandomize || entityData == null) {
            String randomType = RandomUtils.weightedRandom(entityTypeProbability);
            try {
                entityData = controller.getMob(randomType);
                if (entityData == null) {
                    entityData = new com.elmakers.mine.bukkit.entity.EntityData(EntityType.valueOf(randomType.toUpperCase()));
                }
            } catch (Throwable ex) {
                entityData = null;
            }
        }
    }
    if (entityData == null) {
        return SpellResult.FAIL;
    }
    if (force) {
        controller.setForceSpawn(true);
    }
    Entity spawnedEntity = null;
    try {
        spawnedEntity = entityData.spawn(context.getController(), spawnLocation, spawnReason);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    // Special check to assign ownership
    if (spawnedEntity instanceof AreaEffectCloud) {
        ((AreaEffectCloud) spawnedEntity).setSource(context.getLivingEntity());
    } else if (spawnedEntity instanceof Projectile) {
        ((Projectile) spawnedEntity).setShooter(context.getLivingEntity());
    }
    if (force) {
        controller.setForceSpawn(false);
    }
    if (spawnedEntity == null) {
        return SpellResult.FAIL;
    }
    if (!loot) {
        spawnedEntity.setMetadata("nodrops", new FixedMetadataValue(controller.getPlugin(), true));
    }
    if (speed > 0) {
        Vector motion = direction;
        if (motion == null) {
            motion = context.getDirection();
        } else {
            motion = motion.clone();
        }
        if (dyOffset != 0) {
            motion.setY(motion.getY() + dyOffset);
        }
        motion.normalize();
        motion.multiply(speed);
        CompatibilityUtils.setEntityMotion(spawnedEntity, motion);
    }
    Collection<EffectPlayer> projectileEffects = context.getEffects("spawned");
    for (EffectPlayer effectPlayer : projectileEffects) {
        effectPlayer.start(spawnedEntity.getLocation(), spawnedEntity, null, null);
    }
    context.registerForUndo(spawnedEntity);
    if (setTarget) {
        context.setTargetEntity(spawnedEntity);
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) AreaEffectCloud(org.bukkit.entity.AreaEffectCloud) Projectile(org.bukkit.entity.Projectile) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Block(org.bukkit.block.Block) EffectPlayer(com.elmakers.mine.bukkit.api.effect.EffectPlayer) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 2 with EffectPlayer

use of com.elmakers.mine.bukkit.api.effect.EffectPlayer in project MagicPlugin by elBukkit.

the class BaseProjectileAction method track.

protected void track(CastContext context, Entity entity) {
    if (tracking == null) {
        tracking = new HashSet<>();
    }
    tracking.add(entity);
    context.registerForUndo(entity);
    if (setTarget) {
        context.setTargetEntity(entity);
    }
    Collection<EffectPlayer> projectileEffects = context.getEffects(projectileEffectsKey);
    for (EffectPlayer effectPlayer : projectileEffects) {
        effectPlayer.start(entity.getLocation(), entity, null, null);
    }
    if (track) {
        Targeting.track(context.getPlugin(), entity);
    }
}
Also used : EffectPlayer(com.elmakers.mine.bukkit.api.effect.EffectPlayer)

Example 3 with EffectPlayer

use of com.elmakers.mine.bukkit.api.effect.EffectPlayer in project MagicPlugin by elBukkit.

the class CastContext method playEffects.

@Override
public void playEffects(String effectName, float scale, Location sourceLocation, Entity sourceEntity, Location targetLocation, Entity targetEntity, Block sourceBlock) {
    if (targetEntity != null) {
        String entityKey = effectName + "_" + targetEntity.getType().name().toLowerCase();
        if (baseSpell != null && baseSpell.hasEffects(entityKey)) {
            effectName = entityKey;
        }
    }
    Collection<EffectPlayer> effects = getEffects(effectName);
    if (effects.size() > 0) {
        Location location = getLocation();
        Collection<Entity> targeted = getTargetedEntities();
        for (EffectPlayer player : effects) {
            // Set scale
            player.setScale(scale);
            Mage mage = getMage();
            Location source = sourceLocation;
            if (source == null) {
                if (mage.getEntity() == sourceEntity && player.playsAtOrigin()) {
                    source = player.getSourceLocation(this);
                } else {
                    source = location;
                }
            }
            Location target = targetLocation;
            if (target == null) {
                target = player.getTargetLocation(this);
            }
            if (sourceBlock != null) {
                player.setMaterial(sourceBlock);
            }
            player.start(source, sourceEntity, target, targetEntity, targeted);
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Mage(com.elmakers.mine.bukkit.api.magic.Mage) EffectPlayer(com.elmakers.mine.bukkit.api.effect.EffectPlayer) Location(org.bukkit.Location)

Example 4 with EffectPlayer

use of com.elmakers.mine.bukkit.api.effect.EffectPlayer in project MagicPlugin by elBukkit.

the class CastContext method getEffects.

@Override
public Collection<EffectPlayer> getEffects(String effectKey) {
    Collection<EffectPlayer> effects = spell.getEffects(effectKey);
    if (effects.size() == 0)
        return effects;
    // Create parameter map
    Map<String, String> parameterMap = null;
    ConfigurationSection workingParameters = spell.getWorkingParameters();
    ConfigurationSection handlerParameters = spell.getHandlerParameters(effectKey);
    if (handlerParameters != null || workingParameters != null) {
        parameterMap = new HashMap<>();
        addParameters(parameterMap, workingParameters, null);
        addParameters(parameterMap, handlerParameters, workingParameters);
    }
    for (EffectPlayer player : effects) {
        // Track effect plays for cancelling
        player.setEffectPlayList(currentEffects);
        // Set material and color
        player.setMaterial(brush != null ? brush : spell.getEffectMaterial());
        player.setColor(getEffectColor());
        player.setParticleOverride(getEffectParticle());
        // Set parameters
        player.setParameterMap(parameterMap);
    }
    return effects;
}
Also used : EffectPlayer(com.elmakers.mine.bukkit.api.effect.EffectPlayer) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 5 with EffectPlayer

use of com.elmakers.mine.bukkit.api.effect.EffectPlayer 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)

Aggregations

EffectPlayer (com.elmakers.mine.bukkit.api.effect.EffectPlayer)6 Location (org.bukkit.Location)3 Entity (org.bukkit.entity.Entity)3 LivingEntity (org.bukkit.entity.LivingEntity)2 Projectile (org.bukkit.entity.Projectile)2 Vector (org.bukkit.util.Vector)2 Mage (com.elmakers.mine.bukkit.api.magic.Mage)1 MageController (com.elmakers.mine.bukkit.api.magic.MageController)1 DynamicLocation (de.slikey.effectlib.util.DynamicLocation)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Block (org.bukkit.block.Block)1 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)1 AreaEffectCloud (org.bukkit.entity.AreaEffectCloud)1 Arrow (org.bukkit.entity.Arrow)1 Fireball (org.bukkit.entity.Fireball)1 FixedMetadataValue (org.bukkit.metadata.FixedMetadataValue)1 PotionEffect (org.bukkit.potion.PotionEffect)1