Search in sources :

Example 26 with MageController

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

the class UndoQueue method save.

@Override
public void save(UndoData data) {
    MageController controller = owner.getController();
    int maxSize = controller.getMaxUndoPersistSize();
    try {
        int discarded = 0;
        List<com.elmakers.mine.bukkit.api.block.UndoList> undoList = data.getBlockList();
        UndoList list = tail;
        while (list != null) {
            if (maxSize > 0 && list.size() > maxSize) {
                discarded++;
            } else if (list.size() > 0) {
                undoList.add(list);
            }
            list = list.getNext();
        }
        if (discarded > 0) {
            controller.getLogger().info("Not saving " + discarded + " undo batches for mage " + owner.getName() + ", over max size of " + maxSize);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        controller.getLogger().warning("Failed to save undo data: " + ex.getMessage());
    }
}
Also used : MageController(com.elmakers.mine.bukkit.api.magic.MageController)

Example 27 with MageController

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

the class SpawnEntityAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Block targetBlock = context.getTargetBlock();
    targetBlock = targetBlock.getRelative(BlockFace.UP);
    Location spawnLocation = targetBlock.getLocation();
    Location sourceLocation = context.getLocation();
    spawnLocation.setPitch(sourceLocation.getPitch());
    spawnLocation.setYaw(sourceLocation.getYaw());
    MageController controller = context.getController();
    if (entityTypeProbability != null && !entityTypeProbability.isEmpty()) {
        if (repeatRandomize || entityData == null) {
            String randomType = RandomUtils.weightedRandom(entityTypeProbability);
            try {
                entityData = controller.getMob(randomType);
                if (entityData == null) {
                    entityData = new com.elmakers.mine.bukkit.entity.EntityData(EntityType.valueOf(randomType.toUpperCase()));
                }
            } catch (Throwable ex) {
                entityData = null;
            }
        }
    }
    if (entityData == null) {
        return SpellResult.FAIL;
    }
    if (force) {
        controller.setForceSpawn(true);
    }
    Entity spawnedEntity = null;
    try {
        spawnedEntity = entityData.spawn(context.getController(), spawnLocation, spawnReason);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    // Special check to assign ownership
    if (spawnedEntity instanceof AreaEffectCloud) {
        ((AreaEffectCloud) spawnedEntity).setSource(context.getLivingEntity());
    } else if (spawnedEntity instanceof Projectile) {
        ((Projectile) spawnedEntity).setShooter(context.getLivingEntity());
    }
    if (force) {
        controller.setForceSpawn(false);
    }
    if (spawnedEntity == null) {
        return SpellResult.FAIL;
    }
    if (!loot) {
        spawnedEntity.setMetadata("nodrops", new FixedMetadataValue(controller.getPlugin(), true));
    }
    if (speed > 0) {
        Vector motion = direction;
        if (motion == null) {
            motion = context.getDirection();
        } else {
            motion = motion.clone();
        }
        if (dyOffset != 0) {
            motion.setY(motion.getY() + dyOffset);
        }
        motion.normalize();
        motion.multiply(speed);
        CompatibilityUtils.setEntityMotion(spawnedEntity, motion);
    }
    Collection<EffectPlayer> projectileEffects = context.getEffects("spawned");
    for (EffectPlayer effectPlayer : projectileEffects) {
        effectPlayer.start(spawnedEntity.getLocation(), spawnedEntity, null, null);
    }
    context.registerForUndo(spawnedEntity);
    if (setTarget) {
        context.setTargetEntity(spawnedEntity);
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) AreaEffectCloud(org.bukkit.entity.AreaEffectCloud) Projectile(org.bukkit.entity.Projectile) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Block(org.bukkit.block.Block) EffectPlayer(com.elmakers.mine.bukkit.api.effect.EffectPlayer) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 28 with MageController

use of com.elmakers.mine.bukkit.api.magic.MageController 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)

Example 29 with MageController

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

the class TNTAction method start.

@Override
public SpellResult start(CastContext context) {
    Mage mage = context.getMage();
    LivingEntity living = mage.getLivingEntity();
    MageController controller = context.getController();
    int size = (int) (mage.getRadiusMultiplier() * this.size);
    Location loc = context.getEyeLocation();
    if (loc == null) {
        return SpellResult.LOCATION_REQUIRED;
    }
    if (!context.hasBreakPermission(loc.getBlock())) {
        return SpellResult.INSUFFICIENT_PERMISSION;
    }
    final Random rand = new Random();
    for (int i = 0; i < count; i++) {
        Location targetLoc = loc.clone();
        if (count > 1) {
            targetLoc.setX(targetLoc.getX() + rand.nextInt(2 * count) - count);
            targetLoc.setZ(targetLoc.getZ() + rand.nextInt(2 * count) - count);
        }
        TNTPrimed grenade = (TNTPrimed) context.getWorld().spawnEntity(targetLoc, EntityType.PRIMED_TNT);
        if (grenade == null) {
            return SpellResult.FAIL;
        }
        if (living != null) {
            CompatibilityUtils.setTNTSource(grenade, living);
        }
        if (velocity > 0) {
            Vector aim = context.getDirection();
            SafetyUtils.setVelocity(grenade, aim.multiply(velocity));
        }
        grenade.setYield(size);
        grenade.setFuseTicks(fuse);
        grenade.setIsIncendiary(useFire);
        if (!breakBlocks) {
            grenade.setMetadata("cancel_explosion", new FixedMetadataValue(controller.getPlugin(), true));
        }
        track(context, grenade);
    }
    return checkTracking(context);
}
Also used : LivingEntity(org.bukkit.entity.LivingEntity) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Random(java.util.Random) Mage(com.elmakers.mine.bukkit.api.magic.Mage) TNTPrimed(org.bukkit.entity.TNTPrimed) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) Vector(org.bukkit.util.Vector) Location(org.bukkit.Location)

Example 30 with MageController

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

the class WearAction method perform.

@Override
public SpellResult perform(CastContext context) {
    Entity entity = context.getTargetEntity();
    if (entity == null) {
        if (!context.getTargetsCaster())
            return SpellResult.NO_TARGET;
        entity = context.getEntity();
    }
    if (entity == null || !(entity instanceof Player)) {
        return SpellResult.NO_TARGET;
    }
    Player player = (Player) entity;
    MaterialAndData material = this.material;
    MageController controller = context.getController();
    Mage mage = controller.getMage(player);
    if (useItem) {
        Wand activeWand = mage.getActiveWand();
        // Not handling this for now.
        if (activeWand != context.getWand()) {
            return SpellResult.NO_TARGET;
        }
        if (activeWand != null) {
            activeWand.deactivate();
        }
        ItemStack itemInHand = player.getInventory().getItemInMainHand();
        if (itemInHand == null || itemInHand.getType() == Material.AIR) {
            return SpellResult.FAIL;
        }
        ItemStack[] armor = player.getInventory().getArmorContents();
        ItemStack currentItem = armor[slotNumber];
        armor[slotNumber] = itemInHand;
        player.getInventory().setArmorContents(armor);
        if (!InventoryUtils.isTemporary(currentItem)) {
            player.getInventory().setItemInMainHand(currentItem);
        } else {
            player.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
        }
        if (mage instanceof com.elmakers.mine.bukkit.magic.Mage) {
            ((com.elmakers.mine.bukkit.magic.Mage) mage).armorUpdated();
        }
        return SpellResult.CAST;
    }
    ItemStack wearItem = null;
    String materialName = null;
    if (item == null) {
        if (material == null && (context.getSpell().usesBrush() || context.getSpell().hasBrushOverride())) {
            material = context.getBrush();
        }
        if (material == null) {
            Block targetBlock = context.getTargetBlock();
            if (targetBlock != null) {
                material = new com.elmakers.mine.bukkit.block.MaterialAndData(targetBlock);
                // Check for Banners with 1.7 support
                // TODO: this is outdated?
                Material baseMaterial = material.getMaterial();
                if (DeprecatedUtils.getId(baseMaterial) == 176 || DeprecatedUtils.getId(baseMaterial) == 177) {
                    ((com.elmakers.mine.bukkit.block.MaterialAndData) material).setMaterialId(425);
                }
            }
        }
        if (material == null || material.getMaterial() == Material.AIR) {
            return SpellResult.NO_TARGET;
        }
        wearItem = material.getItemStack(1);
        materialName = material.getName();
    } else {
        wearItem = InventoryUtils.getCopy(item);
        materialName = context.getController().describeItem(wearItem);
    }
    ItemMeta meta = wearItem.getItemMeta();
    // Legacy support
    String displayName = context.getMessage("hat_name", "");
    displayName = context.getMessage("wear_name", displayName);
    if (materialName == null || materialName.isEmpty()) {
        materialName = "?";
    }
    if (displayName != null && !displayName.isEmpty()) {
        meta.setDisplayName(displayName.replace("$hat", materialName).replace("$item", materialName));
    }
    List<String> lore = new ArrayList<>();
    String loreLine = context.getMessage("hat_lore");
    loreLine = context.getMessage("wear_lore", loreLine);
    lore.add(loreLine);
    meta.setLore(lore);
    wearItem.setItemMeta(meta);
    wearItem = InventoryUtils.makeReal(wearItem);
    NMSUtils.makeTemporary(wearItem, context.getMessage("removed").replace("$hat", materialName).replace("$item", materialName));
    if (enchantments != null) {
        wearItem.addUnsafeEnchantments(enchantments);
    }
    if (unbreakable) {
        CompatibilityUtils.makeUnbreakable(wearItem);
    }
    ItemStack[] armor = player.getInventory().getArmorContents();
    ItemStack itemStack = armor[slotNumber];
    if (itemStack != null && itemStack.getType() != Material.AIR) {
        if (NMSUtils.isTemporary(itemStack)) {
            ItemStack replacement = NMSUtils.getReplacement(itemStack);
            if (replacement != null) {
                itemStack = replacement;
            }
        }
        NMSUtils.setReplacement(wearItem, itemStack);
    }
    armor[slotNumber] = wearItem;
    player.getInventory().setArmorContents(armor);
    // Sanity check to make sure the block was allowed to be created
    armor = player.getInventory().getArmorContents();
    ItemStack helmetItem = armor[slotNumber];
    if (!NMSUtils.isTemporary(helmetItem)) {
        armor[slotNumber] = itemStack;
        player.getInventory().setArmorContents(armor);
        return SpellResult.NO_TARGET;
    }
    targetMage = mage;
    context.registerForUndo(new WearUndoAction());
    if (mage instanceof com.elmakers.mine.bukkit.magic.Mage) {
        ((com.elmakers.mine.bukkit.magic.Mage) mage).armorUpdated();
    }
    return SpellResult.CAST;
}
Also used : Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) Wand(com.elmakers.mine.bukkit.api.wand.Wand) Material(org.bukkit.Material) MageController(com.elmakers.mine.bukkit.api.magic.MageController) Mage(com.elmakers.mine.bukkit.api.magic.Mage) MaterialAndData(com.elmakers.mine.bukkit.api.block.MaterialAndData) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

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