local S = core.get_translator(core.get_current_modname());
local dp = natural_habitat.dupes;

if not dp.stick then
    dp.stick = m7a.register_craftitem("natural_habitat:stick", {
        description = S("Stick"),
        inventory_image = "nbt_stick.png",
        wield_image = "nbt_stick.png",
        groups = { craftitem=1, stick=1, flammable=2 },
        
        _doc_items_hidden = false,
        _doc_items_longdesc = S(
            "Sticks are a very versatile crafting material; "..
            "used in countless crafting recipes."
        ),
        
        _mcl_toollike_wield = true,
        _mcl_burntime = 5,
    })
end;

m7a.register_multinode("natural_habitat:stick_on_ground", {
    shared = {
        description = S("Stick"),
        drawtype = "mesh",
        tiles = { "nbt_olive_log_side.png", "nbt_olive_log_top.png" },
        paramtype = "light",
        paramtype2 = "facedir",
        walkable = false,
        groups = {
            dig_immediate=3, stick=1, falling_node=1, not_in_creative_inventory=1,
            deco_block=1, dig_by_water=1, attached_node=1,
        },
        sounds = natural_habitat.sound_wood(),
    },
    nodes = {
        [""] = {
            mesh = "nbt_stick.obj",
            drop = dp.stick,
            node_box = { type="fixed", fixed={{-0.4, -0.5, -0.2, 0.4, -0.3, 0.2}} },
            selection_box = { type="fixed", fixed={{-0.4, -0.5, -0.2, 0.4, -0.3, 0.2}} },
        },
        ["_2"] = {
            mesh = "nbt_stick_2.obj";
            drop = dp.stick.." 2";
            node_box = { type="fixed", fixed={{-0.4, -0.5, -0.4, 0.4, -0.2, 0.4}} };
            selection_box = { type="fixed", fixed={{-0.4, -0.5, -0.4, 0.4, -0.2, 0.4}} };
        },
        ["_3"] = {
            mesh = "nbt_stick_3.obj";
            drop = dp.stick.." 3";
            node_box = { type="fixed", fixed={{-0.4, -0.5, -0.4, 0.4, -0.2, 0.4}} };
            selection_box = { type="fixed", fixed={{-0.4, -0.5, -0.4, 0.4, -0.2, 0.4}} };
        },
    },
});

m7a.register_multideco("natural_habitat:deco_stick", {
    deco_type = "simple",
    fill_ratio = 0.005,
    spawn_by = "air",
    num_spawn_by = 1,
    rotation = "random",
    use_map_generator = true,
    place_on = { dp.soil_with_grass, "default:dry_dirt_with_dry_grass" },
    biomes = {},
    decos = {
        { decoration="natural_habitat:stick_on_ground", },
        { decoration="natural_habitat:stick_on_ground_2", },
        { decoration="natural_habitat:stick_on_ground_3", },
    },
});

-- OVERRIDE STICK ITEM
core.override_item(dp.stick, {
    on_place = function(itemstack, placer, pointed_thing)
        local on_rc = m7a.check_on_rightclick(itemstack, user, pointed_thing);
        if on_rc then return itemstack; end;
        
        local pos = pointed_thing.under;
        local node = core.get_node(pos);
        if (node.name == "natural_habitat:stick_on_ground") then
            core.set_node(pos, { name="natural_habitat:stick_on_ground_2", param2=node.param2 });
            m7a.take_item_if_not_creative(placer, itemstack);
            return itemstack;
        elseif (node.name == "natural_habitat:stick_on_ground_2") then
            core.set_node(pos, { name="natural_habitat:stick_on_ground_3", param2=node.param2 });
            m7a.take_item_if_not_creative(placer, itemstack);
            return itemstack;
        end;
        
        if not string.find(core.get_node(pos).name, "natural_habitat:stick_on_ground") then
            return m7a.place_item(
                itemstack, placer, pointed_thing, 
                nil, "natural_habitat:stick_on_ground"
            ).itemstack;
        end;
        return itemstack;
    end,
});