package com.elmakers.mine.bukkit.action.builtin;

import com.elmakers.mine.bukkit.api.action.BlockAction;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.spell.CompoundAction;
import com.elmakers.mine.bukkit.utility.CompatibilityUtils;
import com.elmakers.mine.bukkit.utility.Target;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class AreaOfEffectAction extends CompoundAction implements BlockAction
{
	@Override
	public SpellResult perform(ConfigurationSection parameters, Block block)
	{
		int radius = parameters.getInt("radius", 8);
		boolean targetSelf = parameters.getBoolean("target_self", false);
		int targetCount = parameters.getInt("target_count", -1);
		Mage mage = getMage();
		Entity sourceEntity = null;
		Spell spell = getSpell();
		Location sourceLocation = block.getLocation();

		if (mage != null)
		{
			radius = (int)(mage.getRadiusMultiplier() * radius);
			sourceEntity = mage.getEntity();
		}

		List<Entity> entities = CompatibilityUtils.getNearbyEntities(sourceLocation, radius, radius, radius);
		List<Entity> targetEntities = new ArrayList<Entity>();

		if (targetCount > 0)
		{
			Collection<Target> targets = new ArrayList<Target>();
			for (Entity entity : entities)
			{
				if ((targetSelf || entity != sourceEntity) && spell.canTarget(entity))
				{
					targets.add(new Target(sourceLocation, entity, radius));
				}
			}

			for (Target target : targets)
			{
				if (targetEntities.size() >= targetCount) break;
				targetEntities.add(target.getEntity());
			}
		}
		else
		{
			for (Entity entity : entities)
			{
				if ((targetSelf || entity != sourceEntity) && spell.canTarget(entity))
				{
					targetEntities.add(entity);
				}
			}
		}

		return perform(parameters, block.getLocation(), targetEntities);
	}
}

package com.elmakers.mine.bukkit.action.builtin;

import com.elmakers.mine.bukkit.api.action.BlockAction;
import com.elmakers.mine.bukkit.api.magic.Mage;
import com.elmakers.mine.bukkit.api.spell.Spell;
import com.elmakers.mine.bukkit.api.spell.SpellResult;
import com.elmakers.mine.bukkit.spell.CompoundAction;
import com.elmakers.mine.bukkit.utility.CompatibilityUtils;
import com.elmakers.mine.bukkit.utility.Target;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class AreaOfEffectAction extends CompoundAction implements BlockAction
{
	@Override
	public SpellResult perform(ConfigurationSection parameters, Block block)
	{
		int radius = parameters.getInt("radius", 8);
		boolean targetSelf = parameters.getBoolean("target_self", false);
		int targetCount = parameters.getInt("target_count", -1);
		Mage mage = getMage();
		Entity sourceEntity = null;
		Spell spell = getSpell();
		Location sourceLocation = block.getLocation();

		if (mage != null)
		{
			radius = (int)(mage.getRadiusMultiplier() * radius);
			sourceEntity = mage.getEntity();
		}

		List<Entity> entities = CompatibilityUtils.getNearbyEntities(sourceLocation, radius, radius, radius);
		List<Entity> targetEntities = new ArrayList<Entity>();

		if (targetCount > 0)
		{
			List<Target> targets = new ArrayList<Target>();
			for (Entity entity : entities)
			{
				if ((targetSelf || entity != sourceEntity) && spell.canTarget(entity))
				{
					targets.add(new Target(sourceLocation, entity, radius));
				}
			}
			Collections.sort(targets);
			for (Target target : targets)
			{
				if (targetEntities.size() >= targetCount) break;
				targetEntities.add(target.getEntity());
			}
		}
		else
		{
			for (Entity entity : entities)
			{
				if ((targetSelf || entity != sourceEntity) && spell.canTarget(entity))
				{
					targetEntities.add(entity);
				}
			}
		}

		return perform(parameters, block.getLocation(), targetEntities);
	}
}

Fix prioritization of limited targets in AOE action