Search in sources :

Example 76 with Mage

use of com.elmakers.mine.bukkit.api.magic.Mage in project MagicPlugin by elBukkit.

the class VanishAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity entity = context.getTargetEntity();
    if (entity == null) {
        return SpellResult.NO_TARGET;
    }
    MageController controller = context.getController();
    Mage mage = controller.getMage(entity);
    mage.setVanished(vanish);
    if (vanish) {
        context.registerForUndo(new UndoVanish(mage));
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage)

Example 77 with Mage

use of com.elmakers.mine.bukkit.api.magic.Mage in project MagicPlugin by elBukkit.

the class SkillSelectorAction method clicked.

@Override
public void clicked(InventoryClickEvent event) {
    InventoryAction action = event.getAction();
    if (action == InventoryAction.NOTHING) {
        int direction = event.getClick() == ClickType.LEFT ? 1 : -1;
        page = page + direction;
        openInventory();
        event.setCancelled(true);
        return;
    }
    ItemStack clickedItem = event.getCurrentItem();
    if (clickedItem != null && InventoryUtils.getMetaBoolean(clickedItem, "unavailable", false)) {
        event.setCancelled(true);
        return;
    }
    Mage mage = context.getMage();
    MageController controller = mage.getController();
    Inventory inventory = mage.getInventory();
    boolean isContainerSlot = event.getSlot() == event.getRawSlot();
    boolean isDrop = action == InventoryAction.DROP_ALL_CURSOR || action == InventoryAction.DROP_ALL_SLOT || action == InventoryAction.DROP_ONE_CURSOR || action == InventoryAction.DROP_ONE_SLOT;
    if (!isContainerSlot && isDrop && controller.isSkill(clickedItem) && !InventoryUtils.getMetaBoolean(clickedItem, "undroppable", false)) {
        inventory.setItem(event.getSlot(), null);
        return;
    }
    if (inventoryLimit > 0) {
        boolean isHotbar = event.getAction() == InventoryAction.HOTBAR_SWAP || event.getAction() == InventoryAction.HOTBAR_MOVE_AND_READD;
        if (isHotbar) {
            ItemStack destinationItem = inventory.getItem(event.getHotbarButton());
            if (controller.isSkill(destinationItem))
                return;
            isHotbar = controller.isSkill(clickedItem);
        }
        if (!isContainerSlot && !isHotbar)
            return;
        int skillCount = 0;
        for (int i = 0; i < inventory.getSize(); i++) {
            ItemStack item = inventory.getItem(i);
            if (controller.isSkill(item))
                skillCount++;
        }
        if (skillCount >= inventoryLimit) {
            event.setCancelled(true);
        }
        return;
    }
    // We don't allow quick-casting here if there is an inventory limit.
    if (action == InventoryAction.PICKUP_HALF) {
        Spell spell = mage.getSpell(Wand.getSpell(clickedItem));
        if (spell != null) {
            spell.cast();
        }
        mage.getPlayer().closeInventory();
        event.setCancelled(true);
    }
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) Spell(com.elmakers.mine.bukkit.api.spell.Spell) InventoryAction(org.bukkit.event.inventory.InventoryAction)

Example 78 with Mage

use of com.elmakers.mine.bukkit.api.magic.Mage in project MagicPlugin by elBukkit.

the class WandLevel method randomizeWand.

public boolean randomizeWand(Mage mage, Wand wand, boolean additive, boolean hasUpgrade, boolean addSpells) {
    // Add random spells to the wand
    Mage activeMage = wand.getActiveMage();
    if (mage == null) {
        mage = activeMage;
    }
    wand.setActiveMage(mage);
    boolean addedSpells = false;
    Deque<WeightedPair<String>> remainingSpells = getRemainingSpells(wand);
    if (addSpells) {
        if (remainingSpells.size() > 0) {
            Integer spellCount = RandomUtils.weightedRandom(spellCountProbability);
            for (int i = 0; spellCount != null && i < spellCount; i++) {
                String spellKey = RandomUtils.weightedRandom(remainingSpells);
                boolean added = wand.addSpell(spellKey);
                mage.sendDebugMessage("Trying to add spell: " + spellKey + " ? " + added);
                if (added) {
                    addedSpells = true;
                }
            }
        }
    }
    // Look through all spells for the max mana casting cost
    // Also look for any material-using spells
    boolean needsMaterials = false;
    int maxManaCost = 0;
    Set<String> spells = wand.getSpells();
    for (String spellName : spells) {
        SpellTemplate spell = wand.getController().getSpellTemplate(spellName);
        if (spell != null) {
            needsMaterials = needsMaterials || spell.usesBrush();
            Collection<CastingCost> costs = spell.getCosts();
            if (costs != null) {
                for (CastingCost cost : costs) {
                    maxManaCost = Math.max(maxManaCost, cost.getMana());
                }
            }
        }
    }
    // Add random materials
    boolean addedMaterials = false;
    Deque<WeightedPair<String>> remainingMaterials = getRemainingMaterials(wand);
    if (needsMaterials && remainingMaterials.size() > 0) {
        int currentMaterialCount = wand.getBrushes().size();
        Integer materialCount = RandomUtils.weightedRandom(materialCountProbability);
        // Make sure the wand has at least one material.
        if (materialCount == null) {
            materialCount = 0;
        }
        if (currentMaterialCount == 0) {
            materialCount = Math.max(1, materialCount);
        }
        int retries = 100;
        for (int i = 0; i < materialCount; i++) {
            String materialKey = RandomUtils.weightedRandom(remainingMaterials);
            materialKey = materialKey.replace("|", ":");
            if (!wand.addBrush(materialKey)) {
                // Try again up to a certain number if we picked one the wand already had.
                if (retries-- > 0)
                    i--;
            } else {
                addedMaterials = true;
            }
        }
    }
    // Let them upgrade if they aren't getting any new spells or brushes
    if (hasUpgrade && addSpells && !(addedMaterials && needsMaterials) && !addedSpells && ((getSpellCount() > 0 && spellProbability.size() > 0) || (getMaterialCount() > 0 && materialProbability.size() > 0))) {
        if (mage != null && mage.getDebugLevel() > 0) {
            mage.sendDebugMessage("Has upgrade: " + hasUpgrade);
            mage.sendDebugMessage("Added spells: " + addedSpells + ", should: " + addSpells);
            mage.sendDebugMessage("Spells per enchant: " + getSpellCount());
            mage.sendDebugMessage("Spells in list: " + spellProbability.size());
            mage.sendDebugMessage("Added brushes: " + addedMaterials + ", needed: " + needsMaterials);
        }
        wand.setActiveMage(activeMage);
        return false;
    }
    // Add random wand properties
    boolean addedProperties = false;
    Integer propertyCount = propertyCountProbability.size() == 0 ? Integer.valueOf(0) : RandomUtils.weightedRandom(propertyCountProbability);
    ConfigurationSection wandProperties = new MemoryConfiguration();
    List<String> propertyKeys = new ArrayList<>(propertiesProbability.keySet());
    List<String> propertiesAvailable = new ArrayList<>();
    for (String propertyKey : propertyKeys) {
        double currentValue = wand.getDouble(propertyKey);
        double maxValue = path.getMaxProperty(propertyKey);
        if (currentValue < maxValue) {
            propertiesAvailable.add(propertyKey);
        }
    }
    // Make sure we give them *something* if something is available
    if (propertiesAvailable.size() > 0 && !addedMaterials && !addedSpells && propertyCount == 0) {
        propertyCount = 1;
    }
    while (propertyCount != null && propertyCount-- > 0 && propertiesAvailable.size() > 0) {
        int randomPropertyIndex = (int) (Math.random() * propertiesAvailable.size());
        String randomProperty = propertiesAvailable.get(randomPropertyIndex);
        Deque<WeightedPair<Float>> probabilities = propertiesProbability.get(randomProperty);
        double current = wand.getDouble(randomProperty);
        double maxValue = path.getMaxProperty(randomProperty);
        if (probabilities.size() > 0 && current < maxValue) {
            addedProperties = true;
            current = Math.min(maxValue, current + RandomUtils.weightedRandom(probabilities));
            wandProperties.set(randomProperty, current);
        }
    }
    if (wand.isCostFree()) {
        // Cost-Free wands don't need mana.
        wandProperties.set("mana_regeneration", 0);
        wandProperties.set("mana_max", 0);
        wandProperties.set("mana", 0);
    } else {
        int manaRegeneration = wand.getManaRegeneration();
        if (manaRegenerationProbability.size() > 0 && manaRegeneration < path.getMaxManaRegeneration()) {
            addedProperties = true;
            manaRegeneration = Math.min(path.getMaxManaRegeneration(), manaRegeneration + RandomUtils.weightedRandom(manaRegenerationProbability));
            wandProperties.set("mana_regeneration", manaRegeneration);
        }
        int manaMax = wand.getManaMax();
        if (manaMaxProbability.size() > 0 && manaMax < path.getMaxMaxMana()) {
            manaMax = Math.min(path.getMaxMaxMana(), manaMax + RandomUtils.weightedRandom(manaMaxProbability));
            if (path.getMatchSpellMana()) {
                // Make sure the wand has at least enough mana to cast the highest costing spell it has.
                manaMax = Math.max(maxManaCost, manaMax);
            }
            wandProperties.set("mana_max", manaMax);
            addedProperties = true;
        }
        // Refill the wand's mana, why not
        wandProperties.set("mana", manaMax);
    }
    // Add or set uses to the wand
    if (additive) {
        // Only add uses to a wand if it already has some.
        int wandUses = wand.getRemainingUses();
        if (wandUses > 0 && wandUses < path.getMaxUses() && addUseProbability.size() > 0) {
            wandProperties.set("uses", Math.min(path.getMaxUses(), wandUses + RandomUtils.weightedRandom(addUseProbability)));
            addedProperties = true;
        }
    } else if (useProbability.size() > 0) {
        wandProperties.set("uses", Math.min(path.getMaxUses(), RandomUtils.weightedRandom(useProbability)));
    }
    // Set properties.
    wand.upgrade(wandProperties);
    wand.setActiveMage(activeMage);
    return addedMaterials || addedSpells || addedProperties;
}
Also used : ArrayList(java.util.ArrayList) MemoryConfiguration(org.bukkit.configuration.MemoryConfiguration) CastingCost(com.elmakers.mine.bukkit.api.spell.CastingCost) Mage(com.elmakers.mine.bukkit.api.magic.Mage) WeightedPair(com.elmakers.mine.bukkit.utility.WeightedPair) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 79 with Mage

use of com.elmakers.mine.bukkit.api.magic.Mage in project MagicPlugin by elBukkit.

the class PhaseSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    Location targetLocation = null;
    Target target = getTarget();
    Entity e = target.getEntity();
    LivingEntity entity = e != null && e instanceof LivingEntity ? (LivingEntity) e : null;
    if (entity == null) {
        return SpellResult.NO_TARGET;
    }
    if (entity != mage.getEntity() && controller.isMage(entity)) {
        Mage mage = controller.getMage(entity);
        if (mage.isSuperProtected()) {
            return SpellResult.NO_TARGET;
        }
    }
    Location playerLocation = entity.getLocation();
    String worldName = playerLocation.getWorld().getName();
    if (parameters.contains("target_world")) {
        World targetWorld = getWorld(parameters.getString("target_world"), parameters.getBoolean("load", true));
        if (targetWorld == null) {
            return SpellResult.INVALID_WORLD;
        }
        float scale = (float) parameters.getDouble("scale", 1.0f);
        if (targetWorld.getEnvironment() == World.Environment.THE_END) {
            targetLocation = targetWorld.getSpawnLocation();
        } else {
            targetLocation = new Location(targetWorld, playerLocation.getX() * scale, playerLocation.getY(), playerLocation.getZ() * scale);
        }
    } else if (parameters.contains("worlds")) {
        ConfigurationSection worldMap = parameters.getConfigurationSection("worlds");
        if (!worldMap.contains(worldName)) {
            return SpellResult.NO_TARGET;
        }
        ConfigurationSection worldNode = worldMap.getConfigurationSection(worldName);
        World targetWorld = getWorld(worldNode.getString("target"), worldNode.getBoolean("load", true));
        float scale = (float) worldNode.getDouble("scale", 1.0f);
        if (targetWorld != null) {
            targetLocation = new Location(targetWorld, playerLocation.getX() * scale, playerLocation.getY(), playerLocation.getZ() * scale);
        }
    } else {
        if (worldName.contains("_the_end")) {
            worldName = worldName.replace("_the_end", "");
            World targetWorld = Bukkit.getWorld(worldName);
            if (targetWorld != null) {
                // No scaling here?
                // Just send them to spawn... this is kind of to fix players finding the real spawn
                // on my own server, but I'm not just sure how best to handle this anyway.
                targetLocation = targetWorld.getSpawnLocation();
            }
        } else if (worldName.contains("_nether")) {
            worldName = worldName.replace("_nether", "");
            World targetWorld = Bukkit.getWorld(worldName);
            if (targetWorld != null) {
                targetLocation = new Location(targetWorld, playerLocation.getX() * 8, playerLocation.getY(), playerLocation.getZ() * 8);
            }
        } else {
            worldName = worldName + "_nether";
            World targetWorld = Bukkit.getWorld(worldName);
            if (targetWorld != null) {
                targetLocation = new Location(targetWorld, playerLocation.getX() / 8, Math.min(125, playerLocation.getY()), playerLocation.getZ() / 8);
            }
        }
    }
    if (targetLocation == null) {
        return SpellResult.NO_TARGET;
    }
    retryCount = 0;
    tryPhase(entity, targetLocation);
    return SpellResult.CAST;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Target(com.elmakers.mine.bukkit.utility.Target) Mage(com.elmakers.mine.bukkit.api.magic.Mage) World(org.bukkit.World) Location(org.bukkit.Location) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 80 with Mage

use of com.elmakers.mine.bukkit.api.magic.Mage in project MagicPlugin by elBukkit.

the class PotionEffectSpell method onCast.

@Override
public SpellResult onCast(ConfigurationSection parameters) {
    Target target = getTarget();
    if (!target.hasTarget()) {
        return SpellResult.NO_TARGET;
    }
    List<LivingEntity> targetEntities = new ArrayList<>();
    Entity targetedEntity = target.getEntity();
    if (target.hasEntity() && targetedEntity instanceof LivingEntity) {
        targetEntities.add((LivingEntity) targetedEntity);
    }
    int radius = parameters.getInt("radius", 0);
    radius = (int) (mage.getRadiusMultiplier() * radius);
    if (radius > 0) {
        List<Entity> entities = CompatibilityUtils.getNearbyEntities(target.getLocation(), radius, radius, radius);
        for (Entity entity : entities) {
            if (entity instanceof LivingEntity && entity != targetedEntity && entity != mage.getEntity()) {
                targetEntities.add((LivingEntity) entity);
            }
        }
    }
    if (targetEntities.size() == 0) {
        return SpellResult.NO_TARGET;
    }
    int fallProtection = parameters.getInt("fall_protection", 0);
    Integer duration = null;
    if (parameters.contains("duration")) {
        duration = parameters.getInt("duration");
    }
    Collection<PotionEffect> effects = getPotionEffects(parameters, duration);
    for (LivingEntity targetEntity : targetEntities) {
        Mage targetMage = controller.isMage(targetEntity) ? controller.getMage(targetEntity) : null;
        if (targetMage != null && fallProtection > 0) {
            targetMage.enableFallProtection(fallProtection);
        }
        if (targetEntity != mage.getEntity()) {
            // Check for superprotected mages
            if (targetMage != null) {
                // Check for protected players
                if (isSuperProtected(targetMage)) {
                    continue;
                }
                if (parameters.getBoolean("deactivate_target_mage")) {
                    targetMage.deactivateAllSpells(true, false);
                }
            }
        }
        if (targetEntity instanceof Player && parameters.getBoolean("feed", false)) {
            Player p = (Player) targetEntity;
            p.setExhaustion(0);
            p.setFoodLevel(20);
        }
        if (parameters.getBoolean("cure", false)) {
            Collection<PotionEffect> currentEffects = targetEntity.getActivePotionEffects();
            for (PotionEffect effect : currentEffects) {
                if (negativeEffects.contains(effect.getType())) {
                    targetEntity.removePotionEffect(effect.getType());
                }
            }
        }
        if (parameters.contains("heal")) {
            registerModified(targetEntity);
            double health = targetEntity.getHealth() + parameters.getDouble("heal");
            targetEntity.setHealth(Math.min(health, targetEntity.getMaxHealth()));
        } else if (parameters.contains("heal_percentage")) {
            registerModified(targetEntity);
            double health = targetEntity.getHealth() + targetEntity.getMaxHealth() * parameters.getDouble("heal_percentage");
            targetEntity.setHealth(Math.min(health, targetEntity.getMaxHealth()));
        } else if (parameters.contains("damage")) {
            registerModified(targetEntity);
            CompatibilityUtils.magicDamage(targetEntity, parameters.getDouble("damage") * mage.getDamageMultiplier(), mage.getEntity());
        } else {
            registerPotionEffects(targetEntity);
        }
        if (parameters.contains("fire")) {
            registerModified(targetEntity);
            targetEntity.setFireTicks(parameters.getInt("fire"));
        }
        CompatibilityUtils.applyPotionEffects(targetEntity, effects);
        if (parameters.contains("remove_effects")) {
            List<String> removeKeys = parameters.getStringList("remove_effects");
            for (String removeKey : removeKeys) {
                PotionEffectType removeType = PotionEffectType.getByName(removeKey);
                targetEntity.removePotionEffect(removeType);
            }
        }
    }
    registerForUndo();
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) Player(org.bukkit.entity.Player) PotionEffect(org.bukkit.potion.PotionEffect) PotionEffectType(org.bukkit.potion.PotionEffectType) ArrayList(java.util.ArrayList) LivingEntity(org.bukkit.entity.LivingEntity) Target(com.elmakers.mine.bukkit.utility.Target) Mage(com.elmakers.mine.bukkit.api.magic.Mage)

Aggregations

Mage (com.elmakers.mine.bukkit.api.magic.Mage)187 Player (org.bukkit.entity.Player)62 Entity (org.bukkit.entity.Entity)56 Wand (com.elmakers.mine.bukkit.api.wand.Wand)47 MageController (com.elmakers.mine.bukkit.api.magic.MageController)45 ItemStack (org.bukkit.inventory.ItemStack)38 Location (org.bukkit.Location)33 LivingEntity (org.bukkit.entity.LivingEntity)31 ArrayList (java.util.ArrayList)25 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)18 Inventory (org.bukkit.inventory.Inventory)16 MageClass (com.elmakers.mine.bukkit.api.magic.MageClass)15 Spell (com.elmakers.mine.bukkit.api.spell.Spell)14 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)14 Block (org.bukkit.block.Block)14 Target (com.elmakers.mine.bukkit.utility.Target)13 EventHandler (org.bukkit.event.EventHandler)13 ItemMeta (org.bukkit.inventory.meta.ItemMeta)12 CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)10 Vector (org.bukkit.util.Vector)10