Skip to content

Commit 9b50b06

Browse files
PeterNerlichBuckarooBanzay
authored andcommitted
refactor for after_place_node instead of on_construct, [NOT TESTED]
1 parent d533505 commit 9b50b06

File tree

1 file changed

+19
-41
lines changed

1 file changed

+19
-41
lines changed

train.lua

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -34,55 +34,30 @@ minetest.register_node("mapserver:train", {
3434
groups = {cracky=3,oddly_breakable_by_hand=3},
3535
sounds = moditems.sound_glass(),
3636
can_dig = mapserver.can_interact,
37-
after_place_node = mapserver.after_place_node,
3837

39-
on_construct = function(pos)
38+
after_place_node = function(pos, placer, itemstack, pointed_thing)
4039
local meta = minetest.get_meta(pos)
4140

42-
local find_nearest_player = function(block_pos, radius)
43-
-- adapted from https://forum.minetest.net/viewtopic.php?t=23319
44-
45-
local closest_d = radius+1
46-
local closest_name
47-
48-
for i, obj in ipairs(minetest.get_objects_inside_radius(block_pos, radius)) do
49-
-- 0.4.x compatibility:
50-
--if obj:get_player_name() ~= "" then
51-
52-
-- 5.0.0+ method:
53-
if minetest.is_player(obj) then
54-
local distance = vector.distance(obj:get_pos(), block_pos)
55-
if distance < closest_d then
56-
closest_d = distance
57-
closest_name = obj:get_player_name()
58-
end
59-
end
60-
end
61-
62-
-- if 'closest_name' is nil, there's no player inside that radius
63-
return closest_name
64-
end
65-
66-
-- 10 should be the max possible interaction distance
67-
local name = find_nearest_player(pos, 10)
68-
6941
local last_index = 0
7042
local last_line = ""
7143
local last_color = ""
7244

73-
if name ~= nil then
74-
name = string.lower(name)
75-
if last_set_by[name] ~= nil then
76-
last_index = last_set_by[name].index + 5
77-
last_line = last_set_by[name].line
78-
last_color = last_set_by[name].color
79-
else
80-
last_set_by[name] = {}
81-
end
45+
if minetest.is_player(placer) then
46+
local name = placer:get_player_name()
47+
if name ~= nil then
48+
name = string.lower(name)
49+
if last_set_by[name] ~= nil then
50+
last_index = last_set_by[name].index + 5
51+
last_line = last_set_by[name].line
52+
last_color = last_set_by[name].color
53+
else
54+
last_set_by[name] = {}
55+
end
8256

83-
last_set_by[name].index = last_index
84-
last_set_by[name].line = last_line
85-
last_set_by[name].color = last_color
57+
last_set_by[name].index = last_index
58+
last_set_by[name].line = last_line
59+
last_set_by[name].color = last_color
60+
end
8661
end
8762

8863
meta:set_string("station", "")
@@ -91,6 +66,9 @@ minetest.register_node("mapserver:train", {
9166
meta:set_string("color", last_color)
9267

9368
update_formspec(meta)
69+
70+
71+
return mapserver.after_place_node(pos, placer, itemstack, pointed_thing)
9472
end,
9573

9674
on_receive_fields = function(pos, formname, fields, sender)

0 commit comments

Comments
 (0)