Enforced mod global naming convention and better error reporting
authorPerttu Ahola <celeron55@gmail.com>
Sat, 3 Dec 2011 00:45:55 +0000 (02:45 +0200)
committerPerttu Ahola <celeron55@gmail.com>
Sat, 3 Dec 2011 00:45:55 +0000 (02:45 +0200)
data/mods/bucket/init.lua
data/mods/default/init.lua
data/mods/experimental/init.lua
src/script.cpp
src/script.h
src/scriptapi.cpp
src/server.cpp

index 6d16c0cdef8c56f7391316259314c19f77bbe89b..33beb699d0cae35c4afcb57bc0a692e6c4842508 100644 (file)
@@ -1,12 +1,12 @@
 minetest.register_craft({
-       output = 'craft "bucket" 1',
+       output = 'craft "bucket_empty" 1',
        recipe = {
                {'craft "steel_ingot"', '', 'craft "steel_ingot"'},
                {'', 'craft "steel_ingot"', ''},
        }
 })
 
-minetest.register_craftitem("bucket", {
+minetest.register_craftitem("bucket_empty", {
        image = "bucket.png",
        stack_max = 1,
        liquids_pointable = true,
@@ -43,7 +43,7 @@ minetest.register_craftitem("bucket_water", {
                        else
                                minetest.env:add_node(pointed_thing.above, {name="water_source"})
                        end
-                       player:add_to_inventory_later('craft "bucket" 1')
+                       player:add_to_inventory_later('craft "bucket_empty" 1')
                        return true
                end
                return false
@@ -65,7 +65,7 @@ minetest.register_craftitem("bucket_lava", {
                        else
                                minetest.env:add_node(pointed_thing.above, {name="lava_source"})
                        end
-                       player:add_to_inventory_later('craft "bucket" 1')
+                       player:add_to_inventory_later('craft "bucket_empty" 1')
                        return true
                end
                return false
index afd22d6bbd3eb7ba907f00951e62a93d662b25c4..b9b626f1efd4ea448bec05709b277e04a4bf5ce8 100644 (file)
@@ -2,10 +2,30 @@
 -- dump2(obj, name="_", dumped={})
 -- dump(obj, dumped={})
 --
--- Textures:
--- Mods should prefix their textures with modname_, eg. given the mod
--- name "foomod", a texture could be called "foomod_superfurnace.png"
--- Due to historical reasons, the default mod does not follow this rule.
+-- Naming convention for registered textual names
+-- ----------------------------------------------
+-- "modname_<whatever>"
+--
+-- This is to prevent conflicting names from corrupting maps and is
+-- enforced by the mod loader.
+--
+-- Example: mod "experimental", ideal item/node/entity name "tnt":
+--          -> the name should be "experimental_tnt".
+--
+-- Enforcement can be overridden by prefixing the name with ":". This can
+-- be used for overriding the registrations of some other mod.
+--
+-- Default mod uses ":" for maintaining backwards compatibility.
+--
+-- Textures
+-- --------
+-- Mods should generally prefix their textures with modname_, eg. given
+-- the mod name "foomod", a texture could be called "foomod_superfurnace.png"
+--
+-- This is not crucial and a conflicting name will not corrupt maps.
+--
+-- Representations of simple things
+-- --------------------------------
 --
 -- MapNode representation:
 -- {name="name", param1=num, param2=num}
 -- }
 --
 -- Craftitem definition options:
--- minetest.register_craftitem("name", {
+-- minetest.register_craftitem(":name", {
 --     image = "image.png",
 --     stack_max = <maximum number of items in stack>,
 --     cookresult_item = itemstring (result of cooking),
@@ -256,7 +276,7 @@ LIGHT_MAX = 14
 -- Tool definition
 --
 
-minetest.register_tool("WPick", {
+minetest.register_tool(":WPick", {
        image = "tool_woodpick.png",
        basetime = 2.0,
        dt_weight = 0,
@@ -269,7 +289,7 @@ minetest.register_tool("WPick", {
        dd_crumbliness = 0,
        dd_cuttability = 0,
 })
-minetest.register_tool("STPick", {
+minetest.register_tool(":STPick", {
        image = "tool_stonepick.png",
        basetime = 1.5,
        dt_weight = 0,
@@ -282,7 +302,7 @@ minetest.register_tool("STPick", {
        dd_crumbliness = 0,
        dd_cuttability = 0,
 })
-minetest.register_tool("SteelPick", {
+minetest.register_tool(":SteelPick", {
        image = "tool_steelpick.png",
        basetime = 1.0,
        dt_weight = 0,
@@ -295,7 +315,7 @@ minetest.register_tool("SteelPick", {
        dd_crumbliness = 0,
        dd_cuttability = 0,
 })
-minetest.register_tool("MesePick", {
+minetest.register_tool(":MesePick", {
        image = "tool_mesepick.png",
        basetime = 0,
        dt_weight = 0,
@@ -308,7 +328,7 @@ minetest.register_tool("MesePick", {
        dd_crumbliness = 0,
        dd_cuttability = 0,
 })
-minetest.register_tool("WShovel", {
+minetest.register_tool(":WShovel", {
        image = "tool_woodshovel.png",
        basetime = 2.0,
        dt_weight = 0.5,
@@ -321,7 +341,7 @@ minetest.register_tool("WShovel", {
        dd_crumbliness = 0,
        dd_cuttability = 0,
 })
-minetest.register_tool("STShovel", {
+minetest.register_tool(":STShovel", {
        image = "tool_stoneshovel.png",
        basetime = 1.5,
        dt_weight = 0.5,
@@ -334,7 +354,7 @@ minetest.register_tool("STShovel", {
        dd_crumbliness = 0,
        dd_cuttability = 0,
 })
-minetest.register_tool("SteelShovel", {
+minetest.register_tool(":SteelShovel", {
        image = "tool_steelshovel.png",
        basetime = 1.0,
        dt_weight = 0.5,
@@ -347,7 +367,7 @@ minetest.register_tool("SteelShovel", {
        dd_crumbliness = 0,
        dd_cuttability = 0,
 })
-minetest.register_tool("WAxe", {
+minetest.register_tool(":WAxe", {
        image = "tool_woodaxe.png",
        basetime = 2.0,
        dt_weight = 0.5,
@@ -360,7 +380,7 @@ minetest.register_tool("WAxe", {
        dd_crumbliness = 0,
        dd_cuttability = 0,
 })
-minetest.register_tool("STAxe", {
+minetest.register_tool(":STAxe", {
        image = "tool_stoneaxe.png",
        basetime = 1.5,
        dt_weight = 0.5,
@@ -373,7 +393,7 @@ minetest.register_tool("STAxe", {
        dd_crumbliness = 0,
        dd_cuttability = 0,
 })
-minetest.register_tool("SteelAxe", {
+minetest.register_tool(":SteelAxe", {
        image = "tool_steelaxe.png",
        basetime = 1.0,
        dt_weight = 0.5,
@@ -386,7 +406,7 @@ minetest.register_tool("SteelAxe", {
        dd_crumbliness = 0,
        dd_cuttability = 0,
 })
-minetest.register_tool("WSword", {
+minetest.register_tool(":WSword", {
        image = "tool_woodsword.png",
        basetime = 3.0,
        dt_weight = 3,
@@ -399,7 +419,7 @@ minetest.register_tool("WSword", {
        dd_crumbliness = 0,
        dd_cuttability = 0,
 })
-minetest.register_tool("STSword", {
+minetest.register_tool(":STSword", {
        image = "tool_stonesword.png",
        basetime = 2.5,
        dt_weight = 3,
@@ -412,7 +432,7 @@ minetest.register_tool("STSword", {
        dd_crumbliness = 0,
        dd_cuttability = 0,
 })
-minetest.register_tool("SteelSword", {
+minetest.register_tool(":SteelSword", {
        image = "tool_steelsword.png",
        basetime = 2.0,
        dt_weight = 3,
@@ -426,7 +446,7 @@ minetest.register_tool("SteelSword", {
        dd_cuttability = 0,
 })
 -- The hand
-minetest.register_tool("", {
+minetest.register_tool(":", {
        image = "",
        basetime = 0.5,
        dt_weight = 1,
@@ -794,7 +814,7 @@ end
 
 -- Legacy nodes
 
-minetest.register_node("stone", {
+minetest.register_node(":stone", {
        tile_images = {"stone.png"},
        inventory_image = inventorycube("stone.png"),
        paramtype = "mineral",
@@ -804,7 +824,7 @@ minetest.register_node("stone", {
        dug_item = 'node "cobble" 1',
 })
 
-minetest.register_node("dirt_with_grass", {
+minetest.register_node(":dirt_with_grass", {
        tile_images = {"grass.png", "mud.png", "mud.png^grass_side.png"},
        inventory_image = inventorycube("mud.png^grass_side.png"),
        is_ground_content = true,
@@ -812,7 +832,7 @@ minetest.register_node("dirt_with_grass", {
        dug_item = 'node "dirt" 1',
 })
 
-minetest.register_node("dirt_with_grass_footsteps", {
+minetest.register_node(":dirt_with_grass_footsteps", {
        tile_images = {"grass_footsteps.png", "mud.png", "mud.png^grass_side.png"},
        inventory_image = "grass_footsteps.png",
        is_ground_content = true,
@@ -820,14 +840,14 @@ minetest.register_node("dirt_with_grass_footsteps", {
        dug_item = 'node "dirt" 1',
 })
 
-minetest.register_node("dirt", {
+minetest.register_node(":dirt", {
        tile_images = {"mud.png"},
        inventory_image = inventorycube("mud.png"),
        is_ground_content = true,
        material = digprop_dirtlike(1.0),
 })
 
-minetest.register_node("sand", {
+minetest.register_node(":sand", {
        tile_images = {"sand.png"},
        inventory_image = inventorycube("sand.png"),
        is_ground_content = true,
@@ -835,14 +855,14 @@ minetest.register_node("sand", {
        cookresult_item = 'node "glass" 1',
 })
 
-minetest.register_node("gravel", {
+minetest.register_node(":gravel", {
        tile_images = {"gravel.png"},
        inventory_image = inventorycube("gravel.png"),
        is_ground_content = true,
        material = digprop_gravellike(1.0),
 })
 
-minetest.register_node("sandstone", {
+minetest.register_node(":sandstone", {
        tile_images = {"sandstone.png"},
        inventory_image = inventorycube("sandstone.png"),
        is_ground_content = true,
@@ -850,7 +870,7 @@ minetest.register_node("sandstone", {
        dug_item = 'node "sand" 1',  -- FIXME is this intentional?
 })
 
-minetest.register_node("clay", {
+minetest.register_node(":clay", {
        tile_images = {"clay.png"},
        inventory_image = inventorycube("clay.png"),
        is_ground_content = true,
@@ -858,7 +878,7 @@ minetest.register_node("clay", {
        dug_item = 'craft "lump_of_clay" 4',
 })
 
-minetest.register_node("brick", {
+minetest.register_node(":brick", {
        tile_images = {"brick.png"},
        inventory_image = inventorycube("brick.png"),
        is_ground_content = true,
@@ -866,7 +886,7 @@ minetest.register_node("brick", {
        dug_item = 'craft "clay_brick" 4',
 })
 
-minetest.register_node("tree", {
+minetest.register_node(":tree", {
        tile_images = {"tree_top.png", "tree_top.png", "tree.png"},
        inventory_image = inventorycube("tree_top.png", "tree.png", "tree.png"),
        is_ground_content = true,
@@ -875,7 +895,7 @@ minetest.register_node("tree", {
        furnace_burntime = 30,
 })
 
-minetest.register_node("jungletree", {
+minetest.register_node(":jungletree", {
        tile_images = {"jungletree_top.png", "jungletree_top.png", "jungletree.png"},
        inventory_image = inventorycube("jungletree_top.png", "jungletree.png", "jungletree.png"),
        is_ground_content = true,
@@ -884,7 +904,7 @@ minetest.register_node("jungletree", {
        furnace_burntime = 30,
 })
 
-minetest.register_node("junglegrass", {
+minetest.register_node(":junglegrass", {
        drawtype = "plantlike",
        visual_scale = 1.3,
        tile_images = {"junglegrass.png"},
@@ -896,7 +916,7 @@ minetest.register_node("junglegrass", {
        furnace_burntime = 2,
 })
 
-minetest.register_node("leaves", {
+minetest.register_node(":leaves", {
        drawtype = "allfaces_optional",
        visual_scale = 1.3,
        tile_images = {"leaves.png"},
@@ -909,7 +929,7 @@ minetest.register_node("leaves", {
        furnace_burntime = 1,
 })
 
-minetest.register_node("cactus", {
+minetest.register_node(":cactus", {
        tile_images = {"cactus_top.png", "cactus_top.png", "cactus_side.png"},
        inventory_image = inventorycube("cactus_top.png", "cactus_side.png", "cactus_side.png"),
        is_ground_content = true,
@@ -917,7 +937,7 @@ minetest.register_node("cactus", {
        furnace_burntime = 15,
 })
 
-minetest.register_node("papyrus", {
+minetest.register_node(":papyrus", {
        drawtype = "plantlike",
        tile_images = {"papyrus.png"},
        inventory_image = "papyrus.png",
@@ -929,7 +949,7 @@ minetest.register_node("papyrus", {
        furnace_burntime = 1,
 })
 
-minetest.register_node("bookshelf", {
+minetest.register_node(":bookshelf", {
        tile_images = {"wood.png", "wood.png", "bookshelf.png"},
        -- FIXME: inventorycube only cares for the first texture
        --inventory_image = inventorycube("wood.png", "bookshelf.png", "bookshelf.png")
@@ -939,7 +959,7 @@ minetest.register_node("bookshelf", {
        furnace_burntime = 30,
 })
 
-minetest.register_node("glass", {
+minetest.register_node(":glass", {
        drawtype = "glasslike",
        tile_images = {"glass.png"},
        inventory_image = inventorycube("glass.png"),
@@ -950,7 +970,7 @@ minetest.register_node("glass", {
        material = digprop_glasslike(1.0),
 })
 
-minetest.register_node("wooden_fence", {
+minetest.register_node(":wooden_fence", {
        drawtype = "fencelike",
        tile_images = {"wood.png"},
        inventory_image = "fence.png",
@@ -965,7 +985,7 @@ minetest.register_node("wooden_fence", {
        material = digprop_woodlike(0.75),
 })
 
-minetest.register_node("rail", {
+minetest.register_node(":rail", {
        drawtype = "raillike",
        tile_images = {"rail.png", "rail_curved.png", "rail_t_junction.png", "rail_crossing.png"},
        inventory_image = "rail.png",
@@ -980,7 +1000,7 @@ minetest.register_node("rail", {
        material = digprop_dirtlike(0.75),
 })
 
-minetest.register_node("ladder", {
+minetest.register_node(":ladder", {
        drawtype = "signlike",
        tile_images = {"ladder.png"},
        inventory_image = "ladder.png",
@@ -1000,14 +1020,14 @@ minetest.register_node("ladder", {
        material = digprop_woodlike(0.5),
 })
 
-minetest.register_node("coalstone", {
+minetest.register_node(":coalstone", {
        tile_images = {"stone.png^mineral_coal.png"},
        inventory_image = "stone.png^mineral_coal.png",
        is_ground_content = true,
        material = digprop_stonelike(1.5),
 })
 
-minetest.register_node("wood", {
+minetest.register_node(":wood", {
        tile_images = {"wood.png"},
        inventory_image = inventorycube("wood.png"),
        is_ground_content = true,
@@ -1015,7 +1035,7 @@ minetest.register_node("wood", {
        material = digprop_woodlike(0.75),
 })
 
-minetest.register_node("mese", {
+minetest.register_node(":mese", {
        tile_images = {"mese.png"},
        inventory_image = inventorycube("mese.png"),
        is_ground_content = true,
@@ -1023,13 +1043,13 @@ minetest.register_node("mese", {
        material = digprop_stonelike(0.5),
 })
 
-minetest.register_node("cloud", {
+minetest.register_node(":cloud", {
        tile_images = {"cloud.png"},
        inventory_image = inventorycube("cloud.png"),
        is_ground_content = true,
 })
 
-minetest.register_node("water_flowing", {
+minetest.register_node(":water_flowing", {
        drawtype = "flowingliquid",
        tile_images = {"water.png"},
        alpha = WATER_ALPHA,
@@ -1051,7 +1071,7 @@ minetest.register_node("water_flowing", {
        },
 })
 
-minetest.register_node("water_source", {
+minetest.register_node(":water_source", {
        drawtype = "liquid",
        tile_images = {"water.png"},
        alpha = WATER_ALPHA,
@@ -1073,7 +1093,7 @@ minetest.register_node("water_source", {
        },
 })
 
-minetest.register_node("lava_flowing", {
+minetest.register_node(":lava_flowing", {
        drawtype = "flowingliquid",
        tile_images = {"lava.png"},
        inventory_image = inventorycube("lava.png"),
@@ -1096,7 +1116,7 @@ minetest.register_node("lava_flowing", {
        },
 })
 
-minetest.register_node("lava_source", {
+minetest.register_node(":lava_source", {
        drawtype = "liquid",
        tile_images = {"lava.png"},
        inventory_image = inventorycube("lava.png"),
@@ -1120,7 +1140,7 @@ minetest.register_node("lava_source", {
        furnace_burntime = 60,
 })
 
-minetest.register_node("torch", {
+minetest.register_node(":torch", {
        drawtype = "torchlike",
        tile_images = {"torch_on_floor.png", "torch_on_ceiling.png", "torch.png"},
        inventory_image = "torch_on_floor.png",
@@ -1140,7 +1160,7 @@ minetest.register_node("torch", {
        furnace_burntime = 4,
 })
 
-minetest.register_node("sign_wall", {
+minetest.register_node(":sign_wall", {
        drawtype = "signlike",
        tile_images = {"sign_wall.png"},
        inventory_image = "sign_wall.png",
@@ -1160,7 +1180,7 @@ minetest.register_node("sign_wall", {
        furnace_burntime = 10,
 })
 
-minetest.register_node("chest", {
+minetest.register_node(":chest", {
        tile_images = {"chest_top.png", "chest_top.png", "chest_side.png",
                "chest_side.png", "chest_side.png", "chest_front.png"},
        inventory_image = "chest_top.png",
@@ -1171,7 +1191,7 @@ minetest.register_node("chest", {
        furnace_burntime = 30,
 })
 
-minetest.register_node("locked_chest", {
+minetest.register_node(":locked_chest", {
        tile_images = {"chest_top.png", "chest_top.png", "chest_side.png",
                "chest_side.png", "chest_side.png", "chest_lock.png"},
        inventory_image = "chest_lock.png",
@@ -1181,7 +1201,7 @@ minetest.register_node("locked_chest", {
        furnace_burntime = 30,
 })
 
-minetest.register_node("furnace", {
+minetest.register_node(":furnace", {
        tile_images = {"furnace_side.png", "furnace_side.png", "furnace_side.png",
                "furnace_side.png", "furnace_side.png", "furnace_front.png"},
        inventory_image = "furnace_front.png",
@@ -1190,7 +1210,7 @@ minetest.register_node("furnace", {
        material = digprop_stonelike(3.0),
 })
 
-minetest.register_node("cobble", {
+minetest.register_node(":cobble", {
        tile_images = {"cobble.png"},
        inventory_image = inventorycube("cobble.png"),
        is_ground_content = true,
@@ -1198,21 +1218,21 @@ minetest.register_node("cobble", {
        material = digprop_stonelike(0.9),
 })
 
-minetest.register_node("mossycobble", {
+minetest.register_node(":mossycobble", {
        tile_images = {"mossycobble.png"},
        inventory_image = inventorycube("mossycobble.png"),
        is_ground_content = true,
        material = digprop_stonelike(0.8),
 })
 
-minetest.register_node("steelblock", {
+minetest.register_node(":steelblock", {
        tile_images = {"steel_block.png"},
        inventory_image = inventorycube("steel_block.png"),
        is_ground_content = true,
        material = digprop_stonelike(5.0),
 })
 
-minetest.register_node("nyancat", {
+minetest.register_node(":nyancat", {
        tile_images = {"nc_side.png", "nc_side.png", "nc_side.png",
                "nc_side.png", "nc_back.png", "nc_front.png"},
        inventory_image = "nc_front.png",
@@ -1221,14 +1241,14 @@ minetest.register_node("nyancat", {
        furnace_burntime = 1,
 })
 
-minetest.register_node("nyancat_rainbow", {
+minetest.register_node(":nyancat_rainbow", {
        tile_images = {"nc_rb.png"},
        inventory_image = "nc_rb.png",
        material = digprop_stonelike(3.0),
        furnace_burntime = 1,
 })
 
-minetest.register_node("sapling", {
+minetest.register_node(":sapling", {
        drawtype = "plantlike",
        visual_scale = 1.0,
        tile_images = {"sapling.png"},
@@ -1240,7 +1260,7 @@ minetest.register_node("sapling", {
        furnace_burntime = 10,
 })
 
-minetest.register_node("apple", {
+minetest.register_node(":apple", {
        drawtype = "plantlike",
        visual_scale = 1.0,
        tile_images = {"apple.png"},
@@ -1258,51 +1278,51 @@ minetest.register_node("apple", {
 -- Crafting items
 --
 
-minetest.register_craftitem("Stick", {
+minetest.register_craftitem(":Stick", {
        image = "stick.png",
        --furnace_burntime = ...,
        on_place_on_ground = minetest.craftitem_place_item,
 })
 
-minetest.register_craftitem("paper", {
+minetest.register_craftitem(":paper", {
        image = "paper.png",
        on_place_on_ground = minetest.craftitem_place_item,
 })
 
-minetest.register_craftitem("book", {
+minetest.register_craftitem(":book", {
        image = "book.png",
        on_place_on_ground = minetest.craftitem_place_item,
 })
 
-minetest.register_craftitem("lump_of_coal", {
+minetest.register_craftitem(":lump_of_coal", {
        image = "lump_of_coal.png",
        furnace_burntime = 40;
        on_place_on_ground = minetest.craftitem_place_item,
 })
 
-minetest.register_craftitem("lump_of_iron", {
+minetest.register_craftitem(":lump_of_iron", {
        image = "lump_of_iron.png",
        cookresult_item = 'craft "steel_ingot" 1',
        on_place_on_ground = minetest.craftitem_place_item,
 })
 
-minetest.register_craftitem("lump_of_clay", {
+minetest.register_craftitem(":lump_of_clay", {
        image = "lump_of_clay.png",
        cookresult_item = 'craft "clay_brick" 1',
        on_place_on_ground = minetest.craftitem_place_item,
 })
 
-minetest.register_craftitem("steel_ingot", {
+minetest.register_craftitem(":steel_ingot", {
        image = "steel_ingot.png",
        on_place_on_ground = minetest.craftitem_place_item,
 })
 
-minetest.register_craftitem("clay_brick", {
+minetest.register_craftitem(":clay_brick", {
        image = "clay_brick.png",
        on_place_on_ground = minetest.craftitem_place_item,
 })
 
-minetest.register_craftitem("rat", {
+minetest.register_craftitem(":rat", {
        image = "rat.png",
        cookresult_item = 'craft "cooked_rat" 1',
        on_drop = function(item, dropper, pos)
@@ -1311,19 +1331,19 @@ minetest.register_craftitem("rat", {
        end,
 })
 
-minetest.register_craftitem("cooked_rat", {
+minetest.register_craftitem(":cooked_rat", {
        image = "cooked_rat.png",
        cookresult_item = 'craft "scorched_stuff" 1',
        on_place_on_ground = minetest.craftitem_place_item,
        on_use = minetest.craftitem_eat(6),
 })
 
-minetest.register_craftitem("scorched_stuff", {
+minetest.register_craftitem(":scorched_stuff", {
        image = "scorched_stuff.png",
        on_place_on_ground = minetest.craftitem_place_item,
 })
 
-minetest.register_craftitem("firefly", {
+minetest.register_craftitem(":firefly", {
        image = "firefly.png",
        on_drop = function(item, dropper, pos)
                minetest.env:add_firefly(pos)
@@ -1331,13 +1351,13 @@ minetest.register_craftitem("firefly", {
        end,
 })
 
-minetest.register_craftitem("apple", {
+minetest.register_craftitem(":apple", {
        image = "apple.png",
        on_place_on_ground = minetest.craftitem_place_item,
        on_use = minetest.craftitem_eat(4),
 })
 
-minetest.register_craftitem("apple_iron", {
+minetest.register_craftitem(":apple_iron", {
        image = "apple_iron.png",
        on_place_on_ground = minetest.craftitem_place_item,
        on_use = minetest.craftitem_eat(8),
@@ -1389,7 +1409,7 @@ function nodeupdate_single(p)
                n_bottom = minetest.env:get_node(p_bottom)
                if n_bottom.name == "air" then
                        minetest.env:remove_node(p)
-                       minetest.env:add_luaentity(p, "falling_"..n.name)
+                       minetest.env:add_luaentity(p, "default_falling_"..n.name)
                        nodeupdate(p)
                end
        end
@@ -1411,7 +1431,7 @@ end
 --
 
 function register_falling_node(nodename, texture)
-       minetest.register_entity("falling_"..nodename, {
+       minetest.register_entity("default_falling_"..nodename, {
                -- Static definition
                physical = true,
                collisionbox = {-0.5,-0.5,-0.5, 0.5,0.5,0.5},
@@ -1452,26 +1472,18 @@ end
 minetest.register_globalstep(on_step)
 
 function on_placenode(p, node)
-       print("on_placenode")
+       --print("on_placenode")
        nodeupdate(p)
 end
 minetest.register_on_placenode(on_placenode)
 
 function on_dignode(p, node)
-       print("on_dignode")
+       --print("on_dignode")
        nodeupdate(p)
 end
 minetest.register_on_dignode(on_dignode)
 
 function on_punchnode(p, node)
-       print("on_punchnode")
-       if node.name == "TNT" then
-               minetest.env:remove_node(p)
-               minetest.env:add_luaentity(p, "TNT")
-               --minetest.env:add_luaentity(p, "testentity")
-               --minetest.env:add_firefly(p)
-               nodeupdate(p)
-       end
 end
 minetest.register_on_punchnode(on_punchnode)
 
index 4b95a5fd5fbd093f2b75ea1c556c7786fd8e345c..207b38cbf0b6a464f4ca6d21ca026f94893ac22d 100644 (file)
@@ -4,7 +4,7 @@
 
 -- An example furnace-thing implemented in Lua
 
-minetest.register_node("luafurnace", {
+minetest.register_node("experimental_luafurnace", {
        tile_images = {"lava.png", "furnace_side.png", "furnace_side.png",
                "furnace_side.png", "furnace_side.png", "furnace_front.png"},
        --inventory_image = "furnace_front.png",
@@ -15,7 +15,7 @@ minetest.register_node("luafurnace", {
 })
 
 minetest.register_on_placenode(function(pos, newnode, placer)
-       if newnode.name == "luafurnace" then
+       if newnode.name == "experimental_luafurnace" then
                print("get_meta");
                local meta = minetest.env:get_meta(pos)
                print("inventory_set_list");
@@ -42,7 +42,7 @@ minetest.register_on_placenode(function(pos, newnode, placer)
 end)
 
 minetest.register_abm({
-       nodenames = {"luafurnace"},
+       nodenames = {"experimental_luafurnace"},
        interval = 1.0,
        chance = 1,
        action = function(pos, node, active_object_count, active_object_count_wider)
@@ -97,7 +97,7 @@ minetest.register_abm({
 })
 
 minetest.register_craft({
-       output = 'node "luafurnace" 1',
+       output = 'node "experimental_luafurnace" 1',
        recipe = {
                {'node "cobble"', 'node "cobble"', 'node "cobble"'},
                {'node "cobble"', 'node "cobble"', 'node "cobble"'},
@@ -132,7 +132,7 @@ minetest.register_craft({
        }
 })
 
-minetest.register_node("somenode", {
+minetest.register_node("experimental_somenode", {
        tile_images = {"lava.png", "mese.png", "stone.png", "grass.png", "cobble.png", "tree_top.png"},
        inventory_image = "treeprop.png",
        material = {
@@ -151,7 +151,7 @@ minetest.register_node("somenode", {
 --
 
 minetest.register_craft({
-       output = 'node "TNT" 4',
+       output = 'node "experimental_tnt" 4',
        recipe = {
                {'node "wood" 1'},
                {'craft "lump_of_coal" 1'},
@@ -159,7 +159,7 @@ minetest.register_craft({
        }
 })
 
-minetest.register_node("TNT", {
+minetest.register_node("experimental_tnt", {
        tile_images = {"tnt_top.png", "tnt_bottom.png", "tnt_side.png", "tnt_side.png", "tnt_side.png", "tnt_side.png"},
        inventory_image = "tnt_side.png",
        dug_item = '', -- Get nothing
@@ -168,6 +168,14 @@ minetest.register_node("TNT", {
        },
 })
 
+minetest.register_on_punchnode(function(p, node)
+       if node.name == "experimental_tnt" then
+               minetest.env:remove_node(p)
+               minetest.env:add_luaentity(p, "experimental_tnt")
+               nodeupdate(p)
+       end
+end)
+
 local TNT = {
        -- Static definition
        physical = true, -- Collides with things
@@ -225,16 +233,15 @@ function TNT:on_rightclick(clicker)
        --self.object:moveto(pos, false)
 end
 
-print("TNT dump: "..dump(TNT))
-
-print("Registering TNT");
-minetest.register_entity("TNT", TNT)
+--print("TNT dump: "..dump(TNT))
+--print("Registering TNT");
+minetest.register_entity("experimental_tnt", TNT)
 
 --
 -- A test entity for testing animated and yaw-modulated sprites
 --
 
-minetest.register_entity("testentity", {
+minetest.register_entity("experimental_testentity", {
        -- Static definition
        physical = true, -- Collides with things
        -- weight = 5,
index 5a6c9802636a66108fffcfa08cfad46f3aa43179..8b1b7013e9ff7210854c9fefde58ca57f1b4b701 100644 (file)
@@ -31,6 +31,27 @@ extern "C" {
 #include <lauxlib.h>
 }
 
+LuaError::LuaError(lua_State *L, const std::string &s)
+{
+       m_s = "LuaError: ";
+       m_s += s + "\n";
+       lua_getfield(L, LUA_GLOBALSINDEX, "debug");
+       if(lua_istable(L, -1)){
+               lua_getfield(L, -1, "traceback");
+               if(lua_isfunction(L, -1)){
+                       lua_call(L, 0, 1);
+                       if(lua_isstring(L, -1)){
+                               m_s += lua_tostring(L, -1);
+                       }
+                       lua_pop(L, 1);
+               }
+               else{
+                       lua_pop(L, 1);
+               }
+       }
+       lua_pop(L, 1);
+}
+
 void script_error(lua_State *L, const char *fmt, ...)
 {
        va_list argp;
@@ -39,13 +60,34 @@ void script_error(lua_State *L, const char *fmt, ...)
        vsnprintf(buf, 10000, fmt, argp);
        va_end(argp);
        //errorstream<<"SCRIPT ERROR: "<<buf;
-       throw LuaError(buf);
+       throw LuaError(L, buf);
+}
+
+int luaErrorHandler(lua_State *L) {
+       lua_getfield(L, LUA_GLOBALSINDEX, "debug");
+       if (!lua_istable(L, -1)) {
+               lua_pop(L, 1);
+               return 1;
+       }
+       lua_getfield(L, -1, "traceback");
+       if (!lua_isfunction(L, -1)) {
+               lua_pop(L, 2);
+               return 1;
+       }
+       lua_pushvalue(L, 1);
+       lua_pushinteger(L, 2);
+       lua_call(L, 2, 1);
+       return 1;
 }
 
 bool script_load(lua_State *L, const char *path)
 {
        infostream<<"Loading and running script from "<<path<<std::endl;
-       int ret = luaL_loadfile(L, path) || lua_pcall(L, 0, 0, 0);
+
+       lua_pushcfunction(L, luaErrorHandler);
+       int errorhandler = lua_gettop(L);
+
+       int ret = luaL_loadfile(L, path) || lua_pcall(L, 0, 0, errorhandler);
        if(ret){
                errorstream<<"Failed to load and run script from "<<path<<":"<<std::endl;
                errorstream<<"[LUA] "<<std::endl;
index 6da95acf73fef7fe77773d19f61f7a1c9356a7f9..3c056ae35a1a6694c55fa090f09ab7dea73641c2 100644 (file)
@@ -23,14 +23,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <exception>
 #include <string>
 
+typedef struct lua_State lua_State;
+
 class LuaError : public std::exception
 {
 public:
-       LuaError(const std::string &s)
-       {
-               m_s = "LuaError: ";
-               m_s += s;
-       }
+       LuaError(lua_State *L, const std::string &s);
+
        virtual ~LuaError() throw()
        {}
        virtual const char * what() const throw()
@@ -40,8 +39,6 @@ public:
        std::string m_s;
 };
 
-typedef struct lua_State lua_State;
-
 lua_State* script_init();
 void script_deinit(lua_State *L);
 void script_error(lua_State *L, const char *fmt, ...);
index db6b7e86e22ce27addaa58793eae9ed582c904ee..be136673783029c26da33ad7d08c13203866ed91 100644 (file)
@@ -107,6 +107,59 @@ public:
        }
 };
 
+class ModNameStorer
+{
+private:
+       lua_State *L;
+public:
+       ModNameStorer(lua_State *L_, const std::string modname):
+               L(L_)
+       {
+               // Store current modname in registry
+               lua_pushstring(L, modname.c_str());
+               lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
+       }
+       ~ModNameStorer()
+       {
+               // Clear current modname in registry
+               lua_pushnil(L);
+               lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
+       }
+};
+
+std::string get_current_modname(lua_State *L)
+{
+       lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
+       std::string modname = "";
+       if(lua_type(L, -1) == LUA_TSTRING)
+               modname = lua_tostring(L, -1);
+       lua_pop(L, 1);
+       return modname;
+}
+
+void check_modname_prefix(lua_State *L, std::string &name)
+{
+       if(name.size() == 0)
+               throw LuaError(L, std::string("Name is empty"));
+       
+       if(name[0] == ':'){
+               name = name.substr(1);
+               return;
+       }
+       
+       std::string modname = get_current_modname(L);
+       assert(modname != "");
+       
+       // For __builtin, anything goes
+       if(modname == "__builtin")
+               return;
+
+       if(name.substr(0, modname.size()+1) != modname + "_")
+               throw LuaError(L, std::string("Name \"")+name
+                               +"\" does not follow naming conventions: "
+                               +"\"modname_\" or \":\" prefix required)");
+}
+
 static v3f readFloatPos(lua_State *L, int index)
 {
        v3f pos;
@@ -470,7 +523,7 @@ static void inventory_get_list_to_lua(Inventory *inv, const char *name,
                        lua_pushstring(L, item->getItemString().c_str());
                }
                if(lua_pcall(L, 2, 0, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
        }
 }
 
@@ -611,8 +664,9 @@ static int l_register_nodedef_defaults(lua_State *L)
 // register_entity(name, prototype)
 static int l_register_entity(lua_State *L)
 {
-       const char *name = luaL_checkstring(L, 1);
-       infostream<<"register_entity: "<<name<<std::endl;
+       std::string name = luaL_checkstring(L, 1);
+       check_modname_prefix(L, name);
+       //infostream<<"register_entity: "<<name<<std::endl;
        luaL_checktype(L, 2, LUA_TTABLE);
 
        // Get minetest.registered_entities
@@ -622,7 +676,7 @@ static int l_register_entity(lua_State *L)
        int registered_entities = lua_gettop(L);
        lua_pushvalue(L, 2); // Object = param 2 -> stack top
        // registered_entities[name] = object
-       lua_setfield(L, registered_entities, name);
+       lua_setfield(L, registered_entities, name.c_str());
        
        // Get registered object to top of stack
        lua_pushvalue(L, 2);
@@ -703,14 +757,14 @@ public:
                lua_pushnumber(L, active_object_count);
                lua_pushnumber(L, active_object_count_wider);
                if(lua_pcall(L, 4, 0, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
        }
 };
 
 // register_abm({...})
 static int l_register_abm(lua_State *L)
 {
-       infostream<<"register_abm"<<std::endl;
+       //infostream<<"register_abm"<<std::endl;
        luaL_checktype(L, 1, LUA_TTABLE);
 
        // Get minetest.registered_abms
@@ -744,8 +798,9 @@ static int l_register_abm(lua_State *L)
 // register_tool(name, {lots of stuff})
 static int l_register_tool(lua_State *L)
 {
-       const char *name = luaL_checkstring(L, 1);
-       infostream<<"register_tool: "<<name<<std::endl;
+       std::string name = luaL_checkstring(L, 1);
+       check_modname_prefix(L, name);
+       //infostream<<"register_tool: "<<name<<std::endl;
        luaL_checktype(L, 2, LUA_TTABLE);
        int table = 2;
 
@@ -765,8 +820,9 @@ static int l_register_tool(lua_State *L)
 // register_craftitem(name, {lots of stuff})
 static int l_register_craftitem(lua_State *L)
 {
-       const char *name = luaL_checkstring(L, 1);
-       infostream<<"register_craftitem: "<<name<<std::endl;
+       std::string name = luaL_checkstring(L, 1);
+       check_modname_prefix(L, name);
+       //infostream<<"register_craftitem: "<<name<<std::endl;
        luaL_checktype(L, 2, LUA_TTABLE);
        int table = 2;
 
@@ -806,7 +862,7 @@ static int l_register_craftitem(lua_State *L)
        craftitemdef->registerCraftItem(name, def);
 
        lua_pushvalue(L, table);
-       scriptapi_add_craftitem(L, name);
+       scriptapi_add_craftitem(L, name.c_str());
 
        return 0; /* number of results */
 }
@@ -814,8 +870,9 @@ static int l_register_craftitem(lua_State *L)
 // register_node(name, {lots of stuff})
 static int l_register_node(lua_State *L)
 {
-       const char *name = luaL_checkstring(L, 1);
-       infostream<<"register_node: "<<name<<std::endl;
+       std::string name = luaL_checkstring(L, 1);
+       check_modname_prefix(L, name);
+       //infostream<<"register_node: "<<name<<std::endl;
        luaL_checktype(L, 2, LUA_TTABLE);
        int nodedef_table = 2;
 
@@ -849,7 +906,7 @@ static int l_register_node(lua_State *L)
        lua_getglobal(L, "minetest");
        lua_getfield(L, -1, "registered_nodes");
        luaL_checktype(L, -1, LUA_TTABLE);
-       lua_pushstring(L, name);
+       lua_pushstring(L, name.c_str());
        lua_pushvalue(L, nodedef_table);
        lua_settable(L, -3);
 
@@ -1054,7 +1111,7 @@ static int l_register_node(lua_State *L)
 // register_craft({output=item, recipe={{item00,item10},{item01,item11}})
 static int l_register_craft(lua_State *L)
 {
-       infostream<<"register_craft"<<std::endl;
+       //infostream<<"register_craft"<<std::endl;
        luaL_checktype(L, 1, LUA_TTABLE);
        int table0 = 1;
 
@@ -1104,7 +1161,7 @@ static int l_register_craft(lua_State *L)
                                        std::string error;
                                        error += "Invalid crafting recipe (output=\""
                                                        + output + "\")";
-                                       throw LuaError(error);
+                                       throw LuaError(L, error);
                                }
                        }
                        // removes value, keeps key for next iteration
@@ -2475,13 +2532,16 @@ void scriptapi_export(lua_State *L, Server *server)
 bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath,
                const std::string &modname)
 {
+       ModNameStorer modnamestorer(L, modname);
+
        bool success = false;
 
        try{
                success = script_load(L, scriptpath.c_str());
        }
        catch(LuaError &e){
-               errorstream<<"Error loading mod: "<<e.what()<<std::endl;
+               errorstream<<"Error loading mod \""<<modname
+                               <<"\": "<<e.what()<<std::endl;
        }
 
        return success;
@@ -2567,7 +2627,7 @@ static void dump2(lua_State *L, const char *name)
        lua_pushvalue(L, -2); // Get previous stack top as first parameter
        lua_pushstring(L, name);
        if(lua_pcall(L, 2, 0, 0))
-               script_error(L, "error: %s\n", lua_tostring(L, -1));
+               script_error(L, "error: %s", lua_tostring(L, -1));
 }
 #endif
 
@@ -2645,7 +2705,7 @@ bool scriptapi_on_chat_message(lua_State *L, const std::string &name,
                lua_pushstring(L, name.c_str());
                lua_pushstring(L, message.c_str());
                if(lua_pcall(L, 2, 1, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
                bool ate = lua_toboolean(L, -1);
                lua_pop(L, 1);
                if(ate)
@@ -2678,7 +2738,7 @@ void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player)
                // Call function
                objectref_get_or_create(L, player);
                if(lua_pcall(L, 1, 0, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
                // value removed, keep key for next iteration
        }
 }
@@ -2703,7 +2763,7 @@ bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player)
                // Call function
                objectref_get_or_create(L, player);
                if(lua_pcall(L, 1, 1, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
                bool positioning_handled = lua_toboolean(L, -1);
                lua_pop(L, 1);
                if(positioning_handled)
@@ -2825,7 +2885,7 @@ bool scriptapi_craftitem_on_drop(lua_State *L, const char *name,
                objectref_get_or_create(L, dropper);
                pushFloatPos(L, pos);
                if(lua_pcall(L, 3, 1, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
                result = lua_toboolean(L, -1);
        }
        return result;
@@ -2849,7 +2909,7 @@ bool scriptapi_craftitem_on_place_on_ground(lua_State *L, const char *name,
                objectref_get_or_create(L, placer);
                pushFloatPos(L, pos);
                if(lua_pcall(L, 3, 1, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
                result = lua_toboolean(L, -1);
        }
        return result;
@@ -2873,7 +2933,7 @@ bool scriptapi_craftitem_on_use(lua_State *L, const char *name,
                objectref_get_or_create(L, user);
                pushPointedThing(L, pointed);
                if(lua_pcall(L, 3, 1, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
                result = lua_toboolean(L, -1);
        }
        return result;
@@ -2903,7 +2963,7 @@ void scriptapi_environment_step(lua_State *L, float dtime)
                // Call function
                lua_pushnumber(L, dtime);
                if(lua_pcall(L, 1, 0, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
                // value removed, keep key for next iteration
        }
 }
@@ -2938,7 +2998,7 @@ void scriptapi_environment_on_placenode(lua_State *L, v3s16 p, MapNode newnode,
                pushnode(L, newnode, ndef);
                objectref_get_or_create(L, placer);
                if(lua_pcall(L, 3, 0, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
                // value removed, keep key for next iteration
        }
 }
@@ -2973,7 +3033,7 @@ void scriptapi_environment_on_dignode(lua_State *L, v3s16 p, MapNode oldnode,
                pushnode(L, oldnode, ndef);
                objectref_get_or_create(L, digger);
                if(lua_pcall(L, 3, 0, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
                // value removed, keep key for next iteration
        }
 }
@@ -3008,7 +3068,7 @@ void scriptapi_environment_on_punchnode(lua_State *L, v3s16 p, MapNode node,
                pushnode(L, node, ndef);
                objectref_get_or_create(L, puncher);
                if(lua_pcall(L, 3, 0, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
                // value removed, keep key for next iteration
        }
 }
@@ -3034,7 +3094,7 @@ void scriptapi_environment_on_generated(lua_State *L, v3s16 minp, v3s16 maxp)
                pushpos(L, minp);
                pushpos(L, maxp);
                if(lua_pcall(L, 2, 0, 0))
-                       script_error(L, "error: %s\n", lua_tostring(L, -1));
+                       script_error(L, "error: %s", lua_tostring(L, -1));
                // value removed, keep key for next iteration
        }
 }
index e345d8811ff28155225f0642c6329d41a9151769..19138e52e9d511a4652564d9901c4f7b661d83f5 100644 (file)
@@ -978,7 +978,7 @@ Server::Server(
        // Load and run builtin.lua
        infostream<<"Server: Loading builtin Lua stuff from \""<<builtinpath
                        <<"\""<<std::endl;
-       bool success = script_load(m_lua, builtinpath.c_str());
+       bool success = scriptapi_loadmod(m_lua, builtinpath, "__builtin");
        if(!success){
                errorstream<<"Server: Failed to load and run "
                                <<builtinpath<<std::endl;