function init(hero) {
    hero.addKeyBindFunc("func_LOCATE", locateKey, "Locate", 1);

    hero.addAttributeProfile("WILDMUTT", profile => {
        profile.addAttribute("PUNCH_DAMAGE", 5.0, 0);
        profile.addAttribute("FALL_RESISTANCE", 0.65, 1);
        profile.addAttribute("BASE_SPEED", -0.15, 1);
        profile.addAttribute("SPRINT_SPEED", 0.55, 1);
        profile.addAttribute("JUMP_HEIGHT", 2.0, 0);
    });

    hero.addAttributeProfile("WILDMUTT_LOCATING", profile => {
        profile.addAttribute("PUNCH_DAMAGE", 0.0, 0);
        profile.addAttribute("FALL_RESISTANCE", 0.65, 1);
        profile.addAttribute("BASE_SPEED", -10.15, 1);
        profile.addAttribute("SPRINT_SPEED", -10.5, 1);
        profile.addAttribute("JUMP_HEIGHT", -20.0, 0);
    });

    hero.addDamageProfile("WILDMUTT", {
        "types": {
            "BLUNT": 1.0,
            "SHARP": 0.6
        },
        "properties": {
        }
    });
}

function getAttributeProfile(entity) {
    return entity.getData('tmf:dyn/p_1') && entity.getData('tmf:dyn/pc_1') > 0 && entity.getData('tmf:dyn/pc_1') < 0.3 ? "WILDMUTT_LOCATING" : "WILDMUTT";
}

function getDamageProfile(entity) {
    return "WILDMUTT";
}

function isKeyBindEnabled(entity, keyBind) {
    var nbt = entity.getWornChestplate().nbt();
    var SL = nbt.getByte("wildmutt");
    var PC1 = entity.getData('tmf:dyn/pc_1');
    var P2 = entity.getData('tmf:dyn/p_2');
    var flight = entity.getData("fiskheroes:flying");

    if (keyBind == "func_LOCATE") {
        return SL >= 15 && PC1 == 0 && entity.getHeldItem().name().endsWith("_ore");
    }

    return false;
}

function locateKey(player, manager) {
    var timer = player.getData("tmf:dyn/p_3");
    var block = player.getHeldItem().name();
    var nbt = player.getWornChestplate().nbt();
    var SL = nbt.getByte("wildmutt");

    if (isBlockInRange(player, manager, block, SL == 60 ? 16 : 8)) {
        manager.setData(player, 'tmf:dyn/p_1', true);
        player.playSound("tmf:wildmutt.echo", 2, 0.9 + 0.2 * Math.random());
    }
    if (!isBlockInRange(player, manager, block, SL == 60 ? 16 : 8)) {
        manager.setData(player, 'tmf:dyn/p_1', false);
    }

    return true;
}

function tick(entity, manager, isCurrent) {
    if (!isCurrent) {
        return;
    }
    var nbt = entity.getWornChestplate().nbt();
    var SL = nbt.getByte("wildmutt");

    manager.incrementData(entity, "tmf:dyn/pt_1", 10, entity.isSprinting() ? 1 : 0 );

    if (entity.getData('tmf:dyn/pc_1') == 1) {
        manager.setData(entity, 'tmf:dyn/p_1', false);
    }

    manager.incrementData(entity, "tmf:dyn/pc_1", 300, entity.getData('tmf:dyn/p_1') ? 1 : 0 );

}

function isBlockInRange(player, manager, block, range) {
    for (var x = -1; Math.abs(x) <= range; x < 0 ? x=-x : x=-x-1) {
        for (var y = -1; Math.abs(y) <= range; y < 0 ? y=-y : y=-y-1) {
            for (var z = -1; Math.abs(z) <= range; z < 0 ? z=-z : z=-z-1) {
                if (player.world().getBlock(player.pos().add(x, y, z)) == block) {
                    manager.setData(player, 'tmf:dyn/cX', Math.ceil(player.posX() + x));
                    manager.setData(player, 'tmf:dyn/cY', Math.ceil(player.posY() + y));
                    manager.setData(player, 'tmf:dyn/cZ', Math.ceil(player.posZ() + z));
                    return true;
                }
            }
        }
    }
    return false;
}

function getTierOverride(entity) {
    return 6;
}