Now SAOs will reflect changes to their temporary inventory object
authorJacobF <queatz@gmail.com>
Thu, 1 Sep 2011 21:16:55 +0000 (17:16 -0400)
committerPerttu Ahola <celeron55@gmail.com>
Tue, 6 Sep 2011 13:36:11 +0000 (16:36 +0300)
Also, the temp item wasn't being deleted, might have been a memory leak.
Now you will only eat 1 item off a stack

src/content_sao.cpp
src/inventory.cpp
src/inventory.h

index 638f50c9dbe674fae5dd125cbf8ed2c9abe4ebe2..0bb518c165149678a8747acc1b6ff96f6140aa29 100644 (file)
@@ -226,6 +226,11 @@ void ItemSAO::rightClick(Player *player)
 
        if(to_be_deleted)
                m_removed = true;
+       else
+               // Reflect changes to the item here
+               m_inventorystring = item->getItemString();
+       
+       delete item;
 }
 
 /*
index 116ceeb6df3a838fb06c06ac4a817878d4625eef..f31e19f77fb545bb9e0378347090c63c720a6e93 100644 (file)
@@ -122,16 +122,20 @@ InventoryItem* InventoryItem::deSerialize(std::istream &is)
        }
 }
 
+std::string InventoryItem::getItemString() {
+       // Get item string
+       std::ostringstream os(std::ios_base::binary);
+       serialize(os);
+       return os.str();
+}
+
 ServerActiveObject* InventoryItem::createSAO(ServerEnvironment *env, u16 id, v3f pos)
 {
        /*
                Create an ItemSAO
        */
-       // Get item string
-       std::ostringstream os(std::ios_base::binary);
-       serialize(os);
        // Create object
-       ServerActiveObject *obj = new ItemSAO(env, 0, pos, os.str());
+       ServerActiveObject *obj = new ItemSAO(env, 0, pos, getItemString());
        return obj;
 }
 
@@ -200,12 +204,17 @@ bool CraftItem::use(ServerEnvironment *env, Player *player)
 {
        if(item_craft_is_eatable(m_subname))
        {
+               u16 result_count = getCount() - 1; // Eat one at a time
                s16 hp_change = item_craft_eat_hp_change(m_subname);
                if(player->hp + hp_change > 20)
                        player->hp = 20;
                else
                        player->hp += hp_change;
-               return true;
+               
+               if(result_count < 1)
+                       return true;
+               else
+                       setCount(result_count);
        }
        return false;
 }
index b19a365c8ee1b71266835eaca6d0dab3bc7a5d1d..44ba6a5adf7cc3e5ebe5c56a943fc3137739a4f3 100644 (file)
@@ -58,6 +58,8 @@ public:
 #endif
        // Shall return a text to show in the GUI
        virtual std::string getText() { return ""; }
+       // Returns the string used for inventory
+       virtual std::string getItemString();
        // Creates an object from the item, to be placed in the world.
        virtual ServerActiveObject* createSAO(ServerEnvironment *env, u16 id, v3f pos);
        // Gets amount of items that dropping one SAO will decrement