-
-
Save AntumDeluge/0df910190de698a9d1147f4114feca8d to your computer and use it in GitHub Desktop.
Modified version of rubenwardy's craft debugger to output to debug log: https://gist.github.com/rubenwardy/5504537
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Written by rubenwardy, WTFPL | |
-- To use: | |
-- * Save as mod_debug.lua in builtin/game | |
-- * add dofile(gamepath.."mod_debug.lua") to the last line of builtin/game/init.lua | |
-- OR | |
-- * Install as a mod. Needs to be run before any other mod. | |
local mod = {} | |
mod.recipes = {} | |
mod.aliases = {} | |
-- Sees if there is a node with this group/these groups | |
-- Supports AND view group:name,name | |
local function group_exists(groupname) | |
local flags = groupname:split(",") | |
for name, def in pairs(minetest.registered_items) do | |
local flag = true | |
for k, v in pairs(flags) do | |
local g = def.groups and def.groups[v:gsub('%group:', '')] or 0 | |
if not g or g <= 0 then | |
flag = false | |
break | |
end | |
end | |
if flag then | |
return true | |
end | |
end | |
return false | |
end | |
-- Check that the item exists | |
function mod.assert(recipe, _name, _output) | |
local name = mod.strip_name(_name) | |
local output = mod.strip_name(_output) | |
-- Check if we are outputting a registered item | |
if output == nil then | |
core.log("error", "Recipe output is nil") | |
core.log(recipe.from) | |
return | |
end | |
if mod.aliases[output] ~= nil then | |
output = mod.aliases[output] | |
end | |
if minetest.registered_items[output] == nil and not group_exists(output) then | |
core.log("error", "Unregistered recipe output \"" .. output .. "\"") | |
core.log(recipe.from) | |
end | |
-- Check if ingredients are registered | |
if name == nil then | |
core.log("error", "Ingredient is nil for \"" .. output .. "\" recipe") | |
core.log(recipe.from) | |
return | |
end | |
if mod.aliases[name] ~= nil then | |
name = mod.aliases[name] | |
end | |
if minetest.registered_items[name] == nil and not group_exists(name) then | |
core.log("error", "Unregistered ingredient \"" .. name .. "\" for \"" .. output .. "\" recipe") | |
core.log(recipe.from) | |
end | |
end | |
-- Turns a itemstack name into just its item name | |
-- For example: "mod:item 99" -> "mod:item" | |
function mod.strip_name(name) | |
if name == nil then | |
return | |
end | |
res = name:gsub('%"', '') | |
if res:sub(1, 1) == ":" then | |
res = table.concat{res:sub(1, 1-1), "", res:sub(1+1)} | |
end | |
for str in string.gmatch(res, "([^ ]+)") do | |
if str ~= " " and str ~= nil then | |
res=str | |
break | |
end | |
end | |
if res == nil then | |
res="" | |
end | |
return res | |
end | |
-- Cycles through the recipe table, checking the items. | |
-- Recursive | |
function mod.check_recipe(recipe, table, output) | |
if type(table) == "table" then | |
for i=1,# table do | |
mod.check_recipe(recipe, table[i], output) | |
end | |
else | |
mod.assert(recipe, table,output) | |
end | |
end | |
-- Check recipes once the game has loaded | |
minetest.after(0, function() | |
for i=1, #mod.recipes do | |
if mod.recipes[i] and mod.recipes[i].output then | |
mod.assert(mod.recipes[i], mod.recipes[i].output, mod.recipes[i].output) | |
if type(mod.recipes[i].recipe) == "table" then | |
for a=1,# mod.recipes[i].recipe do | |
mod.check_recipe(mod.recipes[i], mod.recipes[i].recipe[a], mod.recipes[i].output) | |
end | |
else | |
mod.assert(mod.recipes[i], mod.recipes[i].recipe, mod.recipes[i].output) | |
end | |
end | |
end | |
end) | |
-- Override register_craft to catch craft recipes | |
local register_craft = minetest.register_craft | |
minetest.register_craft = function(recipe) | |
register_craft(recipe) | |
local name = mod.strip_name(recipe.output) | |
recipe.from = debug.traceback() | |
if name~=nil then | |
table.insert(mod.recipes, recipe) | |
end | |
end | |
-- Override register_alias to catch aliases | |
local register_alias = minetest.register_alias | |
minetest.register_alias = function(new,old) | |
register_alias(new, old) | |
local name = mod.strip_name(new) | |
local name2 = mod.strip_name(old) | |
if name~=nil and name2~=nil then | |
mod.aliases[new] = old | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment