]> git.lizzy.rs Git - minetest.git/commitdiff
Add on_secondary_use when right clicking an item in the air
authorAlex Ford <gordion42@gmail.com>
Wed, 18 Nov 2015 19:26:09 +0000 (12:26 -0700)
committerparamat <mat.gregory@virginmedia.com>
Wed, 2 Dec 2015 02:18:44 +0000 (02:18 +0000)
builtin/game/item.lua
builtin/game/register.lua
doc/lua_api.txt
src/client.cpp
src/game.cpp
src/network/serverpackethandler.cpp
src/script/cpp_api/s_item.cpp
src/script/cpp_api/s_item.h

index 44ec35cc9d8f394b109e55722b1e5cbee1943e3a..6c2214ef6360964916e836b599e1ebf87a685d54 100644 (file)
@@ -347,6 +347,10 @@ function core.item_place(itemstack, placer, pointed_thing, param2)
        return itemstack
 end
 
+function core.item_secondary_use(itemstack, placer)
+       return itemstack
+end
+
 function core.item_drop(itemstack, dropper, pos)
        if dropper and dropper:is_player() then
                local v = dropper:get_look_dir()
@@ -605,6 +609,7 @@ core.craftitemdef_default = {
        -- Interaction callbacks
        on_place = redef_wrapper(core, 'item_place'), -- core.item_place
        on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
+       on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
        on_use = nil,
 }
 
@@ -622,6 +627,7 @@ core.tooldef_default = {
 
        -- Interaction callbacks
        on_place = redef_wrapper(core, 'item_place'), -- core.item_place
+       on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
        on_drop = redef_wrapper(core, 'item_drop'), -- core.item_drop
        on_use = nil,
 }
@@ -640,6 +646,7 @@ core.noneitemdef_default = {  -- This is used for the hand and unknown items
 
        -- Interaction callbacks
        on_place = redef_wrapper(core, 'item_place'),
+       on_secondary_use = redef_wrapper(core, 'item_secondary_use'),
        on_drop = nil,
        on_use = nil,
 }
index 00bb2327879f5094e541e6b3387aa947ae66c39b..992fdf74481ae0a0f8f7e1347297c635e7e0f43a 100644 (file)
@@ -272,6 +272,7 @@ core.register_item(":unknown", {
        description = "Unknown Item",
        inventory_image = "unknown_item.png",
        on_place = core.item_place,
+       on_secondary_use = core.item_secondary_use,
        on_drop = core.item_drop,
        groups = {not_in_creative_inventory=1},
        diggable = true,
index 9ffb497d70786bd8ce3ebbf02f92a7bb2f765959..11f90ba037840192c2f1f2a540557f0d95d66f7e 100644 (file)
@@ -3303,6 +3303,11 @@ Definition tables
         --[[
         ^ Shall place item and return the leftover itemstack
         ^ default: minetest.item_place ]]
+        on_secondary_use = func(itemstack, user, pointed_thing),
+        --[[
+        ^ Same as on_place but called when pointing at nothing.
+        ^ pointed_thing : always { type = "nothing" }
+        ]]
         on_drop = func(itemstack, dropper, pos),
         --[[
         ^ Shall drop item and return the leftover itemstack
index 5c04632d1ecbe09d8d2c35d7bc3163db63044ec2..8e49ee3ba6d822a0c95335541ba3a5cc696624f6 100644 (file)
@@ -946,6 +946,7 @@ void Client::interact(u8 action, const PointedThing& pointed)
                2: digging completed
                3: place block or item (to abovesurface)
                4: use item
+               5: perform secondary action of item
        */
 
        NetworkPacket pkt(TOSERVER_INTERACT, 1 + 2 + 0);
index e6a1a225603636b21c36e3018cee414f0a8ab04d..5e4f4cacf2ec08ba7d281a38520f3df8a4203b55 100644 (file)
@@ -1525,6 +1525,7 @@ class Game {
        void processPlayerInteraction(std::vector<aabb3f> &highlight_boxes,
                        GameRunData *runData, f32 dtime, bool show_hud,
                        bool show_debug);
+       void handlePointingAtNothing(GameRunData *runData, const ItemStack &playerItem);
        void handlePointingAtNode(GameRunData *runData,
                        const PointedThing &pointed, const ItemDefinition &playeritem_def,
                        const ToolCapabilities &playeritem_toolcap, f32 dtime);
@@ -3603,6 +3604,8 @@ void Game::processPlayerInteraction(std::vector<aabb3f> &highlight_boxes,
        } else if (input->getLeftState()) {
                // When button is held down in air, show continuous animation
                runData->left_punch = true;
+       } else if (input->getRightClicked()) {
+               handlePointingAtNothing(runData, playeritem);
        }
 
        runData->pointed_old = pointed;
@@ -3618,6 +3621,15 @@ void Game::processPlayerInteraction(std::vector<aabb3f> &highlight_boxes,
 }
 
 
+void Game::handlePointingAtNothing(GameRunData *runData, const ItemStack &playerItem)
+{
+       infostream << "Right Clicked in Air" << std::endl;
+       PointedThing fauxPointed;
+       fauxPointed.type = POINTEDTHING_NOTHING;
+       client->interact(5, fauxPointed);
+}
+
+
 void Game::handlePointingAtNode(GameRunData *runData,
                const PointedThing &pointed, const ItemDefinition &playeritem_def,
                const ToolCapabilities &playeritem_toolcap, f32 dtime)
index a4fa502aecf02afe101a2742eaec3a4d15141dc7..a5aaf1ecbba6bac24a55bd876bcef3ecedc87614 100644 (file)
@@ -1653,6 +1653,23 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
                }
 
        } // action == 4
+       
+       /*
+               5: rightclick air
+       */
+       else if (action == 5) {
+               ItemStack item = playersao->getWieldedItem();
+               
+               actionstream << player->getName() << " activates " 
+                               << item.name << std::endl;
+               
+               if (m_script->item_OnSecondaryUse(
+                               item, playersao)) {
+                       if( playersao->setWieldedItem(item)) {
+                               SendInventory(playersao);
+                       }
+               }
+       }
 
 
        /*
index d9a545b4ff9ccea1c68a0a5e07060c140185bcf0..3c84fb8cf60763249e3532200294acbdab36a8c5 100644 (file)
@@ -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)
 {
index 88cc1909d9016491dd5ac2e38e3499b7e576a13d..7350a71c5e3d19d6e0d901d447e40d4b17dc193d 100644 (file)
@@ -42,6 +42,8 @@ class ScriptApiItem
                        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,