package net.wayward_realms.waywardskills.spell;

import net.wayward_realms.waywardlib.character.Character;
import net.wayward_realms.waywardlib.classes.Stat;
import net.wayward_realms.waywardlib.combat.Fight;
import net.wayward_realms.waywardlib.skills.AttackSpellBase;
import net.wayward_realms.waywardlib.skills.SkillType;
import org.bukkit.Material;
import org.bukkit.TreeType;
import org.bukkit.block.Block;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import static net.wayward_realms.waywardlib.util.lineofsight.LineOfSightUtils.getTargetBlock;

public class OvergrowthSpell extends AttackSpellBase {

    public OvergrowthSpell() {
        setName("Overgrowth");
        setPower(65);
        setCoolDown(1500);
        setManaCost(100);
        setCriticalChance(20);
        setAttackStat(Stat.MAGIC_ATTACK);
        setDefenceStat(Stat.MAGIC_DEFENCE);
        setHitChance(80);
        setCriticalMultiplier(2.5D);
        setType(SkillType.MAGIC_NATURE);
    }

    @Override
    public void animate(Fight fight, Character attacking, Character defending, ItemStack weapon) {
        Player defendingPlayer = defending.getPlayer().getPlayer();
        defendingPlayer.getWorld().generateTree(defendingPlayer.getLocation(), TreeType.JUNGLE);
        defendingPlayer.getWorld().getBlockAt(defendingPlayer.getLocation()).setType(Material.AIR);
        defendingPlayer.getWorld().getBlockAt(defendingPlayer.getEyeLocation()).setType(Material.AIR);
    }

    @Override
    public double getWeaponModifier(ItemStack weapon) {
        return getMagicWeaponModifier(weapon);
    }

    @Override
    public String getFightUseMessage(Character attacking, Character defending, double damage) {
        return attacking.getName() + " caused a huge tree to grow around " + defending.getName() + ", dealing " + damage + " damage.";
    }

    @Override
    public String getFightFailManaMessage(Character attacking, Character defending) {
        return attacking.getName() + " attempted to rapidly grow a huge tree, but did not have enough mana.";
    }

    @Override
    public boolean use(Player player) {
        Block targetBlock = getTargetBlock(player, null, 32);
        targetBlock.getWorld().generateTree(targetBlock.getLocation(), TreeType.JUNGLE);
        for (LivingEntity entity : player.getWorld().getLivingEntities()) {
            if (entity.getLocation().distanceSquared(targetBlock.getLocation()) <= 256) {
                entity.damage(15D, player);
            }
        }
        return true;
    }

    @Override
    public ItemStack getIcon() {
        ItemStack icon = new ItemStack(Material.SAPLING);
        ItemMeta meta = icon.getItemMeta();
        meta.setDisplayName("Overgrowth");
        icon.setItemMeta(meta);
        return icon;
    }

    @Override
    public boolean canUse(Character character) {
        return character.getSkillPoints(SkillType.MAGIC_NATURE) >= 80;
    }

}

@@ -314,24 +314,53 @@
 .SkillType;%0A
+import org.bukkit.ChatColor;%0A
 import org.b
@@ -430,24 +430,59 @@
 lock.Block;%0A
+import org.bukkit.block.BlockFace;%0A
 import org.b
@@ -2344,16 +2344,37 @@
 %0A       
+ boolean successful =
  targetB
@@ -2406,32 +2406,58 @@
 ree(targetBlock.
+getRelative(BlockFace.UP).
 getLocation(), T
@@ -2465,32 +2465,62 @@
 eeType.JUNGLE);%0A
+        if (successful) %7B%0A    
         for (Liv
@@ -2587,24 +2587,28 @@
             
+ 
+   
 if (entity.g
@@ -2689,16 +2689,20 @@
         
+    
 entity.d
@@ -2725,34 +2725,42 @@
 r);%0A            
-%7D%0A
+    %7D%0A    
         %7D%0A      
@@ -2765,19 +2765,132 @@
     
-return true
+%7D else %7B%0A            player.sendMessage(ChatColor.RED + %22Could not grow a tree there.%22);%0A        %7D%0A        return successful
 ;%0A  

Fix overgrowth, closes #171