Advertisement
Rochet2

Teleport location saver - item

Nov 21st, 2015
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Teleport location saver
  2. // By Rochet2
  3. // requires http://rochet2.github.io/Player%20and%20Item%20Gossip.html ( item gossip )
  4.  
  5.  
  6. #include "GossipDef.h"
  7. #include "ScriptedGossip.h"
  8. #include "Common.h"
  9. #include "Player.h"
  10. #include "Item.h"
  11. #include "ScriptMgr.h"
  12. #include "Object.h"
  13. #include "WorldSession.h"
  14. #include "DatabaseEnv.h"
  15. #include "Spell.h"
  16.  
  17. struct Data
  18. {
  19.     std::string name;
  20.     WorldLocation loc;
  21. };
  22.  
  23. static const size_t maxteles = 5;
  24. static std::unordered_map<ObjectGuid, std::map<uint32, Data> > playerLocations;
  25.  
  26. class myscriptclass0 : public WorldScript
  27. {
  28. public:
  29.     myscriptclass0() : WorldScript("myscriptclass0")
  30.     {
  31.     }
  32.  
  33.     void OnStartup() override
  34.     {
  35.         CharacterDatabase.DirectExecute("CREATE TABLE IF NOT EXISTS `player_teleports` ("
  36.             " `guid` INT UNSIGNED NOT NULL,"
  37.             " `id` INT UNSIGNED NOT NULL,"
  38.             " `name` TEXT NULL,"
  39.             " `map` INT UNSIGNED NOT NULL,"
  40.             " `x` FLOAT NOT NULL,"
  41.             " `y` FLOAT NOT NULL,"
  42.             " `z` FLOAT NOT NULL,"
  43.             " `o` FLOAT NOT NULL,"
  44.             " PRIMARY KEY(`guid`, `id`)"
  45.             " )"
  46.             " COLLATE = 'utf8_general_ci'"
  47.             " ENGINE = InnoDB"
  48.             " ;");
  49.     }
  50. };
  51.  
  52. class myscriptclass1 : public PlayerScript
  53. {
  54. public:
  55.     myscriptclass1() : PlayerScript("myscriptclass1")
  56.     {
  57.     }
  58.  
  59.     void OnLogin(Player* player, bool /*loginFirst*/) override
  60.     {
  61.         QueryResult res = CharacterDatabase.PQuery("SELECT `id`, `name`, `map`, `x`, `y`, `z`, `o` FROM player_teleports WHERE `guid` = %u", player->GetGUID().GetCounter());
  62.         if (!res)
  63.             return;
  64.         auto& teles = playerLocations[player->GetGUID()];
  65.         teles.clear();
  66.         do
  67.         {
  68.             auto fields = res->Fetch();
  69.             auto& t = teles[fields[0].GetUInt32()];
  70.             t.name = fields[1].GetString();
  71.             t.loc.WorldRelocate(fields[2].GetUInt32(), fields[3].GetFloat(), fields[4].GetFloat(), fields[5].GetFloat(), fields[6].GetFloat());
  72.         } while (res->NextRow());
  73.     }
  74.  
  75.     void OnLogout(Player* player) override
  76.     {
  77.         playerLocations.erase(player->GetGUID());
  78.     }
  79. };
  80.  
  81. class myscriptclass2 : public ItemScript
  82. {
  83. public:
  84.     myscriptclass2() : ItemScript("myscriptclass2")
  85.     {
  86.     }
  87.  
  88.     bool OnUse(Player* player, Item* item, SpellCastTargets const& targets) override
  89.     {
  90.         player->PlayerTalkClass->ClearMenus();
  91.         player->ADD_GOSSIP_ITEM_EXTENDED(4, "Save Position", 1, 1, "Insert teleport name", 0, true);
  92.         player->ADD_GOSSIP_ITEM(3, "Tele Position", 1, 2);
  93.         player->ADD_GOSSIP_ITEM(3, "Delete Position", 1, 3);
  94.         player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, item->GetGUID());
  95.         return true;
  96.     }
  97.  
  98.     void OnGossipSelect(Player* player, Item* item, uint32 sender, uint32 action) override
  99.     {
  100.         player->PlayerTalkClass->ClearMenus();
  101.         if (sender == 1)
  102.         {
  103.             auto& teles = playerLocations[player->GetGUID()];
  104.             if (teles.empty())
  105.             {
  106.                 player->GetSession()->SendNotification("You have not saved any locations");
  107.                 OnUse(player, item, SpellCastTargets());
  108.                 return;
  109.             }
  110.             if (action == 2)
  111.             {
  112.                 for (auto& tele : teles)
  113.                 {
  114.                     player->ADD_GOSSIP_ITEM(2, "Teleport to " + tele.second.name, action, tele.first);
  115.                 }
  116.                 player->ADD_GOSSIP_ITEM(7, "Back..", 999, 1);
  117.                 player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, item->GetGUID());
  118.                 return;
  119.             }
  120.             else if (action == 3)
  121.             {
  122.                 for (auto& tele : teles)
  123.                 {
  124.                     player->ADD_GOSSIP_ITEM(4, "Delete " + tele.second.name, action, tele.first);
  125.                 }
  126.                 player->ADD_GOSSIP_ITEM(7, "Back..", 999, 1);
  127.                 player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, item->GetGUID());
  128.                 return;
  129.             }
  130.         }
  131.         else if (sender == 2)
  132.         {
  133.             auto& teles = playerLocations[player->GetGUID()];
  134.             auto itr = teles.find(action);
  135.             if (itr != teles.end())
  136.                 player->TeleportTo(itr->second.loc);
  137.             OnGossipSelect(player, item, 1, sender);
  138.             return;
  139.         }
  140.         else if (sender == 3)
  141.         {
  142.             auto& teles = playerLocations[player->GetGUID()];
  143.             auto itr = teles.find(action);
  144.             if (itr != teles.end())
  145.             {
  146.                 teles.erase(itr);
  147.                 CharacterDatabase.PExecute("DELETE FROM player_teleports WHERE `guid` = %u and `id` = %u", player->GetGUID().GetCounter(), action);
  148.             }
  149.             OnGossipSelect(player, item, 1, sender);
  150.             return;
  151.         }
  152.  
  153.         OnUse(player, item, SpellCastTargets());
  154.     }
  155.  
  156.     void OnGossipSelectCode(Player* player, Item* item, uint32 sender, uint32 action, const char* code) override
  157.     {
  158.         player->PlayerTalkClass->ClearMenus();
  159.         if (sender == 1 && action == 1)
  160.         {
  161.             auto& teles = playerLocations[player->GetGUID()];
  162.             if (teles.size() < maxteles)
  163.             {
  164.                 std::string escaped(code);
  165.                 CharacterDatabase.EscapeString(escaped);
  166.                 uint32 id = 0;
  167.                 for (auto& tele : teles)
  168.                 {
  169.                     if (tele.first > id)
  170.                         break;
  171.                     ++id;
  172.                 }
  173.  
  174.                 Data data = { code, player->GetWorldLocation() };
  175.                 teles[id] = data;
  176.                 CharacterDatabase.PExecute("INSERT INTO player_teleports (`guid`, `id`, `name`, `map`, `x`, `y`, `z`, `o`) VALUES (%u, %u, \'%s\', %u, %f, %f, %f, %f)",
  177.                     player->GetGUID().GetCounter(), id, escaped.c_str(), data.loc.GetMapId(), data.loc.GetPositionX(), data.loc.GetPositionY(), data.loc.GetPositionZ(), data.loc.GetOrientation());
  178.                 player->GetSession()->SendAreaTriggerMessage("Position saved");
  179.             }
  180.             else
  181.             {
  182.                 player->GetSession()->SendNotification("You have to delete a teleport to add a new one");
  183.             }
  184.         }
  185.         OnUse(player, item, SpellCastTargets());
  186.     }
  187. };
  188.  
  189. void AddSC_myscript()
  190. {
  191.     new myscriptclass0();
  192.     new myscriptclass1();
  193.     new myscriptclass2();
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement