3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "cpp_api/s_item.h"
21 #include "common/c_converter.h"
22 #include "common/c_content.h"
23 #include "lua_api/l_item.h"
26 bool ScriptApiItem::item_OnDrop(ItemStack &item,
27 ServerActiveObject *dropper, v3f pos)
29 SCRIPTAPI_PRECHECKHEADER
31 // Push callback function on stack
32 if(!getItemCallback(item.name.c_str(), "on_drop"))
36 LuaItemStack::create(L, item);
37 objectrefGetOrCreate(dropper);
39 if(lua_pcall(L, 3, 1, 0))
40 scriptError("error: %s", lua_tostring(L, -1));
42 item = read_item(L,-1, getServer());
46 bool ScriptApiItem::item_OnPlace(ItemStack &item,
47 ServerActiveObject *placer, const PointedThing &pointed)
49 SCRIPTAPI_PRECHECKHEADER
51 // Push callback function on stack
52 if(!getItemCallback(item.name.c_str(), "on_place"))
56 LuaItemStack::create(L, item);
57 objectrefGetOrCreate(placer);
58 pushPointedThing(pointed);
59 if(lua_pcall(L, 3, 1, 0))
60 scriptError("error: %s", lua_tostring(L, -1));
62 item = read_item(L,-1, getServer());
66 bool ScriptApiItem::item_OnUse(ItemStack &item,
67 ServerActiveObject *user, const PointedThing &pointed)
69 SCRIPTAPI_PRECHECKHEADER
71 // Push callback function on stack
72 if(!getItemCallback(item.name.c_str(), "on_use"))
76 LuaItemStack::create(L, item);
77 objectrefGetOrCreate(user);
78 pushPointedThing(pointed);
79 if(lua_pcall(L, 3, 1, 0))
80 scriptError("error: %s", lua_tostring(L, -1));
82 item = read_item(L,-1, getServer());
86 // Retrieves minetest.registered_items[name][callbackname]
87 // If that is nil or on error, return false and stack is unchanged
88 // If that is a function, returns true and pushes the
89 // function onto the stack
90 // If minetest.registered_items[name] doesn't exist, minetest.nodedef_default
91 // is tried instead so unknown items can still be manipulated to some degree
92 bool ScriptApiItem::getItemCallback(const char *name, const char *callbackname)
94 lua_State* L = getStack();
96 lua_getglobal(L, "minetest");
97 lua_getfield(L, -1, "registered_items");
99 luaL_checktype(L, -1, LUA_TTABLE);
100 lua_getfield(L, -1, name);
103 if(lua_type(L, -1) != LUA_TTABLE)
105 // Report error and clean up
106 errorstream<<"Item \""<<name<<"\" not defined"<<std::endl;
109 // Try minetest.nodedef_default instead
110 lua_getglobal(L, "minetest");
111 lua_getfield(L, -1, "nodedef_default");
113 luaL_checktype(L, -1, LUA_TTABLE);
115 lua_getfield(L, -1, callbackname);
117 // Should be a function or nil
118 if(lua_type(L, -1) == LUA_TFUNCTION)
122 else if(lua_isnil(L, -1))
129 errorstream<<"Item \""<<name<<"\" callback \""
130 <<callbackname<<" is not a function"<<std::endl;
136 void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
138 lua_State* L = getStack();
141 if(pointed.type == POINTEDTHING_NODE)
143 lua_pushstring(L, "node");
144 lua_setfield(L, -2, "type");
145 push_v3s16(L, pointed.node_undersurface);
146 lua_setfield(L, -2, "under");
147 push_v3s16(L, pointed.node_abovesurface);
148 lua_setfield(L, -2, "above");
150 else if(pointed.type == POINTEDTHING_OBJECT)
152 lua_pushstring(L, "object");
153 lua_setfield(L, -2, "type");
154 objectrefGet(pointed.object_id);
155 lua_setfield(L, -2, "ref");
159 lua_pushstring(L, "nothing");
160 lua_setfield(L, -2, "type");