Update llm_materials_context.lua
Signed-off-by: H5N3RG <janguenni13@web.de>
This commit is contained in:
@@ -2,59 +2,77 @@
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
|
-- Cache for materials to avoid recomputation
|
||||||
|
local materials_cache = nil
|
||||||
|
local materials_cache_hash = nil
|
||||||
|
|
||||||
|
-- Compute a hash for registered items to detect changes
|
||||||
|
local function compute_materials_hash()
|
||||||
|
local str = ""
|
||||||
|
for name, _ in pairs(core.registered_nodes) do str = str .. name end
|
||||||
|
for name, _ in pairs(core.registered_craftitems) do str = str .. name end
|
||||||
|
for name, _ in pairs(core.registered_tools) do str = str .. name end
|
||||||
|
for name, _ in pairs(core.registered_entities) do str = str .. name end
|
||||||
|
return core.sha1(str)
|
||||||
|
end
|
||||||
|
|
||||||
-- Function to collect available materials
|
-- Function to collect available materials
|
||||||
function M.get_available_materials()
|
function M.get_available_materials()
|
||||||
|
local current_hash = compute_materials_hash()
|
||||||
|
if materials_cache and materials_cache_hash == current_hash then
|
||||||
|
return materials_cache
|
||||||
|
end
|
||||||
|
|
||||||
local materials_info = {}
|
local materials_info = {}
|
||||||
local current_mod_name = core.get_current_modname() -- Useful for debugging or filtering
|
local current_mod_name = core.get_current_modname()
|
||||||
|
|
||||||
-- Collect nodes
|
-- Collect nodes
|
||||||
for name, def in pairs(core.registered_nodes) do
|
for name, def in pairs(core.registered_nodes) do
|
||||||
-- Filter out builtin nodes that aren't really "materials"
|
|
||||||
if not name:match("^__builtin:") and not name:match("^ignore$") and not name:match("^air$") then
|
if not name:match("^__builtin:") and not name:match("^ignore$") and not name:match("^air$") then
|
||||||
table.insert(materials_info, " - Node: " .. name .. " (Description: " .. (def.description or "N/A") .. ")")
|
table.insert(materials_info, "Node: " .. name)
|
||||||
-- Optional: Add other relevant info like groups
|
|
||||||
-- table.insert(materials_info, " Groups: " .. core.privs_to_string(def.groups))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Collect craftitems
|
-- Collect craftitems
|
||||||
for name, def in pairs(core.registered_craftitems) do
|
for name, def in pairs(core.registered_craftitems) do
|
||||||
if not name:match("^__builtin:") then
|
if not name:match("^__builtin:") then
|
||||||
table.insert(materials_info, " - Craftitem: " .. name .. " (Description: " .. (def.description or "N/A") .. ")")
|
table.insert(materials_info, "Craftitem: " .. name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Collect tools
|
-- Collect tools
|
||||||
for name, def in pairs(core.registered_tools) do
|
for name, def in pairs(core.registered_tools) do
|
||||||
if not name:match("^__builtin:") then
|
if not name:match("^__builtin:") then
|
||||||
table.insert(materials_info, " - Tool: " .. name .. " (Description: " .. (def.description or "N/A") .. ")")
|
table.insert(materials_info, "Tool: " .. name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Collect entities
|
-- Collect entities
|
||||||
for name, def in pairs(core.registered_entities) do
|
for name, def in pairs(core.registered_entities) do
|
||||||
if not name:match("^__builtin:") then
|
if not name:match("^__builtin:") then
|
||||||
table.insert(materials_info, " - Entity: " .. name .. " (Description: " .. (def.description or "N/A") .. ")")
|
table.insert(materials_info, "Entity: " .. name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Limit the output to avoid exceeding the LLM's token limits
|
-- Limit the output
|
||||||
local max_items_to_list = 100 -- You can adjust this value
|
local max_items_to_list = 50 -- Reduced for token efficiency
|
||||||
local total_items = #materials_info
|
local total_items = #materials_info
|
||||||
local output_string = ""
|
local output_string = ""
|
||||||
|
|
||||||
if total_items > 0 then
|
if total_items > 0 then
|
||||||
output_string = "Registered materials (" .. total_items .. " in total):\n"
|
output_string = "Registered materials (" .. total_items .. " in total):\n"
|
||||||
for i = 1, math.min(total_items, max_items_to_list) do
|
for i = 1, math.min(total_items, max_items_to_list) do
|
||||||
output_string = output_string .. materials_info[i] .. "\n"
|
output_string = output_string .. " - " .. materials_info[i] .. "\n"
|
||||||
end
|
end
|
||||||
if total_items > max_items_to_list then
|
if total_items > max_items_to_list then
|
||||||
output_string = output_string .. " ... and " .. (total_items - max_items_to_list) .. " more materials (truncated for brevity).\n"
|
output_string = output_string .. " ... and " .. (total_items - max_items_to_list) .. " more materials (truncated).\n"
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
output_string = "No registered materials found.\n"
|
output_string = "No registered materials found.\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
materials_cache = output_string
|
||||||
|
materials_cache_hash = current_hash
|
||||||
return output_string
|
return output_string
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user