Search in sources :

Example 1 with Batch

use of com.elmakers.mine.bukkit.api.batch.Batch in project MagicPlugin by elBukkit.

the class BaseSpell method cast.

@Override
public boolean cast(ConfigurationSection extraParameters, Location defaultLocation) {
    if (mage.isPlayer() && mage.getPlayer().getGameMode() == GameMode.SPECTATOR) {
        if (mage.getDebugLevel() > 0 && extraParameters != null) {
            mage.sendDebugMessage("Cannot cast in spectator mode.");
        }
        return false;
    }
    if (toggle != ToggleType.NONE && isActive()) {
        mage.sendDebugMessage(ChatColor.DARK_BLUE + "Deactivating " + ChatColor.GOLD + getName());
        deactivate(true, true);
        processResult(SpellResult.DEACTIVATE, parameters);
        return true;
    }
    if (mage.getDebugLevel() > 5 && extraParameters != null) {
        Collection<String> keys = extraParameters.getKeys(false);
        if (keys.size() > 0) {
            mage.sendDebugMessage(ChatColor.BLUE + "Cast " + ChatColor.GOLD + getName() + " " + ChatColor.GREEN + ConfigurationUtils.getParameters(extraParameters));
        }
    }
    this.reset();
    Location location = mage.getLocation();
    if (location != null) {
        Set<String> overrides = controller.getSpellOverrides(mage, location);
        if (overrides != null && !overrides.isEmpty()) {
            if (extraParameters == null) {
                extraParameters = new MemoryConfiguration();
            }
            for (String entry : overrides) {
                String[] pieces = StringUtils.split(entry, ' ');
                if (pieces.length < 2)
                    continue;
                String fullKey = pieces[0];
                String[] key = StringUtils.split(fullKey, '.');
                if (key.length == 0)
                    continue;
                if (key.length == 2 && !key[0].equals("default") && !key[0].equals(spellKey.getBaseKey()) && !key[0].equals(spellKey.getKey())) {
                    continue;
                }
                fullKey = key.length == 2 ? key[1] : key[0];
                ConfigurationUtils.set(extraParameters, fullKey, pieces[1]);
            }
        }
    }
    if (this.currentCast == null) {
        this.currentCast = new CastContext();
        this.currentCast.setSpell(this);
    }
    this.location = defaultLocation;
    workingParameters = new SpellParameters(this);
    ConfigurationUtils.addConfigurations(workingParameters, this.parameters);
    ConfigurationUtils.addConfigurations(workingParameters, extraParameters);
    processParameters(workingParameters);
    // Check to see if this is allowed to be cast by a command block
    if (!commandBlockAllowed) {
        CommandSender sender = mage.getCommandSender();
        if (sender != null && sender instanceof BlockCommandSender) {
            Block block = mage.getLocation().getBlock();
            if (block.getType() == Material.COMMAND) {
                block.setType(Material.AIR);
            }
            return false;
        }
    }
    // Allow other plugins to cancel this cast
    PreCastEvent preCast = new PreCastEvent(mage, this);
    Bukkit.getPluginManager().callEvent(preCast);
    if (preCast.isCancelled()) {
        processResult(SpellResult.CANCELLED, workingParameters);
        sendCastMessage(SpellResult.CANCELLED, " (no cast)");
        return false;
    }
    // Don't allow casting if the player is confused or weakened
    bypassConfusion = workingParameters.getBoolean("bypass_confusion", bypassConfusion);
    bypassWeakness = workingParameters.getBoolean("bypass_weakness", bypassWeakness);
    LivingEntity livingEntity = mage.getLivingEntity();
    if (livingEntity != null && !mage.isSuperPowered()) {
        if (!bypassConfusion && livingEntity.hasPotionEffect(PotionEffectType.CONFUSION)) {
            processResult(SpellResult.CURSED, workingParameters);
            sendCastMessage(SpellResult.CURSED, " (no cast)");
            return false;
        }
        // Don't allow casting if the player is weakened
        if (!bypassWeakness && livingEntity.hasPotionEffect(PotionEffectType.WEAKNESS)) {
            processResult(SpellResult.CURSED, workingParameters);
            sendCastMessage(SpellResult.CURSED, " (no cast)");
            return false;
        }
    }
    // Don't perform permission check until after processing parameters, in case of overrides
    if (!canCast(getLocation())) {
        processResult(SpellResult.INSUFFICIENT_PERMISSION, workingParameters);
        sendCastMessage(SpellResult.INSUFFICIENT_PERMISSION, " (no cast)");
        if (mage.getDebugLevel() > 1) {
            CommandSender messageTarget = mage.getDebugger();
            if (messageTarget == null) {
                messageTarget = mage.getCommandSender();
            }
            if (messageTarget != null) {
                mage.debugPermissions(messageTarget, this);
            }
        }
        return false;
    }
    this.preCast();
    // PVP override settings
    bypassPvpRestriction = workingParameters.getBoolean("bypass_pvp", false);
    bypassPvpRestriction = workingParameters.getBoolean("bp", bypassPvpRestriction);
    bypassPermissions = workingParameters.getBoolean("bypass_permissions", bypassPermissions);
    bypassFriendlyFire = workingParameters.getBoolean("bypass_friendly_fire", false);
    onlyFriendlyFire = workingParameters.getBoolean("only_friendly", false);
    // Check cooldowns
    cooldown = workingParameters.getInt("cooldown", cooldown);
    cooldown = workingParameters.getInt("cool", cooldown);
    mageCooldown = workingParameters.getInt("cooldown_mage", mageCooldown);
    // Color override
    color = ConfigurationUtils.getColor(workingParameters, "color", color);
    particle = workingParameters.getString("particle", null);
    double cooldownRemaining = getRemainingCooldown() / 1000.0;
    String timeDescription = "";
    if (cooldownRemaining > 0) {
        if (cooldownRemaining > 60 * 60) {
            long hours = (long) Math.ceil(cooldownRemaining / (60 * 60));
            if (hours == 1) {
                timeDescription = controller.getMessages().get("cooldown.wait_hour");
            } else {
                timeDescription = controller.getMessages().get("cooldown.wait_hours").replace("$hours", Long.toString(hours));
            }
        } else if (cooldownRemaining > 60) {
            long minutes = (long) Math.ceil(cooldownRemaining / 60);
            if (minutes == 1) {
                timeDescription = controller.getMessages().get("cooldown.wait_minute");
            } else {
                timeDescription = controller.getMessages().get("cooldown.wait_minutes").replace("$minutes", Long.toString(minutes));
            }
        } else if (cooldownRemaining >= 1) {
            long seconds = (long) Math.ceil(cooldownRemaining);
            if (seconds == 1) {
                timeDescription = controller.getMessages().get("cooldown.wait_second");
            } else {
                timeDescription = controller.getMessages().get("cooldown.wait_seconds").replace("$seconds", Long.toString(seconds));
            }
        } else {
            timeDescription = controller.getMessages().get("cooldown.wait_moment");
            if (timeDescription.contains("$seconds")) {
                timeDescription = timeDescription.replace("$seconds", SECONDS_FORMATTER.format(cooldownRemaining));
            }
        }
        castMessage(getMessage("cooldown").replace("$time", timeDescription));
        processResult(SpellResult.COOLDOWN, workingParameters);
        sendCastMessage(SpellResult.COOLDOWN, " (no cast)");
        return false;
    }
    CastingCost required = getRequiredCost();
    if (required != null) {
        String baseMessage = getMessage("insufficient_resources");
        String costDescription = required.getDescription(controller.getMessages(), mage);
        // Send loud messages when items are required.
        if (required.isItem()) {
            sendMessage(baseMessage.replace("$cost", costDescription));
        } else {
            castMessage(baseMessage.replace("$cost", costDescription));
        }
        processResult(SpellResult.INSUFFICIENT_RESOURCES, workingParameters);
        sendCastMessage(SpellResult.INSUFFICIENT_RESOURCES, " (no cast)");
        return false;
    }
    if (requiredHealth > 0) {
        LivingEntity li = mage.getLivingEntity();
        double healthPercentage = li == null ? 0 : 100 * li.getHealth() / li.getMaxHealth();
        if (healthPercentage < requiredHealth) {
            processResult(SpellResult.INSUFFICIENT_RESOURCES, workingParameters);
            sendCastMessage(SpellResult.INSUFFICIENT_RESOURCES, " (no cast)");
            return false;
        }
    }
    if (controller.isSpellProgressionEnabled()) {
        long progressLevel = getProgressLevel();
        for (Entry<String, EquationTransform> entry : progressLevelEquations.entrySet()) {
            workingParameters.set(entry.getKey(), entry.getValue().get(progressLevel));
        }
    }
    // Check for cancel-on-cast-other spells, after we have determined that this spell can really be cast.
    if (!passive) {
        for (Iterator<Batch> iterator = mage.getPendingBatches().iterator(); iterator.hasNext(); ) {
            Batch batch = iterator.next();
            if (!(batch instanceof SpellBatch))
                continue;
            SpellBatch spellBatch = (SpellBatch) batch;
            Spell spell = spellBatch.getSpell();
            if (spell.cancelOnCastOther()) {
                spell.cancel();
                batch.finish();
                iterator.remove();
            }
        }
    }
    return finalizeCast(workingParameters);
}
Also used : EquationTransform(de.slikey.effectlib.math.EquationTransform) SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) SpellParameters(com.elmakers.mine.bukkit.magic.SpellParameters) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) PreCastEvent(com.elmakers.mine.bukkit.api.event.PreCastEvent) Spell(com.elmakers.mine.bukkit.api.spell.Spell) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) PrerequisiteSpell(com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell) LivingEntity(org.bukkit.entity.LivingEntity) CastingCost(com.elmakers.mine.bukkit.api.spell.CastingCost) CastContext(com.elmakers.mine.bukkit.action.CastContext) Batch(com.elmakers.mine.bukkit.api.batch.Batch) SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) Block(org.bukkit.block.Block) CommandSender(org.bukkit.command.CommandSender) BlockCommandSender(org.bukkit.command.BlockCommandSender) Location(org.bukkit.Location) BlockCommandSender(org.bukkit.command.BlockCommandSender)

Example 2 with Batch

use of com.elmakers.mine.bukkit.api.batch.Batch in project MagicPlugin by elBukkit.

the class Mage method cancelPending.

@Nullable
@Override
public Batch cancelPending(String spellKey, boolean force) {
    Batch stoppedPending = null;
    if (pendingBatches.size() > 0) {
        List<Batch> batches = new ArrayList<>();
        batches.addAll(pendingBatches);
        for (Batch batch : batches) {
            if (spellKey != null || !force) {
                if (!(batch instanceof SpellBatch)) {
                    continue;
                }
                SpellBatch spellBatch = (SpellBatch) batch;
                Spell spell = spellBatch.getSpell();
                if (spell == null) {
                    continue;
                }
                if (!force && !spell.isCancellable()) {
                    continue;
                }
                if (spellKey != null && !spell.getSpellKey().getBaseKey().equalsIgnoreCase(spellKey)) {
                    continue;
                }
            }
            if (!(batch instanceof UndoBatch)) {
                if (batch instanceof SpellBatch) {
                    SpellBatch spellBatch = (SpellBatch) batch;
                    Spell spell = spellBatch.getSpell();
                    if (spell != null) {
                        spell.cancel();
                    }
                }
                batch.finish();
                pendingBatches.remove(batch);
                stoppedPending = batch;
            }
        }
    }
    return stoppedPending;
}
Also used : SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) Batch(com.elmakers.mine.bukkit.api.batch.Batch) UndoBatch(com.elmakers.mine.bukkit.batch.UndoBatch) SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) UndoBatch(com.elmakers.mine.bukkit.batch.UndoBatch) ArrayList(java.util.ArrayList) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) ActionSpell(com.elmakers.mine.bukkit.spell.ActionSpell) Spell(com.elmakers.mine.bukkit.api.spell.Spell) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) Nullable(javax.annotation.Nullable)

Example 3 with Batch

use of com.elmakers.mine.bukkit.api.batch.Batch in project MagicPlugin by elBukkit.

the class Mage method processPendingBatches.

public int processPendingBatches(int maxBlockUpdates) {
    int updated = 0;
    if (pendingBatches.size() > 0) {
        List<Batch> processBatches = new ArrayList<>(pendingBatches);
        pendingBatches.clear();
        for (Batch batch : processBatches) {
            if (updated < maxBlockUpdates) {
                int batchUpdated = batch.process(maxBlockUpdates - updated);
                updated += batchUpdated;
            }
            if (!batch.isFinished()) {
                pendingBatches.add(batch);
            }
        }
    }
    return updated;
}
Also used : Batch(com.elmakers.mine.bukkit.api.batch.Batch) UndoBatch(com.elmakers.mine.bukkit.batch.UndoBatch) SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) ArrayList(java.util.ArrayList)

Example 4 with Batch

use of com.elmakers.mine.bukkit.api.batch.Batch in project MagicPlugin by elBukkit.

the class Mage method finishPendingUndo.

@Override
public int finishPendingUndo() {
    int finished = 0;
    if (pendingBatches.size() > 0) {
        List<Batch> batches = new ArrayList<>();
        batches.addAll(pendingBatches);
        for (Batch batch : batches) {
            if (batch instanceof UndoBatch) {
                while (!batch.isFinished()) {
                    batch.process(1000);
                }
                pendingBatches.remove(batch);
                finished++;
            }
        }
    }
    return finished;
}
Also used : Batch(com.elmakers.mine.bukkit.api.batch.Batch) UndoBatch(com.elmakers.mine.bukkit.batch.UndoBatch) SpellBatch(com.elmakers.mine.bukkit.api.batch.SpellBatch) UndoBatch(com.elmakers.mine.bukkit.batch.UndoBatch) ArrayList(java.util.ArrayList)

Example 5 with Batch

use of com.elmakers.mine.bukkit.api.batch.Batch in project MagicPlugin by elBukkit.

the class CancelAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity targetEntity = context.getTargetEntity();
    MageController controller = context.getController();
    if (targetEntity == null || !controller.isMage(targetEntity)) {
        return SpellResult.NO_TARGET;
    }
    SpellResult result = SpellResult.NO_TARGET;
    Mage targetMage = controller.getMage(targetEntity);
    if (spellKeys == null) {
        Batch batch = targetMage.cancelPending(force);
        if (batch != null) {
            result = SpellResult.CAST;
            undoListName = batch.getName();
        }
    } else {
        for (String spellKey : spellKeys) {
            Batch batch = targetMage.cancelPending(spellKey, force);
            if (batch != null) {
                result = SpellResult.CAST;
                undoListName = batch.getName();
            }
        }
    }
    return result;
}
Also used : Entity(org.bukkit.entity.Entity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Batch(com.elmakers.mine.bukkit.api.batch.Batch) Mage(com.elmakers.mine.bukkit.api.magic.Mage) SpellResult(com.elmakers.mine.bukkit.api.spell.SpellResult)

Aggregations

Batch (com.elmakers.mine.bukkit.api.batch.Batch)9 SpellBatch (com.elmakers.mine.bukkit.api.batch.SpellBatch)6 ArrayList (java.util.ArrayList)5 Mage (com.elmakers.mine.bukkit.api.magic.Mage)4 UndoBatch (com.elmakers.mine.bukkit.batch.UndoBatch)4 Block (org.bukkit.block.Block)4 Entity (org.bukkit.entity.Entity)4 MageController (com.elmakers.mine.bukkit.api.magic.MageController)3 MageSpell (com.elmakers.mine.bukkit.api.spell.MageSpell)3 Spell (com.elmakers.mine.bukkit.api.spell.Spell)3 SpellResult (com.elmakers.mine.bukkit.api.spell.SpellResult)3 LivingEntity (org.bukkit.entity.LivingEntity)3 UndoList (com.elmakers.mine.bukkit.api.block.UndoList)2 UndoQueue (com.elmakers.mine.bukkit.api.block.UndoQueue)2 ActionSpell (com.elmakers.mine.bukkit.spell.ActionSpell)2 BaseSpell (com.elmakers.mine.bukkit.spell.BaseSpell)2 Location (org.bukkit.Location)2 CastContext (com.elmakers.mine.bukkit.action.CastContext)1 BlockData (com.elmakers.mine.bukkit.api.block.BlockData)1 PreCastEvent (com.elmakers.mine.bukkit.api.event.PreCastEvent)1