2017-01-09 02:02:23 +00:00
--[[
mcl_clock , renew of the renew of the watch mod
Original from Echo , here : http : // forum.minetest . net / viewtopic.php ? id = 3795
] ] --
2017-02-20 20:16:51 +00:00
mcl_clock = { }
2017-01-09 02:08:24 +00:00
local watch = { }
watch.old_time = - 1
-- Image of all 64 possible faces
watch.images = { }
for frame = 0 , 63 do
table.insert ( watch.images , " mcl_clock_clock.png^[verticalframe:64: " .. frame )
end
local function round ( num )
return math.floor ( num + 0.5 )
end
function watch . get_clock_frame ( )
local t = 64 * minetest.get_timeofday ( )
2017-02-19 20:11:49 +00:00
t = round ( t )
if t == 64 then t = 0 end
return tostring ( t )
2017-01-09 02:08:24 +00:00
end
-- Register items
function watch . register_item ( name , image , creative )
local g = 1
if creative then
g = 0
end
2017-03-02 18:53:53 +00:00
local doc = name == " mcl_clock:clock "
2017-03-02 20:55:25 +00:00
local longdesc , usagehelp
if doc then
longdesc = " Clocks are tools which shows the current time of day in the Overworld. "
usagehelp = " The clock contains a rotating disc with a sun symbol (yellow disc) and moon symbol and a little “pointer” which shows the current time of day by estimating the real position of the sun and the moon in the sky. Noon is represented by the sun symbol and midnight is represented by the moon symbol. "
end
2017-02-08 16:02:45 +00:00
minetest.register_craftitem ( name , {
2017-01-09 02:08:24 +00:00
description = " Clock " ,
2017-03-02 18:53:53 +00:00
_doc_items_create_entry = doc ,
2017-03-02 20:55:25 +00:00
_doc_items_longdesc = longdesc ,
_doc_items_usagehelp = usagehelp ,
2017-01-09 02:08:24 +00:00
inventory_image = image ,
2017-01-20 03:54:09 +00:00
groups = { not_in_creative_inventory = g , tool = 1 , clock = 1 } ,
2017-01-09 02:08:24 +00:00
wield_image = " " ,
2017-02-08 16:02:45 +00:00
stack_max = 64 ,
2017-01-09 02:08:24 +00:00
} )
end
2017-01-09 02:18:28 +00:00
-- This timer makes sure the clocks get updated from time to time regardless of time_speed,
-- just in case some clocks in the world go wrong
local force_clock_update_timer = 0
2017-01-09 02:08:24 +00:00
minetest.register_globalstep ( function ( dtime )
local now = watch.get_clock_frame ( )
2017-01-09 02:18:28 +00:00
force_clock_update_timer = force_clock_update_timer + dtime
2017-01-09 02:08:24 +00:00
2017-01-09 02:18:28 +00:00
if watch.old_time == now and force_clock_update_timer < 60 then
2017-01-09 02:08:24 +00:00
return
end
2017-01-09 02:18:28 +00:00
force_clock_update_timer = 0
2017-01-09 02:08:24 +00:00
watch.old_time = now
local players = minetest.get_connected_players ( )
for p , player in ipairs ( players ) do
for s , stack in ipairs ( player : get_inventory ( ) : get_list ( " main " ) ) do
2017-02-08 16:02:45 +00:00
local count = stack : get_count ( )
2017-01-09 02:08:24 +00:00
if stack : get_name ( ) == " mcl_clock:clock " then
2017-02-08 16:02:45 +00:00
player : get_inventory ( ) : set_stack ( " main " , s , " mcl_clock:clock_ " .. now .. " " .. count )
2017-01-09 02:08:24 +00:00
elseif string.sub ( stack : get_name ( ) , 1 , 16 ) == " mcl_clock:clock_ " then
2017-02-08 16:02:45 +00:00
player : get_inventory ( ) : set_stack ( " main " , s , " mcl_clock:clock_ " .. now .. " " .. count )
2017-01-09 02:08:24 +00:00
end
end
end
end )
2017-01-09 02:18:28 +00:00
-- Immediately set correct clock time after crafting
minetest.register_on_craft ( function ( itemstack )
if itemstack : get_name ( ) == " mcl_clock:clock " then
itemstack : set_name ( " mcl_clock:clock_ " .. watch.get_clock_frame ( ) )
end
end )
2017-01-09 02:08:24 +00:00
-- Clock recipe
minetest.register_craft ( {
output = ' mcl_clock:clock ' ,
recipe = {
2017-01-31 22:32:56 +00:00
{ ' ' , ' mcl_core:gold_ingot ' , ' ' } ,
{ ' mcl_core:gold_ingot ' , ' mesecons:redstone ' , ' mcl_core:gold_ingot ' } ,
{ ' ' , ' mcl_core:gold_ingot ' , ' ' }
2017-01-09 02:08:24 +00:00
}
} )
-- Clock tool
watch.register_item ( " mcl_clock:clock " , watch.images [ 1 ] , true )
-- Faces
for a = 0 , 63 , 1 do
local b = a
if b > 31 then
b = b - 32
else
b = b + 32
end
watch.register_item ( " mcl_clock:clock_ " .. tostring ( a ) , watch.images [ b + 1 ] , false )
end
2017-02-20 20:16:51 +00:00
mcl_clock.stereotype = " mcl_clock:clock "