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, m_errorhandler))
47 if (!lua_isnil(L, -1)) {
49 item = read_item(L,-1, getServer());
50 } catch (LuaError &e) {
51 throw LuaError(std::string(e.what()) + ". item=" + item.name);
54 lua_pop(L, 1); // Pop item
58 bool ScriptApiItem::item_OnPlace(ItemStack &item,
59 ServerActiveObject *placer, const PointedThing &pointed)
61 SCRIPTAPI_PRECHECKHEADER
63 // Push callback function on stack
64 if (!getItemCallback(item.name.c_str(), "on_place"))
68 LuaItemStack::create(L, item);
69 objectrefGetOrCreate(placer);
70 pushPointedThing(pointed);
71 if (lua_pcall(L, 3, 1, m_errorhandler))
73 if (!lua_isnil(L, -1)) {
75 item = read_item(L,-1, getServer());
76 } catch (LuaError &e) {
77 throw LuaError(std::string(e.what()) + ". item=" + item.name);
80 lua_pop(L, 1); // Pop item
84 bool ScriptApiItem::item_OnUse(ItemStack &item,
85 ServerActiveObject *user, const PointedThing &pointed)
87 SCRIPTAPI_PRECHECKHEADER
89 // Push callback function on stack
90 if (!getItemCallback(item.name.c_str(), "on_use"))
94 LuaItemStack::create(L, item);
95 objectrefGetOrCreate(user);
96 pushPointedThing(pointed);
97 if (lua_pcall(L, 3, 1, m_errorhandler))
99 if(!lua_isnil(L, -1)) {
101 item = read_item(L,-1, getServer());
102 } catch (LuaError &e) {
103 throw LuaError(std::string(e.what()) + ". item=" + item.name);
106 lua_pop(L, 1); // Pop item
110 bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
111 const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
113 SCRIPTAPI_PRECHECKHEADER
115 lua_getglobal(L, "core");
116 lua_getfield(L, -1, "on_craft");
117 LuaItemStack::create(L, item);
118 objectrefGetOrCreate(user);
120 // Push inventory list
121 std::vector<ItemStack> items;
122 for (u32 i = 0; i < old_craft_grid->getSize(); i++) {
123 items.push_back(old_craft_grid->getItem(i));
125 push_items(L, items);
127 InvRef::create(L, craft_inv);
128 if (lua_pcall(L, 4, 1, m_errorhandler))
130 if (!lua_isnil(L, -1)) {
132 item = read_item(L,-1, getServer());
133 } catch (LuaError &e) {
134 throw LuaError(std::string(e.what()) + ". item=" + item.name);
137 lua_pop(L, 1); // Pop item
141 bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
142 const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
144 SCRIPTAPI_PRECHECKHEADER
146 lua_getglobal(L, "core");
147 lua_getfield(L, -1, "craft_predict");
148 LuaItemStack::create(L, item);
149 objectrefGetOrCreate(user);
151 //Push inventory list
152 std::vector<ItemStack> items;
153 for (u32 i = 0; i < old_craft_grid->getSize(); i++) {
154 items.push_back(old_craft_grid->getItem(i));
156 push_items(L, items);
158 InvRef::create(L, craft_inv);
159 if (lua_pcall(L, 4, 1, m_errorhandler))
161 if (!lua_isnil(L, -1)) {
163 item = read_item(L,-1, getServer());
164 } catch (LuaError &e) {
165 throw LuaError(std::string(e.what()) + ". item=" + item.name);
168 lua_pop(L, 1); // Pop item
172 // Retrieves core.registered_items[name][callbackname]
173 // If that is nil or on error, return false and stack is unchanged
174 // If that is a function, returns true and pushes the
175 // function onto the stack
176 // If core.registered_items[name] doesn't exist, core.nodedef_default
177 // is tried instead so unknown items can still be manipulated to some degree
178 bool ScriptApiItem::getItemCallback(const char *name, const char *callbackname)
180 lua_State* L = getStack();
182 lua_getglobal(L, "core");
183 lua_getfield(L, -1, "registered_items");
184 lua_remove(L, -2); // Remove core
185 luaL_checktype(L, -1, LUA_TTABLE);
186 lua_getfield(L, -1, name);
187 lua_remove(L, -2); // Remove registered_items
189 if(lua_type(L, -1) != LUA_TTABLE)
191 // Report error and clean up
192 errorstream << "Item \"" << name << "\" not defined" << std::endl;
195 // Try core.nodedef_default instead
196 lua_getglobal(L, "core");
197 lua_getfield(L, -1, "nodedef_default");
199 luaL_checktype(L, -1, LUA_TTABLE);
201 lua_getfield(L, -1, callbackname);
202 lua_remove(L, -2); // Remove item def
203 // Should be a function or nil
204 if (lua_type(L, -1) == LUA_TFUNCTION) {
206 } else if (!lua_isnil(L, -1)) {
207 errorstream << "Item \"" << name << "\" callback \""
208 << callbackname << "\" is not a function" << std::endl;
214 void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
216 lua_State* L = getStack();
219 if(pointed.type == POINTEDTHING_NODE)
221 lua_pushstring(L, "node");
222 lua_setfield(L, -2, "type");
223 push_v3s16(L, pointed.node_undersurface);
224 lua_setfield(L, -2, "under");
225 push_v3s16(L, pointed.node_abovesurface);
226 lua_setfield(L, -2, "above");
228 else if(pointed.type == POINTEDTHING_OBJECT)
230 lua_pushstring(L, "object");
231 lua_setfield(L, -2, "type");
232 objectrefGet(pointed.object_id);
233 lua_setfield(L, -2, "ref");
237 lua_pushstring(L, "nothing");
238 lua_setfield(L, -2, "type");