nodecore-cd2025/mods/nc_items/throw_inertia.lua
Aaron Suen 34a2562964 Polyfill for player:get_velocity, clean up other hooks
This gets rid of the 5.4+ "deprecated get_player_velocity" warning
and modernizes the code to use get_velocity() everywhere, while
still remaining compatible with 5.3 for now (tested).  The polyfill
had to be on joinplayer because the nc_player_pickup auto-one-time
method of using after() won't reliably patch the player before some
other globalstep tries to read player velocity.

When MT 5.5 is released and 5.3 support ends, the polyfill just
needs to be removed to clean it up.

Also tidied up and consistentized the logging for other hooks.
2021-12-03 08:09:57 -05:00

35 lines
1001 B
Lua

-- LUALOCALS < ---------------------------------------------------------
local minetest, nodecore, vector
= minetest, nodecore, vector
-- LUALOCALS > ---------------------------------------------------------
local item_last_added
local old_add = minetest.add_item
function minetest.add_item(...)
local function helper(obj, ...)
item_last_added = obj
return obj, ...
end
return helper(old_add(...))
end
local old_drop = minetest.item_drop
function minetest.item_drop(itemstack, dropper, ...)
if not dropper or not dropper:is_player() then
return old_drop(itemstack, dropper, ...)
end
local function helper(...)
if item_last_added then
item_last_added:add_velocity(dropper:get_velocity())
local speed = vector.length(item_last_added:get_velocity())
local disc = {}
for i = 1, speed do disc["item_drop_speed_" .. i] = true end
nodecore.player_discover(dropper, disc)
end
return ...
end
item_last_added = nil
return helper(old_drop(itemstack, dropper, ...))
end