summaryrefslogtreecommitdiff
path: root/init.lua
blob: 1079f93b0a5ffe7f4b3a380c3ae51c0391bf8424 (plain)
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
local prefs = {}

local path = minetest.get_worldpath()..DIR_DELIM.."daynight.txt"

local file = io.open(path,"r")

if file then
	local data = file:read("*all")
	prefs = minetest.deserialize(data)
	file:close()
end

local function save()
	local file = io.open(path,"w")
	local data = minetest.serialize(prefs)
	file:write(data)
	file:close()
end

local form = "size[6,2.25]"..
	"label[2,-0.25;Select a day/night mode:]"..
	"image_button_exit[0,0.25;2,2;"..minetest.formspec_escape("[combine:128x128:16,16=ui_moon_icon.png:-16,-16=ui_sun_icon.png")..";normal;]"..
	"image_button_exit[2,0.25;2,2;ui_sun_icon.png;day;]"..
	"image_button_exit[4,0.25;2,2;ui_moon_icon.png;night;]"..
	"label[0.5,2;Automatic]"..
	"label[2.5,2;Always Day]"..
	"label[4.5,2;Always Night]"

minetest.register_chatcommand("dayratio",
	{
		params = "[ratio]",
		description = "Sets your day/night ratio to the given value, or opens a GUI if a value is not given",
		func = function(name,param)
			if param ~= "" and param ~= "nil" then
				if not tonumber(param) then
					return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Valid range is 0-1 or nil."
				elseif tonumber(param) > 1 then
					return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Value must be nil or <= 1."
				elseif tonumber(param) < 0 then
					return false,minetest.colorize("#FF0000","ERROR")..": Invalid value. Only positive values are allowed."
				end
			end
			local player = minetest.get_player_by_name(name)
			if param == "nil" then
				prefs[name] = nil
				player:override_day_night_ratio(nil)
				return true,minetest.colorize("#00FFFF","Success")..": Day/night ratio override cleared."
			elseif tonumber(param) then
				prefs[name] = tonumber(param)
				player:override_day_night_ratio(tonumber(param))
				return true,minetest.colorize("#00FFFF","Success")..": Day/night ratio set to "..param
			else
				minetest.show_formspec(name,"daynight:daynight",form)
			end
			save()
		end
	}
)

minetest.register_on_player_receive_fields(
	function(player,formname,fields)
		local name = player:get_player_name()
		if formname ~= "daynight:daynight" then
			return false
		end
		if fields.normal then
			prefs[name] = nil
			player:override_day_night_ratio(nil)
		elseif fields.day then
			prefs[name] = 1
			player:override_day_night_ratio(1)
		elseif fields.night then
			prefs[name] = 0
			player:override_day_night_ratio(0)
		end
		save()
	end
)

minetest.register_on_joinplayer(
	function(player)
		local name = player:get_player_name()
		player:override_day_night_ratio(prefs[name])
	end
)

unified_inventory.register_button("daynight",
	{
		action = function(player)
			minetest.show_formspec(player:get_player_name(),"daynight:daynight",form)
		end,
		tooltip = "Day/Night Options",
		type = "image",
		image = "[combine:128x128:16,16=ui_moon_icon.png:-16,-16=ui_sun_icon.png"
	}
)