2015-02-05 10:03:07 +01:00
|
|
|
local S = unified_inventory.gettext
|
2012-12-02 10:08:32 +01:00
|
|
|
|
2012-12-03 23:36:15 +01:00
|
|
|
-- Create detached creative inventory after loading all mods
|
|
|
|
minetest.after(0.01, function()
|
2014-04-22 21:12:49 +02:00
|
|
|
local rev_aliases = {}
|
|
|
|
for source, target in pairs(minetest.registered_aliases) do
|
|
|
|
if not rev_aliases[target] then rev_aliases[target] = {} end
|
|
|
|
table.insert(rev_aliases[target], source)
|
|
|
|
end
|
2012-12-03 23:36:15 +01:00
|
|
|
unified_inventory.items_list = {}
|
2013-09-29 00:15:37 +02:00
|
|
|
for name, def in pairs(minetest.registered_items) do
|
|
|
|
if (not def.groups.not_in_creative_inventory or
|
|
|
|
def.groups.not_in_creative_inventory == 0) and
|
|
|
|
def.description and def.description ~= "" then
|
2012-12-03 23:36:15 +01:00
|
|
|
table.insert(unified_inventory.items_list, name)
|
2014-04-22 21:12:49 +02:00
|
|
|
local all_names = rev_aliases[name] or {}
|
|
|
|
table.insert(all_names, name)
|
|
|
|
for _, name in ipairs(all_names) do
|
|
|
|
local recipes = minetest.get_all_craft_recipes(name)
|
|
|
|
if recipes then
|
|
|
|
for _, recipe in ipairs(recipes) do
|
2014-04-29 22:38:31 +02:00
|
|
|
unified_inventory.register_craft(recipe)
|
2014-04-22 21:12:49 +02:00
|
|
|
end
|
|
|
|
end
|
2013-10-07 04:16:31 +02:00
|
|
|
end
|
2012-12-03 23:36:15 +01:00
|
|
|
end
|
2012-12-02 10:08:32 +01:00
|
|
|
end
|
2012-12-03 23:36:15 +01:00
|
|
|
table.sort(unified_inventory.items_list)
|
|
|
|
unified_inventory.items_list_size = #unified_inventory.items_list
|
2014-01-19 20:00:49 +01:00
|
|
|
print("Unified Inventory. inventory size: "..unified_inventory.items_list_size)
|
2014-04-29 23:14:08 +02:00
|
|
|
for _, name in ipairs(unified_inventory.items_list) do
|
|
|
|
local def = minetest.registered_items[name]
|
|
|
|
if type(def.drop) == "string" then
|
|
|
|
local dstack = ItemStack(def.drop)
|
|
|
|
if not dstack:is_empty() and dstack:get_name() ~= name then
|
|
|
|
unified_inventory.register_craft({
|
|
|
|
type = "digging",
|
|
|
|
items = {name},
|
|
|
|
output = def.drop,
|
|
|
|
width = 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-06-13 16:04:20 +02:00
|
|
|
for _, recipes in pairs(unified_inventory.crafts_for.recipe) do
|
|
|
|
for _, recipe in ipairs(recipes) do
|
|
|
|
local ingredient_items = {}
|
|
|
|
for _, spec in ipairs(recipe.items) do
|
|
|
|
local matches_spec = unified_inventory.canonical_item_spec_matcher(spec)
|
|
|
|
for _, name in ipairs(unified_inventory.items_list) do
|
|
|
|
if matches_spec(name) then
|
|
|
|
ingredient_items[name] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for name, _ in pairs(ingredient_items) do
|
|
|
|
if unified_inventory.crafts_for.usage[name] == nil then
|
|
|
|
unified_inventory.crafts_for.usage[name] = {}
|
|
|
|
end
|
|
|
|
table.insert(unified_inventory.crafts_for.usage[name], recipe)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-12-03 23:36:15 +01:00
|
|
|
end)
|
2012-12-02 10:08:32 +01:00
|
|
|
|
2013-03-03 16:28:24 +01:00
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
-- load_home
|
|
|
|
local function load_home()
|
|
|
|
local input = io.open(unified_inventory.home_filename, "r")
|
|
|
|
if input then
|
|
|
|
while true do
|
|
|
|
local x = input:read("*n")
|
|
|
|
if x == nil then
|
|
|
|
break
|
2012-12-11 00:48:23 +01:00
|
|
|
end
|
2013-09-29 00:15:37 +02:00
|
|
|
local y = input:read("*n")
|
|
|
|
local z = input:read("*n")
|
|
|
|
local name = input:read("*l")
|
|
|
|
unified_inventory.home_pos[name:sub(2)] = {x = x, y = y, z = z}
|
2012-12-11 00:48:23 +01:00
|
|
|
end
|
2013-09-29 00:15:37 +02:00
|
|
|
io.close(input)
|
|
|
|
else
|
|
|
|
unified_inventory.home_pos = {}
|
2012-12-02 10:08:32 +01:00
|
|
|
end
|
|
|
|
end
|
2013-09-29 00:15:37 +02:00
|
|
|
load_home()
|
2012-12-02 10:08:32 +01:00
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
function unified_inventory.set_home(player, pos)
|
2012-12-02 10:08:32 +01:00
|
|
|
local player_name = player:get_player_name()
|
2013-09-29 00:15:37 +02:00
|
|
|
unified_inventory.home_pos[player_name] = pos
|
2012-12-02 10:08:32 +01:00
|
|
|
-- save the home data from the table to the file
|
2013-09-29 00:15:37 +02:00
|
|
|
local output = io.open(unified_inventory.home_filename, "w")
|
|
|
|
for k, v in pairs(unified_inventory.home_pos) do
|
2012-12-02 10:08:32 +01:00
|
|
|
if v ~= nil then
|
2013-09-29 00:15:37 +02:00
|
|
|
output:write(math.floor(v.x).." "
|
|
|
|
..math.floor(v.y).." "
|
|
|
|
..math.floor(v.z).." "
|
|
|
|
..k.."\n")
|
2012-12-02 10:08:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
io.close(output)
|
|
|
|
end
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
function unified_inventory.go_home(player)
|
|
|
|
local pos = unified_inventory.home_pos[player:get_player_name()]
|
|
|
|
if pos ~= nil then
|
2012-12-02 10:08:32 +01:00
|
|
|
player:setpos(pos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
-- register_craft
|
|
|
|
function unified_inventory.register_craft(options)
|
2013-10-07 04:16:31 +02:00
|
|
|
if options.output == nil then
|
2013-09-29 00:15:37 +02:00
|
|
|
return
|
2012-12-11 00:48:23 +01:00
|
|
|
end
|
2013-09-29 00:15:37 +02:00
|
|
|
local itemstack = ItemStack(options.output)
|
|
|
|
if itemstack:is_empty() then
|
|
|
|
return
|
2012-12-11 00:48:23 +01:00
|
|
|
end
|
2014-04-30 01:11:10 +02:00
|
|
|
if options.type == "normal" and options.width == 0 then
|
|
|
|
options = { type = "shapeless", items = options.items, output = options.output, width = 0 }
|
|
|
|
end
|
2014-06-13 16:04:20 +02:00
|
|
|
if unified_inventory.crafts_for.recipe[itemstack:get_name()] == nil then
|
|
|
|
unified_inventory.crafts_for.recipe[itemstack:get_name()] = {}
|
2013-10-07 04:16:31 +02:00
|
|
|
end
|
2014-06-13 16:04:20 +02:00
|
|
|
table.insert(unified_inventory.crafts_for.recipe[itemstack:get_name()],options)
|
2012-12-11 00:48:23 +01:00
|
|
|
end
|
|
|
|
|
2014-05-12 04:00:32 +02:00
|
|
|
|
2014-04-30 01:11:10 +02:00
|
|
|
local craft_type_defaults = {
|
|
|
|
width = 3,
|
|
|
|
height = 3,
|
|
|
|
uses_crafting_grid = false,
|
|
|
|
}
|
2014-05-12 04:00:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
function unified_inventory.craft_type_defaults(name, options)
|
|
|
|
if not options.description then
|
|
|
|
options.description = name
|
|
|
|
end
|
2014-04-30 01:11:10 +02:00
|
|
|
setmetatable(options, {__index = craft_type_defaults})
|
|
|
|
return options
|
|
|
|
end
|
2014-05-12 04:00:32 +02:00
|
|
|
|
|
|
|
|
2014-04-30 01:11:10 +02:00
|
|
|
function unified_inventory.register_craft_type(name, options)
|
2014-05-12 04:00:32 +02:00
|
|
|
unified_inventory.registered_craft_types[name] =
|
|
|
|
unified_inventory.craft_type_defaults(name, options)
|
2014-04-30 01:11:10 +02:00
|
|
|
end
|
|
|
|
|
2014-05-12 04:00:32 +02:00
|
|
|
|
2014-04-30 01:11:10 +02:00
|
|
|
unified_inventory.register_craft_type("normal", {
|
|
|
|
description = "Crafting",
|
|
|
|
width = 3,
|
|
|
|
height = 3,
|
2014-06-13 13:15:50 +02:00
|
|
|
get_shaped_craft_width = function (craft) return craft.width end,
|
|
|
|
dynamic_display_size = function (craft)
|
|
|
|
local w = craft.width
|
|
|
|
local h = math.ceil(table.maxn(craft.items) / craft.width)
|
|
|
|
local g = w < h and h or w
|
|
|
|
return { width = g, height = g }
|
|
|
|
end,
|
2014-04-30 01:11:10 +02:00
|
|
|
uses_crafting_grid = true,
|
|
|
|
})
|
|
|
|
|
2014-05-12 04:00:32 +02:00
|
|
|
|
2014-04-30 01:11:10 +02:00
|
|
|
unified_inventory.register_craft_type("shapeless", {
|
|
|
|
description = "Mixing",
|
|
|
|
width = 3,
|
|
|
|
height = 3,
|
2014-06-13 13:15:50 +02:00
|
|
|
dynamic_display_size = function (craft)
|
|
|
|
local maxn = table.maxn(craft.items)
|
|
|
|
local g = 1
|
|
|
|
while g*g < maxn do g = g + 1 end
|
|
|
|
return { width = g, height = g }
|
|
|
|
end,
|
2014-04-30 01:11:10 +02:00
|
|
|
uses_crafting_grid = true,
|
|
|
|
})
|
|
|
|
|
2014-05-12 04:00:32 +02:00
|
|
|
|
2014-04-30 01:11:10 +02:00
|
|
|
unified_inventory.register_craft_type("cooking", {
|
|
|
|
description = "Cooking",
|
|
|
|
width = 1,
|
|
|
|
height = 1,
|
|
|
|
})
|
|
|
|
|
2014-05-12 04:00:32 +02:00
|
|
|
|
2014-04-30 01:11:10 +02:00
|
|
|
unified_inventory.register_craft_type("digging", {
|
|
|
|
description = "Digging",
|
|
|
|
width = 1,
|
|
|
|
height = 1,
|
|
|
|
})
|
|
|
|
|
2014-05-01 17:08:24 +02:00
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
function unified_inventory.register_page(name, def)
|
|
|
|
unified_inventory.pages[name] = def
|
2012-12-11 00:48:23 +01:00
|
|
|
end
|
2013-09-21 21:40:20 +02:00
|
|
|
|
2014-05-12 04:00:32 +02:00
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
function unified_inventory.register_button(name, def)
|
|
|
|
if not def.action then
|
|
|
|
def.action = function(player)
|
|
|
|
unified_inventory.set_inventory_formspec(player, name)
|
2013-09-21 21:40:20 +02:00
|
|
|
end
|
|
|
|
end
|
2013-09-29 00:15:37 +02:00
|
|
|
def.name = name
|
|
|
|
table.insert(unified_inventory.buttons, def)
|
2013-09-21 21:40:20 +02:00
|
|
|
end
|
|
|
|
|
2014-05-12 04:00:32 +02:00
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
function unified_inventory.is_creative(playername)
|
|
|
|
if minetest.check_player_privs(playername, {creative=true}) or
|
|
|
|
minetest.setting_getbool("creative_mode") then
|
|
|
|
return true
|
2013-09-21 21:40:20 +02:00
|
|
|
end
|
|
|
|
end
|
2014-05-12 04:00:32 +02:00
|
|
|
|