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(L, dropper);
45 PCALL_RES(lua_pcall(L, 3, 1, m_errorhandler));
46 if (!lua_isnil(L, -1)) {
48 item = read_item(L,-1, getServer());
49 } catch (LuaError &e) {
50 throw LuaError(std::string(e.what()) + ". item=" + item.name);
53 lua_pop(L, 1); // Pop item
57 bool ScriptApiItem::item_OnPlace(ItemStack &item,
58 ServerActiveObject *placer, const PointedThing &pointed)
60 SCRIPTAPI_PRECHECKHEADER
62 // Push callback function on stack
63 if (!getItemCallback(item.name.c_str(), "on_place"))
67 LuaItemStack::create(L, item);
68 objectrefGetOrCreate(L, placer);
69 pushPointedThing(pointed);
70 PCALL_RES(lua_pcall(L, 3, 1, m_errorhandler));
71 if (!lua_isnil(L, -1)) {
73 item = read_item(L,-1, getServer());
74 } catch (LuaError &e) {
75 throw LuaError(std::string(e.what()) + ". item=" + item.name);
78 lua_pop(L, 1); // Pop item
82 bool ScriptApiItem::item_OnUse(ItemStack &item,
83 ServerActiveObject *user, const PointedThing &pointed)
85 SCRIPTAPI_PRECHECKHEADER
87 // Push callback function on stack
88 if (!getItemCallback(item.name.c_str(), "on_use"))
92 LuaItemStack::create(L, item);
93 objectrefGetOrCreate(L, user);
94 pushPointedThing(pointed);
95 PCALL_RES(lua_pcall(L, 3, 1, m_errorhandler));
96 if(!lua_isnil(L, -1)) {
98 item = read_item(L,-1, getServer());
99 } catch (LuaError &e) {
100 throw LuaError(std::string(e.what()) + ". item=" + item.name);
103 lua_pop(L, 1); // Pop item
107 bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
108 const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
110 SCRIPTAPI_PRECHECKHEADER
112 lua_getglobal(L, "core");
113 lua_getfield(L, -1, "on_craft");
114 LuaItemStack::create(L, item);
115 objectrefGetOrCreate(L, user);
117 // Push inventory list
118 std::vector<ItemStack> items;
119 for (u32 i = 0; i < old_craft_grid->getSize(); i++) {
120 items.push_back(old_craft_grid->getItem(i));
122 push_items(L, items);
124 InvRef::create(L, craft_inv);
125 PCALL_RES(lua_pcall(L, 4, 1, m_errorhandler));
126 if (!lua_isnil(L, -1)) {
128 item = read_item(L,-1, getServer());
129 } catch (LuaError &e) {
130 throw LuaError(std::string(e.what()) + ". item=" + item.name);
133 lua_pop(L, 1); // Pop item
137 bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
138 const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
140 SCRIPTAPI_PRECHECKHEADER
142 lua_getglobal(L, "core");
143 lua_getfield(L, -1, "craft_predict");
144 LuaItemStack::create(L, item);
145 objectrefGetOrCreate(L, user);
147 //Push inventory list
148 std::vector<ItemStack> items;
149 for (u32 i = 0; i < old_craft_grid->getSize(); i++) {
150 items.push_back(old_craft_grid->getItem(i));
152 push_items(L, items);
154 InvRef::create(L, craft_inv);
155 PCALL_RES(lua_pcall(L, 4, 1, m_errorhandler));
156 if (!lua_isnil(L, -1)) {
158 item = read_item(L,-1, getServer());
159 } catch (LuaError &e) {
160 throw LuaError(std::string(e.what()) + ". item=" + item.name);
163 lua_pop(L, 1); // Pop item
167 // Retrieves core.registered_items[name][callbackname]
168 // If that is nil or on error, return false and stack is unchanged
169 // If that is a function, returns true and pushes the
170 // function onto the stack
171 // If core.registered_items[name] doesn't exist, core.nodedef_default
172 // is tried instead so unknown items can still be manipulated to some degree
173 bool ScriptApiItem::getItemCallback(const char *name, const char *callbackname)
175 lua_State* L = getStack();
177 lua_getglobal(L, "core");
178 lua_getfield(L, -1, "registered_items");
179 lua_remove(L, -2); // Remove core
180 luaL_checktype(L, -1, LUA_TTABLE);
181 lua_getfield(L, -1, name);
182 lua_remove(L, -2); // Remove registered_items
184 if(lua_type(L, -1) != LUA_TTABLE)
186 // Report error and clean up
187 errorstream << "Item \"" << name << "\" not defined" << std::endl;
190 // Try core.nodedef_default instead
191 lua_getglobal(L, "core");
192 lua_getfield(L, -1, "nodedef_default");
194 luaL_checktype(L, -1, LUA_TTABLE);
196 lua_getfield(L, -1, callbackname);
197 lua_remove(L, -2); // Remove item def
198 // Should be a function or nil
199 if (lua_type(L, -1) == LUA_TFUNCTION) {
201 } else if (!lua_isnil(L, -1)) {
202 errorstream << "Item \"" << name << "\" callback \""
203 << callbackname << "\" is not a function" << std::endl;
209 void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
211 lua_State* L = getStack();
214 if(pointed.type == POINTEDTHING_NODE)
216 lua_pushstring(L, "node");
217 lua_setfield(L, -2, "type");
218 push_v3s16(L, pointed.node_undersurface);
219 lua_setfield(L, -2, "under");
220 push_v3s16(L, pointed.node_abovesurface);
221 lua_setfield(L, -2, "above");
223 else if(pointed.type == POINTEDTHING_OBJECT)
225 lua_pushstring(L, "object");
226 lua_setfield(L, -2, "type");
227 objectrefGet(L, pointed.object_id);
228 lua_setfield(L, -2, "ref");
232 lua_pushstring(L, "nothing");
233 lua_setfield(L, -2, "type");