Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ScriptMgr.h"
- #include "ScriptedCreature.h"
- #include "ScriptedGossip.h"
- #include "GameEventMgr.h"
- #include "Player.h"
- #include "WorldSession.h"
- class NPC_Crafter : public CreatureScript
- {
- public:
- NPC_Crafter() : CreatureScript("NPC_Crafter")
- {
- }
- //Protection against SQL Injection
- bool string_is_valid(const std::string &str)
- {
- return find_if(str.begin(), str.end(),
- [](char c) { return !(isalnum(c) || (c == ' ')); }) == str.end();
- }
- void Zapros(Player* player, Creature* creature, const char* code) {
- if (!string_is_valid(code))
- {
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- std::string code2 = code;
- QueryResult result = WorldDatabase.PQuery("SELECT `name`, `item`, `count`, `r_item1`, `r_count1`, `r_item2`, `r_count2`, `r_item3`, `r_count3`, `r_item4`, `r_count4`, `r_item5`, `r_count5` FROM `craft_system` WHERE `name` = '%s'", code);
- if (!result)
- {
- creature->Whisper("Recipe named '" + code2 + "' not found.", LANG_UNIVERSAL, player, false);
- //TODO: UPDATE GOSSIP MENU INSTEAD CLOSE
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- Field * fields = NULL;
- fields = result->Fetch();
- std::string name = fields[0].GetString();
- uint32 item = fields[1].GetUInt32();
- uint32 count = fields[2].GetUInt32();
- uint32 r_item1 = fields[3].GetUInt32();
- uint32 r_count1 = fields[4].GetUInt32();
- uint32 r_item2 = fields[5].GetUInt32();
- uint32 r_count2 = fields[6].GetUInt32();
- uint32 r_item3 = fields[7].GetInt32();
- uint32 r_count3 = fields[8].GetInt32();
- uint32 r_item4 = fields[9].GetInt32();
- uint32 r_count4 = fields[10].GetInt32();
- uint32 r_item5 = fields[11].GetInt32();
- uint32 r_count5 = fields[12].GetInt32();
- if (item == 0 || count == 0 || r_item1 == 0 || r_count1 == 0) // empty row
- {
- creature->Whisper("Error! Row '" + name + "' incorrectly filled.", LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- ItemPosCountVec dest;
- uint32 no_space = 0;
- InventoryResult msg = player->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, item, count, &no_space);
- if (msg != EQUIP_ERR_OK)
- {
- creature->Whisper("Inventory is full!", LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (player->HasItemCount(r_item1, r_count1) &&
- player->HasItemCount(r_item2, r_count2) &&
- player->HasItemCount(r_item3, r_count3) &&
- player->HasItemCount(r_item4, r_count4) &&
- player->HasItemCount(r_item5, r_count5))
- {
- player->AddItem(item, count);
- player->DestroyItemCount(r_item1, r_count1, true);
- player->DestroyItemCount(r_item2, r_count2, true);
- player->DestroyItemCount(r_item3, r_count3, true);
- player->DestroyItemCount(r_item4, r_count4, true);
- player->DestroyItemCount(r_item5, r_count5, true);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (player->HasItemCount(r_item1, r_count1) &&
- player->HasItemCount(r_item2, r_count2) &&
- player->HasItemCount(r_item3, r_count3) &&
- player->HasItemCount(r_item4, r_count4) &&
- r_item5 == 0)
- {
- player->AddItem(item, count);
- player->DestroyItemCount(r_item1, r_count1, true);
- player->DestroyItemCount(r_item2, r_count2, true);
- player->DestroyItemCount(r_item3, r_count3, true);
- player->DestroyItemCount(r_item4, r_count4, true);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (player->HasItemCount(r_item1, r_count1) &&
- player->HasItemCount(r_item2, r_count2) &&
- player->HasItemCount(r_item3, r_count3) &&
- r_item4 == 0)
- {
- player->AddItem(item, count);
- player->DestroyItemCount(r_item1, r_count1, true);
- player->DestroyItemCount(r_item2, r_count2, true);
- player->DestroyItemCount(r_item3, r_count3, true);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (player->HasItemCount(r_item1, r_count1) &&
- player->HasItemCount(r_item2, r_count2) &&
- r_item3 == 0)
- {
- player->AddItem(item, count);
- player->DestroyItemCount(r_item1, r_count1, true);
- player->DestroyItemCount(r_item2, r_count2, true);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (player->HasItemCount(r_item1, r_count1) &&
- r_item2 == 0)
- {
- player->AddItem(item, count);
- player->DestroyItemCount(r_item1, r_count1, true);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- else
- {
- std::ostringstream ss;
- if (r_item1 != 0 && r_item2 != 0 && r_item3 != 0 && r_item4 !=0 && r_item5 !=0) //5 items
- {
- ss << "To create " << GetItemLink(item, player->GetSession()) << "x" << count << " (" << name << ") required:\n" <<
- GetItemLink(r_item1, player->GetSession()) << "x" << r_count1 << "\n" <<
- GetItemLink(r_item2, player->GetSession()) << "x" << r_count2 << "\n" <<
- GetItemLink(r_item3, player->GetSession()) << "x" << r_count3 << "\n" <<
- GetItemLink(r_item4, player->GetSession()) << "x" << r_count4 << "\n" <<
- GetItemLink(r_item5, player->GetSession()) << "x" << r_count5;
- creature->Whisper(ss.str().c_str(), LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (r_item1 != 0 && r_item2 != 0 && r_item3 != 0 && r_item4 != 0 && r_item5 == 0) //4 items
- {
- ss << "To create " << GetItemLink(item, player->GetSession()) << "x" << count << " (" << name << ") required:\n" <<
- GetItemLink(r_item1, player->GetSession()) << "x" << r_count1 << "\n" <<
- GetItemLink(r_item2, player->GetSession()) << "x" << r_count2 << "\n" <<
- GetItemLink(r_item3, player->GetSession()) << "x" << r_count3 << "\n" <<
- GetItemLink(r_item4, player->GetSession()) << "x" << r_count4;
- creature->Whisper(ss.str().c_str(), LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (r_item1 != 0 && r_item2 != 0 && r_item3 != 0 && r_item4 == 0) //3 items
- {
- ss << "To create " << GetItemLink(item, player->GetSession()) << "x" << count << " (" << name << ") required:\n" <<
- GetItemLink(r_item1, player->GetSession()) << "x" << r_count1 << "\n" <<
- GetItemLink(r_item2, player->GetSession()) << "x" << r_count2 << "\n" <<
- GetItemLink(r_item3, player->GetSession()) << "x" << r_count3;
- creature->Whisper(ss.str().c_str(), LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (r_item1 != 0 && r_item2 != 0 && r_item3 == 0) //2 items
- {
- ss << "To create " << GetItemLink(item, player->GetSession()) << "x" << count << " (" << name << ") required:\n" <<
- GetItemLink(r_item1, player->GetSession()) << "x" << r_count1 << "\n" <<
- GetItemLink(r_item2, player->GetSession()) << "x" << r_count2;
- creature->Whisper(ss.str().c_str(), LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (r_item1 != 0 && r_item2 == 0) //1 item
- {
- ss << "To create " << GetItemLink(item, player->GetSession()) << "x" << count << " (" << name << ") required:\n" <<
- GetItemLink(r_item1, player->GetSession()) << "x" << r_count1;
- creature->Whisper(ss.str().c_str(), LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- return;
- }
- return;
- }
- void Info(Player* player, Creature* creature, const char* code) {
- if (!string_is_valid(code))
- {
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- std::string code2 = code;
- QueryResult result = WorldDatabase.PQuery("SELECT `name`, `item`, `count`, `r_item1`, `r_count1`, `r_item2`, `r_count2`, `r_item3`, `r_count3`, `r_item4`, `r_count4`, `r_item5`, `r_count5` FROM `craft_system` WHERE `name` = '%s'", code);
- if (!result)
- {
- creature->Whisper("Recipe named '" + code2 + "' not found.", LANG_UNIVERSAL, player, false);
- //TODO: UPDATE GOSSIP MENU INSTEAD CLOSE
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- Field * fields = NULL;
- fields = result->Fetch();
- std::string name = fields[0].GetString();
- uint32 item = fields[1].GetUInt32();
- uint32 count = fields[2].GetUInt32();
- uint32 r_item1 = fields[3].GetUInt32();
- uint32 r_count1 = fields[4].GetUInt32();
- uint32 r_item2 = fields[5].GetUInt32();
- uint32 r_count2 = fields[6].GetUInt32();
- uint32 r_item3 = fields[7].GetInt32();
- uint32 r_count3 = fields[8].GetInt32();
- uint32 r_item4 = fields[9].GetInt32();
- uint32 r_count4 = fields[10].GetInt32();
- uint32 r_item5 = fields[11].GetInt32();
- uint32 r_count5 = fields[12].GetInt32();
- if (item == 0 || count == 0 || r_item1 == 0 || r_count1 == 0) // empty row
- {
- creature->Whisper("Error! Row '" + name + "' incorrectly filled.", LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- std::ostringstream ss;
- if (r_item1 != 0 && r_item2 != 0 && r_item3 != 0 && r_item4 != 0 && r_item5 != 0) //5 items
- {
- ss << "To create " << GetItemLink(item, player->GetSession()) << "x" << count << " (" << name << ") required:\n" <<
- GetItemLink(r_item1, player->GetSession()) << "x" << r_count1 << "\n" <<
- GetItemLink(r_item2, player->GetSession()) << "x" << r_count2 << "\n" <<
- GetItemLink(r_item3, player->GetSession()) << "x" << r_count3 << "\n" <<
- GetItemLink(r_item4, player->GetSession()) << "x" << r_count4 << "\n" <<
- GetItemLink(r_item5, player->GetSession()) << "x" << r_count5;
- creature->Whisper(ss.str().c_str(), LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (r_item1 != 0 && r_item2 != 0 && r_item3 != 0 && r_item4 != 0 && r_item5 == 0) //4 items
- {
- ss << "To create " << GetItemLink(item, player->GetSession()) << "x" << count << " (" << name << ") required:\n" <<
- GetItemLink(r_item1, player->GetSession()) << "x" << r_count1 << "\n" <<
- GetItemLink(r_item2, player->GetSession()) << "x" << r_count2 << "\n" <<
- GetItemLink(r_item3, player->GetSession()) << "x" << r_count3 << "\n" <<
- GetItemLink(r_item4, player->GetSession()) << "x" << r_count4;
- creature->Whisper(ss.str().c_str(), LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (r_item1 != 0 && r_item2 != 0 && r_item3 != 0 && r_item4 == 0) //3 items
- {
- ss << "To create " << GetItemLink(item, player->GetSession()) << "x" << count << " (" << name << ") required:\n" <<
- GetItemLink(r_item1, player->GetSession()) << "x" << r_count1 << "\n" <<
- GetItemLink(r_item2, player->GetSession()) << "x" << r_count2 << "\n" <<
- GetItemLink(r_item3, player->GetSession()) << "x" << r_count3;
- creature->Whisper(ss.str().c_str(), LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (r_item1 != 0 && r_item2 != 0 && r_item3 == 0) //2 items
- {
- ss << "To create " << GetItemLink(item, player->GetSession()) << "x" << count << " (" << name << ") required:\n" <<
- GetItemLink(r_item1, player->GetSession()) << "x" << r_count1 << "\n" <<
- GetItemLink(r_item2, player->GetSession()) << "x" << r_count2;
- creature->Whisper(ss.str().c_str(), LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- if (r_item1 != 0 && r_item2 == 0) //1 item
- {
- ss << "To create " << GetItemLink(item, player->GetSession()) << "x" << count << " (" << name << ") required:\n" <<
- GetItemLink(r_item1, player->GetSession()) << "x" << r_count1;
- creature->Whisper(ss.str().c_str(), LANG_UNIVERSAL, player, false);
- player->PlayerTalkClass->SendCloseGossip();
- return;
- }
- return;
- }
- //TODO: Fix if item doesn't exists
- std::string NPC_Crafter::GetItemLink(uint32 entry, WorldSession* session) const
- {
- const ItemTemplate* temp = sObjectMgr->GetItemTemplate(entry);
- int loc_idx = session->GetSessionDbLocaleIndex();
- std::string name = temp->Name1;
- if (ItemLocale const* il = sObjectMgr->GetItemLocale(entry))
- ObjectMgr::GetLocaleString(il->Name, loc_idx, name);
- std::ostringstream oss;
- oss << "|c" << std::hex << ItemQualityColors[temp->Quality] << std::dec <<
- "|Hitem:" << entry << ":0:0:0:0:0:0:0:0:0|h[" << name << "]|h|r";
- return oss.str();
- }
- bool OnGossipHello(Player* player, Creature* creature)
- {
- player->ADD_GOSSIP_ITEM_EXTENDED(GOSSIP_ICON_TRAINER, "Create", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 1, "Input name of recipe", 0, 1);
- player->ADD_GOSSIP_ITEM_EXTENDED(GOSSIP_ICON_CHAT, "Required reagents", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 2, "Input name of recipe", 0, 1);
- //player->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, "TEST", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 3);
- player->ADD_GOSSIP_ITEM(GOSSIP_ICON_TALK, "Выйти", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 4);
- player->PlayerTalkClass->SendGossipMenu(1, creature->GetGUID());
- return true;
- }
- bool OnGossipSelectCode(Player* player, Creature* creature, uint32 sender, uint32 action, const char* code)
- {
- player->PlayerTalkClass->ClearMenus();
- switch (action)
- {
- case GOSSIP_ACTION_INFO_DEF + 1:
- Zapros(player, creature, code);
- break;
- case GOSSIP_ACTION_INFO_DEF + 2:
- Info(player, creature, code);
- break;
- }
- return true;
- }
- bool OnGossipSelect(Player *player, Creature * creature, uint32 sender, uint32 action)
- {
- player->PlayerTalkClass->ClearMenus();
- switch (action)
- {
- case GOSSIP_ACTION_INFO_DEF + 3:
- //NEED FIX FOR THIS
- //creature->Whisper(GetItemLink(0, player->GetSession()), LANG_UNIVERSAL, player, false);
- break;
- case GOSSIP_ACTION_INFO_DEF + 4:
- player->PlayerTalkClass->SendCloseGossip();
- break;
- }
- return true;
- }
- };
- void AddSC_NPC_Crafter()
- {
- new NPC_Crafter();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement