Search in sources :

Example 6 with Spell

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

the class CastCommandExecutor method onCommand.

@Override
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
    String commandName = command.getName();
    if (commandName.equalsIgnoreCase("castp")) {
        if (!api.hasPermission(sender, "Magic.commands.castp")) {
            sendNoPermission(sender);
            return true;
        }
        if (args.length < 1) {
            if (sender != null)
                sender.sendMessage("Usage: /castp [player] [spell] <parameters>");
            return true;
        }
        String playerName = args[0];
        // Look for Entity-based Mages
        Mage mage = null;
        if (playerName.contains(",")) {
            String[] idPieces = StringUtils.split(playerName, ',');
            if (idPieces.length == 4 || idPieces.length == 2) {
                try {
                    String worldName = idPieces[0];
                    String entityId = idPieces[idPieces.length - 1];
                    World world = Bukkit.getWorld(worldName);
                    if (world == null) {
                        if (sender != null)
                            sender.sendMessage("Unknown world: " + worldName);
                        return false;
                    }
                    Entity entity = NMSUtils.getEntity(world, UUID.fromString(entityId));
                    if (entity == null) {
                        if (sender != null)
                            sender.sendMessage("Entity not found with id " + entityId + " in " + world.getName());
                        return false;
                    }
                    MageController controller = api.getController();
                    mage = controller.getMage(entity);
                    // If we have the mage, we no longer want to send anything to the console.
                    sender = null;
                } catch (Throwable ex) {
                    if (sender != null)
                        sender.sendMessage("Your spell failed (badly... check server logs)");
                    ex.printStackTrace();
                    return false;
                }
            }
        } else if (playerName.contains(":")) {
            // Look for custom id/name Mages
            String[] pieces = StringUtils.split(playerName, ':');
            String mageId = pieces[0];
            String mageName = (pieces.length > 0) ? pieces[1] : mageId;
            MageController controller = api.getController();
            mage = controller.getMage(mageId, mageName);
        }
        if (mage != null && !mage.isLoading()) {
            String[] castParameters = Arrays.copyOfRange(args, 1, args.length);
            if (castParameters.length < 1) {
                if (sender != null)
                    sender.sendMessage("Invalid command line, expecting more parameters");
                return false;
            }
            String spellName = castParameters[0];
            Spell spell = mage.getSpell(spellName);
            if (spell == null) {
                if (sender != null)
                    sender.sendMessage("Unknown spell " + spellName);
                return false;
            }
            String[] parameters = new String[castParameters.length - 1];
            for (int i = 1; i < castParameters.length; i++) {
                parameters[i - 1] = castParameters[i];
            }
            if (spell.cast(parameters)) {
                if (sender != null)
                    sender.sendMessage("Cast " + spell.getName() + " as " + mage.getName());
            } else {
                if (sender != null)
                    sender.sendMessage("Failed to cast " + spell.getName() + " as " + mage.getName());
            }
            return true;
        }
        Player player = DeprecatedUtils.getPlayer(playerName);
        if (player == null) {
            if (sender != null)
                sender.sendMessage("Can't find player " + playerName);
            return true;
        }
        if (!player.isOnline()) {
            if (sender != null)
                sender.sendMessage("Player " + playerName + " is not online");
            return true;
        }
        String[] args2 = Arrays.copyOfRange(args, 1, args.length);
        return processCastCommand(sender, player, args2);
    }
    if (commandName.equalsIgnoreCase("cast")) {
        if (!api.hasPermission(sender, "Magic.commands.cast")) {
            sendNoPermission(sender);
            return true;
        }
        Player player = null;
        if (sender instanceof Player) {
            player = (Player) sender;
        }
        return processCastCommand(sender, player, args);
    }
    return false;
}
Also used : Entity(org.bukkit.entity.Entity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Player(org.bukkit.entity.Player) Mage(com.elmakers.mine.bukkit.api.magic.Mage) World(org.bukkit.World) Spell(com.elmakers.mine.bukkit.api.spell.Spell)

Example 7 with Spell

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

the class MageCommandExecutor method onMageSetData.

public boolean onMageSetData(CommandSender sender, Player player, String[] args) {
    Mage mage = controller.getMage(player);
    if (args.length == 1) {
        ConfigurationSection data = mage.getData();
        String key = args[0];
        if (!data.contains(key)) {
            sender.sendMessage(ChatColor.RED + "No data found with key " + ChatColor.AQUA + key + ChatColor.RED + " for " + ChatColor.DARK_AQUA + player.getDisplayName());
            return true;
        }
        data.set(key, null);
        sender.sendMessage(ChatColor.GOLD + "Removed data for key " + ChatColor.AQUA + key + ChatColor.GOLD + " for " + ChatColor.DARK_AQUA + player.getDisplayName());
        return true;
    }
    if (args.length != 2) {
        return false;
    }
    if (args[0].equals("*")) {
        long value = 0;
        try {
            value = Long.parseLong(args[1]);
        } catch (Exception ex) {
            sender.sendMessage(ChatColor.RED + "Cast count must be a number");
            return true;
        }
        Collection<Spell> spells = mage.getSpells();
        for (Spell spell : spells) {
            spell.setCastCount(value);
        }
        sender.sendMessage(ChatColor.GOLD + "Set all spell cast counts to " + ChatColor.AQUA + value + ChatColor.GOLD + " for " + ChatColor.DARK_AQUA + player.getDisplayName());
        return true;
    }
    Spell spell = mage.getSpell(args[0]);
    if (spell != null) {
        long value = 0;
        try {
            value = Long.parseLong(args[1]);
        } catch (Exception ex) {
            sender.sendMessage(ChatColor.RED + "Cast count must be a number");
            return true;
        }
        spell.setCastCount(value);
        sender.sendMessage(ChatColor.GOLD + "Set " + ChatColor.AQUA + spell.getName() + ChatColor.GOLD + " cast count to " + ChatColor.AQUA + value + ChatColor.GOLD + " for " + ChatColor.DARK_AQUA + player.getDisplayName());
        return true;
    }
    ConfigurationSection data = mage.getData();
    String key = args[0];
    String value = args[1];
    data.set(key, value);
    sender.sendMessage(ChatColor.GOLD + "Set " + ChatColor.AQUA + key + ChatColor.GOLD + " to " + ChatColor.AQUA + value + ChatColor.GOLD + " for " + ChatColor.DARK_AQUA + player.getDisplayName());
    return true;
}
Also used : Mage(com.elmakers.mine.bukkit.api.magic.Mage) Spell(com.elmakers.mine.bukkit.api.spell.Spell) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 8 with Spell

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

the class MageCommandExecutor method onMageAdd.

public boolean onMageAdd(CommandSender sender, Player player, String[] parameters) {
    if (parameters.length < 1) {
        sender.sendMessage("Use: /mage add <spell|material> [material:data]");
        return true;
    }
    Mage mage = controller.getMage(player);
    MageClass activeClass = mage.getActiveClass();
    if (activeClass == null) {
        sender.sendMessage("Can't modify player " + player.getName());
        return true;
    }
    String spellName = parameters[0];
    if (spellName.equals("material") || spellName.equals("brush")) {
        if (parameters.length < 2) {
            sender.sendMessage("Use: /mage add brush <material:data>");
            return true;
        }
        String materialKey = parameters[1];
        if (!MaterialBrush.isValidMaterial(materialKey, false)) {
            sender.sendMessage(materialKey + " is not a valid brush");
            return true;
        }
        if (activeClass.addBrush(materialKey)) {
            if (sender != player) {
                sender.sendMessage("Added brush '" + materialKey + "' to " + player.getName());
            } else {
                sender.sendMessage(api.getMessages().get("mage.brush_added").replace("$name", materialKey));
            }
        }
        return true;
    }
    Spell spell = mage.getSpell(spellName);
    if (spell == null) {
        sender.sendMessage("Spell '" + spellName + "' unknown, Use /spells for spell list");
        return true;
    }
    SpellTemplate currentSpell = activeClass.getSpellTemplate(spellName);
    if (activeClass.addSpell(spellName)) {
        if (currentSpell != null) {
            String levelDescription = spell.getLevelDescription();
            if (levelDescription == null || levelDescription.isEmpty()) {
                levelDescription = spell.getName();
            }
            if (sender != player) {
                sender.sendMessage(api.getMessages().get("mage.player_spell_upgraded").replace("$player", player.getName()).replace("$name", currentSpell.getName()).replace("$level", levelDescription));
            }
        } else {
            if (sender != player) {
                sender.sendMessage("Added '" + spell.getName() + "' to " + player.getName());
            }
        }
    } else if (sender != player) {
        sender.sendMessage("Could not add " + spellName + " to " + player.getName());
    }
    return true;
}
Also used : MageClass(com.elmakers.mine.bukkit.api.magic.MageClass) Mage(com.elmakers.mine.bukkit.api.magic.Mage) Spell(com.elmakers.mine.bukkit.api.spell.Spell) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate)

Example 9 with Spell

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

the class MageCommandExecutor method onMageGetData.

public boolean onMageGetData(CommandSender sender, Player player, String[] args) {
    Mage mage = controller.getMage(player);
    ConfigurationSection data = mage.getData();
    if (args != null && args.length > 0) {
        if (args[0].equals("*")) {
            sender.sendMessage(ChatColor.GOLD + "Mage data for " + ChatColor.AQUA + player.getDisplayName() + ChatColor.GOLD + ": ");
            Collection<Spell> spells = mage.getSpells();
            if (spells.size() == 0) {
                sender.sendMessage(ChatColor.RED + "No spell casts!");
                return true;
            }
            for (Spell spell : spells) {
                sender.sendMessage(ChatColor.LIGHT_PURPLE + spell.getName() + ChatColor.AQUA + " Cast Count: " + ChatColor.GOLD + spell.getCastCount());
            }
            return true;
        }
        Spell spell = mage.getSpell(args[0]);
        if (spell != null) {
            sender.sendMessage(ChatColor.GOLD + "Mage data for " + ChatColor.AQUA + player.getDisplayName() + ChatColor.GOLD + ": " + ChatColor.LIGHT_PURPLE + spell.getName());
            sender.sendMessage(ChatColor.AQUA + " Cast Count: " + ChatColor.GOLD + spell.getCastCount());
            return true;
        }
        ConfigurationSection subSection = data.getConfigurationSection(args[0]);
        if (subSection == null) {
            sender.sendMessage(ChatColor.RED + "Unknown subsection or spell: " + args[0]);
            return true;
        }
        data = subSection;
    }
    Collection<String> keys = data.getKeys(false);
    sender.sendMessage(ChatColor.GOLD + "Mage data for " + ChatColor.AQUA + player.getDisplayName());
    for (String key : keys) {
        if (data.isConfigurationSection(key)) {
            ConfigurationSection subSection = data.getConfigurationSection(key);
            sender.sendMessage(ChatColor.AQUA + " " + key + ChatColor.DARK_AQUA + " (" + subSection.getKeys(true).size() + " items)");
        } else {
            String value = data.getString(key);
            if (value != null) {
                sender.sendMessage(ChatColor.AQUA + " " + key + ChatColor.DARK_AQUA + " (" + value + ")");
            } else {
                sender.sendMessage(ChatColor.AQUA + " " + key);
            }
        }
    }
    return true;
}
Also used : Mage(com.elmakers.mine.bukkit.api.magic.Mage) Spell(com.elmakers.mine.bukkit.api.spell.Spell) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 10 with Spell

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

the class SpellShopAction method createShopItem.

@Nullable
private ShopItem createShopItem(String key, Double worth, CastContext context) {
    CasterProperties caster = getCaster(context);
    Mage mage = context.getMage();
    MageController controller = context.getController();
    ProgressionPath currentPath = caster.getPath();
    key = context.parameterize(key);
    String spellKey = key.split(" ", 2)[0];
    if (!castsSpells && caster.hasSpell(spellKey)) {
        mage.sendDebugMessage(ChatColor.GRAY + " Skipping " + spellKey + ", already have it", 3);
        return null;
    }
    SpellTemplate spell = controller.getSpellTemplate(spellKey);
    if (spell == null) {
        mage.sendDebugMessage(ChatColor.RED + " Skipping " + spellKey + ", invalid spell", 0);
        return null;
    }
    if (worth == null) {
        worth = spell.getWorth();
    }
    if (worth <= 0 && !showFree) {
        mage.sendDebugMessage(ChatColor.YELLOW + " Skipping " + spellKey + ", is free (worth " + worth + ")", 3);
        return null;
    }
    if (!spell.hasCastPermission(mage.getCommandSender())) {
        mage.sendDebugMessage(ChatColor.YELLOW + " Skipping " + spellKey + ", no permission", 3);
        return null;
    }
    ItemStack spellItem = controller.createSpellItem(key, castsSpells);
    if (!castsSpells) {
        Spell mageSpell = caster.getSpell(spellKey);
        String requiredPathKey = mageSpell != null ? mageSpell.getRequiredUpgradePath() : null;
        Set<String> requiredPathTags = mageSpell != null ? mageSpell.getRequiredUpgradeTags() : null;
        long requiredCastCount = mageSpell != null ? mageSpell.getRequiredUpgradeCasts() : 0;
        long castCount = mageSpell != null ? Math.min(mageSpell.getCastCount(), requiredCastCount) : 0;
        Collection<PrerequisiteSpell> missingSpells = PrerequisiteSpell.getMissingRequirements(caster, spell);
        ItemMeta meta = spellItem.getItemMeta();
        List<String> itemLore = meta.getLore();
        if (itemLore == null) {
            mage.sendDebugMessage(ChatColor.RED + " Skipping " + spellKey + ", spell item is invalid", 0);
            return null;
        }
        List<String> lore = new ArrayList<>();
        if (spell.getSpellKey().getLevel() > 1 && itemLore.size() > 0) {
            lore.add(itemLore.get(0));
        }
        String upgradeDescription = spell.getUpgradeDescription();
        if (showUpgrades && upgradeDescription != null && !upgradeDescription.isEmpty()) {
            upgradeDescription = controller.getMessages().get("spell.upgrade_description_prefix") + upgradeDescription;
            InventoryUtils.wrapText(upgradeDescription, lore);
        }
        String unpurchasableMessage = null;
        Collection<Requirement> requirements = spell.getRequirements();
        String requirementMissing = controller.checkRequirements(context, requirements);
        if (requirementMissing != null) {
            unpurchasableMessage = requirementMissing;
            InventoryUtils.wrapText(unpurchasableMessage, lore);
        }
        if ((requiredPathKey != null && !currentPath.hasPath(requiredPathKey)) || (requiresCastCounts && requiredCastCount > 0 && castCount < requiredCastCount) || (requiredPathTags != null && !currentPath.hasAllTags(requiredPathTags)) || !missingSpells.isEmpty()) {
            if (mageSpell != null && !spell.getName().equals(mageSpell.getName())) {
                lore.add(context.getMessage("upgrade_name_change", getDefaultMessage(context, "upgrade_name_change")).replace("$name", mageSpell.getName()));
            }
            if (requiredPathKey != null && !currentPath.hasPath(requiredPathKey)) {
                requiredPathKey = currentPath.translatePath(requiredPathKey);
                com.elmakers.mine.bukkit.wand.WandUpgradePath upgradePath = com.elmakers.mine.bukkit.wand.WandUpgradePath.getPath(requiredPathKey);
                if (upgradePath != null) {
                    unpurchasableMessage = context.getMessage("level_requirement", getDefaultMessage(context, "level_requirement")).replace("$path", upgradePath.getName());
                    InventoryUtils.wrapText(unpurchasableMessage, lore);
                }
            } else if (requiredPathTags != null && !requiredPathTags.isEmpty() && !currentPath.hasAllTags(requiredPathTags)) {
                Set<String> tags = currentPath.getMissingTags(requiredPathTags);
                unpurchasableMessage = context.getMessage("tags_requirement", getDefaultMessage(context, "tags_requirement")).replace("$tags", controller.getMessages().formatList("tags", tags, "name"));
                InventoryUtils.wrapText(unpurchasableMessage, lore);
            }
            if (requiresCastCounts && requiredCastCount > 0 && castCount < requiredCastCount) {
                unpurchasableMessage = ChatColor.RED + context.getMessage("cast_requirement", getDefaultMessage(context, "cast_requirement")).replace("$current", Long.toString(castCount)).replace("$required", Long.toString(requiredCastCount));
                InventoryUtils.wrapText(unpurchasableMessage, lore);
            }
            if (!missingSpells.isEmpty()) {
                List<String> spells = new ArrayList<>(missingSpells.size());
                for (PrerequisiteSpell s : missingSpells) {
                    SpellTemplate template = context.getController().getSpellTemplate(s.getSpellKey().getKey());
                    String spellMessage = context.getMessage("prerequisite_spell_level", getDefaultMessage(context, "prerequisite_spell_level")).replace("$name", template.getName());
                    if (s.getProgressLevel() > 1) {
                        spellMessage += context.getMessage("prerequisite_spell_progress_level", getDefaultMessage(context, "prerequisite_spell_progress_level")).replace("$level", Long.toString(s.getProgressLevel())).replace("$max_level", Long.toString(Math.max(1, template.getMaxProgressLevel())));
                    }
                    spells.add(spellMessage);
                }
                unpurchasableMessage = ChatColor.RED + context.getMessage("required_spells", getDefaultMessage(context, "required_spells")).replace("$spells", StringUtils.join(spells, ", "));
                InventoryUtils.wrapText(ChatColor.GOLD + unpurchasableMessage, lore);
            }
        }
        for (int i = (spell.getSpellKey().getLevel() > 1 ? 1 : 0); i < itemLore.size(); i++) {
            lore.add(itemLore.get(i));
        }
        meta.setLore(lore);
        spellItem.setItemMeta(meta);
        if (unpurchasableMessage != null)
            InventoryUtils.setMeta(spellItem, "unpurchasable", unpurchasableMessage);
    }
    return new ShopItem(spellItem, worth);
}
Also used : ProgressionPath(com.elmakers.mine.bukkit.api.magic.ProgressionPath) CasterProperties(com.elmakers.mine.bukkit.api.magic.CasterProperties) Set(java.util.Set) ArrayList(java.util.ArrayList) Spell(com.elmakers.mine.bukkit.api.spell.Spell) MageSpell(com.elmakers.mine.bukkit.api.spell.MageSpell) BaseSpell(com.elmakers.mine.bukkit.spell.BaseSpell) PrerequisiteSpell(com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Requirement(com.elmakers.mine.bukkit.api.requirements.Requirement) Mage(com.elmakers.mine.bukkit.api.magic.Mage) PrerequisiteSpell(com.elmakers.mine.bukkit.api.spell.PrerequisiteSpell) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta) 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