-- bones/init.lua -- Minetest 0.4 mod: bones -- See README.txt for licensing and other information. -- Load support for MT game translation. local S = minetest.get_translator("bones") local bones_max_slots = tonumber(minetest.settings:get("bones_max_slots")) or 15 * 10 local min_inv_size = 4 * 8 -- display and provide at least this many slots bones = {} local function NS(s) return s end local function is_owner(pos, name) local owner = minetest.get_meta(pos):get_string("owner") if owner == "" or owner == name or minetest.check_player_privs(name, "protection_bypass") then return true end return false end local function appendmulti(tbl,...) for _, v in pairs({...}) do table.insert(tbl, v) end end local function get_bones_formspec_for_size(numitems) local cols, rows local scroll=false if numitems <= min_inv_size then cols, rows = 8, 4 else cols, rows = 8, math.ceil(numitems / 8) scroll=true end local output={} appendmulti(output, "size[", 8.5, ",", 9, "]") if scroll then appendmulti(output, "scrollbaroptions[max=",rows*9.3,"]") appendmulti(output, "scrollbar[8,0;0.3,4.5;vertical;bones_scroll;0]") appendmulti(output, "scroll_container[0,0.3;10.3,4.95;bones_scroll;vertical;0.1]") end appendmulti(output, "list[current_name;main;0,0;", cols, ",", rows, ";]") if scroll then appendmulti(output, "scroll_container_end[]") end appendmulti(output, "list[current_player;main;", 0, ",", 4.75, ";8,1;]") appendmulti(output, "list[current_player;main;", 0, ",", 5.98, ";8,3;8]") appendmulti(output, "listring[current_name;main]") appendmulti(output, "listring[current_player;main]") appendmulti(output, default.get_hotbar_bg(0, 4.85)) return table.concat(output) end local share_bones_time = tonumber(minetest.settings:get("share_bones_time")) or 1200 local share_bones_time_early = tonumber(minetest.settings:get("share_bones_time_early")) or share_bones_time / 4 local bones_def = { description = S("Bones"), tiles = { "bones_top.png^[transform2", "bones_bottom.png", "bones_side.png", "bones_side.png", "bones_rear.png", "bones_front.png" }, paramtype2 = "facedir", groups = {dig_immediate = 2}, sounds = default.node_sound_gravel_defaults(), can_dig = function(pos, player) local inv = minetest.get_meta(pos):get_inventory() local name = "" if player then name = player:get_player_name() end return is_owner(pos, name) and inv:is_empty("main") end, allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player) if is_owner(pos, player:get_player_name()) then return count end return 0 end, allow_metadata_inventory_put = function(pos, listname, index, stack, player) return 0 end, allow_metadata_inventory_take = function(pos, listname, index, stack, player) if is_owner(pos, player:get_player_name()) then return stack:get_count() end return 0 end, on_metadata_inventory_take = function(pos, listname, index, stack, player) local meta = minetest.get_meta(pos) if meta:get_inventory():is_empty("main") then local inv = player:get_inventory() if inv:room_for_item("main", {name = "bones:bones"}) then inv:add_item("main", {name = "bones:bones"}) else minetest.add_item(pos, "bones:bones") end minetest.remove_node(pos) end end, on_punch = function(pos, node, player) if not is_owner(pos, player:get_player_name()) then return end if minetest.get_meta(pos):get_string("infotext") == "" then return end local inv = minetest.get_meta(pos):get_inventory() local player_inv = player:get_inventory() local has_space = true for i = 1, inv:get_size("main") do local stk = inv:get_stack("main", i) if player_inv:room_for_item("main", stk) then inv:set_stack("main", i, nil) player_inv:add_item("main", stk) else has_space = false break end end -- remove bones if player emptied them if has_space then if player_inv:room_for_item("main", {name = "bones:bones"}) then player_inv:add_item("main", {name = "bones:bones"}) else minetest.add_item(pos,"bones:bones") end minetest.remove_node(pos) end end, on_timer = function(pos, elapsed) local meta = minetest.get_meta(pos) local time = meta:get_int("time") + elapsed if time >= share_bones_time then meta:set_string("infotext", S("@1's old bones", meta:get_string("owner"))) meta:set_string("owner", "") else meta:set_int("time", time) return true end end, on_blast = function(pos) end, } default.set_inventory_action_loggers(bones_def, "bones") minetest.register_node("bones:bones", bones_def) local function may_replace(pos, player) local node_name = minetest.get_node(pos).name local node_definition = minetest.registered_nodes[node_name] -- if the node is unknown, we return false if not node_definition then return false end -- allow replacing air if node_name == "air" then return true end -- don't replace nodes inside protections if minetest.is_protected(pos, player:get_player_name()) then return false end -- allow replacing liquids if node_definition.liquidtype ~= "none" then return true end -- don't replace filled chests and other nodes that don't allow it local can_dig_func = node_definition.can_dig if can_dig_func and not can_dig_func(pos, player) then return false end -- default to each nodes buildable_to; if a placed block would replace it, why shouldn't bones? -- flowers being squished by bones are more realistical than a squished stone, too return node_definition.buildable_to end local drop = function(pos, itemstack) local obj = minetest.add_item(pos, itemstack:take_item(itemstack:get_count())) if obj then obj:set_velocity({ x = math.random(-10, 10) / 9, y = 5, z = math.random(-10, 10) / 9, }) end end bones.player_inventory_lists = { "main", "craft" } local collect_items_callbacks = {} function bones.register_collect_items(func) table.insert(collect_items_callbacks, func) end bones.register_collect_items(function(player) local items = {} local player_inv = player:get_inventory() for _, list_name in ipairs(bones.player_inventory_lists) do local inv_list=player_inv:get_list(list_name) or {} for _, inv_slot in ipairs(inv_list) do if inv_slot:get_count() > 0 then table.insert(items, inv_slot) end end player_inv:set_list(list_name, {}) end while(#items