Search in sources :

Example 16 with Spell

use of com.elmakers.mine.bukkit.api.spell.Spell in project MagicPlugin by elBukkit.

the class AllEntitiesAction method addEntities.

@Override
public void addEntities(CastContext context, List<WeakReference<Entity>> entities) {
    Spell spell = context.getSpell();
    Entity sourceEntity = context.getMage().getEntity();
    Location sourceLocation = context.getLocation();
    if (sourceLocation == null && !targetAllWorlds) {
        return;
    }
    Class<?> targetType = Player.class;
    if (spell instanceof TargetingSpell) {
        targetType = ((TargetingSpell) spell).getTargetEntityType();
    }
    if (targetType == Player.class) {
        Collection<? extends Player> players = context.getPlugin().getServer().getOnlinePlayers();
        for (Player player : players) {
            if ((context.getTargetsCaster() || player != sourceEntity) && (targetAllWorlds || (sourceLocation != null && sourceLocation.getWorld().equals(player.getWorld()))) && spell.canTarget(player)) {
                entities.add(new WeakReference<Entity>(player));
            }
        }
    } else {
        List<World> worlds;
        if (targetAllWorlds) {
            worlds = Bukkit.getWorlds();
        } else {
            worlds = new ArrayList<>();
            worlds.add(sourceLocation.getWorld());
        }
        for (World world : worlds) {
            List<Entity> candidates = world.getEntities();
            for (Entity entity : candidates) {
                if (spell.canTarget(entity) && (context.getTargetsCaster() || entity != sourceEntity)) {
                    entities.add(new WeakReference<>(entity));
                }
            }
        }
    }
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) World(org.bukkit.World) TargetingSpell(com.elmakers.mine.bukkit.spell.TargetingSpell) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) Spell(com.elmakers.mine.bukkit.api.spell.Spell) TargetingSpell(com.elmakers.mine.bukkit.spell.TargetingSpell) Location(org.bukkit.Location)

Example 17 with Spell

use of com.elmakers.mine.bukkit.api.spell.Spell in project MagicPlugin by elBukkit.

the class MagicController method cast.

public boolean cast(Mage mage, String spellName, ConfigurationSection parameters, CommandSender sender, Entity entity) {
    Player usePermissions = (sender == entity && entity instanceof Player) ? (Player) entity : (sender instanceof Player ? (Player) sender : null);
    if (entity == null && sender instanceof Player) {
        entity = (Player) sender;
    }
    Location targetLocation = null;
    if (mage == null) {
        CommandSender mageController = (entity != null && entity instanceof Player) ? (Player) entity : sender;
        if (sender != null && sender instanceof BlockCommandSender) {
            targetLocation = ((BlockCommandSender) sender).getBlock().getLocation();
        }
        if (entity == null) {
            mage = getMage(mageController);
        } else {
            mage = getMageFromEntity(entity, mageController);
        }
    }
    // This is a bit of a hack to make automata maintain direction
    if (targetLocation != null) {
        Location mageLocation = mage.getLocation();
        if (mageLocation != null) {
            targetLocation.setPitch(mageLocation.getPitch());
            targetLocation.setYaw(mageLocation.getYaw());
        }
    }
    SpellTemplate template = getSpellTemplate(spellName);
    if (template == null || !template.hasCastPermission(usePermissions)) {
        if (sender != null) {
            sender.sendMessage("Spell " + spellName + " unknown");
        }
        return false;
    }
    com.elmakers.mine.bukkit.api.spell.Spell spell = mage.getSpell(spellName);
    if (spell == null) {
        if (sender != null) {
            sender.sendMessage("Spell " + spellName + " unknown");
        }
        return false;
    }
    // TODO: Load configured list of parameters!
    // Make it free and skip cooldowns, if configured to do so.
    toggleCastCommandOverrides(mage, sender, true);
    boolean success = false;
    try {
        success = spell.cast(parameters, targetLocation);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    toggleCastCommandOverrides(mage, sender, false);
    return success;
}
Also used : EffectPlayer(com.elmakers.mine.bukkit.effect.EffectPlayer) Player(org.bukkit.entity.Player) CommandSender(org.bukkit.command.CommandSender) BlockCommandSender(org.bukkit.command.BlockCommandSender) Spell(com.elmakers.mine.bukkit.api.spell.Spell) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException) IOException(java.io.IOException) ParseException(java.text.ParseException) Location(org.bukkit.Location) CastSourceLocation(com.elmakers.mine.bukkit.api.magic.CastSourceLocation) BlockCommandSender(org.bukkit.command.BlockCommandSender) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 18 with Spell

use of com.elmakers.mine.bukkit.api.spell.Spell in project MagicPlugin by elBukkit.

the class MagicController method activateMetrics.

protected void activateMetrics() {
    // Activate Metrics
    final MagicController controller = this;
    metrics = null;
    if (metricsLevel > 0) {
        try {
            metrics = new Metrics(plugin);
            if (metricsLevel > 1) {
                metrics.addCustomChart(new Metrics.MultiLineChart("Plugin Integration") {

                    @Override
                    public HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap) {
                        valueMap.put("Essentials", controller.hasEssentials ? 1 : 0);
                        valueMap.put("Dynmap", controller.hasDynmap ? 1 : 0);
                        valueMap.put("Factions", controller.factionsManager.isEnabled() ? 1 : 0);
                        valueMap.put("WorldGuard", controller.worldGuardManager.isEnabled() ? 1 : 0);
                        valueMap.put("Elementals", controller.elementalsEnabled() ? 1 : 0);
                        valueMap.put("Citizens", controller.citizens != null ? 1 : 0);
                        valueMap.put("CommandBook", controller.hasCommandBook ? 1 : 0);
                        valueMap.put("PvpManager", controller.pvpManager.isEnabled() ? 1 : 0);
                        valueMap.put("Multiverse-Core", controller.multiverseManager.isEnabled() ? 1 : 0);
                        valueMap.put("Towny", controller.townyManager.isEnabled() ? 1 : 0);
                        valueMap.put("GriefPrevention", controller.griefPreventionManager.isEnabled() ? 1 : 0);
                        valueMap.put("PreciousStones", controller.preciousStonesManager.isEnabled() ? 1 : 0);
                        valueMap.put("Lockette", controller.locketteManager.isEnabled() ? 1 : 0);
                        valueMap.put("NoCheatPlus", controller.ncpManager.isEnabled() ? 1 : 0);
                        return valueMap;
                    }
                });
                metrics.addCustomChart(new Metrics.MultiLineChart("Features Enabled") {

                    @Override
                    public HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap) {
                        valueMap.put("Crafting", controller.crafting.isEnabled() ? 1 : 0);
                        valueMap.put("Enchanting", controller.enchanting.isEnabled() ? 1 : 0);
                        valueMap.put("SP", controller.isSPEnabled() ? 1 : 0);
                        return valueMap;
                    }
                });
            }
            if (metricsLevel > 2) {
                metrics.addCustomChart(new Metrics.MultiLineChart("Total Casts by Category") {

                    @Override
                    public HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap) {
                        for (final SpellCategory category : categories.values()) {
                            valueMap.put(category.getName(), (int) category.getCastCount());
                        }
                        return valueMap;
                    }
                });
            }
            if (metricsLevel > 3) {
                metrics.addCustomChart(new Metrics.MultiLineChart("Total Casts") {

                    @Override
                    public HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap) {
                        for (final SpellTemplate spell : spells.values()) {
                            if (!(spell instanceof Spell))
                                continue;
                            valueMap.put(spell.getName(), (int) ((Spell) spell).getCastCount());
                        }
                        return valueMap;
                    }
                });
            }
            plugin.getLogger().info("Activated BStats");
        } catch (Exception ex) {
            plugin.getLogger().warning("Failed to load BStats: " + ex.getMessage());
        }
    }
}
Also used : Metrics(org.bstats.Metrics) SpellCategory(com.elmakers.mine.bukkit.spell.SpellCategory) HashMap(java.util.HashMap) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) Spell(com.elmakers.mine.bukkit.api.spell.Spell) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) InvalidConfigurationException(org.bukkit.configuration.InvalidConfigurationException) IOException(java.io.IOException) ParseException(java.text.ParseException) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 19 with Spell

use of com.elmakers.mine.bukkit.api.spell.Spell in project MagicPlugin by elBukkit.

the class Mage method updateHotbarStatus.

public void updateHotbarStatus() {
    Player player = getPlayer();
    if (player != null) {
        Location location = getLocation();
        for (int i = 0; i < Wand.HOTBAR_SIZE; i++) {
            ItemStack spellItem = player.getInventory().getItem(i);
            String spellKey = Wand.getSpell(spellItem);
            String classKey = Wand.getSpellClass(spellItem);
            if (spellKey != null) {
                Spell spell = getSpell(spellKey);
                if (spell != null) {
                    int targetAmount = 1;
                    long remainingCooldown = 0;
                    CastingCost requiredCost = null;
                    boolean canCastSpell = false;
                    if (classKey != null && !classKey.isEmpty()) {
                        MageClass mageClass = getClass(classKey);
                        if (mageClass != null && spell instanceof BaseSpell) {
                            BaseSpell baseSpell = (BaseSpell) spell;
                            baseSpell.setMageClass(mageClass);
                            remainingCooldown = spell.getRemainingCooldown();
                            requiredCost = spell.getRequiredCost();
                            canCastSpell = spell.canCast(location);
                            baseSpell.setMageClass(null);
                        }
                    } else {
                        remainingCooldown = spell.getRemainingCooldown();
                        requiredCost = spell.getRequiredCost();
                        canCastSpell = spell.canCast(location);
                    }
                    boolean canCast = canCastSpell;
                    if (canCastSpell && remainingCooldown == 0 && requiredCost == null) {
                        targetAmount = 1;
                    } else if (canCastSpell) {
                        canCast = remainingCooldown == 0;
                        targetAmount = Wand.LiveHotbarCooldown ? (int) Math.min(Math.ceil((double) remainingCooldown / 1000), 99) : 99;
                        if (Wand.LiveHotbarCooldown && requiredCost != null) {
                            int mana = requiredCost.getMana();
                            if (mana > 0) {
                                if (mana <= getEffectiveManaMax() && getEffectiveManaRegeneration() > 0) {
                                    float remainingMana = mana - getMana();
                                    canCast = canCast && remainingMana <= 0;
                                    int targetManaTime = (int) Math.min(Math.ceil(remainingMana / getEffectiveManaRegeneration()), 99);
                                    targetAmount = Math.max(targetManaTime, targetAmount);
                                } else {
                                    canCastSpell = false;
                                    canCast = false;
                                }
                            }
                        }
                    }
                    if (targetAmount == 0)
                        targetAmount = 1;
                    boolean setAmount = false;
                    MaterialAndData disabledIcon = spell.getDisabledIcon();
                    MaterialAndData spellIcon = spell.getIcon();
                    String urlIcon = spell.getIconURL();
                    String disabledUrlIcon = spell.getDisabledIconURL();
                    boolean usingURLIcon = (controller.isUrlIconsEnabled() || spellIcon == null || spellIcon.getMaterial() == Material.AIR) && urlIcon != null && !urlIcon.isEmpty();
                    if (disabledIcon != null && spellIcon != null && !usingURLIcon) {
                        if (!canCast) {
                            if (disabledIcon.isValid() && (disabledIcon.getMaterial() != spellItem.getType() || disabledIcon.getData() != spellItem.getDurability())) {
                                disabledIcon.applyToItem(spellItem);
                            }
                            if (!canCastSpell) {
                                if (spellItem.getAmount() != 1) {
                                    spellItem.setAmount(1);
                                }
                                setAmount = true;
                            }
                        } else {
                            if (spellIcon.isValid() && (spellIcon.getMaterial() != spellItem.getType() || spellIcon.getData() != spellItem.getDurability())) {
                                spellIcon.applyToItem(spellItem);
                            }
                        }
                    } else if (usingURLIcon && disabledUrlIcon != null && !disabledUrlIcon.isEmpty() && spellItem.getType() == Material.SKULL_ITEM) {
                        String currentURL = InventoryUtils.getSkullURL(spellItem);
                        if (!canCast) {
                            if (!disabledUrlIcon.equals(currentURL)) {
                                InventoryUtils.setNewSkullURL(spellItem, disabledUrlIcon);
                                player.getInventory().setItem(i, spellItem);
                            }
                            if (!canCastSpell) {
                                if (spellItem.getAmount() != 1) {
                                    spellItem.setAmount(1);
                                }
                                setAmount = true;
                            }
                        } else {
                            if (!urlIcon.equals(currentURL)) {
                                InventoryUtils.setNewSkullURL(spellItem, urlIcon);
                                player.getInventory().setItem(i, spellItem);
                            }
                        }
                    }
                    if (!setAmount && spellItem.getAmount() != targetAmount) {
                        spellItem.setAmount(targetAmount);
                    }
                }
            }
        }
    }
}
Also used : Player(org.bukkit.entity.Player) CastingCost(com.elmakers.mine.bukkit.api.spell.CastingCost) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) ItemStack(org.bukkit.inventory.ItemStack) 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) Location(org.bukkit.Location) CastSourceLocation(com.elmakers.mine.bukkit.api.magic.CastSourceLocation)

Example 20 with Spell

use of com.elmakers.mine.bukkit.api.spell.Spell in project MagicPlugin by elBukkit.

the class Mage method createSpell.

@Nullable
protected MageSpell createSpell(String key) {
    MageSpell playerSpell = spells.get(key);
    if (playerSpell != null) {
        playerSpell.setMage(this);
        return playerSpell;
    }
    SpellTemplate spellTemplate = controller.getSpellTemplate(key);
    if (spellTemplate == null)
        return null;
    Spell newSpell = spellTemplate.createSpell();
    if (newSpell == null || !(newSpell instanceof MageSpell))
        return null;
    playerSpell = (MageSpell) newSpell;
    spells.put(newSpell.getKey(), playerSpell);
    playerSpell.setMage(this);
    return playerSpell;
}
Also used : MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) 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) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate) Nullable(javax.annotation.Nullable)

Aggregations

Spell (com.elmakers.mine.bukkit.api.spell.Spell)39 Mage (com.elmakers.mine.bukkit.api.magic.Mage)14 BaseSpell (com.elmakers.mine.bukkit.spell.BaseSpell)12 Player (org.bukkit.entity.Player)11 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)10 MageSpell (com.elmakers.mine.bukkit.api.spell.MageSpell)9 ItemStack (org.bukkit.inventory.ItemStack)9 MageController (com.elmakers.mine.bukkit.api.magic.MageController)6 Mage (com.elmakers.mine.bukkit.magic.Mage)5 ActionSpell (com.elmakers.mine.bukkit.spell.ActionSpell)5 Wand (com.elmakers.mine.bukkit.wand.Wand)5 ArrayList (java.util.ArrayList)5 Location (org.bukkit.Location)5 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)5 Wand (com.elmakers.mine.bukkit.api.wand.Wand)4 MemoryConfiguration (org.bukkit.configuration.MemoryConfiguration)4 EventHandler (org.bukkit.event.EventHandler)4 Batch (com.elmakers.mine.bukkit.api.batch.Batch)3 SpellBatch (com.elmakers.mine.bukkit.api.batch.SpellBatch)3 CasterProperties (com.elmakers.mine.bukkit.api.magic.CasterProperties)3