A bunch of improvements
*Updated UI to match latest minetest-game *An extra row of items, so less pages *Option to filter items, so no more scrolling through 100 pages *Option to show item strings ("default:glass") as tooltips *Option to show all items, not just craftable items *Show number of items output by craft (with itemstrings only for now) *Probably some other things I forgotmaster
parent
6b9c00c838
commit
ec79a38dab
165
zcg/init.lua
165
zcg/init.lua
|
@ -6,7 +6,13 @@ zcg = {}
|
||||||
|
|
||||||
zcg.users = {}
|
zcg.users = {}
|
||||||
zcg.crafts = {}
|
zcg.crafts = {}
|
||||||
zcg.itemlist = {}
|
|
||||||
|
-- List of all items that can be crafted
|
||||||
|
zcg.craftableitems = {}
|
||||||
|
|
||||||
|
-- List of all items
|
||||||
|
zcg.allitems = {}
|
||||||
|
|
||||||
|
|
||||||
zcg.items_in_group = function(group)
|
zcg.items_in_group = function(group)
|
||||||
local items = {}
|
local items = {}
|
||||||
|
@ -36,49 +42,52 @@ zcg.add_craft = function(input, output, groups)
|
||||||
if minetest.get_item_group(output, "not_in_craft_guide") > 0 then
|
if minetest.get_item_group(output, "not_in_craft_guide") > 0 then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
if not input.items then return end
|
||||||
if not groups then groups = {} end
|
if not groups then groups = {} end
|
||||||
local c = {}
|
local c = {}
|
||||||
c.width = input.width
|
c.width = input.width
|
||||||
|
if c.width == 0 then c.width=3 end
|
||||||
c.type = input.type
|
c.type = input.type
|
||||||
c.items = input.items
|
c.items = input.items
|
||||||
if c.items == nil then return end
|
c.makes, _ = input.output:match("%S+ (%d+)") or 1
|
||||||
|
|
||||||
for i, item in pairs(c.items) do
|
for i, item in pairs(c.items) do
|
||||||
if item:sub(0,6) == "group:" then
|
if item:sub(1,6) == "group:" then
|
||||||
local groupname = item:sub(7)
|
local groupname = item:sub(7)
|
||||||
if groups[groupname] ~= nil then
|
if groups[groupname] then
|
||||||
c.items[i] = groups[groupname]
|
c.items[i] = groups[groupname]
|
||||||
else
|
else
|
||||||
for _, gi in ipairs(zcg.items_in_group(groupname)) do
|
for _, gi in ipairs(zcg.items_in_group(groupname)) do
|
||||||
local g2 = groups
|
local g2 = table_copy(groups)
|
||||||
g2[groupname] = gi
|
g2[groupname] = gi
|
||||||
zcg.add_craft({
|
zcg.add_craft({
|
||||||
width = c.width,
|
width = c.width,
|
||||||
type = c.type,
|
type = c.type,
|
||||||
items = table_copy(c.items)
|
items = table_copy(c.items),
|
||||||
}, output, g2) -- it is needed to copy the table, else groups won't work right
|
output = input.output
|
||||||
|
}, output, g2)
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if c.width == 0 then c.width = 3 end
|
|
||||||
table.insert(zcg.crafts[output], c)
|
table.insert(zcg.crafts[output], c)
|
||||||
end
|
end
|
||||||
|
|
||||||
zcg.load_crafts = function(name)
|
zcg.load_crafts = function(name)
|
||||||
zcg.crafts[name] = {}
|
zcg.crafts[name] = {}
|
||||||
local _recipes = minetest.get_all_craft_recipes(name)
|
local recipes = minetest.get_all_craft_recipes(name)
|
||||||
if _recipes then
|
if recipes then
|
||||||
for i, recipe in ipairs(_recipes) do
|
for i, recipe in ipairs(recipes) do
|
||||||
if (recipe and recipe.items and recipe.type) then
|
if (recipe and recipe.items and recipe.type) then
|
||||||
zcg.add_craft(recipe, name)
|
zcg.add_craft(recipe, name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if zcg.crafts[name] == nil or #zcg.crafts[name] == 0 then
|
if #zcg.crafts[name] == 0 then
|
||||||
zcg.crafts[name] = nil
|
zcg.crafts[name] = nil
|
||||||
else
|
else
|
||||||
table.insert(zcg.itemlist,name)
|
table.insert(zcg.craftableitems,name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -86,26 +95,32 @@ zcg.need_load_all = true
|
||||||
|
|
||||||
zcg.load_all = function()
|
zcg.load_all = function()
|
||||||
print("Loading all crafts, this may take some time...")
|
print("Loading all crafts, this may take some time...")
|
||||||
local i = 0
|
|
||||||
for name, item in pairs(minetest.registered_items) do
|
for name, item in pairs(minetest.registered_items) do
|
||||||
if (name and name ~= "") then
|
if name and name ~= "" and
|
||||||
|
name ~= "unknown" and
|
||||||
|
name ~= "air" and
|
||||||
|
name ~= "ignore" then
|
||||||
|
table.insert(zcg.allitems, name)
|
||||||
zcg.load_crafts(name)
|
zcg.load_crafts(name)
|
||||||
end
|
end
|
||||||
i = i+1
|
|
||||||
end
|
end
|
||||||
table.sort(zcg.itemlist)
|
table.sort(zcg.craftableitems)
|
||||||
|
table.sort(zcg.allitems)
|
||||||
|
|
||||||
zcg.need_load_all = false
|
zcg.need_load_all = false
|
||||||
|
minetest.log("info", "ZCG:items - "..minetest.write_json(zcg.allitems))
|
||||||
|
minetest.log("info", "ZCG:crafts - "..minetest.write_json(zcg.crafts))
|
||||||
print("All crafts loaded !")
|
print("All crafts loaded !")
|
||||||
end
|
end
|
||||||
|
|
||||||
minetest.register_chatcommand("search_items", {
|
minetest.register_chatcommand("search_items", {
|
||||||
params = "<text>",
|
params = "<text>",
|
||||||
description = "search all craft items",
|
description = "search all items",
|
||||||
privs = {},
|
privs = {},
|
||||||
func = function(name, param)
|
func = function(name, param)
|
||||||
if zcg.need_load_all then zcg.load_all() end
|
if zcg.need_load_all then zcg.load_all() end
|
||||||
local found = false
|
local found = false
|
||||||
for _, itemname in ipairs(zcg.itemlist) do
|
for _, itemname in ipairs(zcg.allitems) do
|
||||||
if itemname:find(param, 1, true) then
|
if itemname:find(param, 1, true) then
|
||||||
minetest.chat_send_player(name, itemname)
|
minetest.chat_send_player(name, itemname)
|
||||||
found = true
|
found = true
|
||||||
|
@ -118,12 +133,47 @@ minetest.register_chatcommand("search_items", {
|
||||||
})
|
})
|
||||||
|
|
||||||
zcg.formspec = function(pn)
|
zcg.formspec = function(pn)
|
||||||
|
|
||||||
if zcg.need_load_all then zcg.load_all() end
|
if zcg.need_load_all then zcg.load_all() end
|
||||||
page = zcg.users[pn].page
|
|
||||||
alt = zcg.users[pn].alt
|
-- Read a few things for easier access...
|
||||||
current_item = zcg.users[pn].current_item
|
local page = zcg.users[pn].page
|
||||||
local formspec = "size[8,7.5]"
|
local alt = zcg.users[pn].alt
|
||||||
|
local current_item = zcg.users[pn].current_item
|
||||||
|
local filter = zcg.users[pn].filter
|
||||||
|
local itemstrings = zcg.users[pn].itemstrings
|
||||||
|
|
||||||
|
local formspec = "size[8,8.5]"
|
||||||
|
.. "bgcolor[#080808BB;true]"
|
||||||
|
.. "background[5,5;1,1;gui_formbg.png;true]"
|
||||||
|
.. "listcolors[#00000069;#5A5A5A;#141318;#30434C;#FFF]"
|
||||||
.. "button[0,0;2,.5;main;Back]"
|
.. "button[0,0;2,.5;main;Back]"
|
||||||
|
|
||||||
|
if zcg.users[pn].settings then
|
||||||
|
formspec = formspec .. "field[0.5,1.5;4,1;filter;Filter;"..filter.."]"
|
||||||
|
local sel = "false"
|
||||||
|
sel = zcg.users[pn].allitems and "true" or "false"
|
||||||
|
formspec = formspec .. "checkbox[0,2.5;zcg_allitems;All items;"..sel.."]"
|
||||||
|
sel = zcg.users[pn].itemstrings and "true" or "false"
|
||||||
|
formspec = formspec .. "checkbox[0,3.5;zcg_itemstrings;Item strings;"..sel.."]"
|
||||||
|
formspec = formspec .. "button[6,7.85;2,1;zcg_update_settings;Update]"
|
||||||
|
return formspec
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Get the filtered and correct list of items...
|
||||||
|
local items
|
||||||
|
local list = zcg.users[pn].allitems and zcg.allitems or zcg.craftableitems
|
||||||
|
if filter == "" then
|
||||||
|
items = list
|
||||||
|
else
|
||||||
|
items = {}
|
||||||
|
for _, item in ipairs(list) do
|
||||||
|
if item:find(filter, 1, true) then
|
||||||
|
table.insert(items, item)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if zcg.users[pn].history.index > 1 then
|
if zcg.users[pn].history.index > 1 then
|
||||||
formspec = formspec .. "image_button[0,1;1,1;zcg_previous.png;zcg_previous;;false;false;zcg_previous_press.png]"
|
formspec = formspec .. "image_button[0,1;1,1;zcg_previous.png;zcg_previous;;false;false;zcg_previous_press.png]"
|
||||||
else
|
else
|
||||||
|
@ -152,6 +202,9 @@ zcg.formspec = function(pn)
|
||||||
local y = 0
|
local y = 0
|
||||||
for i, item in pairs(c.items) do
|
for i, item in pairs(c.items) do
|
||||||
formspec = formspec .. "item_image_button["..((i-1)%c.width+x)..","..(math.floor((i-1)/c.width+y))..";1,1;"..item..";zcg:"..item..";]"
|
formspec = formspec .. "item_image_button["..((i-1)%c.width+x)..","..(math.floor((i-1)/c.width+y))..";1,1;"..item..";zcg:"..item..";]"
|
||||||
|
if itemstrings then
|
||||||
|
formspec = formspec .. "tooltip[zcg:"..item..";"..item.."]"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
if c.type == "normal" or c.type == "cooking" then
|
if c.type == "normal" or c.type == "cooking" then
|
||||||
formspec = formspec .. "image[6,2;1,1;zcg_method_"..c.type..".png]"
|
formspec = formspec .. "image[6,2;1,1;zcg_method_"..c.type..".png]"
|
||||||
|
@ -159,29 +212,49 @@ zcg.formspec = function(pn)
|
||||||
formspec = formspec .. "label[0,2;Method: "..c.type.."]"
|
formspec = formspec .. "label[0,2;Method: "..c.type.."]"
|
||||||
end
|
end
|
||||||
formspec = formspec .. "image[6,1;1,1;zcg_craft_arrow.png]"
|
formspec = formspec .. "image[6,1;1,1;zcg_craft_arrow.png]"
|
||||||
formspec = formspec .. "item_image_button[7,1;1,1;"..zcg.users[pn].current_item..";;]"
|
local itemstr = zcg.users[pn].current_item.." "..c.makes
|
||||||
|
formspec = formspec .. "item_image_button[7,1;1,1;"..
|
||||||
|
itemstr..";output:"..itemstr..";]"
|
||||||
|
if itemstrings then
|
||||||
|
formspec = formspec .. "tooltip[output:"..itemstr..
|
||||||
|
";"..itemstr.."]"
|
||||||
|
end
|
||||||
|
-- if c.makes ~= 1 then
|
||||||
|
-- formspec = formspec .. "label[7,2;"..c.makes.."]"
|
||||||
|
-- end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Node list
|
-- Node list
|
||||||
local npp = 8*3 -- nodes per page
|
local npp = 8*4 -- nodes per page
|
||||||
local i = 0 -- for positionning buttons
|
local i = 0 -- for positioning buttons
|
||||||
local s = 0 -- for skipping pages
|
local s = 0 -- for skipping pages
|
||||||
for _, name in ipairs(zcg.itemlist) do
|
for _, name in ipairs(items) do
|
||||||
if s < page*npp then s = s+1 else
|
if s < page*npp then s = s+1 else
|
||||||
if i >= npp then break end
|
if i >= npp then break end
|
||||||
formspec = formspec .. "item_image_button["..(i%8)..","..(math.floor(i/8)+3.5)..";1,1;"..name..";zcg:"..name..";]"
|
formspec = formspec .. "item_image_button["..(i%8)..","..(math.floor(i/8)+3.5)..";1,1;"..name..";zcg:"..name..";]"
|
||||||
|
if itemstrings then
|
||||||
|
formspec = formspec .. "tooltip[zcg:"..name..";"..name.."]"
|
||||||
|
end
|
||||||
i = i+1
|
i = i+1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if page > 0 then
|
if page > 0 then
|
||||||
formspec = formspec .. "button[0,7;1,.5;zcg_page:"..(page-1)..";<<]"
|
formspec = formspec .. "button[0,8;1,.5;zcg_page:"..(page-1)..";<<]"
|
||||||
end
|
end
|
||||||
if i >= npp then
|
if i >= npp then
|
||||||
formspec = formspec .. "button[1,7;1,.5;zcg_page:"..(page+1)..";>>]"
|
formspec = formspec .. "button[1,8;1,.5;zcg_page:"..(page+1)..";>>]"
|
||||||
end
|
end
|
||||||
formspec = formspec .. "label[2,6.85;Page "..(page+1).."/"..(math.floor(#zcg.itemlist/npp+1)).."]" -- The Y is approximatively the good one to have it centered vertically...
|
|
||||||
|
local label = (page+1).."/"..(math.floor(#items/npp+1))
|
||||||
|
if filter ~= "" then
|
||||||
|
label = label .. " (filter='"..filter.."')"
|
||||||
|
end
|
||||||
|
formspec = formspec .. "label[2,7.85;Page "..label.."]"
|
||||||
|
|
||||||
|
formspec = formspec .. "button[6,7.85;2,1;zcg_settings;Settings]"
|
||||||
|
|
||||||
return formspec
|
return formspec
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -190,11 +263,39 @@ minetest.register_on_joinplayer(function(player)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_on_player_receive_fields(function(player,formname,fields)
|
minetest.register_on_player_receive_fields(function(player,formname,fields)
|
||||||
|
minetest.log("info", "ZCG:"..dump(fields))
|
||||||
pn = player:get_player_name();
|
pn = player:get_player_name();
|
||||||
if zcg.users[pn] == nil then zcg.users[pn] = {current_item = "", alt = 1, page = 0, history={index=0,list={}}} end
|
|
||||||
|
-- Initialise all settings for a player when they're first seen...
|
||||||
|
if not zcg.users[pn] then
|
||||||
|
zcg.users[pn] = {current_item = "",
|
||||||
|
alt = 1,
|
||||||
|
page = 0,
|
||||||
|
history={index=0,list={}},
|
||||||
|
filter="",
|
||||||
|
settings=false,
|
||||||
|
allitems=false,
|
||||||
|
itemstrings=false}
|
||||||
|
end
|
||||||
|
|
||||||
if fields.zcg then
|
if fields.zcg then
|
||||||
inventory_plus.set_inventory_formspec(player, zcg.formspec(pn))
|
inventory_plus.set_inventory_formspec(player, zcg.formspec(pn))
|
||||||
return
|
return
|
||||||
|
elseif fields.zcg_settings then
|
||||||
|
zcg.users[pn].settings = true
|
||||||
|
inventory_plus.set_inventory_formspec(player, zcg.formspec(pn))
|
||||||
|
elseif fields.zcg_update_settings then
|
||||||
|
zcg.users[pn].settings = false
|
||||||
|
zcg.users[pn].filter = fields.filter
|
||||||
|
zcg.users[pn].current_item = ""
|
||||||
|
zcg.users[pn].page = 0
|
||||||
|
inventory_plus.set_inventory_formspec(player, zcg.formspec(pn))
|
||||||
|
elseif fields.zcg_allitems then
|
||||||
|
zcg.users[pn].allitems = fields.zcg_allitems == "true"
|
||||||
|
zcg.users[pn].current_item = ""
|
||||||
|
zcg.users[pn].page = 0
|
||||||
|
elseif fields.zcg_itemstrings then
|
||||||
|
zcg.users[pn].itemstrings = fields.zcg_itemstrings == "true"
|
||||||
elseif fields.zcg_previous then
|
elseif fields.zcg_previous then
|
||||||
if zcg.users[pn].history.index > 1 then
|
if zcg.users[pn].history.index > 1 then
|
||||||
zcg.users[pn].history.index = zcg.users[pn].history.index - 1
|
zcg.users[pn].history.index = zcg.users[pn].history.index - 1
|
||||||
|
@ -211,12 +312,10 @@ minetest.register_on_player_receive_fields(function(player,formname,fields)
|
||||||
for k, v in pairs(fields) do
|
for k, v in pairs(fields) do
|
||||||
if (k:sub(0,4)=="zcg:") then
|
if (k:sub(0,4)=="zcg:") then
|
||||||
local ni = k:sub(5)
|
local ni = k:sub(5)
|
||||||
if zcg.crafts[ni] then
|
|
||||||
zcg.users[pn].current_item = ni
|
zcg.users[pn].current_item = ni
|
||||||
table.insert(zcg.users[pn].history.list, ni)
|
table.insert(zcg.users[pn].history.list, ni)
|
||||||
zcg.users[pn].history.index = #zcg.users[pn].history.list
|
zcg.users[pn].history.index = #zcg.users[pn].history.list
|
||||||
inventory_plus.set_inventory_formspec(player,zcg.formspec(pn))
|
inventory_plus.set_inventory_formspec(player,zcg.formspec(pn))
|
||||||
end
|
|
||||||
elseif (k:sub(0,9)=="zcg_page:") then
|
elseif (k:sub(0,9)=="zcg_page:") then
|
||||||
zcg.users[pn].page = tonumber(k:sub(10))
|
zcg.users[pn].page = tonumber(k:sub(10))
|
||||||
inventory_plus.set_inventory_formspec(player,zcg.formspec(pn))
|
inventory_plus.set_inventory_formspec(player,zcg.formspec(pn))
|
||||||
|
|
Loading…
Reference in New Issue