Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Player.h"
- #include "DatabaseWorker.h"
- #include "WorldDatabase.h"
- #include "Field.h"
- #include "QueryResult.h"
- #include "Chat.h"
- #include "RBAC.h"
- #include "Item.h"
- #include "DatabaseEnv.h"
- #include "Spell.h"
- #include "Logger.h"
- #include "ObjectMgr.h"
- #include "Log.h"
- #include "WorldSession.h"
- #include "Player.h"
- #include "ScriptedGossip.h"
- constexpr int size = 6;
- // This is the code which returns the fancy colors and the name of the item
- std::string GetItemLink(uint32 entry, WorldSession* session)
- {
- TC_LOG_DEBUG("custom.transmog", "Transmogrification::GetItemLink");
- const ItemTemplate* temp = sObjectMgr->GetItemTemplate(entry);
- LocaleConstant 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();
- }
- // This is the structure of the ItemUpgrader, if you want to add more requirements do it here
- struct ItemUgpradeStructure
- {
- uint32 itemEntry;
- std::array<uint32, size> reqItems;
- std::array<uint32, size> reqItemCounts;
- uint32 upgradedItem;
- };
- // this is the map in which we're storing all of the data related to the item upgrader
- std::unordered_map<uint32, ItemUgpradeStructure> ItemUpgradeMap;
- // this is the function which loads the item upgrade information from the database and places it into our structure and then the map
- void LoadItemUpgrader()
- {
- static_assert(decltype(ItemUgpradeStructure::reqItems)().size() == decltype(ItemUgpradeStructure::reqItemCounts)().size(), "reqItems and reqItemCounts size must be equal!");
- ItemUpgradeMap.clear();
- QueryResult result = WorldDatabase.PQuery("SELECT entry, reqItem1, reqItem2, reqItem3, reqItem4, reqItem5, reqItem6, reqItemCount1, reqItemCount2, reqItemCount3, reqItemCount4, reqItemCount5, reqItemCount6, upgradedItem FROM z_item_upgrader");
- if (result)
- do
- {
- Field* fields = result->Fetch();
- ItemUgpradeStructure its;
- its.itemEntry = fields[0].GetUInt32();
- for (int i = 0; i < std::declval<ItemUgpradeStructure>().reqItems.size(); i++)
- {
- its.reqItems[i] = fields[i + 1].GetUInt32();
- its.reqItemCounts[i] = fields[i + 7].GetUInt32();
- }
- // this part here places our info into the map
- ItemUpgradeMap.insert({ its.itemEntry, its });
- } while (result->NextRow());
- }
- // this class loads our item upgrade table from the databasse on startup
- class item_upgrade_loader : public WorldScript
- {
- public:
- item_upgrade_loader() : WorldScript("item_upgrade_loader") { }
- void OnStartup()
- {
- LoadItemUpgrader();
- }
- };
- // This class handles coommands for reloading the item upgrade info, use it whenever you edit soemthing in the database
- class item_upgrade_reload_command : public CommandScript
- {
- public:
- item_upgrade_reload_command() : CommandScript("item_upgrade_reload_command") { }
- std::vector<ChatCommand> GetCommands() const
- {
- static std::vector<ChatCommand> reloadCommandTable =
- {
- { "item_upgrade_system", rbac::RBAC_PERM_COMMAND_RELOAD, false, &HandleReloadItemUpgrader, "" },
- };
- static std::vector<ChatCommand> commandTable =
- {
- { "reload", rbac::RBAC_PERM_COMMAND_RELOAD, false, NULL, "", reloadCommandTable },
- };
- return commandTable;
- }
- static bool HandleReloadItemUpgrader(ChatHandler * handler, const char * args)
- {
- handler->SendSysMessage("Reloading Item Upgrade System");
- LoadItemUpgrader();
- handler->SendSysMessage("Item Upgrade System Reloaded");
- return true;
- }
- };
- // If you want to add more requirements in the future
- // You will need to edit this boolean to check if the players have what's required
- bool CanUpgrade(Player* player, uint32 actions)
- {
- for (int i = 0; i < std::declval<ItemUgpradeStructure>().reqItems.size(); i++)
- {
- if (ItemUpgradeMap[actions].reqItems[i] && !player->HasItemCount(ItemUpgradeMap[actions].reqItems[i], ItemUpgradeMap[actions].reqItemCounts[i]))
- return false;
- }
- return true;
- }
- // the code for the item upgrader itself
- class item_upgrader : public ItemScript
- {
- public:
- item_upgrader() : ItemScript("item_upgrader") {}
- bool OnUse(Player* player, Item* item, SpellCastTargets const& targets) override
- {
- uint32 spell = item->GetSpell();
- if (targets.GetItemTarget() && targets.GetItemTarget()->GetTypeId() == TYPEID_ITEM)
- {
- if (ItemUpgradeMap[targets.GetItemTarget()->GetEntry()].itemEntry)
- {
- std::ostringstream upgradeText;
- upgradeText << "Are you sure you want to upgrade\n\n" << GetItemLink(targets.GetItemTarget()->GetEntry(), player->GetSession()) << " into " << GetItemLink(ItemUpgradeMap[targets.GetItemTarget()->GetEntry()].upgradedItem, player->GetSession());
- upgradeText << "\n\nIn order to upgrade your item you will need:\n\n";
- for (int i = 0; i < std::declval<ItemUgpradeStructure>().reqItems.size(); i++)
- {
- if (sObjectMgr->GetItemTemplate(ItemUpgradeMap[targets.GetItemTarget()->GetEntry()].reqItems[i]))
- upgradeText << GetItemLink(ItemUpgradeMap[targets.GetItemTarget()->GetEntry()].reqItems[i], player->GetSession()) << " x " << std::to_string(player->GetItemCount(ItemUpgradeMap[targets.GetItemTarget()->GetEntry()].reqItemCounts[i])) << " / " << std::to_string(ItemUpgradeMap[targets.GetItemTarget()->GetEntry()].reqItemCounts[i]) << "\n";
- }
- player->PlayerTalkClass->ClearMenus();
- AddGossipItemFor(player, -1, "", 1, targets.GetItemTarget()->GetEntry(), upgradeText.str().c_str(), 0, false);
- SendGossipMenuFor(player, 1, item->GetGUID());
- }
- else
- ChatHandler(player->GetSession()).PSendSysMessage("|cff46def2[SYSTEM]: |r|cff5994f2This item is not upgradable.|r");
- }
- else
- player->SendEquipError(EQUIP_ERR_CANT_DO_RIGHT_NOW, item, NULL);
- player->RemoveAura(spell);
- return true;
- }
- // This is the part which actually removes the required items and gives the player the upgraded item
- void OnGossipSelect(Player * player, Item * item, uint32 sender, uint32 actions)
- {
- if (!CanUpgrade(player, actions))
- {
- ChatHandler(player->GetSession()).PSendSysMessage("You do not have the required materials needed to upgrade this item.");
- return;
- }
- for (int i = 0; i < std::declval<ItemUgpradeStructure>().reqItems.size(); i++)
- {
- if (ItemUpgradeMap[actions].reqItems[i])
- player->DestroyItemCount(ItemUpgradeMap[actions].reqItems[i], ItemUpgradeMap[actions].reqItemCounts[i], true);
- }
- player->AddItem(ItemUpgradeMap[actions].upgradedItem, 1);
- player->DestroyItemCount(actions, 1, true);
- }
- };
- // Add to script loader
- void AddSC_item_upgrade_system()
- {
- new item_upgrade_loader();
- new item_upgrade_reload_command();
- new item_upgrader();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement