From c8012c72663b83b15c4681b32499268371ff7806 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 27 Nov 2024 22:32:24 +0100 Subject: [PATCH 01/39] whitespace and minetest. -> core. - use same whitespace style throughout and fix some indents. - change from minetest. to core. namespace. --- crafts.lua | 121 ++++++------- factions.lua | 215 +++++++++++----------- init.lua | 82 +++++---- items.lua | 73 ++++---- nodes.lua | 100 +++++------ smartfs.lua | 493 +++++++++++++++++++++++++++------------------------ 6 files changed, 545 insertions(+), 539 deletions(-) diff --git a/crafts.lua b/crafts.lua index dadaebc..8f6aa89 100644 --- a/crafts.lua +++ b/crafts.lua @@ -1,78 +1,73 @@ -- craft recipes -minetest.register_craft( -- wooden flag pole - { - output = "banners:wooden_pole 1", - recipe = { - {"", "", "default:stick"}, - {"", "default:stick", ""}, - {"default:stick", "", ""} - } + +-- wooden flag pole +core.register_craft({ + output = "banners:wooden_pole 1", + recipe = { + { "", "", "default:stick" }, + { "", "default:stick", "" }, + { "default:stick", "", "" } } -) +}) -minetest.register_craft( -- steel flag pole - { - output = "banners:steel_pole 1", - recipe = { - {"", "", "default:steel_ingot"}, - {"default:stick", "default:steel_ingot", "default:stick"}, - {"default:steel_ingot", "", ""} - } +-- steel flag pole +core.register_craft({ + output = "banners:steel_pole 1", + recipe = { + { "", "", "default:steel_ingot" }, + { "default:stick", "default:steel_ingot", "default:stick" }, + { "default:steel_ingot", "", "" } } -) +}) -minetest.register_craft( -- wooden flag support base - { - output = "banners:wooden_base 1", - recipe = { - {"", "default:stick", ""}, - {"default:stick", "", "default:stick"}, - {"group:wood", "group:wood", "group:wood"} - } +-- wooden flag support base +core.register_craft({ + output = "banners:wooden_base 1", + recipe = { + { "", "default:stick", "" }, + { "default:stick", "", "default:stick" }, + { "group:wood", "group:wood", "group:wood" } } -) +}) -minetest.register_craft( -- steel support - { - output = "banners:steel_base", - recipe = { - {"", "default:steel_ingot", ""}, - {"default:steel_ingot", "", "default:steel_ingot"}, - {"", "default:steelblock", ""} - } +-- steel support +core.register_craft({ + output = "banners:steel_base", + recipe = { + { "", "default:steel_ingot", "" }, + { "default:steel_ingot", "", "default:steel_ingot" }, + { "", "default:steelblock", "" } } -) +}) -minetest.register_craft( -- banner sheet - { - output = "banners:banner_sheet 1", - recipe = { - {"", "", ""}, - {"farming:cotton", "farming:cotton", "farming:cotton"}, - {"farming:cotton", "farming:cotton", "farming:cotton"} - } +-- banner sheet +core.register_craft({ + output = "banners:banner_sheet 1", + recipe = { + { "", "", "" }, + { "farming:cotton", "farming:cotton", "farming:cotton" }, + { "farming:cotton", "farming:cotton", "farming:cotton" } } -) +}) -minetest.register_craft( -- wooden support - { - output = "banners:wooden_banner 1", - recipe = { - {"", "banners:banner_sheet", ""}, - {"", "banners:wooden_pole", ""}, - {"", "banners:wooden_base", ""} - } +-- wooden support +core.register_craft({ + output = "banners:wooden_banner 1", + recipe = { + { "", "banners:banner_sheet", "" }, + { "", "banners:wooden_pole", "" }, + { "", "banners:wooden_base", "" } } -) +}) -minetest.register_craft( -- steel support - { - output = "banners:steel_banner 1", - recipe = { - {"", "banners:banner_sheet", ""}, - {"", "banners:steel_pole", ""}, - {"", "banners:steel_base", ""} - } +-- steel support +core.register_craft({ + output = "banners:steel_banner 1", + recipe = { + { "", "banners:banner_sheet", "" }, + { "", "banners:steel_pole", "" }, + { "", "banners:steel_base", "" } } -) +}) + diff --git a/factions.lua b/factions.lua index 9a327ae..fbb453a 100644 --- a/factions.lua +++ b/factions.lua @@ -1,43 +1,43 @@ banners.power_per_banner = 10. -- items -minetest.register_craftitem("banners:golden_finial", { +core.register_craftitem("banners:golden_finial", { groups = {}, description = "Golden finial", inventory_image = "gold_finial.png", }) -minetest.register_craftitem("banners:silver_pole", { +core.register_craftitem("banners:silver_pole", { groups = {}, description = "Silver pole", inventory_image = "silver_pole.png" }) -minetest.register_craftitem("banners:power_pole", { +core.register_craftitem("banners:power_pole", { groups = {}, description = "Power pole", inventory_image = "power_pole.png" }) -minetest.register_craftitem("banners:golden_sheet", { +core.register_craftitem("banners:golden_sheet", { groups = {}, description = "Golden sheet", inventory_image = "golden_sheet.png" }) -minetest.register_craftitem("banners:death_pole", { +core.register_craftitem("banners:death_pole", { groups = {}, description = "Death pole", inventory_image = "death_pole.png" }) -minetest.register_craftitem("banners:death_sheet", { +core.register_craftitem("banners:death_sheet", { groups = {}, description = "Death sheet", inventory_image = "death_sheet.png" }) -minetest.register_craftitem("banners:death_base", { +core.register_craftitem("banners:death_base", { groups = {}, description = "Death base", inventory_image = "death_base.png" @@ -46,107 +46,98 @@ minetest.register_craftitem("banners:death_base", { -- crafts -minetest.register_craft( -- silver flag pole - { - output = "banners:silver_pole 1", - recipe = { - {"", "", "moreores:silver_ingot"}, - {"", "moreores:silver_ingot", ""}, - {"moreores:silver_ingot", "", ""} - } +-- silver flag pole +core.register_craft({ + output = "banners:silver_pole 1", + recipe = { + { "", "", "moreores:silver_ingot" }, + { "", "moreores:silver_ingot", "" }, + { "moreores:silver_ingot", "", "" } } -) - -minetest.register_craft( -- death flag pole - { - output = "banners:death_pole 1", - recipe = { - {"", "", "default:diamond"}, - {"", "default:obsidian", ""}, - {"default:obsidian", "", ""} - } - } -) - -minetest.register_craft( -- golden finial - { - output = "banners:golden_finial", - recipe = { - {"", "default:gold_ingot", "default:gold_ingot"}, - {"", "default:gold_ingot", "default:gold_ingot"}, - {"default:gold_ingot", "", ""} - } - } -) - -minetest.register_craft( -- power flag pole - { - output = "banners:power_pole 1", - recipe = { - {"", "", ""}, - {"", "banners:golden_finial", ""}, - {"banners:silver_pole", "", ""} - } +}) + +-- death flag pole +core.register_craft({ + output = "banners:death_pole 1", + recipe = { + { "", "", "default:diamond" }, + { "", "default:obsidian", "" }, + { "default:obsidian", "", "" } } -) +}) -minetest.register_craft( -- golden sheet - { - output = "banners:golden_sheet 1", - type = "shapeless", - recipe = { "default:gold_ingot", "banners:banner_sheet"} +-- golden finial +core.register_craft({ + output = "banners:golden_finial", + recipe = { + { "", "default:gold_ingot", "default:gold_ingot" }, + { "", "default:gold_ingot", "default:gold_ingot" }, + { "default:gold_ingot", "", "" } } -) +}) -minetest.register_craft( -- death sheet - { - output = "banners:death_sheet 1", - type = "shapeless", - recipe = { "default:obsidian", "banners:banner_sheet"} +-- power flag pole +core.register_craft({ + output = "banners:power_pole 1", + recipe = { + { "", "", "" }, + { "", "banners:golden_finial", "" }, + { "banners:silver_pole", "", "" } } -) - -minetest.register_craft( -- death sheet - { - output = "banners:death_base 1", - recipe = { - {"", "", ""}, - {"", "banners:steel_base", ""}, - {"default:obsidian", "default:obsidian", "default:obsidian"} - } +}) + +-- golden sheet +core.register_craft({ + output = "banners:golden_sheet 1", + type = "shapeless", + recipe = { "default:gold_ingot", "banners:banner_sheet" } +}) + +-- death sheet +core.register_craft({ + output = "banners:death_sheet 1", + type = "shapeless", + recipe = { "default:obsidian", "banners:banner_sheet" } +}) + +-- death sheet +core.register_craft({ + output = "banners:death_base 1", + recipe = { + { "", "", "" }, + { "", "banners:steel_base", "" }, + { "default:obsidian", "default:obsidian", "default:obsidian" } } -) - -minetest.register_craft( -- power banner - { - output = "banners:power_banner", - recipe = { - {"", "banners:golden_sheet", ""}, - {"", "banners:power_pole", ""}, - {"", "banners:steel_base", ""} - } +}) + +-- power banner +core.register_craft({ + output = "banners:power_banner", + recipe = { + { "", "banners:golden_sheet", "" }, + { "", "banners:power_pole", "" }, + { "", "banners:steel_base", "" } } -) - -minetest.register_craft( -- death banner - { - output = "banners:death_banner", - recipe = { - {"", "banners:death_sheet", ""}, - {"", "banners:death_pole", ""}, - {"", "banners:death_base", ""} - } +}) + +-- death banner +core.register_craft({ + output = "banners:death_banner", + recipe = { + { "", "banners:death_sheet", "" }, + { "", "banners:death_pole", "" }, + { "", "banners:death_base", "" } } -) +}) -- nodes -minetest.register_node("banners:power_banner", { +core.register_node("banners:power_banner", { drawtype = "mesh", mesh = "banner_support.x", - tiles = {"gold_support.png"}, + tiles = { "gold_support.png" }, description = "Power Banner", - groups = {cracky=3}, + groups = { cracky = 3 }, is_ground_content = false, diggable = true, stack_max = 1, @@ -154,15 +145,15 @@ minetest.register_node("banners:power_banner", { paramtype2 = "facedir", after_place_node = function (pos, player, itemstack, pointed_thing) banners.after_powerbanner_placed(pos, player, itemstack, pointed_thing) - end, + end, on_destruct = function(pos) banners.banner_on_destruct(pos) end, on_dig = function(pos, n, p) - if minetest.is_protected(pos, p:get_player_name()) then + if core.is_protected(pos, p:get_player_name()) then return end - local meta = minetest.get_meta(pos) + local meta = core.get_meta(pos) local facname = meta:get_string("faction") if facname then local faction = factions.factions[facname] @@ -174,12 +165,12 @@ minetest.register_node("banners:power_banner", { end, }) -minetest.register_node("banners:death_banner", { +core.register_node("banners:death_banner", { drawtype = "mesh", mesh = "banner_support.x", - tiles = {"death_uv.png"}, + tiles = { "death_uv.png" }, description = "Death Banner", - groups = {cracky=3}, + groups = { cracky = 3 }, is_ground_content = false, diggable = true, stack_max = 1, @@ -187,15 +178,15 @@ minetest.register_node("banners:death_banner", { paramtype2 = "facedir", after_place_node = function (pos, player, itemstack, pointed_thing) banners.after_deathbanner_placed(pos, player, itemstack, pointed_thing) - end, + end, on_destruct = function(pos) banners.banner_on_destruct(pos) end, on_dig = function(pos, n, p) - if minetest.is_protected(pos, p:get_player_name()) then + if core.is_protected(pos, p:get_player_name()) then return end - local meta = minetest.get_meta(pos) + local meta = core.get_meta(pos) local defending_facname = meta:get_string("faction") local parcelpos = factions.get_parcel_pos(pos) if defending_facname then @@ -204,34 +195,34 @@ minetest.register_node("banners:death_banner", { faction:stop_attack(parcelpos) end end - minetest.remove_node(pos) + core.remove_node(pos) end, }) banners.after_powerbanner_placed = function(pos, player, itemstack, pointed_thing) - minetest.get_node(pos).param2 = banners.determine_flag_direction(pos, pointed_thing) + core.get_node(pos).param2 = banners.determine_flag_direction(pos, pointed_thing) local faction = factions.players[player:get_player_name()] if not faction then - minetest.get_meta(pos):set_string("banner", "bg_white.png") + core.get_meta(pos):set_string("banner", "bg_white.png") else local banner_string = factions.factions[faction].banner - minetest.get_meta(pos):set_string("banner", banner_string) - minetest.get_meta(pos):set_string("faction", faction) + core.get_meta(pos):set_string("banner", banner_string) + core.get_meta(pos):set_string("faction", faction) factions.factions[faction]:increase_maxpower(banners.power_per_banner) end - minetest.add_entity(pos, "banners:banner_ent") + core.add_entity(pos, "banners:banner_ent") end banners.after_deathbanner_placed = function(pos, player, itemstack, pointed_thing) - minetest.get_node(pos).param2 = banners.determine_flag_direction(pos, pointed_thing) + core.get_node(pos).param2 = banners.determine_flag_direction(pos, pointed_thing) local attacking_faction = factions.players[player:get_player_name()] if attacking_faction then local parcelpos = factions.get_parcel_pos(pos) attacking_faction = factions.factions[attacking_faction] attacking_faction:attack_parcel(parcelpos) - minetest.get_meta(pos):set_string("faction", attacking_faction.name) + core.get_meta(pos):set_string("faction", attacking_faction.name) end - minetest.get_meta(pos):set_string("banner", "death_uv.png") - minetest.add_entity(pos, "banners:banner_ent") + core.get_meta(pos):set_string("banner", "death_uv.png") + core.add_entity(pos, "banners:banner_ent") end diff --git a/init.lua b/init.lua index a080bf7..8be2917 100644 --- a/init.lua +++ b/init.lua @@ -35,13 +35,15 @@ banners.colors = { "brown", "darkbrown" } -banners.base_transform = ({texture = "bg_white.png", - mask="mask_background.png"}) +banners.base_transform = { + texture = "bg_white.png", + mask = "mask_background.png" +} banners.creation_form_func = function(state) -- helper functions state.update_player_inv = function(self) - local player = minetest.get_player_by_name(self.player) + local player = core.get_player_by_name(self.player) local newbanner = player:get_wielded_item() newbanner:get_meta():set_string("", state.banner:get_transform_string()) player:set_wielded_item(newbanner) @@ -58,7 +60,7 @@ banners.creation_form_func = function(state) state.banner = banners.Banner:new(nil) state.banner:push_transform(banners.base_transform) state.current_color = "bg_white.png" - state:size(20,10) + state:size(20, 10) state:image(3, 0.4, 4, 2, "banner_preview", nil) state:image(2.4, 0.8, 0.7, 0.7, "color_indicator", state.current_color) state:update_all() @@ -68,25 +70,24 @@ banners.creation_form_func = function(state) if #state.banner.transforms > 1 then state.banner:pop_transform() state:update_all() - end - end) + end + end) -- delete button state:button(0.5, 1.3, 2, 1, "delete", "Delete"):click(function(self, state) state.banner.transforms = {banners.base_transform} state:update_all() - end) + end) -- add banners colors local x = 7 local y = .3 - for i in ipairs(banners.colors) do + for i in ipairs(banners.colors) do local b = state:button(x, y, 1, 1, banners.colors[i], "") - b:setImage("bg_"..banners.colors[i]..".png") b:click(function(self, state) state.current_color = "bg_"..self.name..".png" state:update_preview() - -- todo: update masks or something - end - ) + b:setImage("bg_" .. banners.colors[i] .. ".png") + -- todo: update masks or something + end) x = x + 1 if x > 19 then y = y + 1 @@ -98,12 +99,13 @@ banners.creation_form_func = function(state) local y = 3 for i in ipairs(banners.masks) do local b = state:button(x, y, 2, 1, banners.masks[i], "") - b:setImage(banners.masks[i]..".png") b:click(function(self, state) state.banner:push_transform({texture=state.current_color, mask=self.name..".png"}) state:update_all() end ) + b:setImage(banners.masks[i] .. ".png") + end) x = x + 2 if x > 17.5 then y = y + 1 @@ -114,7 +116,7 @@ banners.creation_form_func = function(state) end banners.creation_form = smartfs.create("banners:banner_creation", - banners.creation_form_func); + banners.creation_form_func) -- banner definition @@ -136,7 +138,8 @@ end function banners.Banner.get_transform_string(self) local final = {} for i in ipairs(self.transforms) do - table.insert(final, "("..self.transforms[i].texture.."^[mask:"..self.transforms[i].mask.."^[makealpha:0,0,0)") + table.insert(final, "(" .. self.transforms[i].texture + .. "^[mask:" .. self.transforms[i].mask .. "^[makealpha:0,0,0)") end local ret = table.concat(final, "^") return ret @@ -146,10 +149,12 @@ end banners.determine_flag_direction = function(pos, pointed_thing) local above = pointed_thing.above local under = pointed_thing.under - local dir = {x = under.x - above.x, - y = under.y - above.y, - z = under.z - above.z} - return minetest.dir_to_wallmounted(dir) + local dir = { + x = under.x - above.x, + y = under.y - above.y, + z = under.z - above.z + } + return core.dir_to_wallmounted(dir) end banners.banner_on_use = function(itemstack, player, pointed_thing) @@ -159,18 +164,23 @@ banners.banner_on_use = function(itemstack, player, pointed_thing) end banners.banner_on_dig = function(pos, node, player) - if not player or minetest.is_protected(pos, player:get_player_name()) then + if not player or core.is_protected(pos, player:get_player_name()) then return end - local meta = minetest.get_meta(pos) + local meta = core.get_meta(pos) local inventory = player:get_inventory() - inventory:add_item("main", {name=node.name, count=1, wear=0, metadata=meta:get_string("banner")}) - minetest.remove_node(pos) + inventory:add_item("main", { + name = node.name, + count = 1, + wear = 0, + metadata = meta:get_string("banner") + }) + core.remove_node(pos) end banners.banner_on_destruct = function(pos, node, player) - local objects = minetest.get_objects_inside_radius(pos, 0.5) - for _,v in ipairs(objects) do + local objects = core.get_objects_inside_radius(pos, 0.5) + for _, v in ipairs(objects) do local e = v:get_luaentity() if e and e.name == "banners:banner_ent" then v:remove() @@ -179,22 +189,22 @@ banners.banner_on_destruct = function(pos, node, player) end banners.banner_after_place = function (pos, player, itemstack, pointed_thing) - minetest.get_node(pos).param2 = banners.determine_flag_direction(pos, pointed_thing) - minetest.get_meta(pos):set_string("banner", itemstack:get_meta():get_string("")) - minetest.add_entity(pos, "banners:banner_ent") + core.get_node(pos).param2 = banners.determine_flag_direction(pos, pointed_thing) + core.get_meta(pos):set_string("banner", itemstack:get_meta():get_string("")) + core.add_entity(pos, "banners:banner_ent") end -- banner entity local set_banner_texture -set_banner_texture = function (obj, texture) - obj:set_properties({textures={"banner_uv_text.png^"..texture}}) +set_banner_texture = function(obj, texture) + obj:set_properties({ textures = { "banner_uv_text.png^" .. texture } }) end banners.banner_on_activate = function(self) local pos = self.object:get_pos() - local banner = minetest.get_meta(pos):get_string("banner") - local banner_face = minetest.get_node(pos).param2 + local banner = core.get_meta(pos):get_string("banner") + local banner_face = core.get_node(pos).param2 local yaw = 0. if banner_face == 2 then yaw = 0. @@ -211,18 +221,18 @@ banners.banner_on_activate = function(self) end end -minetest.register_entity("banners:banner_ent", { +core.register_entity("banners:banner_ent", { initial_properties = { - collisionbox = {0,0,0,0,0,0}, + collisionbox = { 0, 0, 0, 0, 0, 0 }, visual = "mesh", - textures = {"banner_uv_text"}, + textures = { "banner_uv_text" }, mesh = "banner_pole.x", }, on_activate = banners.banner_on_activate, }) -if minetest.get_modpath("factions") then dofile(minetest.get_modpath("banners").."/factions.lua") +if core.get_modpath("factions") then end dofile(minetest.get_modpath("banners").."/items.lua") diff --git a/items.lua b/items.lua index 576f97c..8a40da5 100644 --- a/items.lua +++ b/items.lua @@ -2,44 +2,35 @@ -- items -minetest.register_craftitem("banners:banner_sheet", - { - groups = {}, - description = "Banner sheet", - inventory_image = "banner_sheet.png", - stack_max = 1, - metadata = "", - } -) - -minetest.register_craftitem("banners:wooden_pole", - { - groups = {}, - description = "Wooden pole", - inventory_image = "wooden_pole.png" - } -) - -minetest.register_craftitem("banners:wooden_base", - { - groups = {}, - description = "Wooden base", - inventory_image = "wooden_base.png" - } -) - -minetest.register_craftitem("banners:steel_pole", - { - groups = {}, - description = "Steel pole", - inventory_image = "steel_pole.png" - } -) - -minetest.register_craftitem("banners:steel_base", - { - groups = {}, - description = "Steel base", - inventory_image = "steel_base.png" - } -) +core.register_craftitem("banners:banner_sheet", { + groups = {}, + description = "Banner sheet", + inventory_image = "banner_sheet.png", + stack_max = 1, + metadata = "", +}) + +core.register_craftitem("banners:wooden_pole", { + groups = {}, + description = "Wooden pole", + inventory_image = "wooden_pole.png" +}) + +core.register_craftitem("banners:wooden_base", { + groups = {}, + description = "Wooden base", + inventory_image = "wooden_base.png" +}) + +core.register_craftitem("banners:steel_pole", { + groups = {}, + description = "Steel pole", + inventory_image = "steel_pole.png" +}) + +core.register_craftitem("banners:steel_base", { + groups = {}, + description = "Steel base", + inventory_image = "steel_base.png" +}) + diff --git a/nodes.lua b/nodes.lua index 3b869d4..ad2991c 100644 --- a/nodes.lua +++ b/nodes.lua @@ -1,57 +1,53 @@ -- da wooden banner -minetest.register_node("banners:wooden_banner", - { - drawtype = "mesh", - mesh = "banner_support.x", - tiles = {"banner_support.png"}, - description = "Wooden banner", - groups = {choppy=2, dig_immediate=2}, - is_ground_content = false, - diggable = true, - stack_max = 1, - paramtype="light", - paramtype2="facedir", - after_place_node = function (pos, player, itemstack, pointed_thing) - banners.banner_after_place(pos, player, itemstack, pointed_thing) - end, - on_destruct = function(pos) - banners.banner_on_destruct(pos) - end, - on_use = function(i, p, pt) - banners.banner_on_use(i, p, pt) - end, - on_dig = function(pos, n, p) - banners.banner_on_dig(pos, n, p) - end - } -) +core.register_node("banners:wooden_banner", { + drawtype = "mesh", + mesh = "banner_support.x", + tiles = { "banner_support.png" }, + description = "Wooden banner", + groups = { choppy = 2, dig_immediate = 2 }, + is_ground_content = false, + diggable = true, + stack_max = 1, + paramtype = "light", + paramtype2 = "facedir", + after_place_node = function (pos, player, itemstack, pointed_thing) + banners.banner_after_place(pos, player, itemstack, pointed_thing) + end, + on_destruct = function(pos) + banners.banner_on_destruct(pos) + end, + on_use = function(i, p, pt) + banners.banner_on_use(i, p, pt) + end, + on_dig = function(pos, n, p) + banners.banner_on_dig(pos, n, p) + end +}) -- steel banner -minetest.register_node("banners:steel_banner", - { - drawtype = "mesh", - mesh = "banner_support.x", - tiles = {"steel_support.png"}, - description = "Steel banner", - groups = {cracky=2}, - is_ground_content = false, - diggable = true, - stack_max = 1, - paramtype = "light", - paramtype2 = "facedir", - after_place_node = function (pos, player, itemstack, pointed_thing) - banners.banner_after_place(pos, player, itemstack, pointed_thing) - end, - on_destruct = function(pos) - banners.banner_on_destruct(pos) - end, - on_use = function(i, p, pt) - banners.banner_on_use(i, p, pt) - end, - on_dig = function(pos, n, p) - banners.banner_on_dig(pos, n, p) - end +core.register_node("banners:steel_banner", { + drawtype = "mesh", + mesh = "banner_support.x", + tiles = { "steel_support.png" }, + description = "Steel banner", + groups = { cracky = 2 }, + is_ground_content = false, + diggable = true, + stack_max = 1, + paramtype = "light", + paramtype2 = "facedir", + after_place_node = function (pos, player, itemstack, pointed_thing) + banners.banner_after_place(pos, player, itemstack, pointed_thing) + end, + on_destruct = function(pos) + banners.banner_on_destruct(pos) + end, + on_use = function(i, p, pt) + banners.banner_on_use(i, p, pt) + end, + on_dig = function(pos, n, p) + banners.banner_on_dig(pos, n, p) + end - } -) +}) diff --git a/smartfs.lua b/smartfs.lua index b93aaac..f992468 100644 --- a/smartfs.lua +++ b/smartfs.lua @@ -17,9 +17,9 @@ function smartfs.__call(self, name) end -- Register forms and elements -function smartfs.create(name,onload) +function smartfs.create(name, onload) if smartfs._fdef[name] then - error("SmartFS - (Error) Form "..name.." already exists!") + error("SmartFS - (Error) Form " .. name .. " already exists!") end if smartfs.loaded and not smartfs._loaded_override then error("SmartFS - (Error) Forms should be declared while the game loads.") @@ -37,7 +37,7 @@ function smartfs.override_load_checks() smartfs._loaded_override = true end -minetest.after(0, function() +core.after(0, function() smartfs.loaded = true end) function smartfs.dynamic(name,player) @@ -45,14 +45,14 @@ function smartfs.dynamic(name,player) smartfs._dynamic_warned = true print("SmartFS - (Warning) On the fly forms are being used. May cause bad things to happen") end - local state = smartfs._makeState_({name=name},player,nil,false) + local state = smartfs._makeState_({ name = name }, player, nil, false) state.show = state._show_ smartfs.opened[player] = state return state end function smartfs.element(name,data) if smartfs._edef[name] then - error("SmartFS - (Error) Element type "..name.." already exists!") + error("SmartFS - (Error) Element type " .. name .. " already exists!") end smartfs._edef[name] = data return smartfs._edef[name] @@ -68,8 +68,8 @@ function smartfs.inventory_mod() end end -function smartfs.add_to_inventory(form,icon,title) if unified_inventory then +function smartfs.add_to_inventory(form, icon, title) unified_inventory.register_button(form.name, { type = "image", image = icon, @@ -78,15 +78,15 @@ function smartfs.add_to_inventory(form,icon,title) get_formspec = function(player, formspec) local name = player:get_player_name() local opened = smartfs._show_(form, name, nil, true) - return {formspec = opened:_getFS_(false)} + return { formspec = opened:_getFS_(false) } end }) return true elseif inventory_plus then - minetest.register_on_joinplayer(function(player) + core.register_on_joinplayer(function(player) inventory_plus.register_button(player, form.name, title) end) - minetest.register_on_player_receive_fields(function(player, formname, fields) + core.register_on_player_receive_fields(function(player, formname, fields) if formname == "" and fields[form.name] then local name = player:get_player_name() local opened = smartfs._show_(form, name, nil, true) @@ -99,7 +99,7 @@ function smartfs.add_to_inventory(form,icon,title) end end -function smartfs._makeState_(form,player,params,is_inv) +function smartfs._makeState_(form, player, params, is_inv) return { _ele = {}, def = form, @@ -112,13 +112,13 @@ function smartfs._makeState_(form,player,params,is_inv) close = function(self) self.closed = true end, - size = function(self,w,h) - self._size = {w=w,h=h} + size = function(self, w, h) + self._size = { w = w, h = h } end, _getFS_ = function(self,size) local res = "" if self._size and size then - res = "size["..self._size.w..","..self._size.h.."]" + res = "size[" .. self._size.w .. "," .. self._size.h .. "]" end for key,val in pairs(self._ele) do res = res .. val:build() @@ -128,25 +128,27 @@ function smartfs._makeState_(form,player,params,is_inv) _show_ = function(self) if self.is_inv then if unified_inventory then - unified_inventory.set_inventory_formspec(minetest.get_player_by_name(self.player), self.def.name) elseif inventory_plus then - inventory_plus.set_inventory_formspec(minetest.get_player_by_name(self.player), self:_getFS_(true)) + unified_inventory.set_inventory_formspec( + core.get_player_by_name(self.player), self.def.name) + inventory_plus.set_inventory_formspec( + core.get_player_by_name(self.player), self:_getFS_(true)) end else local res = self:_getFS_(true) - minetest.show_formspec(player,form.name,res) + core.show_formspec(player, form.name, res) end end, load = function(self,file) local file = io.open(file, "r") if file then - local table = minetest.deserialize(file:read("*all")) + local table = core.deserialize(file:read("*all")) if type(table) == "table" then if table.size then self._size = table.size end for key,val in pairs(table.ele) do - self:element(val.type,val) + self:element(val.type, val) end return true end @@ -154,7 +156,7 @@ function smartfs._makeState_(form,player,params,is_inv) return false end, save = function(self,file) - local res = {ele={}} + local res = { ele = {} } if self._size then res.size = self._size @@ -166,65 +168,119 @@ function smartfs._makeState_(form,player,params,is_inv) local file = io.open(file, "w") if file then - file:write(minetest.serialize(res)) + file:write(core.serialize(res)) file:close() return true end return false end, - setparam = function(self,key,value) + setparam = function(self, key, value) if not key then return end self.param[key] = value return true end, - getparam = function(self,key,default) + getparam = function(self, key, default) if not key then return end return self.param[key] or default end, - button = function(self,x,y,w,h,name,text,exitf) + button = function(self, x, y, w, h, name, text, exitf) if exitf == nil then exitf = false end - return self:element("button",{pos={x=x,y=y},size={w=w,h=h},name=name,value=text,closes=exitf}) + return self:element("button", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + value = text, + closes = exitf + }) end, - label = function(self,x,y,name,text) - return self:element("label",{pos={x=x,y=y},name=name,value=text}) + label = function(self, x, y, name, text) + return self:element("label", { + pos = { x = x, y = y }, + name = name, + value = text + }) end, - toggle = function(self,x,y,w,h,name,list) - return self:element("toggle",{pos={x=x,y=y},size={w=w,h=h},name=name,id=1,list=list}) + toggle = function(self, x, y, w, h, name, list) + return self:element("toggle", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + id = 1, + list = list + }) end, - field = function(self,x,y,w,h,name,label) - return self:element("field",{pos={x=x,y=y},size={w=w,h=h},name=name,value="",label=label}) + field = function(self, x, y, w, h, name, label) + return self:element("field", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + value = "", + label = label + }) end, - pwdfield = function(self,x,y,w,h,name,label) - local res = self:element("field",{pos={x=x,y=y},size={w=w,h=h},name=name,value="",label=label}) + pwdfield = function(self, x, y, w, h, name, label) + local res = self:element("field", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + value = "", + label = label + }) res:isPassword(true) return res end, - textarea = function(self,x,y,w,h,name,label) - local res = self:element("field",{pos={x=x,y=y},size={w=w,h=h},name=name,value="",label=label}) + textarea = function(self, x, y, w, h, name, label) + local res = self:element("field", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + value = "", + label = label + }) res:isMultiline(true) return res end, - image = function(self,x,y,w,h,name,img) - return self:element("image",{pos={x=x,y=y},size={w=w,h=h},name=name,value=img}) + image = function(self, x, y, w, h, name, img) + return self:element("image", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + value = img + }) end, - checkbox = function(self,x,y,name,label,selected) - return self:element("checkbox",{pos={x=x,y=y},name=name,value=selected,label=label}) + checkbox = function(self, x, y, name, label, selected) + return self:element("checkbox", { + pos = { x = x, y = y }, + name = name, + value = selected, + label = label + }) end, - listbox = function(self,x,y,w,h,name,selected,transparent) - return self:element("list", { pos={x=x,y=y}, size={w=w,h=h}, name=name, selected=selected, transparent=transparent }) + listbox = function(self, x, y, w, h, name, selected, transparent) + return self:element("list", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + selected = selected, + transparent = transparent + }) end, - inventory = function(self,x,y,w,h,name) - return self:element("inventory", { pos={x=x,y=y}, size={w=w,h=h}, name=name }) + inventory = function(self, x, y, w, h, name) + return self:element("inventory", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name + }) end, - element = function(self,typen,data) + element = function(self, typen, data) local type = smartfs._edef[typen] if not type then - error("Element type "..typen.." does not exist!") + error("Element type " .. typen .. " does not exist!") end if self._ele[data.name] then - error("Element "..data.name.." already exists") + error("Element " .. data.name .. " already exists") end data.type = typen @@ -237,7 +293,7 @@ function smartfs._makeState_(form,player,params,is_inv) end } - for key,val in pairs(type) do + for key, val in pairs(type) do ele[key] = val end @@ -252,7 +308,7 @@ end function smartfs._show_(form, player, params, is_inv) local state = smartfs._makeState_(form, player, params, is_inv) state.show = state._show_ - if form._reg(state)~=false then + if form._reg(state) ~= false then if not is_inv then smartfs.opened[player] = state state:_show_() @@ -264,22 +320,22 @@ function smartfs._show_(form, player, params, is_inv) end -- Receive fields from formspec -local function _sfs_recieve_(state,name,fields) - if (fields.quit == "true") then +local function _sfs_recieve_(state, name, fields) + if fields.quit == "true" then if not state.is_inv then smartfs.opened[name] = nil end return true end - for key,val in pairs(fields) do + for key, val in pairs(fields) do if state._ele[key] then state._ele[key].data.value = val end end for key,val in pairs(state._ele) do if val.submit then - if (val:submit(fields)==true) then + if val:submit(fields) == true then return true end end @@ -287,7 +343,8 @@ local function _sfs_recieve_(state,name,fields) if state.closed ~= true then state:_show_() else - minetest.show_formspec(name,"","size[5,1]label[0,0;Formspec closing not yet created!]") + core.show_formspec(name, "", + "size[5,1]label[0,0;Formspec closing not yet created!]") if not state.is_inv then smartfs.opened[name] = nil end @@ -295,18 +352,18 @@ local function _sfs_recieve_(state,name,fields) return true end -minetest.register_on_player_receive_fields(function(player, formname, fields) +core.register_on_player_receive_fields(function(player, formname, fields) local name = player:get_player_name() if smartfs.opened[name] and not smartfs.opened[name].is_inv then if smartfs.opened[name].def.name == formname then local state = smartfs.opened[name] - return _sfs_recieve_(state,name,fields) + return _sfs_recieve_(state, name, fields) else smartfs.opened[name] = nil end elseif smartfs.inv[name] and smartfs.inv[name].is_inv then local state = smartfs.inv[name] - _sfs_recieve_(state,name,fields) + _sfs_recieve_(state, name, fields) end return false end) @@ -316,41 +373,31 @@ end) ------------------------- ELEMENTS ---------------------------- ----------------------------------------------------------------- -smartfs.element("button",{ +smartfs.element("button", { build = function(self) if self.data.img then - return "image_button[".. - self.data.pos.x..","..self.data.pos.y.. - ";".. - self.data.size.w..","..self.data.size.h.. - ";".. - self.data.img.. - ";".. - self.name.. - ";".. - self.data.value.. - "]" + return "image_button[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," ..self.data.size.h + .. ";" .. self.data.img + .. ";" .. self.name + .. ";" .. self.data.value + .. "]" 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[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," ..self.data.size.h + .. ";" .. self.name + .. ";" .. self.data.value + .. "]" 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[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. self.name + .. ";" .. self.data.value + .. "]" end end end, @@ -363,52 +410,49 @@ smartfs.element("button",{ return true end end, - setPosition = function(self,x,y) - self.data.pos = {x=x,y=y} + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } end, getPosition = function(self,x,y) return self.data.pos end, - setSize = function(self,w,h) - self.data.size = {w=w,h=h} + setSize = function(self, w, h) + self.data.size = { w = w, h = h } end, getSize = function(self,x,y) return self.data.size end, - onClick = function(self,func) + onClick = function(self, func) self._click = func end, - click = function(self,func) + click = function(self, func) self._click = func end, - setText = function(self,text) + setText = function(self, text) self.data.value = text end, getText = function(self) return self.data.value end, - setImage = function(self,image) + setImage = function(self, image) self.data.img = image end, getImage = function(self) return self.data.img end, - setClose = function(self,bool) + setClose = function(self, bool) self.data.closes = bool end }) -smartfs.element("toggle",{ +smartfs.element("toggle", { build = function(self) - return "button[".. - self.data.pos.x..","..self.data.pos.y.. - ";".. - self.data.size.w..","..self.data.size.h.. - ";".. - self.name.. - ";".. - self.data.list[self.data.id].. - "]" + return "button[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. self.name + .. ";" .. self.data.list[self.data.id] + .. "]" end, submit = function(self,fields) if fields[self.name] then @@ -421,22 +465,22 @@ smartfs.element("toggle",{ end end end, - onToggle = function(self,func) + onToggle = function(self, func) self._tog = func end, - setPosition = function(self,x,y) - self.data.pos = {x=x,y=y} + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } end, getPosition = function(self,x,y) return self.data.pos end, - setSize = function(self,w,h) - self.data.size = {w=w,h=h} + setSize = function(self, w, h) + self.data.size = { w = w, h = h } end, getSize = function(self,x,y) return self.data.size end, - setId = function(self,id) + setId = function(self, id) self.data.id = id end, getId = function(self) @@ -447,21 +491,20 @@ smartfs.element("toggle",{ end }) -smartfs.element("label",{ +smartfs.element("label", { build = function(self) - return "label[".. - self.data.pos.x..","..self.data.pos.y.. - ";".. - self.data.value.. - "]" + return "label[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.value + .. "]" end, - setPosition = function(self,x,y) - self.data.pos = {x=x,y=y} + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } end, getPosition = function(self,x,y) return self.data.pos end, - setText = function(self,text) + setText = function(self, text) self.data.value = text end, getText = function(self) @@ -469,93 +512,80 @@ smartfs.element("label",{ end }) -smartfs.element("field",{ +smartfs.element("field", { build = function(self) 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.. - ";".. - self.data.value.. - "]" + return "textarea[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. self.name + .. ";" .. self.data.label + .. ";" .. 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.. - "]" + return "pwdfield[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. self.name + .. ";" .. self.data.label + .. "]" else - return "field[".. - self.data.pos.x..","..self.data.pos.y.. - ";".. - self.data.size.w..","..self.data.size.h.. - ";".. - self.name.. - ";".. - self.data.label.. - ";".. - self.data.value.. - "]" + return "field[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. self.name + .. ";" .. self.data.label + .. ";" .. self.data.value + .. "]" end end, - setPosition = function(self,x,y) - self.data.pos = {x=x,y=y} + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } end, getPosition = function(self,x,y) return self.data.pos end, - setSize = function(self,w,h) - self.data.size = {w=w,h=h} + setSize = function(self, w, h) + self.data.size = { w = w, h = h } end, getSize = function(self,x,y) return self.data.size end, - setText = function(self,text) + setText = function(self, text) self.data.value = text end, getText = function(self) return self.data.value end, - isPassword = function(self,bool) + isPassword = function(self, bool) self.data.pwd = bool end, - isMultiline = function(self,bool) + isMultiline = function(self, bool) self.data.ml = bool end }) -smartfs.element("image",{ +smartfs.element("image", { build = function(self) - return "image[".. - self.data.pos.x..","..self.data.pos.y.. - ";".. - self.data.size.w..","..self.data.size.h.. - ";".. - self.data.value.. - "]" + return "image[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. self.data.value + .. "]" end, - setPosition = function(self,x,y) - self.data.pos = {x=x,y=y} + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } end, getPosition = function(self,x,y) return self.data.pos end, - setSize = function(self,w,h) - self.data.size = {w=w,h=h} + setSize = function(self, w, h) + self.data.size = { w = w, h = h } end, getSize = function(self,x,y) return self.data.size end, - setImage = function(self,text) + setImage = function(self, text) self.data.value = text end, getImage = function(self) @@ -563,7 +593,7 @@ smartfs.element("image",{ end }) -smartfs.element("checkbox",{ +smartfs.element("checkbox", { build = function(self) if self.data.value then return "checkbox[".. @@ -583,19 +613,19 @@ smartfs.element("checkbox",{ ";false]" end end, - setPosition = function(self,x,y) - self.data.pos = {x=x,y=y} + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } end, getPosition = function(self,x,y) return self.data.pos end, - setSize = function(self,w,h) - self.data.size = {w=w,h=h} + setSize = function(self, w, h) + self.data.size = { w = w, h = h } end, getSize = function(self,x,y) return self.data.size end, - setText = function(self,text) + setText = function(self, text) self.data.value = text end, getText = function(self) @@ -603,37 +633,33 @@ smartfs.element("checkbox",{ end }) -smartfs.element("list",{ - build = function(self) +smartfs.element("list", { + build = function(self) if not self.data.items then self.data.items = {} end - local listformspec = "textlist[".. - self.data.pos.x..","..self.data.pos.y.. - ";".. - self.data.size.w..","..self.data.size.h.. - ";".. - self.data.name.. - ";".. - table.concat(self.data.items, ",").. - ";".. - tostring(self.data.selected or "").. - ";".. - tostring(self.data.transparent or "false").."]" - - return listformspec - end, - submit = function(self,fields) - if fields[self.name] then - local _type = string.sub(fields[self.data.name],1,3) - local index = string.sub(fields[self.data.name],5) - if _type == "CHG" and self._click then - self:_click(self.root, index) - elseif _type == "DCL" and self._doubleClick then - self:_doubleClick(self.root, index) - end - end - end, + local listformspec = "textlist[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. self.data.name + .. ";" .. table.concat(self.data.items, ",") + .. ";" .. tostring(self.data.selected or "") + .. ";" .. tostring(self.data.transparent or "false") + .. "]" + + return listformspec + end, + submit = function(self, fields) + if fields[self.name] then + local _type = string.sub(fields[self.data.name], 1, 3) + local index = string.sub(fields[self.data.name], 5) + if _type == "CHG" and self._click then + self:_click(self.root, index) + elseif _type == "DCL" and self._doubleClick then + self:_doubleClick(self.root, index) + end + end + end, onClick = function(self, func) self._click = func end, @@ -646,14 +672,14 @@ smartfs.element("list",{ doubleclick = function(self, func) self._doubleClick = func end, - setPosition = function(self,x,y) - self.data.pos = {x=x,y=y} + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } end, getPosition = function(self,x,y) return self.data.pos end, - setSize = function(self,w,h) - self.data.size = {w=w,h=h} + setSize = function(self, w, h) + self.data.size = { w = w, h = h } end, getSize = function(self,x,y) return self.data.size @@ -664,11 +690,11 @@ smartfs.element("list",{ end table.insert(self.data.items, item) end, - removeItem = function(self,idx) + removeItem = function(self, idx) if not self.data.items then self.data.items = {} end - table.remove(self.data.items,idx) + table.remove(self.data.items, idx) end, popItem = function(self) if not self.data.items then @@ -680,28 +706,24 @@ smartfs.element("list",{ end }) -smartfs.element("inventory",{ +smartfs.element("inventory", { build = function(self) - return "list[".. - (self.data.location or "current_player") .. - ";".. - self.name.. - ";".. - self.data.pos.x..","..self.data.pos.y.. - ";".. - self.data.size.w..","..self.data.size.h.. - ";".. - (self.data.index or "") .. - "]" - end, - setPosition = function(self,x,y) - self.data.pos = {x=x,y=y} + return "list[" + .. (self.data.location or "current_player") + .. ";" .. self.name + .. ";" .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. (self.data.index or "") + .. "]" + end, + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } end, getPosition = function(self,x,y) return self.data.pos end, - setSize = function(self,w,h) - self.data.size = {w=w,h=h} + setSize = function(self, w, h) + self.data.size = { w = w, h = h } end, getSize = function(self,x,y) return self.data.size @@ -712,7 +734,7 @@ smartfs.element("inventory",{ -- "nodemeta:,,": Any node metadata -- "detached:": A detached inventory -- "context" does not apply to smartfs, since there is no node-metadata as context available - setLocation = function(self,location) + setLocation = function(self, location) self.data.location = location end, getLocation = function(self) @@ -727,7 +749,7 @@ smartfs.element("inventory",{ useDetached = function(self, name) self.data.location = "detached:" .. name end, - setIndex = function(self,index) + setIndex = function(self, index) self.data.index = index end, getIndex = function(self) @@ -735,7 +757,7 @@ smartfs.element("inventory",{ end }) -smartfs.element("code",{ +smartfs.element("code", { build = function(self) if self._build then self:_build() @@ -743,21 +765,22 @@ smartfs.element("code",{ return self.data.code end, - submit = function(self,fields) + submit = function(self, fields) if self._sub then self:_sub(fields) end end, - onSubmit = function(self,func) + onSubmit = function(self, func) self._sub = func end, - onBuild = function(self,func) + onBuild = function(self, func) self._build = func end, - setCode = function(self,code) + setCode = function(self, code) self.data.code = code end, getCode = function(self) return self.data.code end }) + From 51ec2a8695a1ab2bd0801b1d2fe74b26ae94ff35 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 27 Nov 2024 22:33:14 +0100 Subject: [PATCH 02/39] Update Readme: Minetest -> Luanti --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cd4e270..9457854 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -###Banners mod for Minetest -This is the banner mod for minetest. +###Banners mod for Luanti (formerly Minetest) +This is a banner mod for Luanti. The source code is licensed under GPLv3, and the game assets are licensed under Creative Commons 0 (CC0, [https://wiki.creativecommons.org/wiki/CC0](see here)). From 3b3ea242f79dbef80d0c7d0b9df05fcec963b9e9 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 27 Nov 2024 22:33:36 +0100 Subject: [PATCH 03/39] add luacheck --- .github/workflows/luacheck.yml | 10 ++++++++++ .luacheckrc | 12 ++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 .github/workflows/luacheck.yml create mode 100644 .luacheckrc diff --git a/.github/workflows/luacheck.yml b/.github/workflows/luacheck.yml new file mode 100644 index 0000000..3c99a99 --- /dev/null +++ b/.github/workflows/luacheck.yml @@ -0,0 +1,10 @@ +name: luacheck +on: [push, pull_request] +jobs: + luacheck: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@master + - name: Luacheck + uses: lunarmodules/luacheck@master diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..77837ad --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,12 @@ +globals = { + "banners", + "smartfs", +} + +read_globals = { + "core", + "dump", + "factions", + "inventory_plus", + "unified_inventory", +} From 8d6b2f264f17aebb488a90f1e8f15379eb0cf943 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 27 Nov 2024 22:40:38 +0100 Subject: [PATCH 04/39] update optional depends --- mod.conf | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mod.conf b/mod.conf index 342d3b8..66bc024 100644 --- a/mod.conf +++ b/mod.conf @@ -1,3 +1,4 @@ name = banners description = Adds customisable banners. -depends = default,farming \ No newline at end of file +depends = default, farming +optional_depends = factions, inventory_plus, unified_inventory From ae679a14d42cb2fe3d1eb01509d8d4e70524a4ab Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 27 Nov 2024 22:54:39 +0100 Subject: [PATCH 05/39] luacheck cleanups - unused arguments - shaddowed vars - var redeclarations - some whitespace involved in above lines --- factions.lua | 11 +++++++---- init.lua | 47 +++++++++++++++++++++++-------------------- smartfs.lua | 56 +++++++++++++++++++++++++++------------------------- 3 files changed, 62 insertions(+), 52 deletions(-) diff --git a/factions.lua b/factions.lua index fbb453a..aab0cf1 100644 --- a/factions.lua +++ b/factions.lua @@ -182,8 +182,9 @@ core.register_node("banners:death_banner", { on_destruct = function(pos) banners.banner_on_destruct(pos) end, - on_dig = function(pos, n, p) - if core.is_protected(pos, p:get_player_name()) then + -- (pos, node, player) + on_dig = function(pos, _, player) + if core.is_protected(pos, player:get_player_name()) then return end local meta = core.get_meta(pos) @@ -199,7 +200,8 @@ core.register_node("banners:death_banner", { end, }) -banners.after_powerbanner_placed = function(pos, player, itemstack, pointed_thing) +-- (pos, player, itemstack, pointed_thing) +banners.after_powerbanner_placed = function(pos, player, _, pointed_thing) core.get_node(pos).param2 = banners.determine_flag_direction(pos, pointed_thing) local faction = factions.players[player:get_player_name()] if not faction then @@ -213,7 +215,8 @@ banners.after_powerbanner_placed = function(pos, player, itemstack, pointed_thin core.add_entity(pos, "banners:banner_ent") end -banners.after_deathbanner_placed = function(pos, player, itemstack, pointed_thing) +-- (pos, player, itemstack, pointed_thing) +banners.after_deathbanner_placed = function(pos, player, _, pointed_thing) core.get_node(pos).param2 = banners.determine_flag_direction(pos, pointed_thing) local attacking_faction = factions.players[player:get_player_name()] if attacking_faction then diff --git a/init.lua b/init.lua index 8be2917..c894622 100644 --- a/init.lua +++ b/init.lua @@ -66,26 +66,26 @@ banners.creation_form_func = function(state) state:update_all() -- color indicator -- undo button - state:button(0.5, 0.3, 2, 1, "undo", "Undo"):click(function(self, state) - if #state.banner.transforms > 1 then - state.banner:pop_transform() - state:update_all() + state:button(0.5, 0.3, 2, 1, "undo", "Undo"):click(function(_, state2) + if #state2.banner.transforms > 1 then + state2.banner:pop_transform() + state2:update_all() end end) -- delete button - state:button(0.5, 1.3, 2, 1, "delete", "Delete"):click(function(self, state) - state.banner.transforms = {banners.base_transform} - state:update_all() + state:button(0.5, 1.3, 2, 1, "delete", "Delete"):click(function(_, state2) + state2.banner.transforms = { banners.base_transform } + state2:update_all() end) -- add banners colors local x = 7 local y = .3 for i in ipairs(banners.colors) do local b = state:button(x, y, 1, 1, banners.colors[i], "") - b:click(function(self, state) - state.current_color = "bg_"..self.name..".png" - state:update_preview() b:setImage("bg_" .. banners.colors[i] .. ".png") + b:click(function(self, state2) + state2.current_color = "bg_" .. self.name .. ".png" + state2:update_preview() -- todo: update masks or something end) x = x + 1 @@ -95,16 +95,17 @@ banners.creation_form_func = function(state) end end -- add banners buttons - local x = 1 - local y = 3 + x = 1 + y = 3 for i in ipairs(banners.masks) do local b = state:button(x, y, 2, 1, banners.masks[i], "") - b:click(function(self, state) - state.banner:push_transform({texture=state.current_color, mask=self.name..".png"}) - state:update_all() - end - ) b:setImage(banners.masks[i] .. ".png") + b:click(function(self, state2) + state2.banner:push_transform({ + texture = state2.current_color, + mask = self.name .. ".png" + }) + state2:update_all() end) x = x + 2 if x > 17.5 then @@ -146,7 +147,8 @@ function banners.Banner.get_transform_string(self) end -- helper function for determining the flag's direction -banners.determine_flag_direction = function(pos, pointed_thing) +-- (pos, pointed_thing) +banners.determine_flag_direction = function(_, pointed_thing) local above = pointed_thing.above local under = pointed_thing.under local dir = { @@ -157,7 +159,8 @@ banners.determine_flag_direction = function(pos, pointed_thing) return core.dir_to_wallmounted(dir) end -banners.banner_on_use = function(itemstack, player, pointed_thing) +-- (itemstack, player, pointed_thing) +banners.banner_on_use = function(_, player) if player.is_player then banners.creation_form:show(player:get_player_name()) end @@ -178,7 +181,8 @@ banners.banner_on_dig = function(pos, node, player) core.remove_node(pos) end -banners.banner_on_destruct = function(pos, node, player) +-- (pos, node, player) +banners.banner_on_destruct = function(pos) local objects = core.get_objects_inside_radius(pos, 0.5) for _, v in ipairs(objects) do local e = v:get_luaentity() @@ -188,7 +192,8 @@ banners.banner_on_destruct = function(pos, node, player) end end -banners.banner_after_place = function (pos, player, itemstack, pointed_thing) +-- (pos, player, itemstack, pointed_thing) +banners.banner_after_place = function(pos, _, itemstack, pointed_thing) core.get_node(pos).param2 = banners.determine_flag_direction(pos, pointed_thing) core.get_meta(pos):set_string("banner", itemstack:get_meta():get_string("")) core.add_entity(pos, "banners:banner_ent") diff --git a/smartfs.lua b/smartfs.lua index f992468..8ddb3c7 100644 --- a/smartfs.lua +++ b/smartfs.lua @@ -12,7 +12,7 @@ smartfs = { } -- the smartfs() function -function smartfs.__call(self, name) +function smartfs.__call(_, name) return smartfs._fdef[name] end @@ -75,7 +75,8 @@ function smartfs.add_to_inventory(form, icon, title) image = icon, }) unified_inventory.register_page(form.name, { - get_formspec = function(player, formspec) + -- (player, formspec) + get_formspec = function(player) local name = player:get_player_name() local opened = smartfs._show_(form, name, nil, true) return { formspec = opened:_getFS_(false) } @@ -120,7 +121,7 @@ function smartfs._makeState_(form, player, params, is_inv) if self._size and size then res = "size[" .. self._size.w .. "," .. self._size.h .. "]" end - for key,val in pairs(self._ele) do + for _, val in pairs(self._ele) do res = res .. val:build() end return res @@ -139,15 +140,15 @@ function smartfs._makeState_(form, player, params, is_inv) core.show_formspec(player, form.name, res) end end, - load = function(self,file) - local file = io.open(file, "r") + load = function(self, file_name) + local file = io.open(file_name, "r") if file then local table = core.deserialize(file:read("*all")) if type(table) == "table" then if table.size then self._size = table.size end - for key,val in pairs(table.ele) do + for _, val in pairs(table.ele) do self:element(val.type, val) end return true @@ -155,7 +156,7 @@ function smartfs._makeState_(form, player, params, is_inv) end return false end, - save = function(self,file) + save = function(self, file_name) local res = { ele = {} } if self._size then @@ -166,7 +167,7 @@ function smartfs._makeState_(form, player, params, is_inv) res.ele[key] = val.data end - local file = io.open(file, "w") + local file = io.open(file_name, "w") if file then file:write(core.serialize(res)) file:close() @@ -288,8 +289,8 @@ function smartfs._makeState_(form, player, params, is_inv) name = data.name, root = self, data = data, - remove = function(self) - self.root._ele[self.name] = nil + remove = function(self2) + self2.root._ele[self2.name] = nil end } @@ -333,7 +334,7 @@ local function _sfs_recieve_(state, name, fields) state._ele[key].data.value = val end end - for key,val in pairs(state._ele) do + for _, val in pairs(state._ele) do if val.submit then if val:submit(fields) == true then return true @@ -401,7 +402,8 @@ smartfs.element("button", { end end end, - submit = function(self,fields,state) + -- (self, fields, state) + submit = function(self, fields) if fields[self.name] and self._click then self:_click(self.root) end @@ -413,13 +415,13 @@ smartfs.element("button", { setPosition = function(self, x, y) self.data.pos = { x = x, y = y } end, - getPosition = function(self,x,y) + getPosition = function(self) return self.data.pos end, setSize = function(self, w, h) self.data.size = { w = w, h = h } end, - getSize = function(self,x,y) + getSize = function(self) return self.data.size end, onClick = function(self, func) @@ -471,13 +473,13 @@ smartfs.element("toggle", { setPosition = function(self, x, y) self.data.pos = { x = x, y = y } end, - getPosition = function(self,x,y) + getPosition = function(self) return self.data.pos end, setSize = function(self, w, h) self.data.size = { w = w, h = h } end, - getSize = function(self,x,y) + getSize = function(self) return self.data.size end, setId = function(self, id) @@ -501,7 +503,7 @@ smartfs.element("label", { setPosition = function(self, x, y) self.data.pos = { x = x, y = y } end, - getPosition = function(self,x,y) + getPosition = function(self) return self.data.pos end, setText = function(self, text) @@ -542,13 +544,13 @@ smartfs.element("field", { setPosition = function(self, x, y) self.data.pos = { x = x, y = y } end, - getPosition = function(self,x,y) + getPosition = function(self) return self.data.pos end, setSize = function(self, w, h) self.data.size = { w = w, h = h } end, - getSize = function(self,x,y) + getSize = function(self) return self.data.size end, setText = function(self, text) @@ -576,13 +578,13 @@ smartfs.element("image", { setPosition = function(self, x, y) self.data.pos = { x = x, y = y } end, - getPosition = function(self,x,y) + getPosition = function(self) return self.data.pos end, setSize = function(self, w, h) self.data.size = { w = w, h = h } end, - getSize = function(self,x,y) + getSize = function(self) return self.data.size end, setImage = function(self, text) @@ -616,13 +618,13 @@ smartfs.element("checkbox", { setPosition = function(self, x, y) self.data.pos = { x = x, y = y } end, - getPosition = function(self,x,y) + getPosition = function(self) return self.data.pos end, setSize = function(self, w, h) self.data.size = { w = w, h = h } end, - getSize = function(self,x,y) + getSize = function(self) return self.data.size end, setText = function(self, text) @@ -675,13 +677,13 @@ smartfs.element("list", { setPosition = function(self, x, y) self.data.pos = { x = x, y = y } end, - getPosition = function(self,x,y) + getPosition = function(self) return self.data.pos end, setSize = function(self, w, h) self.data.size = { w = w, h = h } end, - getSize = function(self,x,y) + getSize = function(self) return self.data.size end, addItem = function(self, item) @@ -719,13 +721,13 @@ smartfs.element("inventory", { setPosition = function(self, x, y) self.data.pos = { x = x, y = y } end, - getPosition = function(self,x,y) + getPosition = function(self) return self.data.pos end, setSize = function(self, w, h) self.data.size = { w = w, h = h } end, - getSize = function(self,x,y) + getSize = function(self) return self.data.size end, -- available inventory locations From 7ff3dd0311389ad43ff95d91e64fc474c277345e Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 27 Nov 2024 22:55:15 +0100 Subject: [PATCH 06/39] clean close of file after reading --- smartfs.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/smartfs.lua b/smartfs.lua index 8ddb3c7..1d78dfc 100644 --- a/smartfs.lua +++ b/smartfs.lua @@ -144,6 +144,7 @@ function smartfs._makeState_(form, player, params, is_inv) local file = io.open(file_name, "r") if file then local table = core.deserialize(file:read("*all")) + file:close() if type(table) == "table" then if table.size then self._size = table.size From c8af7c71e5e1753c5e06e7603ce4b5f11932c9ce Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 27 Nov 2024 23:08:26 +0100 Subject: [PATCH 07/39] 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) From aa514d114ad5a4c78f103db7d520abc36aebc326 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 27 Nov 2024 23:10:35 +0100 Subject: [PATCH 08/39] avoid error: "unknown global" --- smartfs.lua | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/smartfs.lua b/smartfs.lua index 8829d30..0289b98 100644 --- a/smartfs.lua +++ b/smartfs.lua @@ -4,6 +4,9 @@ -- by Rubenwardy --------------------------- +local has_inventory_plus = core.get_modpath("inventory_plus") and true or false +local has_unified_inventory = core.get_modpath("unified_inventory") and true or false + smartfs = { _fdef = {}, _edef = {}, @@ -59,17 +62,17 @@ function smartfs.element(name,data) end function smartfs.inventory_mod() - if unified_inventory then + if has_unified_inventory then return "unified_inventory" - elseif inventory_plus then + elseif has_inventory_plus then return "inventory_plus" else return nil end end - if unified_inventory then function smartfs.add_to_inventory(form, icon, title) + if has_unified_inventory then unified_inventory.register_button(form.name, { type = "image", image = icon, @@ -83,7 +86,7 @@ function smartfs.add_to_inventory(form, icon, title) end }) return true - elseif inventory_plus then + elseif has_inventory_plus then core.register_on_joinplayer(function(player) inventory_plus.register_button(player, form.name, title) end) @@ -128,10 +131,10 @@ function smartfs._makeState_(form, player, params, is_inv) end, _show_ = function(self) if self.is_inv then - if unified_inventory then - elseif inventory_plus then + if has_unified_inventory then unified_inventory.set_inventory_formspec( core.get_player_by_name(self.player), self.def.name) + elseif has_inventory_plus then inventory_plus.set_inventory_formspec( core.get_player_by_name(self.player), self:_getFS_(true)) end From deb750af999bf2ff0db5b9ac347a4ace2da534e8 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 27 Nov 2024 23:10:56 +0100 Subject: [PATCH 09/39] stray whitespace fixups --- smartfs.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smartfs.lua b/smartfs.lua index 0289b98..6d8357a 100644 --- a/smartfs.lua +++ b/smartfs.lua @@ -167,7 +167,7 @@ function smartfs._makeState_(form, player, params, is_inv) res.size = self._size end - for key,val in pairs(self._ele) do + for key, val in pairs(self._ele) do res.ele[key] = val.data end @@ -452,7 +452,7 @@ smartfs.element("toggle", { .. ";" .. self.data.list[self.data.id] .. "]" end, - submit = function(self,fields) + submit = function(self, fields) if fields[self.name] then self.data.id = self.data.id + 1 if self.data.id > #self.data.list then From c8a480135e8e2f4829b9d46816c32ac0a07395b0 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Wed, 27 Nov 2024 23:14:33 +0100 Subject: [PATCH 10/39] whitespace: switch all files to same indent character tabs -> spaces --- smartfs.lua | 1380 +++++++++++++++++++++++++-------------------------- 1 file changed, 690 insertions(+), 690 deletions(-) diff --git a/smartfs.lua b/smartfs.lua index 6d8357a..4bdab0f 100644 --- a/smartfs.lua +++ b/smartfs.lua @@ -8,369 +8,369 @@ local has_inventory_plus = core.get_modpath("inventory_plus") and true or false local has_unified_inventory = core.get_modpath("unified_inventory") and true or false smartfs = { - _fdef = {}, - _edef = {}, - opened = {}, - inv = {} + _fdef = {}, + _edef = {}, + opened = {}, + inv = {} } -- the smartfs() function function smartfs.__call(_, name) - return smartfs._fdef[name] + return smartfs._fdef[name] end -- Register forms and elements function smartfs.create(name, onload) - if smartfs._fdef[name] then - error("SmartFS - (Error) Form " .. name .. " already exists!") - end - if smartfs.loaded and not smartfs._loaded_override then - error("SmartFS - (Error) Forms should be declared while the game loads.") - end - - smartfs._fdef[name] = { - _reg = onload, - name = name, - show = smartfs._show_ - } - - return smartfs._fdef[name] + if smartfs._fdef[name] then + error("SmartFS - (Error) Form " .. name .. " already exists!") + end + if smartfs.loaded and not smartfs._loaded_override then + error("SmartFS - (Error) Forms should be declared while the game loads.") + end + + smartfs._fdef[name] = { + _reg = onload, + name = name, + show = smartfs._show_ + } + + return smartfs._fdef[name] end function smartfs.override_load_checks() - smartfs._loaded_override = true + smartfs._loaded_override = true end core.after(0, function() - smartfs.loaded = true + smartfs.loaded = true end) function smartfs.dynamic(name,player) - if not smartfs._dynamic_warned then - smartfs._dynamic_warned = true - print("SmartFS - (Warning) On the fly forms are being used. May cause bad things to happen") - end - local state = smartfs._makeState_({ name = name }, player, nil, false) - state.show = state._show_ - smartfs.opened[player] = state - return state + if not smartfs._dynamic_warned then + smartfs._dynamic_warned = true + print("SmartFS - (Warning) On the fly forms are being used. May cause bad things to happen") + end + local state = smartfs._makeState_({ name = name }, player, nil, false) + state.show = state._show_ + smartfs.opened[player] = state + return state end function smartfs.element(name,data) - if smartfs._edef[name] then - error("SmartFS - (Error) Element type " .. name .. " already exists!") - end - smartfs._edef[name] = data - return smartfs._edef[name] + if smartfs._edef[name] then + error("SmartFS - (Error) Element type " .. name .. " already exists!") + end + smartfs._edef[name] = data + return smartfs._edef[name] end function smartfs.inventory_mod() - if has_unified_inventory then - return "unified_inventory" - elseif has_inventory_plus then - return "inventory_plus" - else - return nil - end + if has_unified_inventory then + return "unified_inventory" + elseif has_inventory_plus then + return "inventory_plus" + else + return nil + end end function smartfs.add_to_inventory(form, icon, title) - if has_unified_inventory then - unified_inventory.register_button(form.name, { - type = "image", - image = icon, - }) - unified_inventory.register_page(form.name, { - -- (player, formspec) - get_formspec = function(player) - local name = player:get_player_name() - local opened = smartfs._show_(form, name, nil, true) - return { formspec = opened:_getFS_(false) } - end - }) - return true - elseif has_inventory_plus then - core.register_on_joinplayer(function(player) - inventory_plus.register_button(player, form.name, title) - end) - core.register_on_player_receive_fields(function(player, formname, fields) - if formname == "" and fields[form.name] then - local name = player:get_player_name() - local opened = smartfs._show_(form, name, nil, true) - inventory_plus.set_inventory_formspec(player, opened:_getFS_(true)) - end - end) - return true - else - return false - end + if has_unified_inventory then + unified_inventory.register_button(form.name, { + type = "image", + image = icon, + }) + unified_inventory.register_page(form.name, { + -- (player, formspec) + get_formspec = function(player) + local name = player:get_player_name() + local opened = smartfs._show_(form, name, nil, true) + return { formspec = opened:_getFS_(false) } + end + }) + return true + elseif has_inventory_plus then + core.register_on_joinplayer(function(player) + inventory_plus.register_button(player, form.name, title) + end) + core.register_on_player_receive_fields(function(player, formname, fields) + if formname == "" and fields[form.name] then + local name = player:get_player_name() + local opened = smartfs._show_(form, name, nil, true) + inventory_plus.set_inventory_formspec(player, opened:_getFS_(true)) + end + end) + return true + else + return false + end end function smartfs._makeState_(form, player, params, is_inv) - return { - _ele = {}, - def = form, - player = player, - param = params or {}, - is_inv = is_inv, - get = function(self,name) - return self._ele[name] - end, - close = function(self) - self.closed = true - end, - size = function(self, w, h) - self._size = { w = w, h = h } - end, - _getFS_ = function(self,size) - local res = "" - if self._size and size then - res = "size[" .. self._size.w .. "," .. self._size.h .. "]" - end - for _, val in pairs(self._ele) do - res = res .. val:build() - end - return res - end, - _show_ = function(self) - if self.is_inv then - if has_unified_inventory then - unified_inventory.set_inventory_formspec( - core.get_player_by_name(self.player), self.def.name) - elseif has_inventory_plus then - inventory_plus.set_inventory_formspec( - core.get_player_by_name(self.player), self:_getFS_(true)) - end - else - local res = self:_getFS_(true) - core.show_formspec(player, form.name, res) - end - end, - load = function(self, file_name) - local file = io.open(file_name, "r") - if file then - local table = core.deserialize(file:read("*all")) - file:close() - if type(table) == "table" then - if table.size then - self._size = table.size - end - for _, val in pairs(table.ele) do - self:element(val.type, val) - end - return true - end - end - return false - end, - save = function(self, file_name) - local res = { ele = {} } - - if self._size then - res.size = self._size - end - - for key, val in pairs(self._ele) do - res.ele[key] = val.data - end - - local file = io.open(file_name, "w") - if file then - file:write(core.serialize(res)) - file:close() - return true - end - return false - end, - setparam = function(self, key, value) - if not key then return end - self.param[key] = value - return true - end, - getparam = function(self, key, default) - if not key then return end - return self.param[key] or default - end, - button = function(self, x, y, w, h, name, text, exitf) - if exitf == nil then exitf = false end - return self:element("button", { - pos = { x = x, y = y }, - size = { w = w, h = h }, - name = name, - value = text, - closes = exitf - }) - end, - label = function(self, x, y, name, text) - return self:element("label", { - pos = { x = x, y = y }, - name = name, - value = text - }) - end, - toggle = function(self, x, y, w, h, name, list) - return self:element("toggle", { - pos = { x = x, y = y }, - size = { w = w, h = h }, - name = name, - id = 1, - list = list - }) - end, - field = function(self, x, y, w, h, name, label) - return self:element("field", { - pos = { x = x, y = y }, - size = { w = w, h = h }, - name = name, - value = "", - label = label - }) - end, - pwdfield = function(self, x, y, w, h, name, label) - local res = self:element("field", { - pos = { x = x, y = y }, - size = { w = w, h = h }, - name = name, - value = "", - label = label - }) - res:isPassword(true) - return res - end, - textarea = function(self, x, y, w, h, name, label) - local res = self:element("field", { - pos = { x = x, y = y }, - size = { w = w, h = h }, - name = name, - value = "", - label = label - }) - res:isMultiline(true) - return res - end, - image = function(self, x, y, w, h, name, img) - return self:element("image", { - pos = { x = x, y = y }, - size = { w = w, h = h }, - name = name, - value = img - }) - end, - checkbox = function(self, x, y, name, label, selected) - return self:element("checkbox", { - pos = { x = x, y = y }, - name = name, - value = selected, - label = label - }) - end, - listbox = function(self, x, y, w, h, name, selected, transparent) - return self:element("list", { - pos = { x = x, y = y }, - size = { w = w, h = h }, - name = name, - selected = selected, - transparent = transparent - }) - end, - inventory = function(self, x, y, w, h, name) - return self:element("inventory", { - pos = { x = x, y = y }, - size = { w = w, h = h }, - name = name - }) - end, - element = function(self, typen, data) - local type = smartfs._edef[typen] - - if not type then - error("Element type " .. typen .. " does not exist!") - end - - if self._ele[data.name] then - error("Element " .. data.name .. " already exists") - end - data.type = typen - - local ele = { - name = data.name, - root = self, - data = data, - remove = function(self2) - self2.root._ele[self2.name] = nil - end - } - - for key, val in pairs(type) do - ele[key] = val - end - - self._ele[data.name] = ele - - return self._ele[data.name] - end - } + return { + _ele = {}, + def = form, + player = player, + param = params or {}, + is_inv = is_inv, + get = function(self,name) + return self._ele[name] + end, + close = function(self) + self.closed = true + end, + size = function(self, w, h) + self._size = { w = w, h = h } + end, + _getFS_ = function(self,size) + local res = "" + if self._size and size then + res = "size[" .. self._size.w .. "," .. self._size.h .. "]" + end + for _, val in pairs(self._ele) do + res = res .. val:build() + end + return res + end, + _show_ = function(self) + if self.is_inv then + if has_unified_inventory then + unified_inventory.set_inventory_formspec( + core.get_player_by_name(self.player), self.def.name) + elseif has_inventory_plus then + inventory_plus.set_inventory_formspec( + core.get_player_by_name(self.player), self:_getFS_(true)) + end + else + local res = self:_getFS_(true) + core.show_formspec(player, form.name, res) + end + end, + load = function(self, file_name) + local file = io.open(file_name, "r") + if file then + local table = core.deserialize(file:read("*all")) + file:close() + if type(table) == "table" then + if table.size then + self._size = table.size + end + for _, val in pairs(table.ele) do + self:element(val.type, val) + end + return true + end + end + return false + end, + save = function(self, file_name) + local res = { ele = {} } + + if self._size then + res.size = self._size + end + + for key, val in pairs(self._ele) do + res.ele[key] = val.data + end + + local file = io.open(file_name, "w") + if file then + file:write(core.serialize(res)) + file:close() + return true + end + return false + end, + setparam = function(self, key, value) + if not key then return end + self.param[key] = value + return true + end, + getparam = function(self, key, default) + if not key then return end + return self.param[key] or default + end, + button = function(self, x, y, w, h, name, text, exitf) + if exitf == nil then exitf = false end + return self:element("button", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + value = text, + closes = exitf + }) + end, + label = function(self, x, y, name, text) + return self:element("label", { + pos = { x = x, y = y }, + name = name, + value = text + }) + end, + toggle = function(self, x, y, w, h, name, list) + return self:element("toggle", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + id = 1, + list = list + }) + end, + field = function(self, x, y, w, h, name, label) + return self:element("field", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + value = "", + label = label + }) + end, + pwdfield = function(self, x, y, w, h, name, label) + local res = self:element("field", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + value = "", + label = label + }) + res:isPassword(true) + return res + end, + textarea = function(self, x, y, w, h, name, label) + local res = self:element("field", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + value = "", + label = label + }) + res:isMultiline(true) + return res + end, + image = function(self, x, y, w, h, name, img) + return self:element("image", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + value = img + }) + end, + checkbox = function(self, x, y, name, label, selected) + return self:element("checkbox", { + pos = { x = x, y = y }, + name = name, + value = selected, + label = label + }) + end, + listbox = function(self, x, y, w, h, name, selected, transparent) + return self:element("list", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name, + selected = selected, + transparent = transparent + }) + end, + inventory = function(self, x, y, w, h, name) + return self:element("inventory", { + pos = { x = x, y = y }, + size = { w = w, h = h }, + name = name + }) + end, + element = function(self, typen, data) + local type = smartfs._edef[typen] + + if not type then + error("Element type " .. typen .. " does not exist!") + end + + if self._ele[data.name] then + error("Element " .. data.name .. " already exists") + end + data.type = typen + + local ele = { + name = data.name, + root = self, + data = data, + remove = function(self2) + self2.root._ele[self2.name] = nil + end + } + + for key, val in pairs(type) do + ele[key] = val + end + + self._ele[data.name] = ele + + return self._ele[data.name] + end + } end -- Show a formspec to a user function smartfs._show_(form, player, params, is_inv) - local state = smartfs._makeState_(form, player, params, is_inv) - state.show = state._show_ - if form._reg(state) ~= false then - if not is_inv then - smartfs.opened[player] = state - state:_show_() - else - smartfs.inv[player] = state - end - end - return state + local state = smartfs._makeState_(form, player, params, is_inv) + state.show = state._show_ + if form._reg(state) ~= false then + if not is_inv then + smartfs.opened[player] = state + state:_show_() + else + smartfs.inv[player] = state + end + end + return state end -- Receive fields from formspec local function _sfs_recieve_(state, name, fields) - if fields.quit == "true" then - if not state.is_inv then - smartfs.opened[name] = nil - end - return true - end - - for key, val in pairs(fields) do - if state._ele[key] then - state._ele[key].data.value = val - end - end - for _, val in pairs(state._ele) do - if val.submit then - if val:submit(fields) == true then - return true - end - end - end - if state.closed ~= true then - state:_show_() - else - core.show_formspec(name, "", - "size[5,1]label[0,0;Formspec closing not yet created!]") - if not state.is_inv then - smartfs.opened[name] = nil - end - end - return true + if fields.quit == "true" then + if not state.is_inv then + smartfs.opened[name] = nil + end + return true + end + + for key, val in pairs(fields) do + if state._ele[key] then + state._ele[key].data.value = val + end + end + for _, val in pairs(state._ele) do + if val.submit then + if val:submit(fields) == true then + return true + end + end + end + if state.closed ~= true then + state:_show_() + else + core.show_formspec(name, "", + "size[5,1]label[0,0;Formspec closing not yet created!]") + if not state.is_inv then + smartfs.opened[name] = nil + end + end + return true end core.register_on_player_receive_fields(function(player, formname, fields) - local name = player:get_player_name() - if smartfs.opened[name] and not smartfs.opened[name].is_inv then - if smartfs.opened[name].def.name == formname then - local state = smartfs.opened[name] - return _sfs_recieve_(state, name, fields) - else - smartfs.opened[name] = nil - end - elseif smartfs.inv[name] and smartfs.inv[name].is_inv then - local state = smartfs.inv[name] - _sfs_recieve_(state, name, fields) - end - return false + local name = player:get_player_name() + if smartfs.opened[name] and not smartfs.opened[name].is_inv then + if smartfs.opened[name].def.name == formname then + local state = smartfs.opened[name] + return _sfs_recieve_(state, name, fields) + else + smartfs.opened[name] = nil + end + elseif smartfs.inv[name] and smartfs.inv[name].is_inv then + local state = smartfs.inv[name] + _sfs_recieve_(state, name, fields) + end + return false end) @@ -379,393 +379,393 @@ 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[" - .. common_prefix - .. ";" .. self.data.img - .. common_sufix - else - if self.data.closes then - return "button_exit[" .. common_prefix .. common_sufix - else - return "button[" .. common_prefix .. common_sufix - end - end - end, - -- (self, fields, state) - submit = function(self, fields) - if fields[self.name] and self._click then - self:_click(self.root) - end - - if self.data.closes then - return true - end - end, - setPosition = function(self, x, y) - self.data.pos = { x = x, y = y } - end, - getPosition = function(self) - return self.data.pos - end, - setSize = function(self, w, h) - self.data.size = { w = w, h = h } - end, - getSize = function(self) - return self.data.size - end, - onClick = function(self, func) - self._click = func - end, - click = function(self, func) - self._click = func - end, - setText = function(self, text) - self.data.value = text - end, - getText = function(self) - return self.data.value - end, - setImage = function(self, image) - self.data.img = image - end, - getImage = function(self) - return self.data.img - end, - setClose = function(self, bool) - self.data.closes = bool - end + 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[" + .. common_prefix + .. ";" .. self.data.img + .. common_sufix + else + if self.data.closes then + return "button_exit[" .. common_prefix .. common_sufix + else + return "button[" .. common_prefix .. common_sufix + end + end + end, + -- (self, fields, state) + submit = function(self, fields) + if fields[self.name] and self._click then + self:_click(self.root) + end + + if self.data.closes then + return true + end + end, + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } + end, + getPosition = function(self) + return self.data.pos + end, + setSize = function(self, w, h) + self.data.size = { w = w, h = h } + end, + getSize = function(self) + return self.data.size + end, + onClick = function(self, func) + self._click = func + end, + click = function(self, func) + self._click = func + end, + setText = function(self, text) + self.data.value = text + end, + getText = function(self) + return self.data.value + end, + setImage = function(self, image) + self.data.img = image + end, + getImage = function(self) + return self.data.img + end, + setClose = function(self, bool) + self.data.closes = bool + end }) smartfs.element("toggle", { - build = function(self) - return "button[" - .. self.data.pos.x .. "," .. self.data.pos.y - .. ";" .. self.data.size.w .. "," .. self.data.size.h - .. ";" .. self.name - .. ";" .. self.data.list[self.data.id] - .. "]" - end, - submit = function(self, fields) - if fields[self.name] then - self.data.id = self.data.id + 1 - if self.data.id > #self.data.list then - self.data.id = 1 - end - if self._tog then - self:_tog(self.root) - end - end - end, - onToggle = function(self, func) - self._tog = func - end, - setPosition = function(self, x, y) - self.data.pos = { x = x, y = y } - end, - getPosition = function(self) - return self.data.pos - end, - setSize = function(self, w, h) - self.data.size = { w = w, h = h } - end, - getSize = function(self) - return self.data.size - end, - setId = function(self, id) - self.data.id = id - end, - getId = function(self) - return self.data.id - end, - getText = function(self) - return self.data.list[self.data.id] - end + build = function(self) + return "button[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. self.name + .. ";" .. self.data.list[self.data.id] + .. "]" + end, + submit = function(self, fields) + if fields[self.name] then + self.data.id = self.data.id + 1 + if self.data.id > #self.data.list then + self.data.id = 1 + end + if self._tog then + self:_tog(self.root) + end + end + end, + onToggle = function(self, func) + self._tog = func + end, + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } + end, + getPosition = function(self) + return self.data.pos + end, + setSize = function(self, w, h) + self.data.size = { w = w, h = h } + end, + getSize = function(self) + return self.data.size + end, + setId = function(self, id) + self.data.id = id + end, + getId = function(self) + return self.data.id + end, + getText = function(self) + return self.data.list[self.data.id] + end }) smartfs.element("label", { - build = function(self) - return "label[" - .. self.data.pos.x .. "," .. self.data.pos.y - .. ";" .. self.data.value - .. "]" - end, - setPosition = function(self, x, y) - self.data.pos = { x = x, y = y } - end, - getPosition = function(self) - return self.data.pos - end, - setText = function(self, text) - self.data.value = text - end, - getText = function(self) - return self.data.value - end + build = function(self) + return "label[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.value + .. "]" + end, + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } + end, + getPosition = function(self) + return self.data.pos + end, + setText = function(self, text) + self.data.value = text + end, + getText = function(self) + return self.data.value + end }) 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[" - .. common - .. ";" .. self.data.value - .. "]" - elseif self.data.pwd then - return "pwdfield[" - .. common - .. "]" - else - return "field[" - .. common - .. ";" .. self.data.value - .. "]" - end - end, - setPosition = function(self, x, y) - self.data.pos = { x = x, y = y } - end, - getPosition = function(self) - return self.data.pos - end, - setSize = function(self, w, h) - self.data.size = { w = w, h = h } - end, - getSize = function(self) - return self.data.size - end, - setText = function(self, text) - self.data.value = text - end, - getText = function(self) - return self.data.value - end, - isPassword = function(self, bool) - self.data.pwd = bool - end, - isMultiline = function(self, bool) - self.data.ml = bool - end + 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[" + .. common + .. ";" .. self.data.value + .. "]" + elseif self.data.pwd then + return "pwdfield[" + .. common + .. "]" + else + return "field[" + .. common + .. ";" .. self.data.value + .. "]" + end + end, + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } + end, + getPosition = function(self) + return self.data.pos + end, + setSize = function(self, w, h) + self.data.size = { w = w, h = h } + end, + getSize = function(self) + return self.data.size + end, + setText = function(self, text) + self.data.value = text + end, + getText = function(self) + return self.data.value + end, + isPassword = function(self, bool) + self.data.pwd = bool + end, + isMultiline = function(self, bool) + self.data.ml = bool + end }) smartfs.element("image", { - build = function(self) - return "image[" - .. self.data.pos.x .. "," .. self.data.pos.y - .. ";" .. self.data.size.w .. "," .. self.data.size.h - .. ";" .. self.data.value - .. "]" - end, - setPosition = function(self, x, y) - self.data.pos = { x = x, y = y } - end, - getPosition = function(self) - return self.data.pos - end, - setSize = function(self, w, h) - self.data.size = { w = w, h = h } - end, - getSize = function(self) - return self.data.size - end, - setImage = function(self, text) - self.data.value = text - end, - getImage = function(self) - return self.data.value - end + build = function(self) + return "image[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. self.data.value + .. "]" + end, + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } + end, + getPosition = function(self) + return self.data.pos + end, + setSize = function(self, w, h) + self.data.size = { w = w, h = h } + end, + getSize = function(self) + return self.data.size + end, + setImage = function(self, text) + self.data.value = text + end, + getImage = function(self) + return self.data.value + end }) 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 out .. ";true]" - else - return out .. ";false]" - end - end, - setPosition = function(self, x, y) - self.data.pos = { x = x, y = y } - end, - getPosition = function(self) - return self.data.pos - end, - setSize = function(self, w, h) - self.data.size = { w = w, h = h } - end, - getSize = function(self) - return self.data.size - end, - setText = function(self, text) - self.data.value = text - end, - getText = function(self) - return self.data.value - end + 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 out .. ";true]" + else + return out .. ";false]" + end + end, + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } + end, + getPosition = function(self) + return self.data.pos + end, + setSize = function(self, w, h) + self.data.size = { w = w, h = h } + end, + getSize = function(self) + return self.data.size + end, + setText = function(self, text) + self.data.value = text + end, + getText = function(self) + return self.data.value + end }) smartfs.element("list", { - build = function(self) - if not self.data.items then - self.data.items = {} - end - local listformspec = "textlist[" - .. self.data.pos.x .. "," .. self.data.pos.y - .. ";" .. self.data.size.w .. "," .. self.data.size.h - .. ";" .. self.data.name - .. ";" .. table.concat(self.data.items, ",") - .. ";" .. tostring(self.data.selected or "") - .. ";" .. tostring(self.data.transparent or "false") - .. "]" - - return listformspec - end, - submit = function(self, fields) - if fields[self.name] then - local _type = string.sub(fields[self.data.name], 1, 3) - local index = string.sub(fields[self.data.name], 5) - if _type == "CHG" and self._click then - self:_click(self.root, index) - elseif _type == "DCL" and self._doubleClick then - self:_doubleClick(self.root, index) - end - end - end, - onClick = function(self, func) - self._click = func - end, - click = function(self, func) - self._click = func - end, - onDoubleClick = function(self, func) - self._doubleClick = func - end, - doubleclick = function(self, func) - self._doubleClick = func - end, - setPosition = function(self, x, y) - self.data.pos = { x = x, y = y } - end, - getPosition = function(self) - return self.data.pos - end, - setSize = function(self, w, h) - self.data.size = { w = w, h = h } - end, - getSize = function(self) - return self.data.size - end, - addItem = function(self, item) - if not self.data.items then - self.data.items = {} - end - table.insert(self.data.items, item) - end, - removeItem = function(self, idx) - if not self.data.items then - self.data.items = {} - end - table.remove(self.data.items, idx) - end, - popItem = function(self) - if not self.data.items then - self.data.items = {} - end - local item = self.data.items[#self.data.items] - table.remove(self.data.items) - return item - end + build = function(self) + if not self.data.items then + self.data.items = {} + end + local listformspec = "textlist[" + .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. self.data.name + .. ";" .. table.concat(self.data.items, ",") + .. ";" .. tostring(self.data.selected or "") + .. ";" .. tostring(self.data.transparent or "false") + .. "]" + + return listformspec + end, + submit = function(self, fields) + if fields[self.name] then + local _type = string.sub(fields[self.data.name], 1, 3) + local index = string.sub(fields[self.data.name], 5) + if _type == "CHG" and self._click then + self:_click(self.root, index) + elseif _type == "DCL" and self._doubleClick then + self:_doubleClick(self.root, index) + end + end + end, + onClick = function(self, func) + self._click = func + end, + click = function(self, func) + self._click = func + end, + onDoubleClick = function(self, func) + self._doubleClick = func + end, + doubleclick = function(self, func) + self._doubleClick = func + end, + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } + end, + getPosition = function(self) + return self.data.pos + end, + setSize = function(self, w, h) + self.data.size = { w = w, h = h } + end, + getSize = function(self) + return self.data.size + end, + addItem = function(self, item) + if not self.data.items then + self.data.items = {} + end + table.insert(self.data.items, item) + end, + removeItem = function(self, idx) + if not self.data.items then + self.data.items = {} + end + table.remove(self.data.items, idx) + end, + popItem = function(self) + if not self.data.items then + self.data.items = {} + end + local item = self.data.items[#self.data.items] + table.remove(self.data.items) + return item + end }) smartfs.element("inventory", { - build = function(self) - return "list[" - .. (self.data.location or "current_player") - .. ";" .. self.name - .. ";" .. self.data.pos.x .. "," .. self.data.pos.y - .. ";" .. self.data.size.w .. "," .. self.data.size.h - .. ";" .. (self.data.index or "") - .. "]" - end, - setPosition = function(self, x, y) - self.data.pos = { x = x, y = y } - end, - getPosition = function(self) - return self.data.pos - end, - setSize = function(self, w, h) - self.data.size = { w = w, h = h } - end, - getSize = function(self) - return self.data.size - end, - -- available inventory locations - -- "current_player": Player to whom the menu is shown - -- "player:": Any player - -- "nodemeta:,,": Any node metadata - -- "detached:": A detached inventory - -- "context" does not apply to smartfs, since there is no node-metadata as context available - setLocation = function(self, location) - self.data.location = location - end, - getLocation = function(self) - return self.data.location or "current_player" - end, - usePosition = function(self, pos) - self.data.location = string.format("nodemeta:%d,%d,%d", pos.x, pos.y, pos.z) - end, - usePlayer = function(self, name) - self.data.location = "player:" .. name - end, - useDetached = function(self, name) - self.data.location = "detached:" .. name - end, - setIndex = function(self, index) - self.data.index = index - end, - getIndex = function(self) - return self.data.index - end + build = function(self) + return "list[" + .. (self.data.location or "current_player") + .. ";" .. self.name + .. ";" .. self.data.pos.x .. "," .. self.data.pos.y + .. ";" .. self.data.size.w .. "," .. self.data.size.h + .. ";" .. (self.data.index or "") + .. "]" + end, + setPosition = function(self, x, y) + self.data.pos = { x = x, y = y } + end, + getPosition = function(self) + return self.data.pos + end, + setSize = function(self, w, h) + self.data.size = { w = w, h = h } + end, + getSize = function(self) + return self.data.size + end, + -- available inventory locations + -- "current_player": Player to whom the menu is shown + -- "player:": Any player + -- "nodemeta:,,": Any node metadata + -- "detached:": A detached inventory + -- "context" does not apply to smartfs, since there is no node-metadata as context available + setLocation = function(self, location) + self.data.location = location + end, + getLocation = function(self) + return self.data.location or "current_player" + end, + usePosition = function(self, pos) + self.data.location = string.format("nodemeta:%d,%d,%d", pos.x, pos.y, pos.z) + end, + usePlayer = function(self, name) + self.data.location = "player:" .. name + end, + useDetached = function(self, name) + self.data.location = "detached:" .. name + end, + setIndex = function(self, index) + self.data.index = index + end, + getIndex = function(self) + return self.data.index + end }) smartfs.element("code", { - build = function(self) - if self._build then - self:_build() - end - - return self.data.code - end, - submit = function(self, fields) - if self._sub then - self:_sub(fields) - end - end, - onSubmit = function(self, func) - self._sub = func - end, - onBuild = function(self, func) - self._build = func - end, - setCode = function(self, code) - self.data.code = code - end, - getCode = function(self) - return self.data.code - end + build = function(self) + if self._build then + self:_build() + end + + return self.data.code + end, + submit = function(self, fields) + if self._sub then + self:_sub(fields) + end + end, + onSubmit = function(self, func) + self._sub = func + end, + onBuild = function(self, func) + self._build = func + end, + setCode = function(self, code) + self.data.code = code + end, + getCode = function(self) + return self.data.code + end }) From 9c688d416bf8f793fb484a367d60dc6f1f567639 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 28 Nov 2024 01:04:03 +0100 Subject: [PATCH 11/39] refactor weird declaration --- init.lua | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/init.lua b/init.lua index 8ddd288..cf29948 100644 --- a/init.lua +++ b/init.lua @@ -201,8 +201,7 @@ banners.banner_after_place = function(pos, _, itemstack, pointed_thing) end -- banner entity -local set_banner_texture -set_banner_texture = function(obj, texture) +local set_banner_texture = function(obj, texture) obj:set_properties({ textures = { "banner_uv_text.png^" .. texture } }) end From 2175d771868af8e256a0cc956db746d71b6b403f Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 28 Nov 2024 10:15:52 +0100 Subject: [PATCH 12/39] whitespace: two stray spaces ...that slipped through the cracks on previous whitespace edit --- smartfs.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smartfs.lua b/smartfs.lua index 4bdab0f..07dfb34 100644 --- a/smartfs.lua +++ b/smartfs.lua @@ -43,7 +43,7 @@ end core.after(0, function() smartfs.loaded = true end) -function smartfs.dynamic(name,player) +function smartfs.dynamic(name, player) if not smartfs._dynamic_warned then smartfs._dynamic_warned = true print("SmartFS - (Warning) On the fly forms are being used. May cause bad things to happen") @@ -53,7 +53,7 @@ function smartfs.dynamic(name,player) smartfs.opened[player] = state return state end -function smartfs.element(name,data) +function smartfs.element(name, data) if smartfs._edef[name] then error("SmartFS - (Error) Element type " .. name .. " already exists!") end From 5cf19460648e4c0b9adbaa5c67b61021b172a3be Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 28 Nov 2024 10:17:03 +0100 Subject: [PATCH 13/39] fix shaddowing global table --- smartfs.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/smartfs.lua b/smartfs.lua index 07dfb34..7675e61 100644 --- a/smartfs.lua +++ b/smartfs.lua @@ -146,13 +146,13 @@ function smartfs._makeState_(form, player, params, is_inv) load = function(self, file_name) local file = io.open(file_name, "r") if file then - local table = core.deserialize(file:read("*all")) + local data = core.deserialize(file:read("*all")) file:close() - if type(table) == "table" then - if table.size then - self._size = table.size + if type(data) == "table" then + if data.size then + self._size = data.size end - for _, val in pairs(table.ele) do + for _, val in pairs(data.ele) do self:element(val.type, val) end return true From b1d63fd7b13f6565f36a4b214ed9da4fd2e54eb3 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 28 Nov 2024 11:20:40 +0100 Subject: [PATCH 14/39] reduce amount of times transformation string is calculated --- init.lua | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/init.lua b/init.lua index cf29948..f6a6e29 100644 --- a/init.lua +++ b/init.lua @@ -43,19 +43,20 @@ banners.base_transform = { banners.creation_form_func = function(state) -- helper functions - state.update_player_inv = function(self) + state.update_player_inv = function(self, transform_string) local player = core.get_player_by_name(self.player) local newbanner = player:get_wielded_item() - newbanner:get_meta():set_string("", state.banner:get_transform_string()) + newbanner:get_meta():set_string("", transform_string) player:set_wielded_item(newbanner) end - state.update_preview = function(self) - self:get("banner_preview"):setImage(self.banner:get_transform_string()) + state.update_preview = function(self, transform_string) + self:get("banner_preview"):setImage(transform_string) self:get("color_indicator"):setImage(self.current_color) end state.update_all = function(self) - self:update_preview() - self:update_player_inv() + local transform_string = self.banner:get_transform_string() + self:update_preview(transform_string) + self:update_player_inv(transform_string) end -- initialize with empty banner state.banner = banners.Banner:new(nil) @@ -86,7 +87,7 @@ banners.creation_form_func = function(state) b:setImage("bg_" .. banners.colors[i] .. ".png") b:click(function(self, state2) state2.current_color = "bg_" .. self.name .. ".png" - state2:update_preview() + state2:get("color_indicator"):setImage(state2.current_color) -- todo: update masks or something end) x = x + 1 From b1ff0eb558d2d94782509a5af5bbdc351c412f9f Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 28 Nov 2024 11:22:49 +0100 Subject: [PATCH 15/39] use ipairs feature or use the faster repeat-until-loop --- init.lua | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/init.lua b/init.lua index f6a6e29..b3270b9 100644 --- a/init.lua +++ b/init.lua @@ -82,9 +82,9 @@ banners.creation_form_func = function(state) -- add banners colors local x = 7 local y = .3 - for i in ipairs(banners.colors) do - local b = state:button(x, y, 1, 1, banners.colors[i], "") - b:setImage("bg_" .. banners.colors[i] .. ".png") + for _, color in ipairs(banners.colors) do + local b = state:button(x, y, 1, 1, color, "") + b:setImage("bg_" .. color .. ".png") b:click(function(self, state2) state2.current_color = "bg_" .. self.name .. ".png" state2:get("color_indicator"):setImage(state2.current_color) @@ -99,9 +99,9 @@ banners.creation_form_func = function(state) -- add banners buttons x = 1 y = 3 - for i in ipairs(banners.masks) do - local b = state:button(x, y, 2, 1, banners.masks[i], "") - b:setImage(banners.masks[i] .. ".png") + for _, mask in ipairs(banners.masks) do + local b = state:button(x, y, 2, 1, mask, "") + b:setImage(mask .. ".png") b:click(function(self, state2) state2.banner:push_transform({ texture = state2.current_color, @@ -140,9 +140,9 @@ function banners.Banner.pop_transform(self) end function banners.Banner.get_transform_string(self) local final = {} - for i in ipairs(self.transforms) do - table.insert(final, "(" .. self.transforms[i].texture - .. "^[mask:" .. self.transforms[i].mask .. "^[makealpha:0,0,0)") + for _, transform in ipairs(self.transforms) do + table.insert(final, "(" .. transform.texture + .. "^[mask:" .. transform.mask .. "^[makealpha:0,0,0)") end local ret = table.concat(final, "^") return ret From 78be1611c3d3113fb7d4d6e6e19b0d54ace7cb31 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 28 Nov 2024 12:55:46 +0100 Subject: [PATCH 16/39] cap max amount of layers fixes #5 --- init.lua | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/init.lua b/init.lua index b3270b9..af70ff0 100644 --- a/init.lua +++ b/init.lua @@ -29,6 +29,16 @@ banners.masks = { "star_chevron", "checkered_8_4", "checkered_16_8" } +-- The amount of transformations needs to be capped somewhere +-- to avoid crashing server. +-- It doesn't make sense to have any one mask multiple times +-- but users normally don't follow strict logic when being +-- creative, so we give them plenty of space to work with. +-- A better approach would be to strip duplicates and to +-- drop all previous masks if a full background is dropped +-- on top of other masks. +banners.max_transformations = #banners.masks * #banners.masks + banners.colors = { "black", "cyan", "green", "white", "blue", "darkblue", "red", "yellow", @@ -134,6 +144,9 @@ function banners.Banner:new(banner) end function banners.Banner.push_transform(self, transform) table.insert(self.transforms, transform) + if #self.transforms > banners.max_transformations then + table.remove(self.transforms, 1) + end end function banners.Banner.pop_transform(self) table.remove(self.transforms) From 3d22f9b76bff4eae7c1cacf47de833f126117722 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 28 Nov 2024 12:59:45 +0100 Subject: [PATCH 17/39] fix #5 make transform history non-global fixes #5 transformation history was shared by all users and kept growing as it was never truely reset. Every time a user used a banner a white background was dumped on top of the stack making it possible to crash the server through an overflow to core.serialize() function. --- init.lua | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/init.lua b/init.lua index af70ff0..18ae3ce 100644 --- a/init.lua +++ b/init.lua @@ -133,11 +133,10 @@ banners.creation_form = smartfs.create("banners:banner_creation", -- banner definition -banners.Banner = { - transforms = {} -} +banners.Banner = {} + function banners.Banner:new(banner) - banner = banner or {} + banner = banner or { transforms = {} } setmetatable(banner, self) self.__index = self return banner From 7e3a84eaacce799afb132568ffa80589b4d2d5d2 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 28 Nov 2024 14:18:26 +0100 Subject: [PATCH 18/39] minimize metadata size also changed banners.max_transformations to banners.max_undo_levels and lowered the value substantially. --- init.lua | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/init.lua b/init.lua index 18ae3ce..f91ddc3 100644 --- a/init.lua +++ b/init.lua @@ -29,15 +29,11 @@ banners.masks = { "star_chevron", "checkered_8_4", "checkered_16_8" } --- The amount of transformations needs to be capped somewhere --- to avoid crashing server. --- It doesn't make sense to have any one mask multiple times --- but users normally don't follow strict logic when being --- creative, so we give them plenty of space to work with. --- A better approach would be to strip duplicates and to --- drop all previous masks if a full background is dropped --- on top of other masks. -banners.max_transformations = #banners.masks * #banners.masks +-- It is now unlikely for the server to crash from too long +-- history since we now trim out garbage when converting to +-- metadata. This limit is now just to avoid run-time +-- memory bloat. +banners.max_undo_levels = 256 banners.colors = { "black", "cyan", "green", "white", @@ -143,7 +139,7 @@ function banners.Banner:new(banner) end function banners.Banner.push_transform(self, transform) table.insert(self.transforms, transform) - if #self.transforms > banners.max_transformations then + if #self.transforms > banners.max_undo_levels then table.remove(self.transforms, 1) end end @@ -152,10 +148,24 @@ function banners.Banner.pop_transform(self) end function banners.Banner.get_transform_string(self) local final = {} - for _, transform in ipairs(self.transforms) do - table.insert(final, "(" .. transform.texture - .. "^[mask:" .. transform.mask .. "^[makealpha:0,0,0)") - end + local used = {} + local transform + -- work backwards to keep resulting data small + local i = #self.transforms + repeat + transform = self.transforms[i] + -- same mask can be trimmed out only using most recent + if not used[transform.mask] then + used[transform.mask] = true + table.insert(final, 1, "(" .. transform.texture + .. "^[mask:" .. transform.mask .. "^[makealpha:0,0,0)") + -- anything before a background is fully covered + if "mask_background.png" == transform.mask then + break + end + end + i = i - 1 + until i == 0 local ret = table.concat(final, "^") return ret end From dc9867f359b76228efab1aaf63fda18fdb57eb63 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 28 Nov 2024 14:19:54 +0100 Subject: [PATCH 19/39] add versioning --- init.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/init.lua b/init.lua index f91ddc3..9d4ebad 100644 --- a/init.lua +++ b/init.lua @@ -1,7 +1,9 @@ local MP = core.get_modpath("banners") .. "/" dofile(MP .. "smartfs.lua") -banners = {} +banners = { + version = 20241128.1419 +} banners.masks = { "bend_left", "bend_left_outline", From b99d10cba549dc969fac924804dae214314339b7 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 28 Nov 2024 15:08:42 +0100 Subject: [PATCH 20/39] add recoverable per player history with saved colour --- init.lua | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/init.lua b/init.lua index 9d4ebad..b0b3e98 100644 --- a/init.lua +++ b/init.lua @@ -2,7 +2,7 @@ local MP = core.get_modpath("banners") .. "/" dofile(MP .. "smartfs.lua") banners = { - version = 20241128.1419 + version = 20241128.1507 } banners.masks = { @@ -37,6 +37,9 @@ banners.masks = { -- memory bloat. banners.max_undo_levels = 256 +-- cache of player histories +local histories = {} + banners.colors = { "black", "cyan", "green", "white", "blue", "darkblue", "red", "yellow", @@ -66,10 +69,16 @@ banners.creation_form_func = function(state) self:update_preview(transform_string) self:update_player_inv(transform_string) end - -- initialize with empty banner - state.banner = banners.Banner:new(nil) - state.banner:push_transform(banners.base_transform) - state.current_color = "bg_white.png" + if histories[state.player] then + -- initialize with saved history + state.banner = histories[state.player] + else + -- initialize with empty banner + state.banner = banners.Banner:new(nil) + state.banner:push_transform(banners.base_transform) + histories[state.player] = state.banner + end + state.current_color = state.banner.color state:size(20, 10) state:image(3, 0.4, 4, 2, "banner_preview", nil) state:image(2.4, 0.8, 0.7, 0.7, "color_indicator", state.current_color) @@ -96,6 +105,7 @@ banners.creation_form_func = function(state) b:click(function(self, state2) state2.current_color = "bg_" .. self.name .. ".png" state2:get("color_indicator"):setImage(state2.current_color) + state2.banner.color = state2.current_color -- todo: update masks or something end) x = x + 1 @@ -134,7 +144,7 @@ banners.creation_form = smartfs.create("banners:banner_creation", banners.Banner = {} function banners.Banner:new(banner) - banner = banner or { transforms = {} } + banner = banner or { color = "bg_black.png", transforms = {} } setmetatable(banner, self) self.__index = self return banner @@ -261,6 +271,10 @@ core.register_entity("banners:banner_ent", { on_activate = banners.banner_on_activate, }) +core.register_on_leaveplayer(function(player) + histories[player:get_player_name()] = nil +end) + if core.get_modpath("factions") then dofile(MP .. "factions.lua") end From f2147ce1b07007cef144e0daa24ca75d47ef1ed9 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 28 Nov 2024 15:36:07 +0100 Subject: [PATCH 21/39] rename update_all -> update_preview_inv since it isn't updating everything, only the preview and the inventory item --- init.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/init.lua b/init.lua index b0b3e98..69ff41f 100644 --- a/init.lua +++ b/init.lua @@ -64,7 +64,7 @@ banners.creation_form_func = function(state) self:get("banner_preview"):setImage(transform_string) self:get("color_indicator"):setImage(self.current_color) end - state.update_all = function(self) + state.update_preview_inv = function(self) local transform_string = self.banner:get_transform_string() self:update_preview(transform_string) self:update_player_inv(transform_string) @@ -82,19 +82,19 @@ banners.creation_form_func = function(state) state:size(20, 10) state:image(3, 0.4, 4, 2, "banner_preview", nil) state:image(2.4, 0.8, 0.7, 0.7, "color_indicator", state.current_color) - state:update_all() + state:update_preview_inv() -- color indicator -- undo button state:button(0.5, 0.3, 2, 1, "undo", "Undo"):click(function(_, state2) if #state2.banner.transforms > 1 then state2.banner:pop_transform() - state2:update_all() + state2:update_preview_inv() end end) -- delete button state:button(0.5, 1.3, 2, 1, "delete", "Delete"):click(function(_, state2) state2.banner.transforms = { banners.base_transform } - state2:update_all() + state2:update_preview_inv() end) -- add banners colors local x = 7 @@ -125,7 +125,7 @@ banners.creation_form_func = function(state) texture = state2.current_color, mask = self.name .. ".png" }) - state2:update_all() + state2:update_preview_inv() end) x = x + 2 if x > 17.5 then From 38bf923967bc3a7693b6ba4a6be8609e84592015 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Thu, 28 Nov 2024 15:37:02 +0100 Subject: [PATCH 22/39] dynamically update masks to reflect the currently selected colour. --- init.lua | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/init.lua b/init.lua index 69ff41f..427294c 100644 --- a/init.lua +++ b/init.lua @@ -2,7 +2,7 @@ local MP = core.get_modpath("banners") .. "/" dofile(MP .. "smartfs.lua") banners = { - version = 20241128.1507 + version = 20241128.1533 } banners.masks = { @@ -106,7 +106,11 @@ banners.creation_form_func = function(state) state2.current_color = "bg_" .. self.name .. ".png" state2:get("color_indicator"):setImage(state2.current_color) state2.banner.color = state2.current_color - -- todo: update masks or something + -- update masks + for _, mask in ipairs(banners.masks) do + state2:get(mask):setImage("(" .. state2.current_color + .. "^[mask:" .. mask .. ".png^[makealpha:0,0,0)") + end end) x = x + 1 if x > 19 then @@ -119,7 +123,8 @@ banners.creation_form_func = function(state) y = 3 for _, mask in ipairs(banners.masks) do local b = state:button(x, y, 2, 1, mask, "") - b:setImage(mask .. ".png") + b:setImage("(" .. state.current_color + .. "^[mask:" .. mask .. ".png^[makealpha:0,0,0)") b:click(function(self, state2) state2.banner:push_transform({ texture = state2.current_color, From 9b5962aba420ec9356081336a10219bca5f77ed6 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 07:42:41 +0100 Subject: [PATCH 23/39] banner always exists even if only "" it exists and there isn't an error. not adding "" or texture string, causes a non critical error. --- init.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/init.lua b/init.lua index 427294c..31e3eb4 100644 --- a/init.lua +++ b/init.lua @@ -261,9 +261,7 @@ banners.banner_on_activate = function(self) yaw = 4.71238898038469 -- 3 * pi / 2 end self.object:set_yaw(yaw) - if banner then - set_banner_texture(self.object, banner) - end + set_banner_texture(self.object, banner) end core.register_entity("banners:banner_ent", { From d86c93bcc5d7f3fb4c7e38ff165fd7a4923d1b1e Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 09:49:40 +0100 Subject: [PATCH 24/39] use same function declaration style throughout --- init.lua | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/init.lua b/init.lua index 31e3eb4..c1006af 100644 --- a/init.lua +++ b/init.lua @@ -52,19 +52,19 @@ banners.base_transform = { mask = "mask_background.png" } -banners.creation_form_func = function(state) +function banners.creation_form_func(state) -- helper functions - state.update_player_inv = function(self, transform_string) + function state:update_player_inv(transform_string) local player = core.get_player_by_name(self.player) local newbanner = player:get_wielded_item() newbanner:get_meta():set_string("", transform_string) player:set_wielded_item(newbanner) end - state.update_preview = function(self, transform_string) + function state:update_preview(transform_string) self:get("banner_preview"):setImage(transform_string) self:get("color_indicator"):setImage(self.current_color) end - state.update_preview_inv = function(self) + function state:update_preview_inv() local transform_string = self.banner:get_transform_string() self:update_preview(transform_string) self:update_player_inv(transform_string) @@ -154,16 +154,13 @@ function banners.Banner:new(banner) self.__index = self return banner end -function banners.Banner.push_transform(self, transform) table.insert(self.transforms, transform) if #self.transforms > banners.max_undo_levels then table.remove(self.transforms, 1) end end -function banners.Banner.pop_transform(self) table.remove(self.transforms) end -function banners.Banner.get_transform_string(self) local final = {} local used = {} local transform @@ -185,11 +182,14 @@ function banners.Banner.get_transform_string(self) until i == 0 local ret = table.concat(final, "^") return ret +function banners.Banner:push_transform(transform) +function banners.Banner:pop_transform() +function banners.Banner:get_transform_string() end -- helper function for determining the flag's direction -- (pos, pointed_thing) -banners.determine_flag_direction = function(_, pointed_thing) +function banners.determine_flag_direction(_, pointed_thing) local above = pointed_thing.above local under = pointed_thing.under local dir = { @@ -201,13 +201,13 @@ banners.determine_flag_direction = function(_, pointed_thing) end -- (itemstack, player, pointed_thing) -banners.banner_on_use = function(_, player) +function banners.banner_on_use(_, player) if player.is_player then banners.creation_form:show(player:get_player_name()) end end -banners.banner_on_dig = function(pos, node, player) +function banners.banner_on_dig(pos, node, player) if not player or core.is_protected(pos, player:get_player_name()) then return end @@ -223,7 +223,7 @@ banners.banner_on_dig = function(pos, node, player) end -- (pos, node, player) -banners.banner_on_destruct = function(pos) +function banners.banner_on_destruct(pos) local objects = core.get_objects_inside_radius(pos, 0.5) for _, v in ipairs(objects) do local e = v:get_luaentity() @@ -234,7 +234,7 @@ banners.banner_on_destruct = function(pos) end -- (pos, player, itemstack, pointed_thing) -banners.banner_after_place = function(pos, _, itemstack, pointed_thing) +function banners.banner_after_place(pos, _, itemstack, pointed_thing) core.get_node(pos).param2 = banners.determine_flag_direction(pos, pointed_thing) core.get_meta(pos):set_string("banner", itemstack:get_meta():get_string("")) core.add_entity(pos, "banners:banner_ent") @@ -246,7 +246,7 @@ local set_banner_texture = function(obj, texture) end -banners.banner_on_activate = function(self) +function banners:banner_on_activate() local pos = self.object:get_pos() local banner = core.get_meta(pos):get_string("banner") local banner_face = core.get_node(pos).param2 From 3d5805c8a9bb15b65f014b31e8d7385a12a4b277 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 09:51:03 +0100 Subject: [PATCH 25/39] remove set_banner_texture() only used once and is easy enough to have inline --- init.lua | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/init.lua b/init.lua index c1006af..6bf19d5 100644 --- a/init.lua +++ b/init.lua @@ -241,10 +241,6 @@ function banners.banner_after_place(pos, _, itemstack, pointed_thing) end -- banner entity -local set_banner_texture = function(obj, texture) - obj:set_properties({ textures = { "banner_uv_text.png^" .. texture } }) -end - function banners:banner_on_activate() local pos = self.object:get_pos() @@ -261,7 +257,9 @@ function banners:banner_on_activate() yaw = 4.71238898038469 -- 3 * pi / 2 end self.object:set_yaw(yaw) - set_banner_texture(self.object, banner) + self.object:set_properties({ + textures = { "banner_uv_text.png^" .. banner } + }) end core.register_entity("banners:banner_ent", { From 2fea87dbde33c79f638460c1d4c88a6ce85fa7cc Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 09:56:17 +0100 Subject: [PATCH 26/39] preserve player undo-history over joins player could have timed-out and thus lost their history. It isn't crucial to remove their history, so let's give them this feature. --- init.lua | 4 ---- 1 file changed, 4 deletions(-) diff --git a/init.lua b/init.lua index 6bf19d5..e3a737e 100644 --- a/init.lua +++ b/init.lua @@ -272,10 +272,6 @@ core.register_entity("banners:banner_ent", { on_activate = banners.banner_on_activate, }) -core.register_on_leaveplayer(function(player) - histories[player:get_player_name()] = nil -end) - if core.get_modpath("factions") then dofile(MP .. "factions.lua") end From 1c5b11609b6ec287bbd3a50a0be23bc281815d42 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 10:04:22 +0100 Subject: [PATCH 27/39] refactor: reusable transformation string creation unfortunately the git diff is a bit messy on this one. Nothing changed with: - banners.Banner - banners.Banner:new - banners.Banner:push_transform - banners.Banner:pop_transform content of banners.Banner:get_transform_string was moved to banners.transform_table_to_string with very few changes --- init.lua | 53 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 20 deletions(-) diff --git a/init.lua b/init.lua index e3a737e..f5ba3dc 100644 --- a/init.lua +++ b/init.lua @@ -144,31 +144,20 @@ end banners.creation_form = smartfs.create("banners:banner_creation", banners.creation_form_func) - --- banner definition -banners.Banner = {} - -function banners.Banner:new(banner) - banner = banner or { color = "bg_black.png", transforms = {} } - setmetatable(banner, self) - self.__index = self - return banner -end - table.insert(self.transforms, transform) - if #self.transforms > banners.max_undo_levels then - table.remove(self.transforms, 1) end end - table.remove(self.transforms) -end + +function banners.transform_table_to_string(transforms) local final = {} local used = {} local transform -- work backwards to keep resulting data small - local i = #self.transforms + local i = #transforms + if 0 == i then return "" end + repeat - transform = self.transforms[i] - -- same mask can be trimmed out only using most recent + transform = transforms[i] + -- duplicate mask can be trimmed out only use most recent if not used[transform.mask] then used[transform.mask] = true table.insert(final, 1, "(" .. transform.texture @@ -180,11 +169,35 @@ end end i = i - 1 until i == 0 - local ret = table.concat(final, "^") - return ret + return table.concat(final, "^") +end + +-- banner definition +banners.Banner = {} + +function banners.Banner:new(banner) +p('new') + banner = banner or { color = "bg_pink.png", transforms = {} } + setmetatable(banner, self) + self.__index = self + return banner +end + function banners.Banner:push_transform(transform) + table.insert(self.transforms, transform) + if #self.transforms > banners.max_undo_levels then + table.remove(self.transforms, 1) + end +end + function banners.Banner:pop_transform() + table.remove(self.transforms) +end + function banners.Banner:get_transform_string() + return banners.transform_table_to_string(self.transforms) +end + end -- helper function for determining the flag's direction From e3c216eba7c0cddd1b07f405e8480e6a747a93e3 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 10:07:32 +0100 Subject: [PATCH 28/39] add transform_string_to_table allows us to add more features such as: - clean existing banners to slim down data - read transforms from items in inventory -> allow players to change existing patterns without having to start from scratch. --- init.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/init.lua b/init.lua index f5ba3dc..d909cec 100644 --- a/init.lua +++ b/init.lua @@ -47,6 +47,25 @@ banners.colors = { "brown", "darkbrown" } +local valid_masks = {} +local valid_colors = {} +do + local i, s + i = #banners.masks + repeat + s = banners.masks[i] + valid_masks[s .. ".png"] = true + i = i - 1 + until i == 0 + + i = #banners.colors + repeat + s = banners.colors[i] + valid_colors["bg_" .. s .. ".png"] = true + i = i - 1 + until i == 0 +end + banners.base_transform = { texture = "bg_white.png", mask = "mask_background.png" @@ -144,7 +163,23 @@ end banners.creation_form = smartfs.create("banners:banner_creation", banners.creation_form_func) +function banners.transform_string_to_table(transform_string) +p('transform_string_to_table') + local transforms = {} + for part in transform_string:gmatch("%(([^%)]+)%)") do + parts = part:split("^[") + if 3 == #parts then + texture = parts[1] + mask = parts[2]:sub(6) + if valid_masks[mask] and valid_colors[texture] then + table.insert(transforms, { + texture = texture, + mask = mask + }) + end + end end + return transforms end function banners.transform_table_to_string(transforms) From 88d628d73fa5250feabd6bf04a787a5c2e6d87ce Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 10:09:31 +0100 Subject: [PATCH 29/39] cleanup old banners when their entities are generated --- init.lua | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/init.lua b/init.lua index d909cec..d2b06a3 100644 --- a/init.lua +++ b/init.lua @@ -284,7 +284,9 @@ end -- (pos, player, itemstack, pointed_thing) function banners.banner_after_place(pos, _, itemstack, pointed_thing) core.get_node(pos).param2 = banners.determine_flag_direction(pos, pointed_thing) - core.get_meta(pos):set_string("banner", itemstack:get_meta():get_string("")) + local meta = core.get_meta(pos) + meta:set_string("banner", itemstack:get_meta():get_string("")) + meta:set_float("version", banners.version) core.add_entity(pos, "banners:banner_ent") end @@ -292,7 +294,15 @@ end function banners:banner_on_activate() local pos = self.object:get_pos() - local banner = core.get_meta(pos):get_string("banner") + local meta = core.get_meta(pos) + local banner = meta:get_string("banner") + -- cleanup meta of old banners + if meta:get_float("version") < 20241122 then + meta:set_float("version", banners.version) + banner = banners.transform_table_to_string( + banners.transform_string_to_table(banner)) + meta:set_string("banner", banner) + end local banner_face = core.get_node(pos).param2 local yaw = 0. if banner_face == 2 then From 578bd700d1ee414a09cfce9c2cd0d9a1d1536d1b Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 10:11:33 +0100 Subject: [PATCH 30/39] read item meta allowing players to edit existing banners --- init.lua | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/init.lua b/init.lua index d2b06a3..553d301 100644 --- a/init.lua +++ b/init.lua @@ -97,6 +97,7 @@ function banners.creation_form_func(state) state.banner:push_transform(banners.base_transform) histories[state.player] = state.banner end + state.banner:read_item(state.player) state.current_color = state.banner.color state:size(20, 10) state:image(3, 0.4, 4, 2, "banner_preview", nil) @@ -233,6 +234,22 @@ function banners.Banner:get_transform_string() return banners.transform_table_to_string(self.transforms) end +function banners.Banner:read_item(player_name) + local player = core.get_player_by_name(player_name) + local item = player:get_wielded_item() + if "banners:" ~= item:get_name():sub(1, 8) then return end + + local parts, mask, texture + local transforms = banners.transform_string_to_table( + item:get_meta():get_string("")) + local total = #transforms + if 0 == total then return end + + local i = 1 + repeat + self:push_transform(transforms[i]) + i = i + 1 + until i > total end -- helper function for determining the flag's direction From 4178bd1501102099ba3f95ebbca04a69373cf8c1 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 10:16:30 +0100 Subject: [PATCH 31/39] version bump --- init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.lua b/init.lua index 553d301..ab2c398 100644 --- a/init.lua +++ b/init.lua @@ -2,7 +2,7 @@ local MP = core.get_modpath("banners") .. "/" dofile(MP .. "smartfs.lua") banners = { - version = 20241128.1533 + version = 20241129.1012 } banners.masks = { From 143e549ff9aba7a663d01f046cb5290c2ed9997c Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 10:27:50 +0100 Subject: [PATCH 32/39] oopsie fixes --- init.lua | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/init.lua b/init.lua index ab2c398..f6be842 100644 --- a/init.lua +++ b/init.lua @@ -165,7 +165,7 @@ banners.creation_form = smartfs.create("banners:banner_creation", banners.creation_form_func) function banners.transform_string_to_table(transform_string) -p('transform_string_to_table') + local mask, parts, texture local transforms = {} for part in transform_string:gmatch("%(([^%)]+)%)") do parts = part:split("^[") @@ -184,13 +184,13 @@ p('transform_string_to_table') end function banners.transform_table_to_string(transforms) + local i = #transforms + if 0 == i then return "" end + local final = {} local used = {} local transform -- work backwards to keep resulting data small - local i = #transforms - if 0 == i then return "" end - repeat transform = transforms[i] -- duplicate mask can be trimmed out only use most recent @@ -213,7 +213,7 @@ banners.Banner = {} function banners.Banner:new(banner) p('new') - banner = banner or { color = "bg_pink.png", transforms = {} } + banner = banner or { color = "bg_black.png", transforms = {} } setmetatable(banner, self) self.__index = self return banner @@ -239,7 +239,6 @@ function banners.Banner:read_item(player_name) local item = player:get_wielded_item() if "banners:" ~= item:get_name():sub(1, 8) then return end - local parts, mask, texture local transforms = banners.transform_string_to_table( item:get_meta():get_string("")) local total = #transforms From 767a49a43296623c7c5f8933a39f394d94cff3e5 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 10:38:17 +0100 Subject: [PATCH 33/39] remove stray debug point --- init.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/init.lua b/init.lua index f6be842..1d889c3 100644 --- a/init.lua +++ b/init.lua @@ -212,7 +212,6 @@ end banners.Banner = {} function banners.Banner:new(banner) -p('new') banner = banner or { color = "bg_black.png", transforms = {} } setmetatable(banner, self) self.__index = self From fe72fc27a3763ff248f548e18efbfaa847e446b6 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 11:59:11 +0100 Subject: [PATCH 34/39] add chatcommand banners_fix fixes #7 provides a work-around for the engine problem with entities. --- .luacheckrc | 2 ++ chatcommands.lua | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ init.lua | 1 + 3 files changed, 56 insertions(+) create mode 100644 chatcommands.lua diff --git a/.luacheckrc b/.luacheckrc index 77837ad..1f118a0 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -8,5 +8,7 @@ read_globals = { "dump", "factions", "inventory_plus", + "table" = { "insert_all" }, "unified_inventory", + "vector", } diff --git a/chatcommands.lua b/chatcommands.lua new file mode 100644 index 0000000..0fdcbd0 --- /dev/null +++ b/chatcommands.lua @@ -0,0 +1,53 @@ +-- Due to some engine troubles there are sometimes stray +-- banner entities and more rarely there are banner nodes without entities. +-- Calling this command fixes both situations. +core.register_chatcommand("banners_fix", { + description = "recreates the banner-visuals in your area", + func = function(name) + local player = core.get_player_by_name(name) + if not player then + return + end + + local pos = player:get_pos() + local t1 = core.get_us_time() + + local radius = 10 + local entity_count = 0 + local objects = core.get_objects_inside_radius(pos, radius) + for _, v in ipairs(objects) do + local e = v:get_luaentity() + if e and e.name == "banners:banner_ent" then + entity_count = entity_count + 1 + v:remove() + end + end + + local pos1 = vector.subtract(pos, radius) + local pos2 = vector.add(pos, radius) + local nodes = { + "banners:wooden_banner", + "banners:steel_banner", + } + if core.get_modpath("factions") then + table.insert_all(nodes, { + "banners:power_banner", + "banners:death_banner", + + }) + end + local pos_list = core.find_nodes_in_area(pos1, pos2, nodes) + + for _, node_pos in ipairs(pos_list) do + core.add_entity(node_pos, "banners:banner_ent") + end + + local t2 = core.get_us_time() + local diff = t2 - t1 + local millis = diff / 1000 + + return true, "Removed " .. entity_count .. " banner entities and restored " + .. #pos_list .. " banners in " .. millis .. " ms" + end +}) + diff --git a/init.lua b/init.lua index 1d889c3..4ada229 100644 --- a/init.lua +++ b/init.lua @@ -352,4 +352,5 @@ end dofile(MP .. "items.lua") dofile(MP .. "nodes.lua") dofile(MP .. "crafts.lua") +dofile(MP .. "chatcommands.lua") From 088f94b76a7eafdfc5c8dd837a3ac080a9fdf39f Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 12:02:58 +0100 Subject: [PATCH 35/39] sensitive github luacheck syntax --- .luacheckrc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.luacheckrc b/.luacheckrc index 1f118a0..a819f33 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -8,7 +8,7 @@ read_globals = { "dump", "factions", "inventory_plus", - "table" = { "insert_all" }, + ["table"] = { fields = { "insert_all" } }, "unified_inventory", "vector", } From 80aff0dc7dc80a203ee6dead0245526fd141b827 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Fri, 29 Nov 2024 12:32:58 +0100 Subject: [PATCH 36/39] version bump --- init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init.lua b/init.lua index 4ada229..e806261 100644 --- a/init.lua +++ b/init.lua @@ -2,7 +2,7 @@ local MP = core.get_modpath("banners") .. "/" dofile(MP .. "smartfs.lua") banners = { - version = 20241129.1012 + version = 20241129.1232 } banners.masks = { From cb6bfafc2c8e4f016865f7bae88b47864b0fe2ec Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Sat, 30 Nov 2024 19:00:06 +0100 Subject: [PATCH 37/39] cleanup: compact node definitions --- nodes.lua | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/nodes.lua b/nodes.lua index ad2991c..462d36c 100644 --- a/nodes.lua +++ b/nodes.lua @@ -10,18 +10,10 @@ core.register_node("banners:wooden_banner", { stack_max = 1, paramtype = "light", paramtype2 = "facedir", - after_place_node = function (pos, player, itemstack, pointed_thing) - banners.banner_after_place(pos, player, itemstack, pointed_thing) - end, - on_destruct = function(pos) - banners.banner_on_destruct(pos) - end, - on_use = function(i, p, pt) - banners.banner_on_use(i, p, pt) - end, - on_dig = function(pos, n, p) - banners.banner_on_dig(pos, n, p) - end + after_place_node = banners.banner_after_place, + on_destruct = banners.banner_on_destruct, + on_use = banners.banner_on_use, + on_dig = banners.banner_on_dig, }) -- steel banner @@ -36,18 +28,9 @@ core.register_node("banners:steel_banner", { stack_max = 1, paramtype = "light", paramtype2 = "facedir", - after_place_node = function (pos, player, itemstack, pointed_thing) - banners.banner_after_place(pos, player, itemstack, pointed_thing) - end, - on_destruct = function(pos) - banners.banner_on_destruct(pos) - end, - on_use = function(i, p, pt) - banners.banner_on_use(i, p, pt) - end, - on_dig = function(pos, n, p) - banners.banner_on_dig(pos, n, p) - end - + after_place_node = banners.banner_after_place, + on_destruct = banners.banner_on_destruct, + on_use = banners.banner_on_use, + on_dig = banners.banner_on_dig, }) From f448d73c841105eeb933c8282d65e561634b0bc7 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Sat, 30 Nov 2024 19:21:28 +0100 Subject: [PATCH 38/39] adds [jumpdrive] compat github.com/mt-mods/jumpdrive closes https://github.com/mt-mods/jumpdrive/issues/90 --- init.lua | 8 +++++++- nodes.lua | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/init.lua b/init.lua index e806261..7c34a1e 100644 --- a/init.lua +++ b/init.lua @@ -2,7 +2,7 @@ local MP = core.get_modpath("banners") .. "/" dofile(MP .. "smartfs.lua") banners = { - version = 20241129.1232 + version = 20241130.1920 } banners.masks = { @@ -305,6 +305,12 @@ function banners.banner_after_place(pos, _, itemstack, pointed_thing) core.add_entity(pos, "banners:banner_ent") end +-- [jumpdrive] compat +-- (from_pos, to_pos, additional_info) +function banners.banner_on_movenode(_, to_pos) + core.add_entity(to_pos, "banners:banner_ent") +end + -- banner entity function banners:banner_on_activate() diff --git a/nodes.lua b/nodes.lua index 462d36c..3dd86aa 100644 --- a/nodes.lua +++ b/nodes.lua @@ -14,6 +14,7 @@ core.register_node("banners:wooden_banner", { on_destruct = banners.banner_on_destruct, on_use = banners.banner_on_use, on_dig = banners.banner_on_dig, + on_movenode = banners.banner_on_movenode, }) -- steel banner @@ -32,5 +33,6 @@ core.register_node("banners:steel_banner", { on_destruct = banners.banner_on_destruct, on_use = banners.banner_on_use, on_dig = banners.banner_on_dig, + on_movenode = banners.banner_on_movenode, }) From d0989cb62f1d6843a1fb4c156bacd2859409fbed Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Sat, 30 Nov 2024 19:54:56 +0100 Subject: [PATCH 39/39] also make factions banners jump-able just for completeness sake --- factions.lua | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/factions.lua b/factions.lua index aab0cf1..1abbd70 100644 --- a/factions.lua +++ b/factions.lua @@ -143,12 +143,8 @@ core.register_node("banners:power_banner", { stack_max = 1, paramtype = "light", paramtype2 = "facedir", - after_place_node = function (pos, player, itemstack, pointed_thing) - banners.after_powerbanner_placed(pos, player, itemstack, pointed_thing) - end, - on_destruct = function(pos) - banners.banner_on_destruct(pos) - end, + after_place_node = banners.after_powerbanner_placed, + on_destruct = banners.banner_on_destruct, on_dig = function(pos, n, p) if core.is_protected(pos, p:get_player_name()) then return @@ -163,6 +159,7 @@ core.register_node("banners:power_banner", { end banners.banner_on_dig(pos, n, p) end, + on_movenode = banners.banner_on_movenode, }) core.register_node("banners:death_banner", { @@ -176,12 +173,8 @@ core.register_node("banners:death_banner", { stack_max = 1, paramtype = "light", paramtype2 = "facedir", - after_place_node = function (pos, player, itemstack, pointed_thing) - banners.after_deathbanner_placed(pos, player, itemstack, pointed_thing) - end, - on_destruct = function(pos) - banners.banner_on_destruct(pos) - end, + after_place_node = banners.after_deathbanner_placed, + on_destruct = banners.banner_on_destruct, -- (pos, node, player) on_dig = function(pos, _, player) if core.is_protected(pos, player:get_player_name()) then @@ -198,6 +191,7 @@ core.register_node("banners:death_banner", { end core.remove_node(pos) end, + on_movenode = banners.banner_on_movenode, }) -- (pos, player, itemstack, pointed_thing)