package com.elmakers.mine.bukkit.magicworlds.spawn.builtin;

import com.elmakers.mine.bukkit.api.entity.EntityData;
import com.elmakers.mine.bukkit.api.magic.MageController;
import com.elmakers.mine.bukkit.magicworlds.spawn.SpawnRule;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.plugin.Plugin;

public class ReplaceRule extends SpawnRule {
    protected EntityData replaceWith;

    @Override
    public void finalizeLoad(String worldName)
    {
        // Legacy support
        if (!parameters.contains("type")) {
            parameters.set("type", parameters.get("replace_type"));
        }
        if (!parameters.contains("sub_type")) {
            parameters.set("sub_type", parameters.get("replace_sub_type"));
        }

        MageController mageController = controller.getMagic().getController();
        replaceWith = mageController.getMob(parameters.getString("type"));
        if (replaceWith == null) {
            replaceWith = mageController.loadMob(parameters);
        }

        if (replaceWith == null) {
            controller.getLogger().warning("Error reading in configuration for custom mob in " + worldName);
            return;
        }
        String message = " Replacing: " + getTargetEntityTypeName() + " in " + worldName + " at y > " + minY
                + " with " + replaceWith.describe() + " at a " + (percentChance * 100) + "% chance";

        if (tags != null) {
            message = message + " in regions tagged with any of " + tags.toString();
        }
        if (biomes != null) {
            message = message + " in biomes " + biomes.toString();
        }
        if (notBiomes != null) {
            message = message + " not in biomes " + notBiomes.toString();
        }
        controller.getLogger().info(message);
    }
    
    @Override
    public LivingEntity onProcess(Plugin plugin, LivingEntity entity) {
        if (replaceWith == null) return null;

        // This makes replacing the same type of mob have better balance,
        // particularly with mob spawners
        if (entity.getType() == replaceWith.getType()) {
            replaceWith.modify(controller.getMagic().getController(), entity);
            return entity;
        }

        Entity spawned = replaceWith.spawn(controller.getMagic().getController(), entity.getLocation());
        return spawned instanceof LivingEntity ? (LivingEntity)spawned : null;
    }
}

@@ -1048,32 +1048,65 @@
  (replaceWith ==
+ null %7C%7C replaceWith.getType() ==
  null) %7B%0A       
@@ -1204,16 +1204,37 @@
 orldName
+ + %22 for rule %22 + key
 );%0A     

Improve log message when a magic mob type fails to load