Search in sources :

Example 1 with Target

use of com.elmakers.mine.bukkit.utility.Target in project MagicPlugin by elBukkit.

the class ConstructSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    Block target = null;
    boolean isSelect = getTargetType() == TargetType.SELECT;
    boolean finalCast = !isSelect || this.targetBlock != null;
    if (finalCast && parameters.getBoolean("select_self", true) && isLookingDown()) {
        target = mage.getLocation().getBlock().getRelative(BlockFace.DOWN);
    } else {
        Target t = getTarget();
        target = t.getBlock();
    }
    if (target == null) {
        return SpellResult.NO_TARGET;
    }
    MaterialBrush buildWith = getBrush();
    boolean hasPermission = buildWith != null && buildWith.isErase() ? hasBreakPermission(target) : hasBuildPermission(target);
    if (!hasPermission) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    int radius = parameters.getInt("radius", DEFAULT_RADIUS);
    radius = parameters.getInt("r", radius);
    radius = parameters.getInt("size", radius);
    boolean falling = parameters.getBoolean("falling", false);
    boolean physics = parameters.getBoolean("physics", false);
    boolean commit = parameters.getBoolean("commit", false);
    boolean consume = parameters.getBoolean("consume", false);
    double breakable = parameters.getDouble("breakable", 0);
    double backfireChance = parameters.getDouble("reflect_chance", 0);
    Vector orientTo = null;
    Vector bounds = null;
    if (parameters.getBoolean("use_brush_size", false)) {
        if (!buildWith.isReady()) {
            long timeout = System.currentTimeMillis() + 10000;
            while (System.currentTimeMillis() < timeout) {
                try {
                    Thread.sleep(500);
                    if (buildWith.isReady()) {
                        break;
                    }
                } catch (InterruptedException ex) {
                    break;
                }
            }
            if (!buildWith.isReady()) {
                return SpellResult.NO_ACTION;
            }
        }
        bounds = buildWith.getSize();
        radius = (int) Math.max(Math.max(bounds.getX() / 2, bounds.getZ() / 2), bounds.getY());
    } else if (isSelect) {
        if (targetLocation2 != null) {
            this.targetBlock = targetLocation2.getBlock();
        }
        if (targetBlock == null || !targetBlock.getWorld().equals(target.getWorld())) {
            targetBlock = target;
            activate();
            return SpellResult.TARGET_SELECTED;
        } else {
            radius = (int) targetBlock.getLocation().distance(target.getLocation());
            if (parameters.getBoolean("orient")) {
                orientTo = target.getLocation().toVector().subtract(targetBlock.getLocation().toVector());
                orientTo.setX(Math.abs(orientTo.getX()));
                orientTo.setY(Math.abs(orientTo.getY()));
                orientTo.setZ(Math.abs(orientTo.getZ()));
                if (orientTo.getX() < orientTo.getZ() && orientTo.getX() < orientTo.getY()) {
                    orientTo = new Vector(1, 0, 0);
                } else if (orientTo.getZ() < orientTo.getX() && orientTo.getZ() < orientTo.getY()) {
                    orientTo = new Vector(0, 0, 1);
                } else {
                    orientTo = new Vector(0, 1, 0);
                }
            }
            target = targetBlock;
            targetBlock = null;
        }
    } else if (parameters.getBoolean("orient")) {
        // orientTo = mage.getLocation().toVector().crossProduct(target.getLocation().toVector());
        orientTo = mage.getLocation().toVector().subtract(target.getLocation().toVector());
        orientTo.setX(Math.abs(orientTo.getX()));
        orientTo.setY(Math.abs(orientTo.getY()));
        orientTo.setZ(Math.abs(orientTo.getZ()));
        if (orientTo.getX() > orientTo.getZ() && orientTo.getX() > orientTo.getY()) {
            orientTo = new Vector(1, 0, 0);
        } else if (orientTo.getZ() > orientTo.getX() && orientTo.getZ() > orientTo.getY()) {
            orientTo = new Vector(0, 0, 1);
        } else {
            orientTo = new Vector(0, 1, 0);
        }
    }
    if (!parameters.contains("radius")) {
        int maxDimension = parameters.getInt("max_dimension", DEFAULT_MAX_DIMENSION);
        maxDimension = parameters.getInt("md", maxDimension);
        maxDimension = (int) (mage.getConstructionMultiplier() * maxDimension);
        int diameter = radius * 2;
        if (diameter > maxDimension) {
            return SpellResult.FAIL;
        }
    }
    // TODO : Is this needed? Or just use "ty"?
    if (parameters.contains("y_offset")) {
        target = target.getRelative(BlockFace.UP, parameters.getInt("y_offset", 0));
    }
    buildWith.setTarget(target.getLocation());
    ConstructionType conType = DEFAULT_CONSTRUCTION_TYPE;
    int thickness = parameters.getInt("thickness", 0);
    String typeString = parameters.getString("type", "");
    ConstructionType testType = ConstructionType.parseString(typeString, ConstructionType.UNKNOWN);
    if (testType != ConstructionType.UNKNOWN) {
        conType = testType;
    }
    ConstructBatch batch = new ConstructBatch(this, target.getLocation(), conType, radius, thickness, falling, orientTo);
    batch.setCommit(commit);
    batch.setConsume(consume);
    UndoList undoList = getUndoList();
    if (undoList != null && !currentCast.isConsumeFree()) {
        undoList.setConsumed(consume);
    }
    if (parameters.getBoolean("replace", false)) {
        List<com.elmakers.mine.bukkit.api.block.MaterialAndData> replaceMaterials = new ArrayList<>();
        MaterialAndData wildReplace = new MaterialAndData(target);
        if (!parameters.getBoolean("match_data", true)) {
            wildReplace.setData(null);
        }
        // Hacky, but generally desired - maybe abstract to a parameterized list?
        Material targetMaterial = target.getType();
        if (targetMaterial == Material.STATIONARY_WATER || targetMaterial == Material.WATER || targetMaterial == Material.STATIONARY_LAVA || targetMaterial == Material.LAVA) {
            wildReplace.setData(null);
        }
        replaceMaterials.add(wildReplace);
        batch.setReplace(replaceMaterials);
    }
    // Check for command block overrides
    if (parameters.contains("commands")) {
        ConfigurationSection commandMap = parameters.getConfigurationSection("commands");
        Set<String> keys = commandMap.getKeys(false);
        for (String key : keys) {
            batch.addCommandMapping(key, commandMap.getString(key));
        }
    }
    if (falling) {
        float force = (float) parameters.getDouble("speed", 0);
        batch.setFallingDirection(ConfigurationUtils.getVector(parameters, "falling_direction"));
        batch.setFallingBlockSpeed(force);
    }
    batch.setApplyPhysics(physics);
    if (breakable > 0) {
        batch.setBreakable(breakable);
    }
    if (backfireChance > 0) {
        batch.setBackfireChance(backfireChance);
    }
    if (parameters.contains("orient_dimension_max")) {
        batch.setOrientDimensionMax(parameters.getInt("orient_dimension_max"));
    } else if (parameters.contains("odmax")) {
        batch.setOrientDimensionMax(parameters.getInt("odmax"));
    }
    if (parameters.contains("orient_dimension_min")) {
        batch.setOrientDimensionMin(parameters.getInt("orient_dimension_min"));
    } else if (parameters.contains("odmin")) {
        batch.setOrientDimensionMin(parameters.getInt("odmin"));
    }
    if (parameters.getBoolean("power", false)) {
        batch.setPower(true);
    }
    if (bounds != null) {
        batch.setBounds(bounds);
        batch.setOrientDimensionMin(0);
    }
    boolean success = mage.addBatch(batch);
    deactivate();
    return success ? SpellResult.CAST : SpellResult.FAIL;
}
Also used : ConstructBatch(com.elmakers.mine.bukkit.batch.ConstructBatch) ConstructionType(com.elmakers.mine.bukkit.block.ConstructionType) ArrayList(java.util.ArrayList) Material(org.bukkit.Material) Target(com.elmakers.mine.bukkit.utility.Target) MaterialBrush(com.elmakers.mine.bukkit.api.block.MaterialBrush) UndoList(com.elmakers.mine.bukkit.block.UndoList) MaterialAndData(com.elmakers.mine.bukkit.block.MaterialAndData) Block(org.bukkit.block.Block) Vector(org.bukkit.util.Vector) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 2 with Target

use of com.elmakers.mine.bukkit.utility.Target in project MagicPlugin by elBukkit.

the class DisarmSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    Target target = getTarget();
    if (!target.hasEntity() || !(target.getEntity() instanceof LivingEntity)) {
        return SpellResult.NO_TARGET;
    }
    LivingEntity entity = (LivingEntity) target.getEntity();
    EntityEquipment equipment = entity.getEquipment();
    ItemStack stack = equipment.getItemInHand();
    if (stack == null || stack.getType() == Material.AIR) {
        return SpellResult.NO_TARGET;
    }
    // Special case for wands
    if (Wand.isWand(stack) && controller.isMage(entity)) {
        Mage targetMage = controller.getMage(entity);
        // This gets overridden by superpower...
        if (!mage.isSuperPowered() && isSuperProtected(targetMage)) {
            return SpellResult.NO_TARGET;
        }
        if (targetMage.getActiveWand() != null) {
            targetMage.getActiveWand().deactivate();
        }
    }
    Integer targetSlot = null;
    PlayerInventory targetInventory = null;
    ItemStack swapItem = null;
    if (entity instanceof Player && parameters.getBoolean("keep_in_inventory", false)) {
        Player targetPlayer = (Player) entity;
        targetInventory = targetPlayer.getInventory();
        List<Integer> validSlots = new ArrayList<>();
        ItemStack[] contents = targetInventory.getContents();
        int minSlot = parameters.getInt("min_slot", Wand.HOTBAR_SIZE);
        int maxSlot = parameters.getInt("max_slot", contents.length - 1);
        for (int i = minSlot; i <= maxSlot; i++) {
            if (contents[i] == null || contents[i].getType() == Material.AIR) {
                validSlots.add(i);
            }
        }
        // Randomly choose a slot if no empty one was found
        if (validSlots.size() == 0) {
            int swapSlot = random.nextInt(maxSlot - minSlot) + minSlot;
            swapItem = targetInventory.getItem(swapSlot);
            validSlots.add(swapSlot);
        }
        int chosen = random.nextInt(validSlots.size());
        targetSlot = validSlots.get(chosen);
    }
    equipment.setItemInHand(swapItem);
    if (targetSlot != null && targetInventory != null) {
        targetInventory.setItem(targetSlot, stack);
    } else {
        Location location = entity.getLocation();
        location.setY(location.getY() + 1);
        Item item = entity.getWorld().dropItemNaturally(location, stack);
        Vector velocity = item.getVelocity();
        velocity.setY(velocity.getY() * 5);
        SafetyUtils.setVelocity(item, velocity);
    }
    return SpellResult.CAST;
}
Also used : Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) PlayerInventory(org.bukkit.inventory.PlayerInventory) LivingEntity(org.bukkit.entity.LivingEntity) Item(org.bukkit.entity.Item) Target(com.elmakers.mine.bukkit.utility.Target) EntityEquipment(org.bukkit.inventory.EntityEquipment) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 3 with Target

use of com.elmakers.mine.bukkit.utility.Target in project MagicPlugin by elBukkit.

the class FamiliarSpell method onCast.

@SuppressWarnings("deprecation")
@Override
public SpellResult onCast(ConfigurationSection parameters) {
    spawnCount = 0;
    Target target = getTarget();
    if (!target.hasTarget()) {
        return SpellResult.NO_TARGET;
    }
    Block originalTarget = target.getBlock();
    Block targetBlock = originalTarget;
    LivingEntity targetEntity = null;
    boolean track = parameters.getBoolean("track", true);
    boolean loot = parameters.getBoolean("loot", false);
    boolean setTarget = parameters.getBoolean("set_target", true);
    double spawnRange = parameters.getInt("spawn_range", 0);
    String entityName = parameters.getString("name", "");
    if (hasFamiliar() && track) {
        // Dispel familiars if you target them and cast
        boolean isFamiliar = target.hasEntity() && isFamiliar(target.getEntity());
        if (isFamiliar) {
            checkListener();
            releaseFamiliar(target.getEntity());
            return SpellResult.DEACTIVATE;
        }
        releaseFamiliars();
    }
    if (target.hasEntity()) {
        targetBlock = targetBlock.getRelative(BlockFace.SOUTH);
        Entity e = target.getEntity();
        if (e instanceof LivingEntity) {
            targetEntity = (LivingEntity) e;
        }
    }
    targetBlock = targetBlock.getRelative(BlockFace.UP);
    Location centerLoc = targetBlock.getLocation();
    Location caster = getLocation();
    if (spawnRange > 0) {
        double distanceSquared = targetBlock.getLocation().distanceSquared(caster);
        if (spawnRange * spawnRange < distanceSquared) {
            Vector direction = caster.getDirection().normalize().multiply(spawnRange);
            centerLoc = caster.clone().add(direction);
            for (int i = 0; i < spawnRange; i++) {
                Material blockType = centerLoc.getBlock().getType();
                if (blockType == Material.AIR || blockType == Material.WATER || blockType != Material.STATIONARY_WATER) {
                    break;
                }
                centerLoc = centerLoc.add(0, 1, 0);
            }
        }
    }
    EntityType famType = null;
    int famCount = parameters.getInt("count", 1);
    String famTypeName = parameters.getString("type", null);
    if (famTypeName != null && !famTypeName.isEmpty()) {
        try {
            famType = EntityType.valueOf(famTypeName.toUpperCase());
        } catch (Throwable ex) {
            sendMessage("Unknown entity type: " + famTypeName);
            return SpellResult.FAIL;
        }
    }
    if (originalTarget.getType() == Material.WATER || originalTarget.getType() == Material.STATIONARY_WATER) {
        famType = EntityType.SQUID;
    }
    boolean spawnBaby = parameters.getBoolean("baby", false);
    List<LivingEntity> newFamiliars = new ArrayList<>();
    for (int i = 0; i < famCount; i++) {
        EntityType entityType = famType;
        if (entityType == null) {
            String randomType = RandomUtils.weightedRandom(entityTypeProbability);
            try {
                entityType = EntityType.fromName(randomType);
            } catch (Throwable ex) {
                sendMessage("Unknown entity type: " + randomType);
                return SpellResult.FAIL;
            }
        }
        if (parameters.contains("reason")) {
            String reasonText = parameters.getString("reason").toUpperCase();
            try {
                spawnReason = CreatureSpawnEvent.SpawnReason.valueOf(reasonText);
            } catch (Exception ex) {
                sendMessage("Unknown spawn reason: " + reasonText);
                return SpellResult.FAIL;
            }
        }
        final Location targetLoc = centerLoc.clone();
        if (famCount > 1) {
            targetLoc.setX(targetLoc.getX() + rand.nextInt(2 * famCount) - famCount);
            targetLoc.setZ(targetLoc.getZ() + rand.nextInt(2 * famCount) - famCount);
        }
        targetLoc.setPitch(caster.getPitch());
        targetLoc.setYaw(caster.getYaw());
        if (entityType != null) {
            final LivingEntity entity = spawnFamiliar(targetLoc, entityType, targetBlock.getLocation(), targetEntity, setTarget);
            if (entity != null) {
                if (entityName != null && !entityName.isEmpty()) {
                    entity.setCustomName(entityName);
                }
                if (!loot) {
                    entity.setMetadata("nodrops", new FixedMetadataValue(mage.getController().getPlugin(), true));
                }
                if (spawnBaby && entity instanceof Ageable) {
                    Ageable ageable = (Ageable) entity;
                    ageable.setBaby();
                }
                entity.teleport(targetLoc);
                newFamiliars.add(entity);
                spawnCount++;
                registerForUndo(entity);
            }
        }
    }
    registerForUndo();
    if (track) {
        setFamiliars(newFamiliars);
        checkListener();
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) ArrayList(java.util.ArrayList) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) Material(org.bukkit.Material) Ageable(org.bukkit.entity.Ageable) LivingEntity(org.bukkit.entity.LivingEntity) EntityType(org.bukkit.entity.EntityType) Target(com.elmakers.mine.bukkit.utility.Target) Block(org.bukkit.block.Block) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 4 with Target

use of com.elmakers.mine.bukkit.utility.Target in project MagicPlugin by elBukkit.

the class BoomSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    int size = parameters.getInt("size", defaultSize);
    boolean useFire = parameters.getBoolean("fire", false);
    boolean breakBlocks = parameters.getBoolean("break_blocks", true);
    size = (int) (mage.getRadiusMultiplier() * size);
    Target target = getTarget();
    if (!target.hasTarget()) {
        return SpellResult.NO_TARGET;
    }
    return createExplosionAt(target.getLocation(), size, useFire, breakBlocks);
}
Also used : Target(com.elmakers.mine.bukkit.utility.Target)

Example 5 with Target

use of com.elmakers.mine.bukkit.utility.Target in project MagicPlugin by elBukkit.

the class CameraSpell method onCast.

@SuppressWarnings("deprecation")
@Override
public SpellResult onCast(ConfigurationSection parameters) {
    ItemStack newMapItem = null;
    Integer priority = ConfigurationUtils.getInteger(parameters, "priority", null);
    boolean selfie = false;
    // Check for special case id
    if (parameters.contains("id")) {
        newMapItem = new ItemStack(Material.MAP, 1, (short) parameters.getInt("id", 0));
        String mapName = parameters.getString("name", "Image");
        ItemMeta meta = newMapItem.getItemMeta();
        // TODO: How to handle names with spaces in them?
        meta.setDisplayName(mapName);
        newMapItem.setItemMeta(meta);
    }
    MapController maps = controller.getMaps();
    // Check for special case url
    if (newMapItem == null) {
        String url = parameters.getString("url");
        if (url != null) {
            int x = parameters.getInt("x", 0);
            int y = parameters.getInt("y", 0);
            int width = parameters.getInt("width", 0);
            int height = parameters.getInt("height", 0);
            String mapName = parameters.getString("name", "Photo");
            newMapItem = maps.getURLItem(getWorld().getName(), url, mapName, x, y, width, height, priority);
        }
    }
    if (newMapItem == null) {
        Target target = getTarget();
        String playerName = parameters.getString("name");
        String metaName = null;
        if (playerName == null) {
            if (target != null) {
                if (target.hasEntity()) {
                    Entity targetEntity = target.getEntity();
                    selfie = (targetEntity == mage.getEntity());
                    if (targetEntity instanceof Player) {
                        playerName = ((Player) targetEntity).getName();
                    } else {
                        playerName = getMobSkin(targetEntity.getType());
                        if (playerName != null) {
                            metaName = targetEntity.getType().getName();
                        }
                    }
                } else {
                    Block targetBlock = target.getBlock();
                    if (targetBlock == null) {
                        return SpellResult.NO_TARGET;
                    }
                    playerName = getBlockSkin(targetBlock.getType());
                    if (playerName != null) {
                        metaName = target.getBlock().getType().name().toLowerCase();
                    }
                }
            }
        }
        if (playerName == null) {
            Player player = mage.getPlayer();
            if (player == null) {
                return SpellResult.NO_TARGET;
            }
            playerName = player.getName();
            selfie = true;
        }
        if (parameters.contains("reload")) {
            maps.forceReloadPlayerPortrait(getWorld().getName(), playerName);
        }
        metaName = (metaName == null) ? playerName : metaName;
        newMapItem = maps.getPlayerPortrait(getWorld().getName(), playerName, priority, "Photo of " + metaName);
    }
    if (newMapItem == null) {
        return SpellResult.FAIL;
    }
    getWorld().dropItemNaturally(getLocation(), newMapItem);
    return selfie ? SpellResult.CAST_SELF : SpellResult.CAST_TARGET;
}
Also used : Entity(org.bukkit.entity.Entity) Target(com.elmakers.mine.bukkit.utility.Target) Player(org.bukkit.entity.Player) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta) MapController(com.elmakers.mine.bukkit.api.maps.MapController)

Aggregations

Target (com.elmakers.mine.bukkit.utility.Target)44 Entity (org.bukkit.entity.Entity)24 Location (org.bukkit.Location)20 Block (org.bukkit.block.Block)20 LivingEntity (org.bukkit.entity.LivingEntity)17 Player (org.bukkit.entity.Player)14 Mage (com.elmakers.mine.bukkit.api.magic.Mage)13 ArrayList (java.util.ArrayList)9 Vector (org.bukkit.util.Vector)7 MaterialAndData (com.elmakers.mine.bukkit.block.MaterialAndData)5 ActionHandler (com.elmakers.mine.bukkit.action.ActionHandler)4 CoverAction (com.elmakers.mine.bukkit.action.builtin.CoverAction)4 ItemStack (org.bukkit.inventory.ItemStack)4 MaterialBrush (com.elmakers.mine.bukkit.api.block.MaterialBrush)3 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)3 Material (org.bukkit.Material)3 Ageable (org.bukkit.entity.Ageable)3 SpellResult (com.elmakers.mine.bukkit.api.spell.SpellResult)2 ConstructionType (com.elmakers.mine.bukkit.block.ConstructionType)2 World (org.bukkit.World)2