Search in sources :

Example 1 with EquationTransform

use of de.slikey.effectlib.math.EquationTransform 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 EquationTransform

use of de.slikey.effectlib.math.EquationTransform in project MagicPlugin by elBukkit.

the class ParameterizedConfiguration method evaluate.

@Nullable
protected Double evaluate(String expression) {
    Set<String> parameters = getParameters();
    if (parameters == null || parameters.isEmpty())
        return null;
    EquationTransform transform = EquationStore.getInstance().getTransform(expression, parameters);
    for (String parameter : transform.getParameters()) {
        double value = getParameter(parameter);
        transform.setVariable(parameter, value);
    }
    return transform.get();
}
Also used : EquationTransform(de.slikey.effectlib.math.EquationTransform) Nullable(javax.annotation.Nullable)

Example 3 with EquationTransform

use of de.slikey.effectlib.math.EquationTransform in project MagicPlugin by elBukkit.

the class MageCommandExecutor method onMageAttribute.

public boolean onMageAttribute(CommandSender sender, Player player, String[] args) {
    Mage mage = controller.getMage(player);
    Set<String> attributes = api.getController().getAttributes();
    if (attributes.isEmpty()) {
        sender.sendMessage(ChatColor.RED + "No attributes configured, see attributes.yml");
        return true;
    }
    if (args.length == 0) {
        sender.sendMessage(ChatColor.GOLD + "Attributes for: " + ChatColor.AQUA + player.getName());
        for (String key : attributes) {
            Double value = mage.getAttribute(key);
            String valueDescription = value == null ? ChatColor.RED + "(not set)" : ChatColor.AQUA + Double.toString(value);
            sender.sendMessage(ChatColor.DARK_AQUA + key + ChatColor.BLUE + " = " + valueDescription);
        }
        return true;
    }
    String key = args[0];
    if (args.length == 1) {
        if (!attributes.contains(key)) {
            sender.sendMessage(ChatColor.RED + "Unknown attribute: " + ChatColor.YELLOW + key);
            return true;
        }
        Double value = mage.getAttribute(key);
        String valueDescription = value == null ? ChatColor.RED + "(not set)" : ChatColor.AQUA + Double.toString(value);
        sender.sendMessage(ChatColor.AQUA + player.getName() + " has " + ChatColor.DARK_AQUA + key + ChatColor.BLUE + " of " + valueDescription);
        return true;
    }
    MageClass activeClass = mage.getActiveClass();
    CasterProperties attributeProperties = activeClass == null ? mage.getProperties() : activeClass;
    String value = args[1];
    for (int i = 2; i < args.length; i++) {
        value = value + " " + args[i];
    }
    if (value.equals("-")) {
        Double oldValue = attributeProperties.getAttribute(key);
        attributeProperties.setAttribute(key, null);
        String valueDescription = oldValue == null ? ChatColor.RED + "(not set)" : ChatColor.AQUA + Double.toString(oldValue);
        sender.sendMessage(ChatColor.BLUE + "Removed attribute " + ChatColor.DARK_AQUA + key + ChatColor.BLUE + ", was " + valueDescription);
        return true;
    }
    double transformed = Double.NaN;
    try {
        transformed = Double.parseDouble(value);
    } catch (Exception ex) {
        EquationTransform transform = EquationStore.getInstance().getTransform(value);
        if (transform.getException() == null) {
            Double property = attributeProperties.getAttribute(key);
            if (property == null || Double.isNaN(property)) {
                property = 0.0;
            }
            transform.setVariable("x", property);
            transformed = transform.get();
        }
    }
    if (Double.isNaN(transformed)) {
        sender.sendMessage(ChatColor.RED + "Could not set " + ChatColor.YELLOW + key + ChatColor.RED + " to " + ChatColor.YELLOW + value);
        return true;
    }
    attributeProperties.setAttribute(key, transformed);
    sender.sendMessage(ChatColor.GOLD + "Set " + ChatColor.DARK_AQUA + key + ChatColor.GOLD + " to " + ChatColor.AQUA + transformed + ChatColor.GOLD + " for " + ChatColor.DARK_AQUA + player.getDisplayName());
    return true;
}
Also used : CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) EquationTransform(de.slikey.effectlib.math.EquationTransform) MageClass(com.elmakers.mine.bukkit.api.magic.MageClass) Mage(com.elmakers.mine.bukkit.api.magic.Mage)

Example 4 with EquationTransform

use of de.slikey.effectlib.math.EquationTransform in project MagicPlugin by elBukkit.

the class ModifyPropertiesAction method perform.

@Override
public SpellResult perform(CastContext context) {
    if (modify == null) {
        return SpellResult.FAIL;
    }
    Entity entity = context.getTargetEntity();
    Mage mage = entity == null ? null : context.getController().getRegisteredMage(entity);
    if (mage == null) {
        return SpellResult.NO_TARGET;
    }
    CasterProperties properties = null;
    if (modifyTarget.equals("wand")) {
        properties = mage.getActiveWand();
    } else if (modifyTarget.equals("player")) {
        properties = mage.getProperties();
    } else {
        properties = mage.getClass(modifyTarget);
    }
    // I am now wishing I hadn't made a base class called "mage" :(
    if (properties == null && modifyTarget.equals("mage")) {
        properties = mage.getProperties();
    }
    if (properties == null) {
        return SpellResult.NO_TARGET;
    }
    ConfigurationSection original = new MemoryConfiguration();
    ConfigurationSection changed = new MemoryConfiguration();
    for (ModifyProperty property : modify) {
        Object originalValue = properties.getProperty(property.path);
        Object newValue = property.value;
        if ((originalValue == null || originalValue instanceof Number) && property.value instanceof String) {
            EquationTransform transform = EquationStore.getInstance().getTransform((String) property.value);
            originalValue = originalValue == null ? null : NumberConversions.toDouble(originalValue);
            double defaultValue = property.defaultValue == null ? 0 : property.defaultValue;
            if (transform.isValid()) {
                if (originalValue == null) {
                    originalValue = defaultValue;
                } else if (property.max != null && (Double) originalValue >= property.max) {
                    continue;
                } else if (property.min != null && (Double) originalValue <= property.min) {
                    continue;
                }
                transform.setVariable("x", (Double) originalValue);
                double transformedValue = transform.get();
                if (!Double.isNaN(transformedValue)) {
                    if (property.max != null) {
                        transformedValue = Math.min(transformedValue, property.max);
                    }
                    if (property.min != null) {
                        transformedValue = Math.max(transformedValue, property.min);
                    }
                    newValue = transformedValue;
                }
            }
        }
        changed.set(property.path, newValue);
        original.set(property.path, originalValue);
    }
    if (changed.getKeys(false).isEmpty())
        return SpellResult.NO_TARGET;
    if (upgrade)
        properties.upgrade(changed);
    else
        properties.configure(changed);
    context.registerForUndo(new ModifyPropertyUndoAction(original, properties));
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) EquationTransform(de.slikey.effectlib.math.EquationTransform) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 5 with EquationTransform

use of de.slikey.effectlib.math.EquationTransform in project MagicPlugin by elBukkit.

the class MagicConfigurableExecutor method onConfigure.

public boolean onConfigure(String command, MagicConfigurable target, CommandSender sender, Player player, String[] parameters, boolean safe) {
    if (parameters.length < 1 || (safe && parameters.length < 2)) {
        sender.sendMessage("Use: /" + command + " configure <property> [value]");
        sender.sendMessage("Properties: " + StringUtils.join(BaseMagicConfigurable.PROPERTY_KEYS, ", "));
        return true;
    }
    Mage mage = controller.getMage(player);
    String value = "";
    for (int i = 1; i < parameters.length; i++) {
        if (i != 1)
            value = value + " ";
        value = value + parameters[i];
    }
    if (value.isEmpty()) {
        value = null;
    } else if (value.equals("\"\"")) {
        value = "";
    }
    if (value != null) {
        value = value.replace("\\n", "\n");
    }
    boolean modified = false;
    if (value == null) {
        if (target.removeProperty(parameters[0])) {
            modified = true;
            mage.sendMessage(api.getMessages().get(command + ".removed_property").replace("$name", parameters[0]));
        } else {
            mage.sendMessage(api.getMessages().get(command + ".no_property").replace("$name", parameters[0]));
        }
    } else {
        ConfigurationSection node = new MemoryConfiguration();
        double transformed = Double.NaN;
        try {
            transformed = Double.parseDouble(value);
        } catch (Exception ex) {
            EquationTransform transform = EquationStore.getInstance().getTransform(value);
            if (transform.getException() == null) {
                double property = target.getProperty(parameters[0], Double.NaN);
                if (!Double.isNaN(property)) {
                    transform.setVariable("x", property);
                    transformed = transform.get();
                }
            }
        }
        if (!Double.isNaN(transformed)) {
            node.set(parameters[0], transformed);
        } else {
            node.set(parameters[0], value);
        }
        if (safe) {
            modified = target.upgrade(node);
        } else {
            target.configure(node);
            modified = true;
        }
        if (modified) {
            mage.sendMessage(api.getMessages().get(command + ".reconfigured"));
        } else {
            mage.sendMessage(api.getMessages().get(command + ".not_reconfigured"));
        }
    }
    if (sender != player) {
        if (modified) {
            sender.sendMessage(api.getMessages().getParameterized(command + ".player_reconfigured", "$name", player.getName()));
        } else {
            sender.sendMessage(api.getMessages().getParameterized(command + ".player_not_reconfigured", "$name", player.getName()));
        }
    }
    return true;
}
Also used : EquationTransform(de.slikey.effectlib.math.EquationTransform) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Aggregations

EquationTransform (de.slikey.effectlib.math.EquationTransform)7 Mage (com.elmakers.mine.bukkit.api.magic.Mage)3 Location (org.bukkit.Location)3 MemoryConfiguration (org.bukkit.configuration.MemoryConfiguration)3 CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)2 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)2 CastContext (com.elmakers.mine.bukkit.action.CastContext)1 Batch (com.elmakers.mine.bukkit.api.batch.Batch)1 SpellBatch (com.elmakers.mine.bukkit.api.batch.SpellBatch)1 PreCastEvent (com.elmakers.mine.bukkit.api.event.PreCastEvent)1 MageClass (com.elmakers.mine.bukkit.api.magic.MageClass)1 CastingCost (com.elmakers.mine.bukkit.api.spell.CastingCost)1 MageSpell (com.elmakers.mine.bukkit.api.spell.MageSpell)1 PrerequisiteSpell (com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell)1 Spell (com.elmakers.mine.bukkit.api.spell.Spell)1 SpellParameters (com.elmakers.mine.bukkit.magic.SpellParameters)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1