forked from PilzAdam/beds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.lua
295 lines (252 loc) · 7.31 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
-- Table of players currently in bed
-- key = player name, value = the bed node position
local players_in_bed = {}
local beds_list = {
{ "Red Bed", "red"},
{ "Orange Bed", "orange"},
{ "Yellow Bed", "yellow"},
{ "Green Bed", "green"},
{ "Blue Bed", "blue"},
{ "Violet Bed", "violet"},
{ "Black Bed", "black"},
{ "Grey Bed", "grey"},
{ "White Bed", "white"},
}
local remove_from_bed = function(player)
local playername = player:get_player_name()
if players_in_bed[playername] then
local meta = minetest.get_meta(players_in_bed[playername])
meta:set_string("player", "")
players_in_bed[player] = nil
player:setpos(beds_player_spawns[playername])
if health then
health.set_attr(playername, "asleep", 0)
else
player:set_physics_override(1, 1, 1)
end
end
end
for i in ipairs(beds_list) do
local beddesc = beds_list[i][1]
local colour = beds_list[i][2]
minetest.register_node("beds:bed_bottom_"..colour, {
description = beddesc,
drawtype = "nodebox",
tiles = {"beds_bed_top_bottom_"..colour..".png", "default_wood.png", "beds_bed_side_"..colour..".png", "beds_bed_side_"..colour..".png", "beds_bed_side_"..colour..".png", "beds_bed_side_"..colour..".png"},
paramtype = "light",
paramtype2 = "facedir",
stack_max = 1,
groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
sounds = default.node_sound_wood_defaults(),
node_box = {
type = "fixed",
fixed = {
-- bed
{-0.5, 0.0, -0.5, 0.5, 0.3125, 0.5},
-- legs
{-0.5, -0.5, -0.5, -0.4, 0.0, -0.4},
{0.4, 0.0, -0.4, 0.5, -0.5, -0.5},
}
},
selection_box = {
type = "fixed",
fixed = {
{-0.5, -0.5, -0.5, 0.5, 0.3125, 1.5},
}
},
after_place_node = function(pos, placer, itemstack)
local node = minetest.env:get_node(pos)
local p = {x=pos.x, y=pos.y, z=pos.z}
local param2 = node.param2
node.name = "beds:bed_top_"..colour
if param2 == 0 then
pos.z = pos.z+1
elseif param2 == 1 then
pos.x = pos.x+1
elseif param2 == 2 then
pos.z = pos.z-1
elseif param2 == 3 then
pos.x = pos.x-1
end
if minetest.registered_nodes[minetest.env:get_node(pos).name].buildable_to then
minetest.env:set_node(pos, node)
else
minetest.env:remove_node(p)
return true
end
end,
on_destruct = function(pos)
local node = minetest.env:get_node(pos)
-- If there's a player in a destroyed/dug bed, they need removing
for playername, bedpos in pairs(players_in_bed) do
if vector.equals(bedpos, pos) then
local player = minetest.get_player_by_name(playername)
if player then
remove_from_bed(player)
end
end
end
local param2 = node.param2
if param2 == 0 then
pos.z = pos.z+1
elseif param2 == 1 then
pos.x = pos.x+1
elseif param2 == 2 then
pos.z = pos.z-1
elseif param2 == 3 then
pos.x = pos.x-1
end
if( minetest.env:get_node({x=pos.x, y=pos.y, z=pos.z}).name == "beds:bed_top_"..colour ) then
if( minetest.env:get_node({x=pos.x, y=pos.y, z=pos.z}).param2 == param2 ) then
minetest.env:remove_node(pos)
end
end
end,
on_rightclick = function(pos, node, clicker)
if not clicker:is_player() then
return
end
local playername = clicker:get_player_name()
local meta = minetest.get_meta(pos)
local bedplayer = meta:get_string("player")
if playername == bedplayer then
remove_from_bed(clicker)
elseif bedplayer == "" then
-- Save the spawn position before we move the player into
-- the bed.
beds_player_spawns[playername] = clicker:getpos()
local file = io.open(minetest.get_worldpath().."/beds_player_spawns", "w")
if file then
file:write(minetest.serialize(beds_player_spawns))
file:close()
end
meta:set_string("player", playername)
players_in_bed[playername] = vector.new(pos)
if health then
health.set_attr(playername, "asleep", 1)
else
clicker:set_physics_override(0, 0, 0)
end
local param2 = node.param2
if param2 == 0 then
pos.z = pos.z+1
clicker:set_look_yaw(math.pi)
elseif param2 == 1 then
pos.x = pos.x+1
clicker:set_look_yaw(0.5*math.pi)
elseif param2 == 2 then
pos.z = pos.z-1
clicker:set_look_yaw(0)
elseif param2 == 3 then
pos.x = pos.x-1
clicker:set_look_yaw(1.5*math.pi)
end
pos.y = pos.y - 0.5
clicker:setpos(pos)
end
end
})
minetest.register_node("beds:bed_top_"..colour, {
drawtype = "nodebox",
tiles = {"beds_bed_top_top_"..colour..".png", "default_wood.png", "beds_bed_side_top_r_"..colour..".png", "beds_bed_side_top_l_"..colour..".png", "beds_bed_top_front.png", "beds_bed_side_"..colour..".png"},
paramtype = "light",
paramtype2 = "facedir",
groups = {snappy=1,choppy=2,oddly_breakable_by_hand=2,flammable=3},
sounds = default.node_sound_wood_defaults(),
node_box = {
type = "fixed",
fixed = {
-- bed
{-0.5, 0.0, -0.5, 0.5, 0.3125, 0.5},
{-0.4375, 0.3125, 0.1, 0.4375, 0.4375, 0.5},
-- legs
{-0.4, 0.0, 0.4, -0.5, -0.5, 0.5},
{0.5, -0.5, 0.5, 0.4, 0.0, 0.4},
}
},
selection_box = {
type = "fixed",
fixed = {
{0, 0, 0, 0, 0, 0},
}
},
})
minetest.register_alias("beds:bed_"..colour, "beds:bed_bottom_"..colour)
minetest.register_craft({
output = "beds:bed_"..colour,
recipe = {
{"wool:"..colour, "wool:"..colour, "wool:white", },
{"default:stick", "", "default:stick", }
}
})
minetest.register_craft({
output = "beds:bed_"..colour,
recipe = {
{"wool:white", "wool:"..colour, "wool:"..colour, },
{"default:stick", "", "default:stick", }
}
})
end
minetest.register_alias("beds:bed_bottom", "beds:bed_bottom_blue")
minetest.register_alias("beds:bed_top", "beds:bed_top_blue")
minetest.register_alias("beds:bed", "beds:bed_bottom_blue")
beds_player_spawns = {}
local file = io.open(minetest.get_worldpath().."/beds_player_spawns", "r")
if file then
beds_player_spawns = minetest.deserialize(file:read("*all"))
file:close()
end
local timer = 0
local wait = false
minetest.register_globalstep(function(dtime)
if wait then return end
if timer < 4 then
timer = timer+dtime
return
end
timer = 0
if minetest.env:get_timeofday() < 0.2 or minetest.env:get_timeofday() > 0.805 then
local players = minetest.get_connected_players()
-- Don't want to do this when nobody is online...
if #players > 0 then
allinbed = true
for _, player in pairs(players) do
if not players_in_bed[player:get_player_name()] then
allinbed = false
break
end
end
if allinbed then
minetest.chat_send_all("Good night!!!")
minetest.after(2, function()
minetest.env:set_timeofday(0.23)
wait = false
end)
wait = true
end
end
end
end)
minetest.register_on_shutdown(function(player)
local players = minetest.get_connected_players()
for _, player in pairs(players) do
if players_in_bed[player:get_player_name()] then
remove_from_bed(player)
end
end
end)
minetest.register_on_leaveplayer(function(player)
remove_from_bed(player)
end)
minetest.register_on_respawnplayer(function(player)
local playername = player:get_player_name()
remove_from_bed(player)
if beds_player_spawns[playername] then
player:setpos(beds_player_spawns[playername])
return true
end
return false
end)
if minetest.setting_get("log_mods") then
minetest.log("action", "beds loaded")
end