]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - builtin/game/item.lua
Add on_authplayer callback and 'last_login' to on_joinplayer (#9574)
[dragonfireclient.git] / builtin / game / item.lua
index 59ec5ff65110f02831a63ed39b078ce25ad3b4c1..3aaa71ef23a47cc32a6223061e90bd8dcd36feea 100644 (file)
@@ -33,7 +33,7 @@ function core.get_pointed_thing_position(pointed_thing, above)
                -- The position where a node would be dug
                return pointed_thing.under
        elseif pointed_thing.type == "object" then
-               return pointed_thing.ref and pointed_thing.ref:getpos()
+               return pointed_thing.ref and pointed_thing.ref:get_pos()
        end
 end
 
@@ -155,6 +155,24 @@ function core.yaw_to_dir(yaw)
        return {x = -math.sin(yaw), y = 0, z = math.cos(yaw)}
 end
 
+function core.is_colored_paramtype(ptype)
+       return (ptype == "color") or (ptype == "colorfacedir") or
+               (ptype == "colorwallmounted")
+end
+
+function core.strip_param2_color(param2, paramtype2)
+       if not core.is_colored_paramtype(paramtype2) then
+               return nil
+       end
+       if paramtype2 == "colorfacedir" then
+               param2 = math.floor(param2 / 32) * 32
+       elseif paramtype2 == "colorwallmounted" then
+               param2 = math.floor(param2 / 8) * 8
+       end
+       -- paramtype2 == "color" requires no modification.
+       return param2
+end
+
 function core.get_node_drops(node, toolname)
        -- Compatibility, if node is string
        local nodename = node
@@ -166,27 +184,20 @@ function core.get_node_drops(node, toolname)
        end
        local def = core.registered_nodes[nodename]
        local drop = def and def.drop
+       local ptype = def and def.paramtype2
+       -- get color, if there is color (otherwise nil)
+       local palette_index = core.strip_param2_color(param2, ptype)
        if drop == nil then
                -- default drop
-               local stack = ItemStack(nodename)
-               if def then
-                       local type = def.paramtype2
-                       if (type == "color") or (type == "colorfacedir") or
-                                       (type == "colorwallmounted") then
-                               local meta = stack:get_meta()
-                               local color_part = param2
-                               if (type == "colorfacedir") then
-                                       color_part = math.floor(color_part / 32) * 32;
-                               elseif (type == "colorwallmounted") then
-                                       color_part = math.floor(color_part / 8) * 8;
-                               end
-                               meta:set_int("palette_index", color_part)
-                       end
+               if palette_index then
+                       local stack = ItemStack(nodename)
+                       stack:get_meta():set_int("palette_index", palette_index)
+                       return {stack:to_string()}
                end
-               return {stack:to_string()}
+               return {nodename}
        elseif type(drop) == "string" then
                -- itemstring drop
-               return {drop}
+               return drop ~= "" and {drop} or {}
        elseif drop.items == nil then
                -- drop = {} to disable default drop
                return {}
@@ -195,7 +206,6 @@ function core.get_node_drops(node, toolname)
        -- Extended drop table
        local got_items = {}
        local got_count = 0
-       local _, item, tool
        for _, item in ipairs(drop.items) do
                local good_rarity = true
                local good_tool = true
@@ -204,6 +214,8 @@ function core.get_node_drops(node, toolname)
                end
                if item.tools ~= nil then
                        good_tool = false
+               end
+               if item.tools ~= nil and toolname then
                        for _, tool in ipairs(item.tools) do
                                if tool:sub(1, 1) == '~' then
                                        good_tool = toolname:find(tool:sub(2)) ~= nil
@@ -214,10 +226,16 @@ function core.get_node_drops(node, toolname)
                                        break
                                end
                        end
-               end
+               end
                if good_rarity and good_tool then
                        got_count = got_count + 1
                        for _, add_item in ipairs(item.items) do
+                               -- add color, if necessary
+                               if item.inherit_color and palette_index then
+                                       local stack = ItemStack(add_item)
+                                       stack:get_meta():set_int("palette_index", palette_index)
+                                       add_item = stack:to_string()
+                               end
                                got_items[#got_items+1] = add_item
                        end
                        if drop.max_items ~= nil and got_count == drop.max_items then
@@ -228,22 +246,33 @@ function core.get_node_drops(node, toolname)
        return got_items
 end
 
-function core.item_place_node(itemstack, placer, pointed_thing, param2)
+local function user_name(user)
+       return user and user:get_player_name() or ""
+end
+
+-- Returns a logging function. For empty names, does not log.
+local function make_log(name)
+       return name ~= "" and core.log or function() end
+end
+
+function core.item_place_node(itemstack, placer, pointed_thing, param2,
+               prevent_after_place)
        local def = itemstack:get_definition()
        if def.type ~= "node" or pointed_thing.type ~= "node" then
-               return itemstack, false
+               return itemstack, nil
        end
 
        local under = pointed_thing.under
        local oldnode_under = core.get_node_or_nil(under)
        local above = pointed_thing.above
        local oldnode_above = core.get_node_or_nil(above)
-       local playername = placer:get_player_name()
+       local playername = user_name(placer)
+       local log = make_log(playername)
 
        if not oldnode_under or not oldnode_above then
-               core.log("info", playername .. " tried to place"
+               log("info", playername .. " tried to place"
                        .. " node in unloaded position " .. core.pos_to_string(above))
-               return itemstack, false
+               return itemstack, nil
        end
 
        local olddef_under = core.registered_nodes[oldnode_under.name]
@@ -252,10 +281,10 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2)
        olddef_above = olddef_above or core.nodedef_default
 
        if not olddef_above.buildable_to and not olddef_under.buildable_to then
-               core.log("info", playername .. " tried to place"
+               log("info", playername .. " tried to place"
                        .. " node in invalid position " .. core.pos_to_string(above)
                        .. ", replacing " .. oldnode_above.name)
-               return itemstack, false
+               return itemstack, nil
        end
 
        -- Place above pointed node
@@ -263,23 +292,19 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2)
 
        -- If node under is buildable_to, place into it instead (eg. snow)
        if olddef_under.buildable_to then
-               core.log("info", "node under is buildable to")
+               log("info", "node under is buildable to")
                place_to = {x = under.x, y = under.y, z = under.z}
        end
 
-       if core.is_protected(place_to, playername) and
-                       not minetest.check_player_privs(placer, "protection_bypass") then
-               core.log("action", playername
+       if core.is_protected(place_to, playername) then
+               log("action", playername
                                .. " tried to place " .. def.name
                                .. " at protected position "
                                .. core.pos_to_string(place_to))
                core.record_protection_violation(place_to, playername)
-               return itemstack
+               return itemstack, nil
        end
 
-       core.log("action", playername .. " places node "
-               .. def.name .. " at " .. core.pos_to_string(place_to))
-
        local oldnode = core.get_node(place_to)
        local newnode = {name = def.name, param1 = 0, param2 = param2 or 0}
 
@@ -297,7 +322,7 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2)
        -- Calculate the direction for furnaces and chests and stuff
        elseif (def.paramtype2 == "facedir" or
                        def.paramtype2 == "colorfacedir") and not param2 then
-               local placer_pos = placer:getpos()
+               local placer_pos = placer and placer:get_pos()
                if placer_pos then
                        local dir = {
                                x = above.x - placer_pos.x,
@@ -305,7 +330,7 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2)
                                z = above.z - placer_pos.z
                        }
                        newnode.param2 = core.dir_to_facedir(dir)
-                       core.log("action", "facedir: " .. newnode.param2)
+                       log("info", "facedir: " .. newnode.param2)
                end
        end
 
@@ -331,18 +356,29 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2)
        -- Check if the node is attached and if it can be placed there
        if core.get_item_group(def.name, "attached_node") ~= 0 and
                not builtin_shared.check_attached_node(place_to, newnode) then
-               core.log("action", "attached node " .. def.name ..
+               log("action", "attached node " .. def.name ..
                        " can not be placed at " .. core.pos_to_string(place_to))
-               return itemstack, false
+               return itemstack, nil
        end
 
+       log("action", playername .. " places node "
+               .. def.name .. " at " .. core.pos_to_string(place_to))
+
        -- Add node and update
        core.add_node(place_to, newnode)
 
+       -- Play sound if it was done by a player
+       if playername ~= "" and def.sounds and def.sounds.place then
+               core.sound_play(def.sounds.place, {
+                       pos = place_to,
+                       exclude_player = playername,
+               }, true)
+       end
+
        local take_item = true
 
        -- Run callback
-       if def.after_place_node then
+       if def.after_place_node and not prevent_after_place then
                -- Deepcopy place_to and pointed_thing because callback can modify it
                local place_to_copy = {x=place_to.x, y=place_to.y, z=place_to.z}
                local pointed_thing_copy = copy_pointed_thing(pointed_thing)
@@ -367,9 +403,10 @@ function core.item_place_node(itemstack, placer, pointed_thing, param2)
        if take_item then
                itemstack:take_item()
        end
-       return itemstack, true
+       return itemstack, place_to
 end
 
+-- deprecated, item_place does not call this
 function core.item_place_object(itemstack, placer, pointed_thing)
        local pos = core.get_pointed_thing_position(pointed_thing, true)
        if pos ~= nil then
@@ -387,14 +424,15 @@ function core.item_place(itemstack, placer, pointed_thing, param2)
                local nn = n.name
                if core.registered_nodes[nn] and core.registered_nodes[nn].on_rightclick then
                        return core.registered_nodes[nn].on_rightclick(pointed_thing.under, n,
-                                       placer, itemstack, pointed_thing) or itemstack, false
+                                       placer, itemstack, pointed_thing) or itemstack, nil
                end
        end
 
+       -- Place if node, otherwise do nothing
        if itemstack:get_definition().type == "node" then
                return core.item_place_node(itemstack, placer, pointed_thing, param2)
        end
-       return itemstack
+       return itemstack, nil
 end
 
 function core.item_secondary_use(itemstack, placer)
@@ -402,28 +440,24 @@ function core.item_secondary_use(itemstack, placer)
 end
 
 function core.item_drop(itemstack, dropper, pos)
-       if dropper and dropper:is_player() then
-               local v = dropper:get_look_dir()
-               local p = {x=pos.x, y=pos.y+1.2, z=pos.z}
-               local cs = itemstack:get_count()
-               if dropper:get_player_control().sneak then
-                       cs = 1
-               end
-               local item = itemstack:take_item(cs)
-               local obj = core.add_item(p, item)
-               if obj then
-                       v.x = v.x*2
-                       v.y = v.y*2 + 2
-                       v.z = v.z*2
-                       obj:setvelocity(v)
+       local dropper_is_player = dropper and dropper:is_player()
+       local p = table.copy(pos)
+       local cnt = itemstack:get_count()
+       if dropper_is_player then
+               p.y = p.y + 1.2
+       end
+       local item = itemstack:take_item(cnt)
+       local obj = core.add_item(p, item)
+       if obj then
+               if dropper_is_player then
+                       local dir = dropper:get_look_dir()
+                       dir.x = dir.x * 2.9
+                       dir.y = dir.y * 2.9 + 2
+                       dir.z = dir.z * 2.9
+                       obj:set_velocity(dir)
                        obj:get_luaentity().dropped_by = dropper:get_player_name()
-                       return itemstack
-               end
-
-       else
-               if core.add_item(pos, itemstack) then
-                       return itemstack
                end
+               return itemstack
        end
        -- If we reach this, adding the object to the
        -- environment failed
@@ -436,18 +470,27 @@ function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed
                        return result
                end
        end
+       local def = itemstack:get_definition()
        if itemstack:take_item() ~= nil then
                user:set_hp(user:get_hp() + hp_change)
 
+               if def and def.sound and def.sound.eat then
+                       core.sound_play(def.sound.eat, {
+                               pos = user:get_pos(),
+                               max_hear_distance = 16
+                       }, true)
+               end
+
                if replace_with_item then
                        if itemstack:is_empty() then
                                itemstack:add_item(replace_with_item)
                        else
                                local inv = user:get_inventory()
-                               if inv:room_for_item("main", {name=replace_with_item}) then
+                               -- Check if inv is null, since non-players don't have one
+                               if inv and inv:room_for_item("main", {name=replace_with_item}) then
                                        inv:add_item("main", replace_with_item)
                                else
-                                       local pos = user:getpos()
+                                       local pos = user:get_pos()
                                        pos.y = math.floor(pos.y + 0.5)
                                        core.add_item(pos, replace_with_item)
                                end
@@ -459,7 +502,9 @@ end
 
 function core.item_eat(hp_change, replace_with_item)
        return function(itemstack, user, pointed_thing)  -- closure
-               return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
+               if user then
+                       return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
+               end
        end
 end
 
@@ -476,63 +521,93 @@ end
 
 function core.handle_node_drops(pos, drops, digger)
        -- Add dropped items to object's inventory
-       if digger:get_inventory() then
-               local _, dropped_item
-               for _, dropped_item in ipairs(drops) do
-                       local left = digger:get_inventory():add_item("main", dropped_item)
-                       if not left:is_empty() then
-                               local p = {
-                                       x = pos.x + math.random()/2-0.25,
-                                       y = pos.y + math.random()/2-0.25,
-                                       z = pos.z + math.random()/2-0.25,
-                               }
-                               core.add_item(p, left)
-                       end
+       local inv = digger and digger:get_inventory()
+       local give_item
+       if inv then
+               give_item = function(item)
+                       return inv:add_item("main", item)
+               end
+       else
+               give_item = function(item)
+                       -- itemstring to ItemStack for left:is_empty()
+                       return ItemStack(item)
+               end
+       end
+
+       for _, dropped_item in pairs(drops) do
+               local left = give_item(dropped_item)
+               if not left:is_empty() then
+                       local p = {
+                               x = pos.x + math.random()/2-0.25,
+                               y = pos.y + math.random()/2-0.25,
+                               z = pos.z + math.random()/2-0.25,
+                       }
+                       core.add_item(p, left)
                end
        end
 end
 
 function core.node_dig(pos, node, digger)
+       local diggername = user_name(digger)
+       local log = make_log(diggername)
        local def = core.registered_nodes[node.name]
        if def and (not def.diggable or
                        (def.can_dig and not def.can_dig(pos, digger))) then
-               core.log("info", digger:get_player_name() .. " tried to dig "
+               log("info", diggername .. " tried to dig "
                        .. node.name .. " which is not diggable "
                        .. core.pos_to_string(pos))
                return
        end
 
-       if core.is_protected(pos, digger:get_player_name()) and
-                       not minetest.check_player_privs(digger, "protection_bypass") then
-               core.log("action", digger:get_player_name()
+       if core.is_protected(pos, diggername) then
+               log("action", diggername
                                .. " tried to dig " .. node.name
                                .. " at protected position "
                                .. core.pos_to_string(pos))
-               core.record_protection_violation(pos, digger:get_player_name())
+               core.record_protection_violation(pos, diggername)
                return
        end
 
-       core.log('action', digger:get_player_name() .. " digs "
+       log('action', diggername .. " digs "
                .. node.name .. " at " .. core.pos_to_string(pos))
 
-       local wielded = digger:get_wielded_item()
-       local drops = core.get_node_drops(node, wielded:get_name())
+       local wielded = digger and digger:get_wielded_item()
+       local drops = core.get_node_drops(node, wielded and wielded:get_name())
 
-       local wdef = wielded:get_definition()
-       local tp = wielded:get_tool_capabilities()
-       local dp = core.get_dig_params(def and def.groups, tp)
-       if wdef and wdef.after_use then
-               wielded = wdef.after_use(wielded, digger, node, dp) or wielded
-       else
-               -- Wear out tool
-               if not core.settings:get_bool("creative_mode") then
-                       wielded:add_wear(dp.wear)
-                       if wielded:get_count() == 0 and wdef.sound and wdef.sound.breaks then
-                               core.sound_play(wdef.sound.breaks, {pos = pos, gain = 0.5})
+       if wielded then
+               local wdef = wielded:get_definition()
+               local tp = wielded:get_tool_capabilities()
+               local dp = core.get_dig_params(def and def.groups, tp)
+               if wdef and wdef.after_use then
+                       wielded = wdef.after_use(wielded, digger, node, dp) or wielded
+               else
+                       -- Wear out tool
+                       if not core.settings:get_bool("creative_mode") then
+                               wielded:add_wear(dp.wear)
+                               if wielded:get_count() == 0 and wdef.sound and wdef.sound.breaks then
+                                       core.sound_play(wdef.sound.breaks, {
+                                               pos = pos,
+                                               gain = 0.5
+                                       }, true)
+                               end
                        end
                end
+               digger:set_wielded_item(wielded)
+       end
+
+       -- Check to see if metadata should be preserved.
+       if def and def.preserve_metadata then
+               local oldmeta = core.get_meta(pos):to_table().fields
+               -- Copy pos and node because the callback can modify them.
+               local pos_copy = {x=pos.x, y=pos.y, z=pos.z}
+               local node_copy = {name=node.name, param1=node.param1, param2=node.param2}
+               local drop_stacks = {}
+               for k, v in pairs(drops) do
+                       drop_stacks[k] = ItemStack(v)
+               end
+               drops = drop_stacks
+               def.preserve_metadata(pos_copy, node_copy, oldmeta, drops)
        end
-       digger:set_wielded_item(wielded)
 
        -- Handle drops
        core.handle_node_drops(pos, drops, digger)
@@ -545,6 +620,14 @@ function core.node_dig(pos, node, digger)
        -- Remove node and update
        core.remove_node(pos)
 
+       -- Play sound if it was done by a player
+       if diggername ~= "" and def and def.sounds and def.sounds.dug then
+               core.sound_play(def.sounds.dug, {
+                       pos = pos,
+                       exclude_player = diggername,
+               }, true)
+       end
+
        -- Run callback
        if def and def.after_dig_node then
                -- Copy pos and node because callback can modify them
@@ -554,15 +637,10 @@ function core.node_dig(pos, node, digger)
        end
 
        -- Run script hook
-       local _, callback
        for _, callback in ipairs(core.registered_on_dignodes) do
                local origin = core.callback_origins[callback]
                if origin then
                        core.set_last_run_mod(origin.mod)
-                       --print("Running " .. tostring(callback) ..
-                       --      " (a " .. origin.name .. " callback in " .. origin.mod .. ")")
-               else
-                       --print("No data associated with callback")
                end
 
                -- Copy pos and node because callback can modify them
@@ -597,6 +675,8 @@ end
 -- Item definition defaults
 --
 
+local default_stack_max = tonumber(minetest.settings:get("default_stack_max")) or 99
+
 core.nodedef_default = {
        -- Item properties
        type="node",
@@ -606,7 +686,7 @@ core.nodedef_default = {
        inventory_image = "",
        wield_image = "",
        wield_scale = {x=1,y=1,z=1},
-       stack_max = 99,
+       stack_max = default_stack_max,
        usable = false,
        liquids_pointable = false,
        tool_capabilities = nil,
@@ -670,7 +750,7 @@ core.craftitemdef_default = {
        inventory_image = "",
        wield_image = "",
        wield_scale = {x=1,y=1,z=1},
-       stack_max = 99,
+       stack_max = default_stack_max,
        liquids_pointable = false,
        tool_capabilities = nil,
 
@@ -708,7 +788,7 @@ core.noneitemdef_default = {  -- This is used for the hand and unknown items
        inventory_image = "",
        wield_image = "",
        wield_scale = {x=1,y=1,z=1},
-       stack_max = 99,
+       stack_max = default_stack_max,
        liquids_pointable = false,
        tool_capabilities = nil,