From 6a76c226e10e92c3e3339096f07f8ab065e2098b Mon Sep 17 00:00:00 2001 From: Kahrl Date: Thu, 12 Jan 2012 06:10:39 +0100 Subject: The huge item definition and item namespace unification patch (itemdef), see http://c55.me/minetest/wiki/doku.php?id=changes:itemdef --- data/mods/bucket/init.lua | 139 +++-- data/mods/default/init.lua | 1251 ++++++++++++++++++++++++--------------- data/mods/experimental/init.lua | 48 +- data/mods/legacy/init.lua | 171 +++--- 4 files changed, 954 insertions(+), 655 deletions(-) (limited to 'data/mods') diff --git a/data/mods/bucket/init.lua b/data/mods/bucket/init.lua index 639a614d4..8ed9da522 100644 --- a/data/mods/bucket/init.lua +++ b/data/mods/bucket/init.lua @@ -1,80 +1,95 @@ -- bucket (Minetest 0.4 mod) -- A bucket, which can pick up water and lava -minetest.alias_craftitem("bucket", "bucket:bucket_empty") -minetest.alias_craftitem("bucket_water", "bucket:bucket_water") -minetest.alias_craftitem("bucket_lava", "bucket:bucket_lava") +minetest.register_alias("bucket", "bucket:bucket_empty") +minetest.register_alias("bucket_water", "bucket:bucket_water") +minetest.register_alias("bucket_lava", "bucket:bucket_lava") minetest.register_craft({ - output = 'craft "bucket:bucket_empty" 1', + output = 'bucket:bucket_empty 1', recipe = { - {'craft "steel_ingot"', '', 'craft "steel_ingot"'}, - {'', 'craft "steel_ingot"', ''}, + {'default:steel_ingot', '', 'default:steel_ingot'}, + {'', 'default:steel_ingot', ''}, } }) -minetest.register_craftitem("bucket:bucket_empty", { - image = "bucket.png", - stack_max = 1, - liquids_pointable = true, - on_place_on_ground = minetest.craftitem_place_item, - on_use = function(item, player, pointed_thing) - if pointed_thing.type == "node" then - n = minetest.env:get_node(pointed_thing.under) - if n.name == "default:water_source" then - minetest.env:add_node(pointed_thing.under, {name="air"}) - player:add_to_inventory_later('craft "bucket:bucket_water" 1') - return true - elseif n.name == "default:lava_source" then - minetest.env:add_node(pointed_thing.under, {name="air"}) - player:add_to_inventory_later('craft "bucket:bucket_lava" 1') - return true +bucket = {} +bucket.liquids = {} + +-- Register a new liquid +-- source = name of the source node +-- flowing = name of the flowing node +-- itemname = name of the new bucket item (or nil if liquid is not takeable) +-- inventory_image = texture of the new bucket item (ignored if itemname == nil) +-- This function can be called from any mod (that depends on bucket). +function bucket.register_liquid(source, flowing, itemname, inventory_image) + bucket.liquids[source] = { + source = source, + flowing = flowing, + itemname = itemname, + } + bucket.liquids[flowing] = bucket.liquids[source] + + if itemname ~= nil then + minetest.register_craftitem(itemname, { + inventory_image = inventory_image, + stack_max = 1, + liquids_pointable = true, + on_use = function(itemstack, user, pointed_thing) + -- Must be pointing to node + if pointed_thing.type ~= "node" then + return + end + -- Check if pointing to a liquid + n = minetest.env:get_node(pointed_thing.under) + if bucket.liquids[n.name] == nil then + -- Not a liquid + minetest.env:add_node(pointed_thing.above, {name=source}) + elseif n.name ~= source then + -- It's a liquid + minetest.env:add_node(pointed_thing.under, {name=source}) + end + return {name="bucket:bucket_empty"} end - end - return false - end, -}) + }) + end +end -minetest.register_craftitem("bucket:bucket_water", { - image = "bucket_water.png", +minetest.register_craftitem("bucket:bucket_empty", { + inventory_image = "bucket.png", stack_max = 1, liquids_pointable = true, - on_place_on_ground = minetest.craftitem_place_item, - on_use = function(item, player, pointed_thing) - if pointed_thing.type == "node" then - n = minetest.env:get_node(pointed_thing.under) - if n.name == "default:water_source" then - -- unchanged - elseif n.name == "default:water_flowing" or n.name == "default:lava_source" or n.name == "default:lava_flowing" then - minetest.env:add_node(pointed_thing.under, {name="default:water_source"}) - else - minetest.env:add_node(pointed_thing.above, {name="default:water_source"}) - end - player:add_to_inventory_later('craft "bucket:bucket_empty" 1') - return true + on_use = function(itemstack, user, pointed_thing) + -- Must be pointing to node + if pointed_thing.type ~= "node" then + return + end + -- Check if pointing to a liquid source + n = minetest.env:get_node(pointed_thing.under) + liquiddef = bucket.liquids[n.name] + if liquiddef ~= nil and liquiddef.source == n.name and liquiddef.itemname ~= nil then + minetest.env:add_node(pointed_thing.under, {name="air"}) + return {name=liquiddef.itemname} end - return false end, }) -minetest.register_craftitem("bucket:bucket_lava", { - image = "bucket_lava.png", - stack_max = 1, - liquids_pointable = true, - on_place_on_ground = minetest.craftitem_place_item, - on_use = function(item, player, pointed_thing) - if pointed_thing.type == "node" then - n = minetest.env:get_node(pointed_thing.under) - if n.name == "default:lava_source" then - -- unchanged - elseif n.name == "default:water_source" or n.name == "default:water_flowing" or n.name == "default:lava_flowing" then - minetest.env:add_node(pointed_thing.under, {name="default:lava_source"}) - else - minetest.env:add_node(pointed_thing.above, {name="default:lava_source"}) - end - player:add_to_inventory_later('craft "bucket:bucket_empty" 1') - return true - end - return false - end, +bucket.register_liquid( + "default:water_source", + "default:water_flowing", + "bucket:bucket_water", + "bucket_water.png" +) + +bucket.register_liquid( + "default:lava_source", + "default:lava_flowing", + "bucket:bucket_lava", + "bucket_lava.png" +) + +minetest.register_craft({ + type = "fuel", + recipe = "default:bucket_lava", + burntime = 60, }) diff --git a/data/mods/default/init.lua b/data/mods/default/init.lua index aa03eabe8..2bd950230 100644 --- a/data/mods/default/init.lua +++ b/data/mods/default/init.lua @@ -77,24 +77,25 @@ -- eg. 'tool "default:pick_wood" 21323' -- eg. 'craft "default:apple" 2' -- --- item: A single item in Lua table format. --- eg. {type="node", name="default:dirt"} +-- item: A stack of items in Lua table format. +-- eg. {name="default:dirt", count=1, wear=0, metadata=""} -- ^ a single dirt node --- eg. {type="tool", name="default:pick_wood", wear=21323} +-- eg. {name="default:pick_wood", count=1, wear=21323, metadata=""} -- ^ a wooden pick about 1/3 weared out --- eg. {type="craft", name="default:apple"} +-- eg. {name="default:apple", count=1, wear=0, metadata=""} -- ^ an apple. -- +-- Any time an item must be passed to a function, it can be an +-- ItemStack (see below), an itemstring or a table in the above format. +-- -- Global functions: -- minetest.register_entity(name, prototype table) --- minetest.register_tool(name, tool definition) +-- minetest.register_abm(abm definition) -- minetest.register_node(name, node definition) --- minetest.register_craftitem(name, craftitem definition) +-- minetest.register_tool(name, item definition) +-- minetest.register_craftitem(name, item definition) +-- minetest.register_alias(name, convert_to) -- minetest.register_craft(recipe) --- minetest.register_abm(abm definition) --- minetest.alias_node(name, convert_to) --- minetest.alias_tool(name, convert_to) --- minetest.alias_craftitem(name, convert_to) -- minetest.register_globalstep(func(dtime)) -- minetest.register_on_placenode(func(pos, newnode, placer)) -- minetest.register_on_dignode(func(pos, oldnode, digger)) @@ -113,13 +114,16 @@ -- minetest.chat_send_player(name, text) -- minetest.get_player_privs(name) -> set of privs -- minetest.get_inventory(location) -> InvRef +-- minetest.get_current_modname() -> string -- minetest.get_modpath(modname) -> eg. "/home/user/.minetest/usermods/modname" -- ^ location = eg. {type="player", name="celeron55"} -- {type="node", pos={x=, y=, z=}} -- --- stackstring_take_item(stackstring) -> stackstring, item --- stackstring_put_item(stackstring, item) -> stackstring, success --- stackstring_put_stackstring(stackstring, stackstring) -> stackstring, success +-- minetest.debug(line) +-- ^ Goes to dstream +-- minetest.log(line) +-- minetest.log(loglevel, line) +-- ^ loglevel one of "error", "action", "info", "verbose" -- -- minetest.digprop_constanttime(time) -- minetest.digprop_stonelike(toughness) @@ -133,10 +137,14 @@ -- minetest.env - environment reference -- -- Global tables: +-- minetest.registered_items +-- ^ List of registered items, indexed by name -- minetest.registered_nodes -- ^ List of registered node definitions, indexed by name -- minetest.registered_craftitems -- ^ List of registered craft item definitions, indexed by name +-- minetest.registered_tools +-- ^ List of registered tool definitions, indexed by name -- minetest.registered_entities -- ^ List of registered entity prototypes, indexed by name -- minetest.object_refs @@ -171,8 +179,6 @@ -- Generic node metadata specific: -- - set_infotext(infotext) -- - get_inventory() -> InvRef --- - inventory_set_list(name, {item1, item2, ...}) --- - inventory_get_list(name) -- - set_inventory_draw_spec(string) -- - set_allow_text_input(bool) -- - set_allow_removal(bool) @@ -194,12 +200,13 @@ -- ^ puncher = an another ObjectRef, -- ^ time_from_last_punch = time since last punch action of the puncher -- - right_click(clicker); clicker = an another ObjectRef --- - get_wield_digging_properties() -> digging property table --- - damage_wielded_item(num) (item damage/wear range is 0-65535) --- - add_to_inventory(itemstring): add an item to object inventory (actually only works for the player as of now) --- - add_to_inventory_later(itemstring): like above, but after callback returns (only allowed for craftitem callbacks) -- - get_hp(): returns number of hitpoints (2 * number of hearts) -- - set_hp(hp): set number of hitpoints (2 * number of hearts) +-- - get_inventory() -> InvRef +-- - get_wield_list(): returns the name of the inventory list the wielded item is in +-- - get_wield_index(): returns the index of the wielded item +-- - get_wielded_item() -> ItemStack +-- - set_wielded_item(item): replaces the wielded item, returns true if successful -- LuaEntitySAO-only: (no-op for other objects) -- - setvelocity({x=num, y=num, z=num}) -- - getvelocity() -> {x=num, y=num, z=num} @@ -216,9 +223,6 @@ -- - get_luaentity() -- Player-only: (no-op for other objects) -- - get_player_name(): will return nil if is not a player --- - get_inventory() -> InvRef --- - inventory_set_list(name, {item1, item2, ...}) --- - inventory_get_list(name) -> {item1, item2, ...} -- - get_look_dir(): get camera direction as a unit vector -- - get_look_pitch(): pitch in radians -- - get_look_yaw(): yaw in radians (wraps around pretty randomly as of now) @@ -230,14 +234,41 @@ -- - set_stack(listname, i, stack): copy stack to index i in list -- - get_list(listname): return full list -- - set_list(listname, list): set full list (size will not change) --- - autoinsert_stack(listname, stack): insert stack somewhere in list --- - autoinsert_stackstring(listname, stackstring) +-- - add_item(listname, stack): add item somewhere in list, returns leftover ItemStack +-- - room_for_item(listname, stack): returns true if the stack of items +-- can be fully added to the list +-- - contains_item(listname, stack): returns true if the stack of items +-- can be fully taken from the list +-- remove_item(listname, stack): take as many items as specified from the list, +-- returns the items that were actually removed (as an ItemStack) -- -- ItemStack methods: --- - peek_item(): return item from stack without removing it --- - take_item(): remove item from stack and return it --- - put_item(item): put item in stack; return false if not possible --- - put_stackstring(stackstring): return false if not possible +-- - is_empty(): return true if stack is empty +-- - get_name(): returns item name (e.g. "default:stone") +-- - get_count(): returns number of items on the stack +-- - get_wear(): returns tool wear (0-65535), 0 for non-tools +-- - get_metadata(): returns metadata (a string attached to an item stack) +-- - clear(): removes all items from the stack, making it empty +-- - replace(item): replace the contents of this stack (item can also +-- be an itemstring or table) +-- - to_string(): returns the stack in itemstring form +-- - to_table(): returns the stack in Lua table form +-- - get_stack_max(): returns the maximum size of the stack (depends on the item) +-- - get_free_space(): returns get_stack_max() - get_count() +-- - is_known(): returns true if the item name refers to a defined item type +-- - get_definition(): returns the item definition table +-- - get_tool_digging_properties(): returns the digging properties of the item, +-- ^ or those of the hand if none are defined for this item type +-- - add_wear(amount): increases wear by amount if the item is a tool +-- - add_item(item): put some item or stack onto this stack, +-- ^ returns leftover ItemStack +-- - item_fits(item): returns true if item or stack can be fully added to this one +-- - take_item(n): take (and remove) up to n items from this stack +-- ^ returns taken ItemStack +-- ^ if n is omitted, n=1 is used +-- - peek_item(n): copy (don't remove) up to n items from this stack +-- ^ returns copied ItemStack +-- ^ if n is omitted, n=1 is used -- -- Registered entities: -- - Functions receive a "luaentity" as self: @@ -271,29 +302,38 @@ -- myvariable = whatever, -- } -- --- Tool definition: +-- Item definition: -- { --- image = "default_tool_steelaxe.png", --- full_punch_interval = 1.0, --- basetime = 1.0, --- dt_weight = 0.5, --- dt_crackiness = -0.2, --- dt_crumbliness = 1, --- dt_cuttability = -0.5, --- basedurability = 330, --- dd_weight = 0, --- dd_crackiness = 0, --- dd_crumbliness = 0, --- dd_cuttability = 0, +-- description = "Steel Axe", +-- inventory_image = "default_tool_steelaxe.png", +-- wield_image = "", +-- wield_scale = {x=1,y=1,z=1}, +-- stack_max = 99, +-- liquids_pointable = false, +-- tool_digging_properties = { +-- full_punch_interval = 1.0, +-- basetime = 1.0, +-- dt_weight = 0.5, +-- dt_crackiness = -0.2, +-- dt_crumbliness = 1, +-- dt_cuttability = -0.5, +-- basedurability = 330, +-- dd_weight = 0, +-- dd_crackiness = 0, +-- dd_crumbliness = 0, +-- dd_cuttability = 0, +-- } +-- on_drop = func(item, dropper, pos), +-- on_place = func(item, placer, pointed_thing), +-- on_use = func(item, user, pointed_thing), -- } -- -- Node definition options: -- { --- name = "modname:somenode", +-- , -- drawtype = "normal", -- visual_scale = 1.0, -- tile_images = {"default_unknown_block.png"}, --- inventory_image = "default_unknown_block.png", -- special_materials = { -- {image="", backface_culling=true}, -- {image="", backface_culling=true}, @@ -309,7 +349,6 @@ -- climbable = false, -- buildable_to = false, -- wall_mounted = false, --- often_contains_mineral = false, -- dug_item = "", -- extra_dug_item = "", -- extra_dug_item_rarity = 2, @@ -329,34 +368,67 @@ -- cuttability = 0, -- flammability = 0, -- }, --- cookresult_itemstring = "", -- Cannot be cooked --- furnace_cooktime = 3.0, --- furnace_burntime = -1, -- Cannot be used as fuel +-- on_drop = func(item, dropper), +-- on_place = func(item, placer, pointed_thing), +-- on_use = func(item, user, pointed_thing), -- } -- -- Craftitem definition options: --- minetest.register_craftitem("modname_name", { --- image = "default_image.png", +-- { +-- description = , +-- inventory_image = "default_unknown_block.png", +-- wield_image = "", -- stack_max = , --- cookresult_itemstring = itemstring (result of cooking), --- furnace_cooktime = , --- furnace_burntime =