From 6d3bb38a5e1f08fb4f2fbb7ac90141b7630db281 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Mon, 9 Jan 2017 03:08:24 +0100 Subject: [PATCH] Refactor mcl_clock to only use one Lua file --- mods/mcl_clock/clock.lua | 59 ----------------------------- mods/mcl_clock/init.lua | 80 +++++++++++++++++++++++++++++++++++++++- mods/mcl_clock/items.lua | 25 ------------- 3 files changed, 78 insertions(+), 86 deletions(-) delete mode 100644 mods/mcl_clock/clock.lua delete mode 100644 mods/mcl_clock/items.lua diff --git a/mods/mcl_clock/clock.lua b/mods/mcl_clock/clock.lua deleted file mode 100644 index 9db741a66..000000000 --- a/mods/mcl_clock/clock.lua +++ /dev/null @@ -1,59 +0,0 @@ ---[[ - mcl_clock, renew of the renew of the watch mod - - Original from Echo, here: http://forum.minetest.net/viewtopic.php?id=3795 -]]-- - -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() - return tostring(round(t)) -end - --- Register items -function watch.register_item(name, image, creative) - local g = 1 - if creative then - g = 0 - end - minetest.register_tool(name, { - description = "Clock", - inventory_image = image, - groups = {not_in_creative_inventory=g, clock=1}, - wield_image = "", - stack_max = 1, - }) -end - -minetest.register_globalstep(function(dtime) - local now = watch.get_clock_frame() - - if watch.old_time == now then - return - end - - 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 - if stack:get_name() == "mcl_clock:clock" then - player:get_inventory():set_stack("main", s, "mcl_clock:clock_"..now) - elseif string.sub(stack:get_name(), 1, 16) == "mcl_clock:clock_" then - player:get_inventory():set_stack("main", s, "mcl_clock:clock_"..now) - end - end - end -end) diff --git a/mods/mcl_clock/init.lua b/mods/mcl_clock/init.lua index 38b9513b7..033afc4cf 100644 --- a/mods/mcl_clock/init.lua +++ b/mods/mcl_clock/init.lua @@ -4,5 +4,81 @@ Original from Echo, here: http://forum.minetest.net/viewtopic.php?id=3795 ]]-- -dofile(minetest.get_modpath("mcl_clock").."/clock.lua") -dofile(minetest.get_modpath("mcl_clock").."/items.lua") +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() + return tostring(round(t)) +end + +-- Register items +function watch.register_item(name, image, creative) + local g = 1 + if creative then + g = 0 + end + minetest.register_tool(name, { + description = "Clock", + inventory_image = image, + groups = {not_in_creative_inventory=g, clock=1}, + wield_image = "", + stack_max = 1, + }) +end + +minetest.register_globalstep(function(dtime) + local now = watch.get_clock_frame() + + if watch.old_time == now then + return + end + + 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 + if stack:get_name() == "mcl_clock:clock" then + player:get_inventory():set_stack("main", s, "mcl_clock:clock_"..now) + elseif string.sub(stack:get_name(), 1, 16) == "mcl_clock:clock_" then + player:get_inventory():set_stack("main", s, "mcl_clock:clock_"..now) + end + end + end +end) +-- Clock recipe +minetest.register_craft({ + description = "Clock", + output = 'mcl_clock:clock', + groups = {not_in_creative_inventory=1}, + recipe = { + {'', 'default:gold_ingot', ''}, + {'default:gold_ingot', 'mesecons:redstone_dust', 'default:gold_ingot'}, + {'', 'default:gold_ingot', ''} + } +}) + +-- 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 diff --git a/mods/mcl_clock/items.lua b/mods/mcl_clock/items.lua deleted file mode 100644 index 80a3a76bc..000000000 --- a/mods/mcl_clock/items.lua +++ /dev/null @@ -1,25 +0,0 @@ --- Clock recipe -minetest.register_craft({ - description = "Clock", - output = 'mcl_clock:clock', - groups = {not_in_creative_inventory=1}, - recipe = { - {'', 'default:gold_ingot', ''}, - {'default:gold_ingot', 'mesecons:redstone_dust', 'default:gold_ingot'}, - {'', 'default:gold_ingot', ''} - } -}) - --- 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