Advertisement
Rochet2

Add item if space

Aug 9th, 2012
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #define ITEMID 25
  2. #define AMOUNT 5
  3. #define ITEM_MYSTERY_TICKET 9999
  4.  
  5. class Test_NPC: public CreatureScript
  6. {
  7. public:
  8.     Test_NPC() : CreatureScript("Test_NPC") { }
  9.  
  10.     bool OnGossipHello(Player * player, Creature * creature)
  11.     {
  12.         player->ADD_GOSSIP_ITEM(3, "test", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+1);
  13.         player->ADD_GOSSIP_ITEM(1, "Nevermind", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+2);
  14.         player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, creature->GetGUID());
  15.         return true;
  16.     }
  17.  
  18.     bool OnGossipSelect(Player * player, Creature * creature, uint32 sender, uint32 uiAction)
  19.     {
  20.         player->PlayerTalkClass->ClearMenus();
  21.         switch(uiAction)
  22.         {
  23.         case GOSSIP_ACTION_INFO_DEF+1:
  24.             {
  25.                 if (player->GetItemCount(ITEM_MYSTERY_TICKET) >= 2)
  26.                 {
  27.                     if(!ModifyItemAmount(player, ITEMID, AMOUNT)) // ModifyItemAmount gives the player the items if he has space. If no space, returns false.
  28.                         player->GetSession()->SendNotification("You do not have enough space!");
  29.                     else
  30.                         ModifyItemAmount(player, ITEM_MYSTERY_TICKET, -2); // ModifyItemAmount can also remove items from inventory if you provide negative amount.
  31.                 }
  32.                 else
  33.                     player->GetSession()->SendNotification("You do not have enough space!");
  34.             } break;
  35.         case GOSSIP_ACTION_INFO_DEF+2:
  36.             {
  37.                 player->CLOSE_GOSSIP_MENU();
  38.             } break;
  39.         }
  40.     }
  41.  
  42.     // Custom function:
  43.     bool ModifyItemAmount(Player * player, uint32 itemEntry, uint32 amount)
  44.     {
  45.         if (amount < 0)
  46.         {
  47.             player->DestroyItemCount(itemEntry, -amount, true, false);
  48.             return true;
  49.         }
  50.         if (amount == 0)
  51.             amount = 1;
  52.  
  53.         uint32 noSpaceForCount = 0;
  54.  
  55.         ItemPosCountVec dest;
  56.         uint8 msg = player->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, itemEntry, amount, &noSpaceForCount);
  57.  
  58.         if (msg != EQUIP_ERR_OK)
  59.             amount -= noSpaceForCount;
  60.  
  61.         if (amount == 0 || dest.empty())
  62.             return false;
  63.  
  64.         Item* item = player->StoreNewItem(dest, itemEntry, true, Item::GenerateItemRandomPropertyId(60000));
  65.         return true;
  66.     }
  67. };
  68.  
  69. void AddSC_Test_NPC()
  70. {
  71.     new Test_NPC();
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement