Files
luanti-stripped-tree/functions.lua
Gergely Polonkai 787a40378a
Some checks are pending
build / build (push) Waiting to run
Recreate textures using the original trunk tops
2025-12-16 19:04:34 +01:00

127 lines
4.5 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
local S = core.get_translator(core.get_current_modname())
stripped_tree.stripped_pairs = {}
function stripped_tree.has_stripped(pos)
local node = core.get_node(pos).name
return stripped_tree.stripped_pairs[node]
end
-- Function to swap nodes
--
-- The third parameter is a placeholder for backwards compatibility
stripped_tree.swap_node = function(pos, user, _, tool)
local old_node = core.get_node(pos)
local stripped_name = stripped_tree.stripped_pairs[old_node.name]
-- TODO: Maybe we could log an error here
if not stripped_name then return end
core.swap_node(pos, {name = stripped_name, param2 = old_node.param2})
-- itemstack:add_wear(65535 / 299) this is not useful at moment.
if not core.is_creative_enabled(user:get_player_name()) then
local inv = user:get_inventory()
-- If the player has room in their inventory for a bark, give them one; otherwise, drop the bark to the ground.
if inv:room_for_item("main", "default:tree_bark") then
inv:add_item("main", {name = "default:tree_bark"})
else
core.add_item(pos, "default:tree_bark")
end
end
return tool
end
-- Function to register a single strippable trunk
function stripped_tree.register_strippable_trunk(trunk_name, plank_name, stripped_tiles)
-- If we have already registered this trunk theres no need to do anything
if stripped_tree.stripped_pairs[trunk_name] then return end
local mod_name, trunk_node = unpack(trunk_name:split(":"))
local stripped_name = "stripped_tree:" .. mod_name .. "_stripped_" .. trunk_node
stripped_tree.stripped_pairs[trunk_name] = stripped_name
local trunk_def = core.registered_nodes[trunk_name]
local stripped_def = table.copy(trunk_def)
stripped_def.description = S("Stripped @1", trunk_def.description)
stripped_def.groups = table.copy(trunk_def.groups)
stripped_def.groups.not_in_creative_inventory = 1
stripped_def.tiles = stripped_tiles or {
mod_name .. "_" .. trunk_node .. "_top.png^stripped_trees_new_" .. mod_name .. "_" .. trunk_node .. "_top.png",
mod_name .. "_" .. trunk_node .. "_top.png^stripped_trees_new_" .. mod_name .. "_" .. trunk_node .. "_top.png",
"stripped_trees_new_" .. mod_name .. "_" .. trunk_node .. ".png",
}
core.register_node(stripped_name, stripped_def)
core.register_craft(
{
output = trunk_name,
recipe = {
{"", "default:tree_bark", ""},
{"default:tree_bark", stripped_name, "default:tree_bark"},
{"", "default:tree_bark", ""},
},
}
)
if plank_name then core.register_craft({output = plank_name .. " 4", recipe = {{stripped_name}}}) end
end
-- Compatibility function from the previous version, able to register multiple stripped tree nodes at once
function stripped_tree.register_trunk(mod_name, trunk_names)
for _, name in ipairs(trunk_names) do stripped_tree.register_strippable_trunk(mod_name .. ":" .. name) end
end
function stripped_tree.maybe_strip_trunk(pos, player, tool, wear)
local player_name = player:get_player_name()
if core.is_protected(pos, player_name) then
core.record_protection_violation(pos, player_name)
return
end
wear = wear and tonumber(wear)
if stripped_tree.has_stripped(pos) then
stripped_tree.swap_node(pos, player, nil, tool)
if not core.is_creative_enabled(player_name) and wear and wear ~= 0 then tool:add_wear(wear) end
end
end
-- Function to override axes
function stripped_tree.register_axes(mod_n, axe_types)
if stripped_tree.axes_strip_trees ~= true then return end
for _, axe_name in ipairs(axe_types) do
core.override_item(
mod_n .. ":" .. axe_name, {
on_place = function(itemstack, user, pointed_thing)
if pointed_thing.type ~= "node" then return end
local pos = pointed_thing.under
-- TODO: Add wear to the axe, but it should depend on the material maybe?
stripped_tree.maybe_strip_trunk(pos, user, itemstack)
-- Taken from VoxelGarden
local node = core.get_node_or_nil(pos)
if not node then return end
local node_def = core.registered_nodes[node.name]
if node_def and node_def.on_rightclick then
return node_def.on_rightclick(pos, node, user, itemstack, pointed_thing)
end
end,
}
)
end
end