Wieldhand: Allow overriding the hand
authorTeTpaAka <TeTpaAka@users.noreply.github.com>
Thu, 25 Jun 2015 11:06:49 +0000 (13:06 +0200)
committerparamat <mat.gregory@virginmedia.com>
Sat, 26 Nov 2016 03:49:30 +0000 (03:49 +0000)
doc/lua_api.txt
src/content_sao.cpp
src/content_sao.h
src/game.cpp
src/network/serverpackethandler.cpp
src/player.cpp

index 760a829d330d05803b19472b823ba64ab7672f17..9da0fb4f98549e5cd6f5684d60590a3f30fe026a 100644 (file)
@@ -1759,6 +1759,13 @@ Inventory locations
 * `"nodemeta:<X>,<Y>,<Z>"`: Any node metadata
 * `"detached:<name>"`: A detached inventory
 
+Player Inventory lists
+----------------------
+* `main`: list containing the default inventory
+* `craft`: list containing the craft input
+* `craftpreview`: list containing the craft output
+* `hand`: list containing an override for the empty hand
+
 `ColorString`
 -------------
 `#RGB` defines a color in hexadecimal format.
index 6caea5198d4823a45234be761c3e6426256a2902..609673ed93e7873aa2dd07bcba5315f145ec3d9d 100644 (file)
@@ -1341,6 +1341,42 @@ std::string PlayerSAO::getWieldList() const
        return "main";
 }
 
+ItemStack PlayerSAO::getWieldedItem() const
+{
+       const Inventory *inv = getInventory();
+       ItemStack ret;
+       const InventoryList *mlist = inv->getList(getWieldList());
+       if (mlist && getWieldIndex() < (s32)mlist->getSize())
+               ret = mlist->getItem(getWieldIndex());
+       if (ret.name.empty()) {
+               const InventoryList *hlist = inv->getList("hand");
+               if (hlist)
+                       ret = hlist->getItem(0);
+       }
+       return ret;
+}
+
+bool PlayerSAO::setWieldedItem(const ItemStack &item)
+{
+       Inventory *inv = getInventory();
+       if (inv) {
+               InventoryList *mlist = inv->getList(getWieldList());
+               if (mlist) {
+                       ItemStack olditem = mlist->getItem(getWieldIndex());
+                       if (olditem.name.empty()) {
+                               InventoryList *hlist = inv->getList("hand");
+                               if (hlist) {
+                                       hlist->changeItem(0, item);
+                                       return true;
+                               }
+                       }
+                       mlist->changeItem(getWieldIndex(), item);
+                       return true;
+               }
+       }
+       return false;
+}
+
 int PlayerSAO::getWieldIndex() const
 {
        return m_wield_index;
index f58c7dadbf7447ca35c0475723e8a48cec663e86..c5b066f505249fffa99d48cb752632b1d9e45bf8 100644 (file)
@@ -251,6 +251,8 @@ public:
        const Inventory* getInventory() const;
        InventoryLocation getInventoryLocation() const;
        std::string getWieldList() const;
+       ItemStack getWieldedItem() const;
+       bool setWieldedItem(const ItemStack &item);
        int getWieldIndex() const;
        void setWieldIndex(int i);
 
index 6716823482a3e51e43ad29980709f274895eff4d..e6d38d0a08e938e377f98c379a33c254ac7ad135 100644 (file)
@@ -3684,6 +3684,12 @@ void Game::updateCamera(VolatileRunFlags *flags, u32 busy_time,
                if (mlist && client->getPlayerItem() < mlist->getSize())
                        playeritem = mlist->getItem(client->getPlayerItem());
        }
+       if (playeritem.getDefinition(itemdef_manager).name.empty()) { // override the hand
+               InventoryList *hlist = local_inventory->getList("hand");
+               if (hlist)
+                       playeritem = hlist->getItem(0);
+       }
+
 
        ToolCapabilities playeritem_toolcap =
                playeritem.getToolCapabilities(itemdef_manager);
@@ -3768,6 +3774,11 @@ void Game::processPlayerInteraction(GameRunData *runData,
                        playeritem = mlist->getItem(client->getPlayerItem());
        }
 
+       if (playeritem.getDefinition(itemdef_manager).name.empty()) { // override the hand
+               InventoryList *hlist = local_inventory->getList("hand");
+               if (hlist)
+                       playeritem = hlist->getItem(0);
+       }
        const ItemDefinition &playeritem_def =
                        playeritem.getDefinition(itemdef_manager);
 
@@ -4321,8 +4332,14 @@ void Game::updateFrame(ProfilerGraph *graph, RunStats *stats,
 
                if (mlist && (client->getPlayerItem() < mlist->getSize())) {
                        ItemStack item = mlist->getItem(client->getPlayerItem());
+                       if (item.getDefinition(itemdef_manager).name.empty()) { // override the hand
+                               InventoryList *hlist = local_inventory->getList("hand");
+                               if (hlist)
+                                       item = hlist->getItem(0);
+                       }
                        camera->wield(item);
                }
+
                runData->update_wielded_item_trigger = false;
        }
 
index d56424b75f076ee558ee76515df7c9476f49ebf2..70eb0a8284135e65a6b6cbee62f2827b2563444c 100644 (file)
@@ -1529,10 +1529,7 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
                                        m_script->on_cheat(playersao, "finished_unknown_dig");
                                }
                                // Get player's wielded item
-                               ItemStack playeritem;
-                               InventoryList *mlist = playersao->getInventory()->getList("main");
-                               if (mlist != NULL)
-                                       playeritem = mlist->getItem(playersao->getWieldIndex());
+                               ItemStack playeritem = playersao->getWieldedItem();
                                ToolCapabilities playeritem_toolcap =
                                                playeritem.getToolCapabilities(m_itemdef);
                                // Get diggability and expected digging time
index 9c321d57192faba116f9516c7914b14ce5604f3d..85bc639ec17970b2f05f998795a021ed6663211e 100644 (file)
@@ -40,6 +40,7 @@ Player::Player(const char *name, IItemDefManager *idef):
 
        inventory.clear();
        inventory.addList("main", PLAYER_INVENTORY_SIZE);
+       inventory.addList("hand", 1);
        InventoryList *craft = inventory.addList("craft", 9);
        craft->setWidth(3);
        inventory.addList("craftpreview", 1);