From c8af7c71e5e1753c5e06e7603ce4b5f11932c9ce Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 27 Nov 2024 23:08:26 +0100 Subject: [PATCH] avoid duplicate code save some lines and some extra method calls. --- init.lua | 11 ++++----- smartfs.lua | 65 ++++++++++++++++++----------------------------------- 2 files changed, 28 insertions(+), 48 deletions(-) diff --git a/init.lua b/init.lua index c894622..8ddd288 100644 --- a/init.lua +++ b/init.lua @@ -1,4 +1,5 @@ -dofile(minetest.get_modpath("banners").."/smartfs.lua") +local MP = core.get_modpath("banners") .. "/" +dofile(MP .. "smartfs.lua") banners = {} @@ -236,11 +237,11 @@ core.register_entity("banners:banner_ent", { on_activate = banners.banner_on_activate, }) - dofile(minetest.get_modpath("banners").."/factions.lua") if core.get_modpath("factions") then + dofile(MP .. "factions.lua") end -dofile(minetest.get_modpath("banners").."/items.lua") -dofile(minetest.get_modpath("banners").."/nodes.lua") -dofile(minetest.get_modpath("banners").."/crafts.lua") +dofile(MP .. "items.lua") +dofile(MP .. "nodes.lua") +dofile(MP .. "crafts.lua") diff --git a/smartfs.lua b/smartfs.lua index 1d78dfc..8829d30 100644 --- a/smartfs.lua +++ b/smartfs.lua @@ -377,29 +377,21 @@ end) smartfs.element("button", { build = function(self) + local common_prefix = self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," ..self.data.size.h + local common_sufix = ";" .. self.name + .. ";" .. self.data.value + .. "]" if self.data.img then return "image_button[" - .. self.data.pos.x .. "," .. self.data.pos.y - .. ";" .. self.data.size.w .. "," ..self.data.size.h + .. common_prefix .. ";" .. self.data.img - .. ";" .. self.name - .. ";" .. self.data.value - .. "]" + .. common_sufix else if self.data.closes then - return "button_exit[" - .. self.data.pos.x .. "," .. self.data.pos.y - .. ";" .. self.data.size.w .. "," ..self.data.size.h - .. ";" .. self.name - .. ";" .. self.data.value - .. "]" + return "button_exit[" .. common_prefix .. common_sufix else - return "button[" - .. self.data.pos.x .. "," .. self.data.pos.y - .. ";" .. self.data.size.w .. "," .. self.data.size.h - .. ";" .. self.name - .. ";" .. self.data.value - .. "]" + return "button[" .. common_prefix .. common_sufix end end end, @@ -517,27 +509,22 @@ smartfs.element("label", { smartfs.element("field", { build = function(self) + local common = self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. self.name + .. ";" .. self.data.label if self.data.ml then return "textarea[" - .. self.data.pos.x .. "," .. self.data.pos.y - .. ";" .. self.data.size.w .. "," .. self.data.size.h - .. ";" .. self.name - .. ";" .. self.data.label + .. common .. ";" .. self.data.value .. "]" elseif self.data.pwd then return "pwdfield[" - .. self.data.pos.x .. "," .. self.data.pos.y - .. ";" .. self.data.size.w .. "," .. self.data.size.h - .. ";" .. self.name - .. ";" .. self.data.label + .. common .. "]" else return "field[" - .. self.data.pos.x .. "," .. self.data.pos.y - .. ";" .. self.data.size.w .. "," .. self.data.size.h - .. ";" .. self.name - .. ";" .. self.data.label + .. common .. ";" .. self.data.value .. "]" end @@ -598,22 +585,14 @@ smartfs.element("image", { smartfs.element("checkbox", { build = function(self) + local out = "checkbox[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.name + .. ";" .. self.data.label if self.data.value then - return "checkbox[".. - self.data.pos.x..","..self.data.pos.y.. - ";".. - self.name.. - ";".. - self.data.label.. - ";true]" + return out .. ";true]" else - return "checkbox[".. - self.data.pos.x..","..self.data.pos.y.. - ";".. - self.name.. - ";".. - self.data.label.. - ";false]" + return out .. ";false]" end end, setPosition = function(self, x, y)