From 97908cc65670d3f6cf2e286390bfea10f653aaa8 Mon Sep 17 00:00:00 2001 From: Alex Ford Date: Wed, 18 Nov 2015 12:26:09 -0700 Subject: Add on_secondary_use when right clicking an item in the air --- src/script/cpp_api/s_item.cpp | 26 ++++++++++++++++++++++++++ src/script/cpp_api/s_item.h | 2 ++ 2 files changed, 28 insertions(+) (limited to 'src/script') diff --git a/src/script/cpp_api/s_item.cpp b/src/script/cpp_api/s_item.cpp index d9a545b4f..3c84fb8cf 100644 --- a/src/script/cpp_api/s_item.cpp +++ b/src/script/cpp_api/s_item.cpp @@ -110,6 +110,32 @@ bool ScriptApiItem::item_OnUse(ItemStack &item, return true; } +bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item, ServerActiveObject *user) +{ + SCRIPTAPI_PRECHECKHEADER + + int error_handler = PUSH_ERROR_HANDLER(L); + + if (!getItemCallback(item.name.c_str(), "on_secondary_use")) + return false; + + LuaItemStack::create(L, item); + objectrefGetOrCreate(L, user); + PointedThing pointed; + pointed.type = POINTEDTHING_NOTHING; + pushPointedThing(pointed); + PCALL_RES(lua_pcall(L, 3, 1, error_handler)); + if (!lua_isnil(L, -1)) { + try { + item = read_item(L, -1, getServer()); + } catch (LuaError &e) { + throw LuaError(std::string(e.what()) + ". item=" + item.name); + } + } + lua_pop(L, 2); // Pop item and error handler + return true; +} + bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user, const InventoryList *old_craft_grid, const InventoryLocation &craft_inv) { diff --git a/src/script/cpp_api/s_item.h b/src/script/cpp_api/s_item.h index 88cc1909d..7350a71c5 100644 --- a/src/script/cpp_api/s_item.h +++ b/src/script/cpp_api/s_item.h @@ -42,6 +42,8 @@ public: ServerActiveObject *placer, const PointedThing &pointed); bool item_OnUse(ItemStack &item, ServerActiveObject *user, const PointedThing &pointed); + bool item_OnSecondaryUse(ItemStack &item, + ServerActiveObject *user); bool item_OnCraft(ItemStack &item, ServerActiveObject *user, const InventoryList *old_craft_grid, const InventoryLocation &craft_inv); bool item_CraftPredict(ItemStack &item, ServerActiveObject *user, -- cgit v1.2.3