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 "cpp_api/s_internal.h"
22 #include "common/c_converter.h"
23 #include "common/c_content.h"
24 #include "lua_api/l_item.h"
25 #include "lua_api/l_inventory.h"
28 #include "util/pointedthing.h"
29 #include "inventory.h"
30 #include "inventorymanager.h"
32 bool ScriptApiItem::item_OnDrop(ItemStack &item,
33 ServerActiveObject *dropper, v3f pos)
35 SCRIPTAPI_PRECHECKHEADER
37 // Push callback function on stack
38 if(!getItemCallback(item.name.c_str(), "on_drop"))
42 LuaItemStack::create(L, item);
43 objectrefGetOrCreate(dropper);
45 if(lua_pcall(L, 3, 1, 0))
46 scriptError("error: %s", lua_tostring(L, -1));
48 item = read_item(L,-1, getServer());
52 bool ScriptApiItem::item_OnPlace(ItemStack &item,
53 ServerActiveObject *placer, const PointedThing &pointed)
55 SCRIPTAPI_PRECHECKHEADER
57 // Push callback function on stack
58 if(!getItemCallback(item.name.c_str(), "on_place"))
62 LuaItemStack::create(L, item);
63 objectrefGetOrCreate(placer);
64 pushPointedThing(pointed);
65 if(lua_pcall(L, 3, 1, 0))
66 scriptError("error: %s", lua_tostring(L, -1));
68 item = read_item(L,-1, getServer());
72 bool ScriptApiItem::item_OnUse(ItemStack &item,
73 ServerActiveObject *user, const PointedThing &pointed)
75 SCRIPTAPI_PRECHECKHEADER
77 // Push callback function on stack
78 if(!getItemCallback(item.name.c_str(), "on_use"))
82 LuaItemStack::create(L, item);
83 objectrefGetOrCreate(user);
84 pushPointedThing(pointed);
85 if(lua_pcall(L, 3, 1, 0))
86 scriptError("error: %s", lua_tostring(L, -1));
88 item = read_item(L,-1, getServer());
92 bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
93 const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
95 SCRIPTAPI_PRECHECKHEADER
97 lua_getglobal(L, "minetest");
98 lua_getfield(L, -1, "on_craft");
99 LuaItemStack::create(L, item);
100 objectrefGetOrCreate(user);
102 //Push inventory list
103 std::vector<ItemStack> items;
104 for(u32 i=0; i<old_craft_grid->getSize(); i++)
105 items.push_back(old_craft_grid->getItem(i));
106 push_items(L, items);
108 InvRef::create(L, craft_inv);
109 if(lua_pcall(L, 4, 1, 0))
110 scriptError("error: %s", lua_tostring(L, -1));
111 if(!lua_isnil(L, -1))
112 item = read_item(L,-1, getServer());
116 bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
117 const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
119 SCRIPTAPI_PRECHECKHEADER
121 lua_getglobal(L, "minetest");
122 lua_getfield(L, -1, "craft_predict");
123 LuaItemStack::create(L, item);
124 objectrefGetOrCreate(user);
126 //Push inventory list
127 std::vector<ItemStack> items;
128 for(u32 i=0; i<old_craft_grid->getSize(); i++)
129 items.push_back(old_craft_grid->getItem(i));
130 push_items(L, items);
132 InvRef::create(L, craft_inv);
133 if(lua_pcall(L, 4, 1, 0))
134 scriptError("error: %s", lua_tostring(L, -1));
135 if(!lua_isnil(L, -1))
136 item = read_item(L,-1, getServer());
140 // Retrieves minetest.registered_items[name][callbackname]
141 // If that is nil or on error, return false and stack is unchanged
142 // If that is a function, returns true and pushes the
143 // function onto the stack
144 // If minetest.registered_items[name] doesn't exist, minetest.nodedef_default
145 // is tried instead so unknown items can still be manipulated to some degree
146 bool ScriptApiItem::getItemCallback(const char *name, const char *callbackname)
148 lua_State* L = getStack();
150 lua_getglobal(L, "minetest");
151 lua_getfield(L, -1, "registered_items");
153 luaL_checktype(L, -1, LUA_TTABLE);
154 lua_getfield(L, -1, name);
157 if(lua_type(L, -1) != LUA_TTABLE)
159 // Report error and clean up
160 errorstream<<"Item \""<<name<<"\" not defined"<<std::endl;
163 // Try minetest.nodedef_default instead
164 lua_getglobal(L, "minetest");
165 lua_getfield(L, -1, "nodedef_default");
167 luaL_checktype(L, -1, LUA_TTABLE);
169 lua_getfield(L, -1, callbackname);
171 // Should be a function or nil
172 if(lua_type(L, -1) == LUA_TFUNCTION)
176 else if(lua_isnil(L, -1))
183 errorstream<<"Item \""<<name<<"\" callback \""
184 <<callbackname<<" is not a function"<<std::endl;
190 void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
192 lua_State* L = getStack();
195 if(pointed.type == POINTEDTHING_NODE)
197 lua_pushstring(L, "node");
198 lua_setfield(L, -2, "type");
199 push_v3s16(L, pointed.node_undersurface);
200 lua_setfield(L, -2, "under");
201 push_v3s16(L, pointed.node_abovesurface);
202 lua_setfield(L, -2, "above");
204 else if(pointed.type == POINTEDTHING_OBJECT)
206 lua_pushstring(L, "object");
207 lua_setfield(L, -2, "type");
208 objectrefGet(pointed.object_id);
209 lua_setfield(L, -2, "ref");
213 lua_pushstring(L, "nothing");
214 lua_setfield(L, -2, "type");