microexpansion/register.lua

104 lines
2.2 KiB
Lua
Raw Normal View History

2020-06-17 09:30:47 +02:00
-- register.lua
2017-07-25 19:47:25 +02:00
--[[ Machine Registration API ]]
local me = microexpansion
local power = me.power
2023-11-04 18:00:17 +01:00
if me.uinv_category_enabled and unified_inventory.registered_categories then
if not unified_inventory.registered_categories["storage"] then
unified_inventory.register_category("storage", {
symbol = "default:chest",
label = "Storage"
})
end
end
2017-07-25 19:47:25 +02:00
-- [function] Register machine
function me.register_machine(itemstring, def)
-- Set after_place_node
local def_afterplace = def.after_place_node
2017-07-25 19:47:25 +02:00
def.after_place_node = function(pos, player)
if def_afterplace then
def_afterplace(pos, player)
2017-07-25 19:47:25 +02:00
end
local meta = minetest.get_meta(pos)
local nodes = me.network.adjacent_connected_nodes(pos)
2017-07-25 19:47:25 +02:00
meta:set_string("infotext", def.description.."\nNo Network")
for _, pos2 in pairs(nodes) do
local id = minetest.get_meta(pos2):get_string("network_id")
if id ~= "" then
meta:set_string("infotext", def.description.."\nNetwork ID: "..id)
meta:set_string("network_id", id)
end
end
-- Trace Network
2020-06-17 09:30:47 +02:00
--power.trace(pos)
2017-07-25 19:47:25 +02:00
-- Set demand
if def.demand then
me.network_set_demand(pos, def.demand)
end
if type(def.machine) == "table" then
2020-06-17 09:30:47 +02:00
if power then
power.add_machine(pos, def.machine)
end
2017-07-25 19:47:25 +02:00
end
end
-- Set on_destruct
local def_destruct = def.on_destruct
2017-07-25 19:47:25 +02:00
def.on_destruct = function(pos, player)
if def_destruct then
def_destruct(pos, player)
2017-07-25 19:47:25 +02:00
end
local meta = minetest.get_meta(pos)
if meta:get_string("network_id") ~= "" then
-- Set demand
me.network_set_demand(pos, 0)
-- Remove item from network
me.network_remove(pos)
-- Retrace Network
2020-06-17 09:30:47 +02:00
--power.trace(pos)
2017-07-25 19:47:25 +02:00
end
end
-- Set connects_to
def.connects_to = {"group:me_connect"}
-- Set me_connect group
def.groups = def.groups or {}
def.groups.me_connect = 1
me.register_node(itemstring, def)
end
-- [function] Get machine definition
function me.get_def(name, key)
if type(name) == "table" then
local node = me.get_node(name)
if node then
name = node.name
end
end
local def = minetest.registered_nodes[name]
-- Check name and if registered
if not name or not def then
return
end
if key then
return def[key]
else
return def
end
end
2020-06-03 15:29:04 +02:00
microexpansion.log("Machine Registration API loaded")