Search in sources :

Example 61 with MageController

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

the class SkillSelectorAction method perform.

@Override
public SpellResult perform(CastContext context) {
    this.context = context;
    MageController apiController = context.getController();
    Player player = context.getMage().getPlayer();
    if (player == null) {
        return SpellResult.PLAYER_REQUIRED;
    }
    if (!(apiController instanceof MagicController))
        return SpellResult.FAIL;
    MagicController controller = (MagicController) apiController;
    Messages messages = controller.getMessages();
    HeroesManager heroes = controller.getHeroes();
    allSkills.clear();
    if (controller.useHeroesSkills() && heroes != null) {
        @Nonnull String classString = heroes.getClassName(player);
        @Nonnull String class2String = heroes.getSecondaryClassName(player);
        String messageKey = !class2String.isEmpty() ? "skills.inventory_title_secondary" : "skills.inventory_title";
        inventoryTitle = controller.getMessages().get(messageKey, "Skills ($page/$pages)");
        inventoryTitle = inventoryTitle.replace("$class2", class2String).replace("$class", classString);
        List<String> heroesSkills = heroes.getSkillList(player, true, true);
        for (String heroesSkill : heroesSkills) {
            allSkills.add(new SkillDescription(heroes, controller, player, heroesSkill));
        }
    } else {
        inventoryTitle = messages.get("skills.inventory_title");
    }
    if (controller.usePermissionSkills()) {
        boolean bypassHidden = player.hasPermission("Magic.bypass_hidden");
        Collection<SpellTemplate> spells = controller.getSpellTemplates(bypassHidden);
        for (SpellTemplate spell : spells) {
            SpellKey key = spell.getSpellKey();
            if (key.isVariant())
                continue;
            if (key.getBaseKey().startsWith("heroes*"))
                continue;
            if (!spell.hasCastPermission(player))
                continue;
            allSkills.add(new SkillDescription(spell));
        }
    } else {
        Mage mage = controller.getMage(player);
        MageClass activeClass = mage.getActiveClass();
        if (activeClass != null) {
            // gather spells in player's inventory to avoid showing
            Set<String> hasSpells = new HashSet<>();
            for (ItemStack item : player.getInventory().getContents()) {
                String spellKey = controller.getSpell(item);
                if (spellKey != null) {
                    hasSpells.add(spellKey);
                }
            }
            classKey = activeClass.getKey();
            skillsConfig = activeClass.getProperty("skills", ConfigurationSection.class);
            inventoryLimit = activeClass.getProperty("skills.skill_limit", 0);
            Collection<String> spells = activeClass.getSpells();
            for (String spellKey : spells) {
                if (hasSpells.contains(spellKey)) {
                    extraSlots++;
                    continue;
                }
                SpellTemplate spell = controller.getSpellTemplate(spellKey);
                if (spell != null) {
                    allSkills.add(new SkillDescription(spell));
                }
            }
        }
    }
    if (allSkills.size() == 0) {
        player.sendMessage(messages.get("skills.none"));
        return SpellResult.NO_ACTION;
    }
    Collections.sort(allSkills);
    openInventory();
    return SpellResult.CAST;
}
Also used : Player(org.bukkit.entity.Player) Messages(com.elmakers.mine.bukkit.api.magic.Messages) MageClass(com.elmakers.mine.bukkit.api.magic.MageClass) Nonnull(javax.annotation.Nonnull) HeroesManager(com.elmakers.mine.bukkit.heroes.HeroesManager) SpellKey(com.elmakers.mine.bukkit.api.spell.SpellKey) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MagicController(com.elmakers.mine.bukkit.magic.MagicController) ItemStack(org.bukkit.inventory.ItemStack) SpellTemplate(com.elmakers.mine.bukkit.api.spell.SpellTemplate) HashSet(java.util.HashSet) ConfigurationSection(org.bukkit.configuration.ConfigurationSection)

Example 62 with MageController

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

the class SkillSelectorAction method openInventory.

protected void openInventory() {
    MageController apiController = context.getController();
    if (!(apiController instanceof MagicController))
        return;
    MagicController controller = (MagicController) apiController;
    HeroesManager heroes = controller.getHeroes();
    int inventorySize = 9 * controller.getSkillInventoryRows();
    float numSlots = extraSlots + allSkills.size();
    int numPages = (int) Math.ceil(numSlots / inventorySize);
    if (page < 1)
        page = numPages;
    else if (page > numPages)
        page = 1;
    Mage mage = context.getMage();
    Player player = mage.getPlayer();
    int pageIndex = page - 1;
    int startIndex = pageIndex * inventorySize;
    int maxIndex = (pageIndex + 1) * inventorySize - 1;
    List<SkillDescription> skills = new ArrayList<>();
    for (int i = startIndex; i <= maxIndex && i < allSkills.size(); i++) {
        skills.add(allSkills.get(i));
    }
    if (skills.size() == 0) {
        String messageTemplate = controller.getMessages().get("skills.none_on_page", "$page");
        player.sendMessage(messageTemplate.replace("$page", Integer.toString(page)));
        return;
    }
    int invSize = (int) Math.ceil(numSlots / 9.0f) * 9;
    String title = inventoryTitle;
    title = title.replace("$pages", Integer.toString(numPages)).replace("$page", Integer.toString(page));
    Inventory displayInventory = CompatibilityUtils.createInventory(null, invSize, title);
    for (SkillDescription skill : skills) {
        ItemStack skillItem = controller.getAPI().createItem(skill.getSkillKey(), mage);
        if (skillItem == null)
            continue;
        InventoryUtils.configureSkillItem(skillItem, classKey, skillsConfig);
        if (skill.isHeroes() && heroes != null && !heroes.canUseSkill(player, skill.heroesSkill)) {
            String nameTemplate = controller.getMessages().get("skills.item_name_unavailable", "$skill");
            String spellName = skill.spell != null ? skill.spell.getName() : skill.heroesSkill;
            CompatibilityUtils.setDisplayName(skillItem, nameTemplate.replace("$skill", spellName));
            InventoryUtils.setMetaBoolean(skillItem, "unavailable", true);
            if (skill.spell != null) {
                MaterialAndData disabledIcon = skill.spell.getDisabledIcon();
                if (disabledIcon != null) {
                    disabledIcon.applyToItem(skillItem);
                } else {
                    String disabledIconURL = skill.spell.getDisabledIconURL();
                    if (disabledIconURL != null && !disabledIconURL.isEmpty()) {
                        InventoryUtils.setNewSkullURL(skillItem, disabledIconURL);
                    }
                }
            }
        }
        displayInventory.addItem(skillItem);
    }
    mage.deactivateGUI();
    mage.activateGUI(this, displayInventory);
}
Also used : Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) HeroesManager(com.elmakers.mine.bukkit.heroes.HeroesManager) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MagicController(com.elmakers.mine.bukkit.magic.MagicController) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory)

Example 63 with MageController

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

the class PowerBlockAction method perform.

@SuppressWarnings("deprecation")
@Override
public SpellResult perform(CastContext context) {
    Block block = context.getTargetBlock();
    if (!context.hasBuildPermission(block)) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    if (!context.isDestructible(block)) {
        return SpellResult.NO_TARGET;
    }
    context.getUndoList().setApplyPhysics(true);
    Material material = block.getType();
    BlockState blockState = block.getState();
    MaterialData data = blockState.getData();
    MageController controller = context.getController();
    boolean powerBlock = false;
    if (data instanceof Button) {
        Button powerData = (Button) data;
        context.registerForUndo(block);
        powerData.setPowered(!powerData.isPowered());
        powerBlock = true;
    } else if (data instanceof Lever) {
        Lever powerData = (Lever) data;
        context.registerForUndo(block);
        powerData.setPowered(!powerData.isPowered());
        powerBlock = true;
    } else if (data instanceof PistonBaseMaterial) {
        PistonBaseMaterial powerData = (PistonBaseMaterial) data;
        context.registerForUndo(block);
        powerData.setPowered(!powerData.isPowered());
        powerBlock = true;
    } else if (data instanceof PoweredRail) {
        PoweredRail powerData = (PoweredRail) data;
        context.registerForUndo(block);
        powerData.setPowered(!powerData.isPowered());
        powerBlock = true;
    } else if (data instanceof RedstoneWire) {
        RedstoneWire wireData = (RedstoneWire) data;
        context.registerForUndo(block);
        wireData.setData((byte) (15 - wireData.getData()));
        powerBlock = true;
    } else if (material == Material.REDSTONE_BLOCK) {
        context.registerForUndo(block);
        block.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, material.getId());
        controller.getRedstoneReplacement().modify(block, applyPhysics);
    } else if (material == Material.REDSTONE_TORCH_OFF) {
        context.registerForUndo(block);
        block.setType(Material.REDSTONE_TORCH_ON);
    } else if (material == Material.REDSTONE_TORCH_ON) {
        context.registerForUndo(block);
        block.setType(Material.REDSTONE_TORCH_OFF);
    } else if (material == Material.TNT) {
        context.registerForUndo(block);
        block.setType(Material.AIR);
        // Kaboomy time!
        context.registerForUndo(block.getLocation().getWorld().spawnEntity(block.getLocation(), EntityType.PRIMED_TNT));
    }
    if (powerBlock) {
        blockState.update();
    }
    return SpellResult.CAST;
}
Also used : RedstoneWire(org.bukkit.material.RedstoneWire) MageController(com.elmakers.mine.bukkit.api.magic.MageController) BlockState(org.bukkit.block.BlockState) Lever(org.bukkit.material.Lever) Button(org.bukkit.material.Button) Block(org.bukkit.block.Block) PistonBaseMaterial(org.bukkit.material.PistonBaseMaterial) Material(org.bukkit.Material) MaterialData(org.bukkit.material.MaterialData) PoweredRail(org.bukkit.material.PoweredRail) PistonBaseMaterial(org.bukkit.material.PistonBaseMaterial)

Example 64 with MageController

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

the class RecallAction method getUnknownWarp.

@Nullable
protected Waypoint getUnknownWarp(String warpKey) {
    MageController controller = context.getController();
    Location warpLocation = controller.getWarp(warpKey);
    if (warpLocation == null || warpLocation.getWorld() == null) {
        return null;
    }
    String castMessage = context.getMessage("cast_warp").replace("$name", warpKey);
    String failMessage = context.getMessage("no_target_warp").replace("$name", warpKey);
    return new Waypoint(RecallType.WARP, warpLocation, warpKey, castMessage, failMessage, "", null, null);
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController) Location(org.bukkit.Location) Nullable(javax.annotation.Nullable)

Example 65 with MageController

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

the class DamageAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity entity = context.getTargetEntity();
    if (entity == null || !(entity instanceof Damageable) || entity.isDead()) {
        return SpellResult.NO_TARGET;
    }
    double damage = 1;
    Damageable targetEntity = (Damageable) entity;
    LivingEntity livingTarget = (entity instanceof LivingEntity) ? (LivingEntity) entity : null;
    context.registerDamaged(targetEntity);
    Mage mage = context.getMage();
    MageController controller = context.getController();
    double previousKnockbackResistance = 0D;
    try {
        if (knockbackResistance != null && livingTarget != null) {
            AttributeInstance knockBackAttribute = livingTarget.getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE);
            previousKnockbackResistance = knockBackAttribute.getBaseValue();
            knockBackAttribute.setBaseValue(knockbackResistance);
        }
        if (controller.isElemental(entity)) {
            damage = elementalDamage;
            controller.damageElemental(entity, damage * mage.getDamageMultiplier(), 0, mage.getCommandSender());
        } else {
            if (percentage != null) {
                damage = percentage * targetEntity.getMaxHealth();
            } else if (targetEntity instanceof Player) {
                damage = playerDamage;
            } else {
                damage = entityDamage;
            }
            String multiplierType = damageType;
            if (multiplierType == null) {
                multiplierType = magicDamage ? "magic" : "physical";
            }
            double mageMultiplier = mage.getDamageMultiplier(multiplierType);
            damage *= mageMultiplier;
            if (maxDistanceSquared > 0) {
                double distanceSquared = context.getLocation().distanceSquared(entity.getLocation());
                if (distanceSquared > maxDistanceSquared) {
                    return SpellResult.NO_TARGET;
                }
                if (distanceSquared > 0) {
                    damage = damage * (1 - distanceSquared / maxDistanceSquared);
                }
            }
            if (damageMultiplier != null) {
                damage *= damageMultiplier;
                mageMultiplier *= damageMultiplier;
            }
            if (damageType != null) {
                Mage targetMage = controller.getRegisteredMage(targetEntity);
                String targetAnnotation = "";
                if (targetMage != null) {
                    targetMage.setLastDamageType(damageType);
                } else {
                    targetAnnotation = "*";
                }
                mage.sendDebugMessage(ChatColor.RED + "Damaging (" + ChatColor.DARK_RED + damageType + ChatColor.RED + ") x " + ChatColor.DARK_RED + mageMultiplier + ChatColor.RED + " to " + ChatColor.BLUE + targetEntity.getType() + targetAnnotation + ": " + ChatColor.RED + damage, 5);
                // mages since other plugins may be tracking kills.
                if (mage.isPlayer() && controller.getDamageTypes().contains(damageType)) {
                    CompatibilityUtils.magicDamage(targetEntity, damage, mage.getEntity());
                } else {
                    CompatibilityUtils.damage(targetEntity, damage, mage.getEntity(), damageType);
                }
            } else if (magicDamage && (magicEntityDamage || targetEntity instanceof Player)) {
                mage.sendDebugMessage(ChatColor.RED + "Damaging (Magic) x " + ChatColor.DARK_RED + mageMultiplier + ChatColor.RED + " to " + ChatColor.BLUE + targetEntity.getType() + ": " + damage, 5);
                CompatibilityUtils.magicDamage(targetEntity, damage, mage.getEntity());
            } else {
                mage.sendDebugMessage(ChatColor.RED + "Damaging x " + ChatColor.DARK_RED + mageMultiplier + ChatColor.RED + " to " + ChatColor.BLUE + targetEntity.getType() + ": " + damage, 5);
                CompatibilityUtils.damage(targetEntity, damage, mage.getEntity());
            }
            if (damage == (int) damage) {
                context.addMessageParameter("damage", Integer.toString((int) damage));
            } else {
                context.addMessageParameter("damage", Double.toString(damage));
            }
            if (damageType != null && !damageType.isEmpty()) {
                String typeDescription = context.getController().getMessages().get("damage_types." + damageType, damageType);
                context.addMessageParameter("damage_type", typeDescription);
            }
        }
    } finally {
        if (knockbackResistance != null && livingTarget != null) {
            AttributeInstance knockBackAttribute = livingTarget.getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE);
            knockBackAttribute.setBaseValue(previousKnockbackResistance);
        }
    }
    return SpellResult.CAST;
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) Entity(org.bukkit.entity.Entity) LivingEntity(org.bukkit.entity.LivingEntity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Player(org.bukkit.entity.Player) Damageable(org.bukkit.entity.Damageable) Mage(com.elmakers.mine.bukkit.api.magic.Mage) AttributeInstance(org.bukkit.attribute.AttributeInstance)

Aggregations

MageController (com.elmakers.mine.bukkit.api.magic.MageController)76 Mage (com.elmakers.mine.bukkit.api.magic.Mage)45 Entity (org.bukkit.entity.Entity)27 Player (org.bukkit.entity.Player)26 Location (org.bukkit.Location)16 ItemStack (org.bukkit.inventory.ItemStack)16 Wand (com.elmakers.mine.bukkit.api.wand.Wand)14 Block (org.bukkit.block.Block)11 LivingEntity (org.bukkit.entity.LivingEntity)11 ArrayList (java.util.ArrayList)10 Inventory (org.bukkit.inventory.Inventory)9 ItemMeta (org.bukkit.inventory.meta.ItemMeta)8 Spell (com.elmakers.mine.bukkit.api.spell.Spell)5 MagicController (com.elmakers.mine.bukkit.magic.MagicController)5 Nullable (javax.annotation.Nullable)5 ConfigurationSection (org.bukkit.configuration.ConfigurationSection)5 ItemData (com.elmakers.mine.bukkit.api.item.ItemData)4 SpellTemplate (com.elmakers.mine.bukkit.api.spell.SpellTemplate)4 WandUpgradePath (com.elmakers.mine.bukkit.api.wand.WandUpgradePath)4 File (java.io.File)4