Skip to content

Commit

Permalink
Attachments: Proper data cleanup in callbacks (#2865)
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallJoker committed May 11, 2021
1 parent 642fde4 commit 71ea0c6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
29 changes: 13 additions & 16 deletions mods/boats/init.lua
Expand Up @@ -53,31 +53,24 @@ function boat.on_rightclick(self, clicker)
end end
local name = clicker:get_player_name() local name = clicker:get_player_name()
if self.driver and name == self.driver then if self.driver and name == self.driver then
self.driver = nil -- Cleanup happens in boat.on_detach_child
self.auto = false
clicker:set_detach() clicker:set_detach()
player_api.player_attached[name] = false
player_api.set_animation(clicker, "stand" , 30) player_api.set_animation(clicker, "stand", 30)
local pos = clicker:get_pos() local pos = clicker:get_pos()
pos = {x = pos.x, y = pos.y + 0.2, z = pos.z} pos = {x = pos.x, y = pos.y + 0.2, z = pos.z}
minetest.after(0.1, function() minetest.after(0.1, function()
clicker:set_pos(pos) clicker:set_pos(pos)
end) end)
elseif not self.driver then elseif not self.driver then
local attach = clicker:get_attach()
if attach and attach:get_luaentity() then
local luaentity = attach:get_luaentity()
if luaentity.driver then
luaentity.driver = nil
end
clicker:set_detach()
end
self.driver = name
clicker:set_attach(self.object, "", clicker:set_attach(self.object, "",
{x = 0.5, y = 1, z = -3}, {x = 0, y = 0, z = 0}) {x = 0.5, y = 1, z = -3}, {x = 0, y = 0, z = 0})

self.driver = name
player_api.player_attached[name] = true player_api.player_attached[name] = true

minetest.after(0.2, function() minetest.after(0.2, function()
player_api.set_animation(clicker, "sit" , 30) player_api.set_animation(clicker, "sit", 30)
end) end)
clicker:set_look_horizontal(self.object:get_yaw()) clicker:set_look_horizontal(self.object:get_yaw())
end end
Expand All @@ -86,8 +79,12 @@ end


-- If driver leaves server while driving boat -- If driver leaves server while driving boat
function boat.on_detach_child(self, child) function boat.on_detach_child(self, child)
self.driver = nil if child and child:get_player_name() == self.driver then
self.auto = false player_api.player_attached[child:get_player_name()] = false

self.driver = nil
self.auto = false
end
end end




Expand Down
10 changes: 3 additions & 7 deletions mods/carts/cart_entity.lua
Expand Up @@ -29,15 +29,10 @@ function cart_entity:on_rightclick(clicker)
end end
local player_name = clicker:get_player_name() local player_name = clicker:get_player_name()
if self.driver and player_name == self.driver then if self.driver and player_name == self.driver then
self.driver = nil
carts:manage_attachment(clicker, nil) carts:manage_attachment(clicker, nil)
elseif not self.driver then elseif not self.driver then
self.driver = player_name
carts:manage_attachment(clicker, self.object) carts:manage_attachment(clicker, self.object)

self.driver = player_name
-- player_api does not update the animation
-- when the player is attached, reset to default animation
player_api.set_animation(clicker, "stand")
end end
end end


Expand Down Expand Up @@ -66,8 +61,9 @@ end
-- 0.5.x and later: When the driver leaves -- 0.5.x and later: When the driver leaves
function cart_entity:on_detach_child(child) function cart_entity:on_detach_child(child)
if child and child:get_player_name() == self.driver then if child and child:get_player_name() == self.driver then
self.driver = nil -- Clean up eye height
carts:manage_attachment(child, nil) carts:manage_attachment(child, nil)
self.driver = nil
end end
end end


Expand Down
6 changes: 5 additions & 1 deletion mods/carts/functions.lua
Expand Up @@ -12,14 +12,18 @@ function carts:manage_attachment(player, obj)
end end
local status = obj ~= nil local status = obj ~= nil
local player_name = player:get_player_name() local player_name = player:get_player_name()
if player_api.player_attached[player_name] == status then if obj and player:get_attach() == obj then
return return
end end
player_api.player_attached[player_name] = status player_api.player_attached[player_name] = status


if status then if status then
player:set_attach(obj, "", {x=0, y=-4.5, z=0}, {x=0, y=0, z=0}) player:set_attach(obj, "", {x=0, y=-4.5, z=0}, {x=0, y=0, z=0})
player:set_eye_offset({x=0, y=-4, z=0},{x=0, y=-4, z=0}) player:set_eye_offset({x=0, y=-4, z=0},{x=0, y=-4, z=0})

-- player_api does not update the animation
-- when the player is attached, reset to default animation
player_api.set_animation(player, "stand")
else else
player:set_detach() player:set_detach()
player:set_eye_offset({x=0, y=0, z=0},{x=0, y=0, z=0}) player:set_eye_offset({x=0, y=0, z=0},{x=0, y=0, z=0})
Expand Down

0 comments on commit 71ea0c6

Please sign in to comment.