From f141fbf806e5440fb94052d3b561e7217fcb0427 Mon Sep 17 00:00:00 2001 From: JP Guerrero Date: Tue, 13 Dec 2016 13:10:11 +0100 Subject: [PATCH] Make formspec size a setting in minetest.conf --- README.md | 3 ++- init.lua | 48 +++++++++++++++++++++++------------------------- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 3053d23..5bd932d 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ list the items in the crafting guide for which you already have the ingredients in your inventory. The progressive mode is disabled by default and can be enabled with `craftguide_progressive_mode = true` in `minetest.conf`. -The formspec size is also easily configurable by the user, see [here](https://github.com/minetest-mods/craftguide/blob/master/init.lua#L3-L6). +The formspec size is also easily configurable with this setting in `minetest.conf`: +`craftguide_size = WxH` (default: `8x3`). The minimum accepted size is `8x1`. ![Preview](http://i.imgur.com/xblp1Vs.png) diff --git a/init.lua b/init.lua index 48cabe4..fd3cd44 100644 --- a/init.lua +++ b/init.lua @@ -1,17 +1,14 @@ -local min, ceil, max = math.min, math.ceil, math.max +local craftguide, datas = {}, {} +local min, max, ceil, floor = math.min, math.max, math.ceil, math.floor +local function round(n) return floor((floor(n * 2) + 1) / 2) end ---------- Formspec configuration ----------- -local iX = 8 -- item list width (min. 8) -local iY = 3 -- item list height (min. 1) --------------------------------------------- - -iX, iY = max(8, ceil(iX)), max(1, ceil(iY)) +local progressive_mode = minetest.setting_getbool("craftguide_progressive_mode") +local iX, iY = (minetest.setting_get("craftguide_size") or "8x3"):match( + "([%d]+[.%d+]*)[%s+]*x[%s+]*([%d]+[.%d+]*)") +iX, iY = max(8, round(iX or 8)), max(1, round(iY or 3)) local ipp = iX * iY local offset_X = (iX / 2) + (iX % 2 == 0 and 0.5 or 0) -local craftguide, datas = {}, {} -local progressive_mode = minetest.setting_getbool("craftguide_progressive_mode") - local group_stereotypes = { wool = "wool:white", dye = "dye:white", @@ -259,25 +256,26 @@ minetest.register_on_player_receive_fields(function(player, formname, fields) craftguide:get_formspec(player_name) elseif fields.prev or fields.next then data.pagenum = data.pagenum - (fields.prev and 1 or -1) - data.pagenum = data.pagenum > data.pagemax and 1 or data.pagenum - data.pagenum = data.pagenum < 1 and data.pagemax or data.pagenum + if data.pagenum > data.pagemax then + data.pagenum = 1 + elseif data.pagenum == 0 then + data.pagenum = data.pagemax + end craftguide:get_formspec(player_name) else for item in pairs(fields) do - if not item:find(":") then return end - item = item:sub(-4) == "_inv" and item:sub(1,-5) or item + if not item:find(":") then return end + item = item:sub(-4) == "_inv" and item:sub(1,-5) or item - if minetest.get_craft_recipe(item) then - if progressive_mode then - local _, player_has_item = - craftguide:recipe_in_inv(player_name, item) - if not player_has_item then return end - end + if progressive_mode then + local _, player_has_item = + craftguide:recipe_in_inv(player_name, item) + if not player_has_item then return end + end - data.item = item - data.recipe_num = 1 - data.recipes_item = minetest.get_all_craft_recipes(item) - craftguide:get_formspec(player_name) - end + data.item = item + data.recipe_num = 1 + data.recipes_item = minetest.get_all_craft_recipes(item) + craftguide:get_formspec(player_name) end end end)