pvp_revamped/player_events.lua

141 lines
3.4 KiB
Lua
Raw Normal View History

2020-08-12 22:37:11 -07:00
local player_data = pvp_revamped.player_data
local player_persistent_data = pvp_revamped.player_persistent_data
2020-08-15 21:14:30 -07:00
local remove_text_center = pvp_revamped.remove_text_center
2020-08-12 22:37:11 -07:00
local drop = pvp_revamped.drop
2020-08-15 14:02:57 -07:00
local get_player_by_name = minetest.get_player_by_name
2020-08-12 22:37:11 -07:00
-- Create an empty data sheet for the player.
minetest.register_on_joinplayer(function(player)
player_persistent_data[player:get_player_name()] = {damage_texture_modifier = player:get_properties().damage_texture_modifier}
end)
-- Clear up memory if the player leaves.
-- Drop any item the player is about to throw on leave.
2020-08-13 14:29:19 -07:00
-- Remove the shield entity.
2020-08-12 22:37:11 -07:00
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
2020-08-13 14:29:19 -07:00
local pdata = player_data[name]
2020-08-12 22:37:11 -07:00
2020-08-13 14:29:19 -07:00
if not pdata then
2020-08-12 22:37:11 -07:00
return
end
2020-08-13 14:29:19 -07:00
local throw_data = pdata.throw
2020-08-12 22:37:11 -07:00
if throw_data then
drop(player, throw_data.item)
end
2020-08-13 14:29:19 -07:00
local entity = pdata.entity
if entity then
entity:remove()
end
2020-08-12 22:37:11 -07:00
player_data[name] = nil
player_persistent_data[name] = nil
end)
-- Drop any item the player is about to throw on death.
2020-08-13 14:29:19 -07:00
-- Remove the shield entity.
2020-08-12 22:37:11 -07:00
minetest.register_on_dieplayer(function(player)
local name = player:get_player_name()
2020-08-13 14:29:19 -07:00
local pdata = player_data[name]
2020-08-12 22:37:11 -07:00
2020-08-13 14:29:19 -07:00
if not pdata then
2020-08-12 22:37:11 -07:00
return
end
2020-08-13 14:29:19 -07:00
local throw_data = pdata.throw
2020-08-12 22:37:11 -07:00
if throw_data then
drop(player, throw_data.item)
2020-08-13 14:29:19 -07:00
pdata.throw = nil
2020-08-12 22:37:11 -07:00
end
2020-08-13 14:29:19 -07:00
local entity = pdata.entity
if entity then
entity:remove()
pdata.entity = nil
end
player_data[name] = pdata
2020-08-12 22:37:11 -07:00
end)
-- Drop any item the player is about to throw on shutdown.
2020-08-13 14:29:19 -07:00
-- Remove the shield entity.
2020-08-12 22:37:11 -07:00
minetest.register_on_shutdown(function()
for k, v in pairs(player_data) do
local throw_data = v.throw
if throw_data then
2020-08-15 14:02:57 -07:00
drop(get_player_by_name(k), throw_data.item)
2020-08-12 22:37:11 -07:00
end
end
end)
2020-08-15 21:14:30 -07:00
minetest.register_on_player_inventory_action(function(player)
local name = player:get_player_name()
if not player_data or not player_data[name] then
return
end
local pdata = player_data[name]
local data_shield = pdata.shield
if data_shield and not data_shield.armor_inv and player:get_wielded_item():get_name() ~= data_shield.name then
player_data[name].shield = nil
-- Remove un-used hud element.
remove_text_center(player, "pvp_revamped:shield_pool")
return
end
local data_block = pdata.block
if data_block and player:get_wielded_item():get_name() ~= data_block.name then
player_data[name].block = nil
-- Remove un-used hud element.
remove_text_center(player, "pvp_revamped:block_pool")
end
end)
2020-10-30 13:48:45 -07:00
local function break_guard(player)
local name = player:get_player_name()
if not player_data or not player_data[name] then
return
end
local pdata = player_data[name]
if pdata.shield then
player_data[name].shield = nil
-- Remove hud element.
remove_text_center(player, "pvp_revamped:shield_pool")
end
if pdata.block then
player_data[name].block = nil
-- Remove hud element.
remove_text_center(player, "pvp_revamped:block_pool")
end
end
minetest.register_on_placenode(function(pos, newnode, placer)
-- Break guard if player placed a node.
break_guard(placer)
end)
minetest.register_on_dignode(function(pos, oldnode, digger)
-- Break guard if player dug a node.
break_guard(digger)
end)