summaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_item.cpp
diff options
context:
space:
mode:
authorNovatux <nathanael.courant@laposte.net>2013-10-26 11:25:28 +0200
committerNovatux <nathanael.courant@laposte.net>2013-11-01 15:55:34 +0100
commitbd6d4666abe20247437ede28f73955d413fe5e22 (patch)
treec67cd847a18096a5889f00f8a4e613209bd6382e /src/script/cpp_api/s_item.cpp
parent2f10cfb226c6de8024f5efa9bf14ea22b5b90ce2 (diff)
downloadminetest-bd6d4666abe20247437ede28f73955d413fe5e22.tar.gz
minetest-bd6d4666abe20247437ede28f73955d413fe5e22.tar.bz2
minetest-bd6d4666abe20247437ede28f73955d413fe5e22.zip
Add a callback: minetest.register_on_craft(itemstack, player,
old_craft_grid, craft_inv) and minetest.register_craft_predict(itemstack, player, old_craft_grid, craft_inv)
Diffstat (limited to 'src/script/cpp_api/s_item.cpp')
-rw-r--r--src/script/cpp_api/s_item.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/src/script/cpp_api/s_item.cpp b/src/script/cpp_api/s_item.cpp
index b4536ac63..1d5f218cf 100644
--- a/src/script/cpp_api/s_item.cpp
+++ b/src/script/cpp_api/s_item.cpp
@@ -22,9 +22,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/c_converter.h"
#include "common/c_content.h"
#include "lua_api/l_item.h"
+#include "lua_api/l_inventory.h"
#include "server.h"
#include "log.h"
#include "util/pointedthing.h"
+#include "inventory.h"
+#include "inventorymanager.h"
bool ScriptApiItem::item_OnDrop(ItemStack &item,
ServerActiveObject *dropper, v3f pos)
@@ -86,6 +89,54 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
return true;
}
+bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
+ const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
+{
+ SCRIPTAPI_PRECHECKHEADER
+
+ lua_getglobal(L, "minetest");
+ lua_getfield(L, -1, "on_craft");
+ LuaItemStack::create(L, item);
+ objectrefGetOrCreate(user);
+
+ //Push inventory list
+ std::vector<ItemStack> items;
+ for(u32 i=0; i<old_craft_grid->getSize(); i++)
+ items.push_back(old_craft_grid->getItem(i));
+ push_items(L, items);
+
+ InvRef::create(L, craft_inv);
+ if(lua_pcall(L, 4, 1, 0))
+ scriptError("error: %s", lua_tostring(L, -1));
+ if(!lua_isnil(L, -1))
+ item = read_item(L,-1, getServer());
+ return true;
+}
+
+bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
+ const InventoryList *old_craft_grid, const InventoryLocation &craft_inv)
+{
+ SCRIPTAPI_PRECHECKHEADER
+
+ lua_getglobal(L, "minetest");
+ lua_getfield(L, -1, "craft_predict");
+ LuaItemStack::create(L, item);
+ objectrefGetOrCreate(user);
+
+ //Push inventory list
+ std::vector<ItemStack> items;
+ for(u32 i=0; i<old_craft_grid->getSize(); i++)
+ items.push_back(old_craft_grid->getItem(i));
+ push_items(L, items);
+
+ InvRef::create(L, craft_inv);
+ if(lua_pcall(L, 4, 1, 0))
+ scriptError("error: %s", lua_tostring(L, -1));
+ if(!lua_isnil(L, -1))
+ item = read_item(L,-1, getServer());
+ return true;
+}
+
// Retrieves minetest.registered_items[name][callbackname]
// If that is nil or on error, return false and stack is unchanged
// If that is a function, returns true and pushes the