Skip to content

Commit 71ea0c6

Browse files
authoredMay 11, 2021
Attachments: Proper data cleanup in callbacks (#2865)
1 parent 642fde4 commit 71ea0c6

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed
 

‎mods/boats/init.lua

+13-16
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,24 @@ function boat.on_rightclick(self, clicker)
5353
end
5454
local name = clicker:get_player_name()
5555
if self.driver and name == self.driver then
56-
self.driver = nil
57-
self.auto = false
56+
-- Cleanup happens in boat.on_detach_child
5857
clicker:set_detach()
59-
player_api.player_attached[name] = false
60-
player_api.set_animation(clicker, "stand" , 30)
58+
59+
player_api.set_animation(clicker, "stand", 30)
6160
local pos = clicker:get_pos()
6261
pos = {x = pos.x, y = pos.y + 0.2, z = pos.z}
6362
minetest.after(0.1, function()
6463
clicker:set_pos(pos)
6564
end)
6665
elseif not self.driver then
67-
local attach = clicker:get_attach()
68-
if attach and attach:get_luaentity() then
69-
local luaentity = attach:get_luaentity()
70-
if luaentity.driver then
71-
luaentity.driver = nil
72-
end
73-
clicker:set_detach()
74-
end
75-
self.driver = name
7666
clicker:set_attach(self.object, "",
7767
{x = 0.5, y = 1, z = -3}, {x = 0, y = 0, z = 0})
68+
69+
self.driver = name
7870
player_api.player_attached[name] = true
71+
7972
minetest.after(0.2, function()
80-
player_api.set_animation(clicker, "sit" , 30)
73+
player_api.set_animation(clicker, "sit", 30)
8174
end)
8275
clicker:set_look_horizontal(self.object:get_yaw())
8376
end
@@ -86,8 +79,12 @@ end
8679

8780
-- If driver leaves server while driving boat
8881
function boat.on_detach_child(self, child)
89-
self.driver = nil
90-
self.auto = false
82+
if child and child:get_player_name() == self.driver then
83+
player_api.player_attached[child:get_player_name()] = false
84+
85+
self.driver = nil
86+
self.auto = false
87+
end
9188
end
9289

9390

‎mods/carts/cart_entity.lua

+3-7
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,10 @@ function cart_entity:on_rightclick(clicker)
2929
end
3030
local player_name = clicker:get_player_name()
3131
if self.driver and player_name == self.driver then
32-
self.driver = nil
3332
carts:manage_attachment(clicker, nil)
3433
elseif not self.driver then
35-
self.driver = player_name
3634
carts:manage_attachment(clicker, self.object)
37-
38-
-- player_api does not update the animation
39-
-- when the player is attached, reset to default animation
40-
player_api.set_animation(clicker, "stand")
35+
self.driver = player_name
4136
end
4237
end
4338

@@ -66,8 +61,9 @@ end
6661
-- 0.5.x and later: When the driver leaves
6762
function cart_entity:on_detach_child(child)
6863
if child and child:get_player_name() == self.driver then
69-
self.driver = nil
64+
-- Clean up eye height
7065
carts:manage_attachment(child, nil)
66+
self.driver = nil
7167
end
7268
end
7369

‎mods/carts/functions.lua

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@ function carts:manage_attachment(player, obj)
1212
end
1313
local status = obj ~= nil
1414
local player_name = player:get_player_name()
15-
if player_api.player_attached[player_name] == status then
15+
if obj and player:get_attach() == obj then
1616
return
1717
end
1818
player_api.player_attached[player_name] = status
1919

2020
if status then
2121
player:set_attach(obj, "", {x=0, y=-4.5, z=0}, {x=0, y=0, z=0})
2222
player:set_eye_offset({x=0, y=-4, z=0},{x=0, y=-4, z=0})
23+
24+
-- player_api does not update the animation
25+
-- when the player is attached, reset to default animation
26+
player_api.set_animation(player, "stand")
2327
else
2428
player:set_detach()
2529
player:set_eye_offset({x=0, y=0, z=0},{x=0, y=0, z=0})

0 commit comments

Comments
 (0)
Please sign in to comment.