diff --git a/api.lua b/api.lua new file mode 100644 index 0000000..d17b255 --- /dev/null +++ b/api.lua @@ -0,0 +1,12 @@ +local utils = ... + + + +-- function (spawn_pos, itemstack, owner, spawner_pos, spawner_dir) +function lwcomponents.register_spawner (itemname, spawn_func) + return utils.register_spawner (itemname, spawn_func) +end + + + +-- diff --git a/change.log b/change.log index 47582a4..2c39c40 100644 --- a/change.log +++ b/change.log @@ -26,3 +26,8 @@ v0.1.3 v0.1.4 * Bug fix to spawning owned mobs. + + +v0.1.5 +* Added setting Spawn mobs. +* Added lwcomponents.register_spawner api. diff --git a/dispenser.lua b/dispenser.lua index 14cc655..c2b3190 100644 --- a/dispenser.lua +++ b/dispenser.lua @@ -7,6 +7,22 @@ if utils.digilines_supported or utils.mesecon_supported then +local function dispense_dir (node) + if node.param2 == 0 then + return { x = 0, y = 0, z = -1 } + elseif node.param2 == 1 then + return { x = -1, y = 0, z = 0 } + elseif node.param2 == 2 then + return { x = 0, y = 0, z = 1 } + elseif node.param2 == 3 then + return { x = 1, y = 0, z = 0 } + else + return { x = 0, y = 0, z = 0 } + end +end + + + local function dispense_pos (pos, node) if node.param2 == 0 then return { x = pos.x, y = pos.y, z = pos.z - 1 } @@ -64,8 +80,8 @@ end -local function try_spawn (pos, node, item) - if utils.mobs_supported then +local function try_spawn (pos, node, item, owner) + if utils.mobs_supported and utils.settings.spawn_mobs then local mob = item:get_name () local item_def = minetest.registered_craftitems[mob] local spawn_pos = dispense_pos (pos, node) @@ -80,16 +96,10 @@ local function try_spawn (pos, node, item) local ent = smob and smob:get_luaentity () if ent then - local meta = minetest.get_meta (pos) - - if meta then - local owner = meta:get_string ("owner") - - -- set owner if not a monster - if owner:len () > 0 and ent.type ~= "monster" then - ent.owner = owner - ent.tamed = true - end + -- set owner if not a monster + if owner:len () > 0 and ent.type ~= "monster" then + ent.owner = owner + ent.tamed = true end end @@ -102,16 +112,10 @@ local function try_spawn (pos, node, item) local ent = smob and smob:get_luaentity () if ent then - local meta = minetest.get_meta (pos) - - if meta then - local owner = meta:get_string ("owner") - - -- set owner if not a monster - if owner:len () > 0 and ent.type ~= "monster" then - ent.owner = owner - ent.tamed = true - end + -- set owner if not a monster + if owner:len () > 0 and ent.type ~= "monster" then + ent.owner = owner + ent.tamed = true end end @@ -126,16 +130,10 @@ local function try_spawn (pos, node, item) local ent = smob and smob:get_luaentity () if ent then - local meta = minetest.get_meta (pos) - - if meta then - local owner = meta:get_string ("owner") - - -- set owner if not a monster - if owner:len () > 0 and ent.type ~= "monster" then - ent.owner = owner - ent.tamed = true - end + -- set owner if not a monster + if owner:len () > 0 and ent.type ~= "monster" then + ent.owner = owner + ent.tamed = true end end @@ -148,6 +146,7 @@ local function try_spawn (pos, node, item) end + -- slot: -- nil - next item, no dispense if empty -- number - 1 item from slot, no dispense if empty @@ -202,11 +201,26 @@ local function dispense_item (pos, node, slot) if item then item:set_count (1) + local spawn_pos = dispense_pos (pos, node) + local owner = meta:get_string ("owner") - local obj = try_spawn (pos, node, item) + local obj, cancel = utils.spawn_registered (name, + spawn_pos, + item, + owner, + pos, + dispense_dir (node)) + + if obj == nil and cancel then + return false + end if not obj then - obj = minetest.add_item (dispense_pos (pos, node), item) + obj = try_spawn (pos, node, item, owner) + end + + if not obj then + obj = minetest.add_item (spawn_pos, item) end if obj then diff --git a/init.lua b/init.lua index 521e614..62b9594 100644 --- a/init.lua +++ b/init.lua @@ -1,4 +1,4 @@ -local version = "0.1.4" +local version = "0.1.5" local mod_storage = minetest.get_mod_storage () @@ -16,6 +16,8 @@ local utils = { } local modpath = minetest.get_modpath ("lwcomponents") loadfile (modpath.."/utils.lua") (utils, mod_storage) +loadfile (modpath.."/settings.lua") (utils) +loadfile (modpath.."/api.lua") (utils) loadfile (modpath.."/dropper.lua") (utils) loadfile (modpath.."/collector.lua") (utils) loadfile (modpath.."/dispenser.lua") (utils) diff --git a/readme.txt b/readme.txt index ac1b820..9f7a56c 100644 --- a/readme.txt +++ b/readme.txt @@ -13,7 +13,7 @@ CC BY-SA 3.0 Version ======= -0.1.4 +0.1.5 Minetest Version @@ -103,11 +103,11 @@ Contains an inventory and dispenses (with velocity) an item on command. Also acts as a digilines conductor. If the hopper mod is loaded, will take items from the top and sides, and release them from the bottom. -Dispensers support mobs mod if loaded. Will spawn the entity from an 'egg' -if possible, or the 'egg' is dispensed. If a chicken egg is dispensed a -10% chance a chicken is dispensed instead. If the spawned entity can be -owned (or tamed) and the dispenser is owned the owner of the dispenser is -set as the owner of the entity. +Dispensers support mobs mod if loaded and Spawn mobs setting is enabled. +Will spawn the entity from an 'egg' if possible, or the 'egg' is dispensed. +If a chicken egg is dispensed a 10% chance a chicken is dispensed instead. +If the spawned entity can be owned (or tamed) and the dispenser is owned +the owner of the dispenser is set as the owner of the entity. UI diff --git a/settings.lua b/settings.lua new file mode 100644 index 0000000..28aebbc --- /dev/null +++ b/settings.lua @@ -0,0 +1,11 @@ +local utils = ... + + +utils.settings = { } + +utils.settings.spawn_mobs = + minetest.settings:get_bool ("lwcomponents_spawn_mobs", true) + + + +-- diff --git a/settingtypes.txt b/settingtypes.txt new file mode 100644 index 0000000..44199a9 --- /dev/null +++ b/settingtypes.txt @@ -0,0 +1,2 @@ +# Allow dispensers to spawn mobs instead of spawners. +lwcomponents_spawn_mobs (Spawn mobs) bool true diff --git a/utils.lua b/utils.lua index 4fb0915..2039bc9 100644 --- a/utils.lua +++ b/utils.lua @@ -171,4 +171,43 @@ end +utils.registered_spawners = { } +-- each entry [spawner_itemname] = spawner_func + + + +function utils.register_spawner (itemname, spawn_func) + if type (itemname) == "string" and type (spawn_func) == "function" then + if not utils.registered_spawners[itemname] then + utils.registered_spawners[itemname] = spawn_func + + return true + end + end + + return false +end + + + +function utils.spawn_registered (itemname, spawn_pos, itemstack, owner, spawner_pos, spawner_dir) + local func = utils.registered_spawners[itemname] + + if func then + local result, obj, cancel = pcall (func, spawn_pos, itemstack, owner, spawner_pos, spawner_dir) + + if result and (obj == nil or type (obj) == "userdata") then + return obj, cancel + end + + minetest.log ("error", "lwcomponents.register_spawner spawner function for "..itemname.." failed") + + return nil, true + end + + return nil, false +end + + + --