Search in sources :

Example 16 with UndoList

use of com.elmakers.mine.bukkit.api.block.UndoList in project MagicPlugin by elBukkit.

the class Targeting method breakBlockRecursively.

protected int breakBlockRecursively(CastContext context, Block block, int depth) {
    if (depth <= 0)
        return 0;
    if (!context.isBreakable(block))
        return 0;
    // Play break FX
    Location blockLocation = block.getLocation();
    Location effectLocation = blockLocation.add(0.5, 0.5, 0.5);
    context.playEffects("break", 1, context.getLocation(), null, effectLocation, null);
    // TODO: Re-examime this?
    UndoList undoList = com.elmakers.mine.bukkit.block.UndoList.getUndoList(blockLocation);
    if (undoList != null) {
        undoList.add(block);
    }
    context.clearBreakable(block);
    context.clearReflective(block);
    block.setType(Material.AIR);
    int broken = 1;
    if (depth > broken) {
        broken += breakBlockRecursively(context, block.getRelative(BlockFace.UP), Math.min(targetBreakableDepth, depth - broken));
        broken += breakBlockRecursively(context, block.getRelative(BlockFace.DOWN), Math.min(targetBreakableDepth, depth - broken));
        broken += breakBlockRecursively(context, block.getRelative(BlockFace.EAST), Math.min(targetBreakableDepth, depth - broken));
        broken += breakBlockRecursively(context, block.getRelative(BlockFace.WEST), Math.min(targetBreakableDepth, depth - broken));
        broken += breakBlockRecursively(context, block.getRelative(BlockFace.NORTH), Math.min(targetBreakableDepth, depth - broken));
        broken += breakBlockRecursively(context, block.getRelative(BlockFace.SOUTH), Math.min(targetBreakableDepth, depth - broken));
    }
    return broken;
}
Also used : UndoList(com.elmakers.mine.bukkit.api.block.UndoList) Location(org.bukkit.Location)

Example 17 with UndoList

use of com.elmakers.mine.bukkit.api.block.UndoList in project MagicPlugin by elBukkit.

the class MagicController method getEntityUndo.

@Nullable
public UndoList getEntityUndo(Entity entity) {
    UndoList blockList = null;
    if (entity == null)
        return null;
    Mage mage = getRegisteredMage(entity);
    if (mage == null && entity instanceof Projectile) {
        Projectile projectile = (Projectile) entity;
        ProjectileSource source = projectile.getShooter();
        if (source instanceof LivingEntity) {
            entity = (LivingEntity) source;
            mage = getRegisteredMage(entity);
        }
    }
    if (mage != null) {
        UndoList undoList = mage.getLastUndoList();
        if (undoList != null) {
            long now = System.currentTimeMillis();
            if (undoList.getModifiedTime() > now - undoTimeWindow) {
                blockList = undoList;
            }
        }
    } else {
        blockList = com.elmakers.mine.bukkit.block.UndoList.getUndoList(entity);
    }
    return blockList;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ProjectileSource(org.bukkit.projectiles.ProjectileSource) Projectile(org.bukkit.entity.Projectile) Nullable(javax.annotation.Nullable)

Example 18 with UndoList

use of com.elmakers.mine.bukkit.api.block.UndoList in project MagicPlugin by elBukkit.

the class MagicController method processUndo.

protected void processUndo() {
    long now = System.currentTimeMillis();
    while (scheduledUndo.size() > 0) {
        UndoList undo = scheduledUndo.peek();
        if (now < undo.getScheduledTime()) {
            break;
        }
        scheduledUndo.poll();
        undo.undoScheduled();
    }
}
Also used : UndoList(com.elmakers.mine.bukkit.api.block.UndoList)

Example 19 with UndoList

use of com.elmakers.mine.bukkit.api.block.UndoList in project MagicPlugin by elBukkit.

the class MagicController method undoScheduled.

public void undoScheduled() {
    int undid = 0;
    while (!scheduledUndo.isEmpty()) {
        UndoList undoList = scheduledUndo.poll();
        undoList.undoScheduled(true);
    }
    if (undid > 0) {
        info("Undid " + undid + " pending spells");
    }
}
Also used : UndoList(com.elmakers.mine.bukkit.api.block.UndoList)

Example 20 with UndoList

use of com.elmakers.mine.bukkit.api.block.UndoList in project MagicPlugin by elBukkit.

the class ShrinkSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    String giveName = parameters.getString("name");
    if (giveName != null) {
        String itemName = giveName + "'s Head";
        Player player = Bukkit.getPlayer(giveName);
        if (player != null) {
            dropPlayerHead(getLocation(), player, itemName);
        } else {
            dropPlayerHead(getLocation(), giveName, itemName);
        }
        return SpellResult.CAST;
    }
    Target target = getTarget();
    if (target.hasEntity()) {
        Entity targetEntity = target.getEntity();
        if (controller.isElemental(targetEntity)) {
            double elementalSize = controller.getElementalScale(targetEntity);
            if (elementalSize < 0.1) {
                int elementalDamage = parameters.getInt("elemental_damage", DEFAULT_ENTITY_DAMAGE);
                controller.damageElemental(targetEntity, elementalDamage, 0, mage.getCommandSender());
            } else {
                elementalSize /= 2;
                controller.setElementalScale(targetEntity, elementalSize);
            }
            return SpellResult.CAST;
        }
        if (!(targetEntity instanceof LivingEntity))
            return SpellResult.NO_TARGET;
        // Register for undo in advance to catch entity death.
        registerForUndo();
        int damage = parameters.getInt("entity_damage", DEFAULT_ENTITY_DAMAGE);
        LivingEntity li = (LivingEntity) targetEntity;
        boolean alreadyDead = li.isDead() || li.getHealth() <= 0;
        String ownerName = null;
        String itemName = null;
        byte data = 3;
        if (li instanceof Player) {
            damage = parameters.getInt("player_damage", DEFAULT_PLAYER_DAMAGE);
            ownerName = ((Player) li).getName();
        } else {
            itemName = li.getType().getName() + " Head";
            switch(li.getType()) {
                case CREEPER:
                    data = 4;
                    break;
                case ZOMBIE:
                    data = 2;
                    break;
                case SKELETON:
                    Skeleton skeleton = (Skeleton) li;
                    data = (byte) (skeleton.getSkeletonType() == SkeletonType.NORMAL ? 0 : 1);
                    break;
                default:
                    ownerName = getMobSkin(li.getType());
            }
        }
        if (itemName == null && ownerName != null) {
            itemName = ownerName + "'s Head";
        }
        Location targetLocation = targetEntity.getLocation();
        if (li instanceof Player) {
            CompatibilityUtils.magicDamage(li, damage, mage.getEntity());
            if (li.isDead() && !alreadyDead) {
                dropPlayerHead(targetEntity.getLocation(), (Player) li, itemName);
            }
        } else if (li.getType() == EntityType.GIANT) {
            UndoList spawnedList = com.elmakers.mine.bukkit.block.UndoList.getUndoList(li);
            registerModified(li);
            li.remove();
            Entity zombie = targetLocation.getWorld().spawnEntity(targetLocation, EntityType.ZOMBIE);
            if (zombie instanceof Zombie) {
                ((Zombie) zombie).setBaby(false);
            }
            registerForUndo(zombie);
            if (spawnedList != null) {
                spawnedList.add(zombie);
            }
        } else if (li instanceof Ageable && ((Ageable) li).isAdult() && !(li instanceof Player)) {
            registerModified(li);
            ((Ageable) li).setBaby();
        } else if (li instanceof Zombie && !((Zombie) li).isBaby()) {
            registerModified(li);
            ((Zombie) li).setBaby(true);
        } else if (li instanceof PigZombie && !((PigZombie) li).isBaby()) {
            registerModified(li);
            ((PigZombie) li).setBaby(true);
        } else if (li instanceof Slime && ((Slime) li).getSize() > 1) {
            registerModified(li);
            Slime slime = (Slime) li;
            slime.setSize(slime.getSize() - 1);
        } else {
            CompatibilityUtils.magicDamage(li, damage, mage.getEntity());
            if ((ownerName != null || data != 3) && (li.isDead() || li.getHealth() == 0) && !alreadyDead) {
                dropHead(targetEntity.getLocation(), ownerName, itemName, data);
            }
        }
    } else {
        Block targetBlock = target.getBlock();
        if (targetBlock == null) {
            return SpellResult.NO_TARGET;
        }
        String blockSkin = getBlockSkin(targetBlock.getType());
        if (blockSkin == null)
            return SpellResult.NO_TARGET;
        if (!hasBreakPermission(targetBlock)) {
            return SpellResult.INSUFFICIENT_PERMISSION;
        }
        if (mage.isIndestructible(targetBlock)) {
            return SpellResult.NO_TARGET;
        }
        registerForUndo(targetBlock);
        registerForUndo();
        dropHead(targetBlock.getLocation(), blockSkin, targetBlock.getType().name(), (byte) 3);
        targetBlock.setType(Material.AIR);
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) Zombie(org.bukkit.entity.Zombie) PigZombie(org.bukkit.entity.PigZombie) Ageable(org.bukkit.entity.Ageable) Slime(org.bukkit.entity.Slime) LivingEntity(org.bukkit.entity.LivingEntity) Target(com.elmakers.mine.bukkit.utility.Target) UndoList(com.elmakers.mine.bukkit.api.block.UndoList) PigZombie(org.bukkit.entity.PigZombie) Block(org.bukkit.block.Block) Skeleton(org.bukkit.entity.Skeleton) Location(org.bukkit.Location)

Aggregations

UndoList (com.elmakers.mine.bukkit.api.block.UndoList)36 EventHandler (org.bukkit.event.EventHandler)15 Entity (org.bukkit.entity.Entity)14 Block (org.bukkit.block.Block)13 Location (org.bukkit.Location)10 FallingBlock (org.bukkit.entity.FallingBlock)10 Mage (com.elmakers.mine.bukkit.api.magic.Mage)8 LivingEntity (org.bukkit.entity.LivingEntity)6 Player (org.bukkit.entity.Player)6 ItemStack (org.bukkit.inventory.ItemStack)6 Ageable (org.bukkit.entity.Ageable)4 PigZombie (org.bukkit.entity.PigZombie)4 Slime (org.bukkit.entity.Slime)4 Zombie (org.bukkit.entity.Zombie)4 MageController (com.elmakers.mine.bukkit.api.magic.MageController)3 SpellResult (com.elmakers.mine.bukkit.api.spell.SpellResult)3 Target (com.elmakers.mine.bukkit.utility.Target)3 Nullable (javax.annotation.Nullable)3 BlockFace (org.bukkit.block.BlockFace)3 Skeleton (org.bukkit.entity.Skeleton)3