Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Teleport location saver (not tested)
- // By Rochet2
- #include "GossipDef.h"
- #include "ScriptedGossip.h"
- #include "Common.h"
- #include "Player.h"
- #include "Creature.h"
- #include "ScriptMgr.h"
- #include "Object.h"
- #include "WorldSession.h"
- #include "DatabaseEnv.h"
- struct Data
- {
- std::string name;
- WorldLocation loc;
- };
- static const size_t maxteles = 5;
- static std::unordered_map<ObjectGuid, std::map<uint32, Data> > playerLocations;
- class myscriptclass0 : public WorldScript
- {
- public:
- myscriptclass0() : WorldScript("myscriptclass0")
- {
- }
- void OnStartup() override
- {
- CharacterDatabase.DirectExecute("CREATE TABLE IF NOT EXISTS `player_teleports` ("
- " `guid` INT UNSIGNED NOT NULL,"
- " `id` INT UNSIGNED NOT NULL,"
- " `name` TEXT NULL,"
- " `map` INT UNSIGNED NOT NULL,"
- " `x` FLOAT NOT NULL,"
- " `y` FLOAT NOT NULL,"
- " `z` FLOAT NOT NULL,"
- " `o` FLOAT NOT NULL,"
- " PRIMARY KEY(`guid`, `id`)"
- " )"
- " COLLATE = 'utf8_general_ci'"
- " ENGINE = InnoDB"
- " ;");
- }
- };
- class myscriptclass1 : public PlayerScript
- {
- public:
- myscriptclass1() : PlayerScript("myscriptclass1")
- {
- }
- void OnLogin(Player* player, bool /*loginFirst*/) override
- {
- QueryResult res = CharacterDatabase.PQuery("SELECT `id`, `name`, `map`, `x`, `y`, `z`, `o` FROM player_teleports WHERE `guid` = %u", player->GetGUID().GetCounter());
- if (!res)
- return;
- auto& teles = playerLocations[player->GetGUID()];
- teles.clear();
- do
- {
- auto fields = res->Fetch();
- auto& t = teles[fields[0].GetUInt32()];
- t.name = fields[1].GetString();
- t.loc.WorldRelocate(fields[2].GetUInt32(), fields[3].GetFloat(), fields[4].GetFloat(), fields[5].GetFloat(), fields[6].GetFloat());
- } while (res->NextRow());
- }
- void OnLogout(Player* player) override
- {
- playerLocations.erase(player->GetGUID());
- }
- };
- class myscriptclass2 : public CreatureScript
- {
- public:
- myscriptclass2() : CreatureScript("myscriptclass2")
- {
- }
- bool OnGossipHello(Player* player, Creature* creature) override
- {
- player->PlayerTalkClass->ClearMenus();
- player->ADD_GOSSIP_ITEM_EXTENDED(4, "Save Position", 1, 1, "Insert teleport name", 0, true);
- player->ADD_GOSSIP_ITEM(3, "Tele Position", 1, 2);
- player->ADD_GOSSIP_ITEM(3, "Delete Position", 1, 3);
- player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, creature->GetGUID());
- }
- bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action) override
- {
- player->PlayerTalkClass->ClearMenus();
- if (sender == 1)
- {
- if (action == 2)
- {
- auto& teles = playerLocations[player->GetGUID()];
- for (auto& tele : teles)
- {
- player->ADD_GOSSIP_ITEM(2, "Teleport to " + tele.second.name, action, tele.first);
- }
- player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, creature->GetGUID());
- return true;
- }
- else if (action == 3)
- {
- auto& teles = playerLocations[player->GetGUID()];
- for (auto& tele : teles)
- {
- player->ADD_GOSSIP_ITEM(4, "Delete " + tele.second.name, action, tele.first);
- }
- player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, creature->GetGUID());
- return true;
- }
- }
- else if (sender == 2)
- {
- auto& teles = playerLocations[player->GetGUID()];
- auto itr = teles.find(action);
- if (itr != teles.end())
- player->TeleportTo(itr->second.loc);
- OnGossipSelect(player, creature, 1, sender);
- return true;
- }
- else if (sender == 3)
- {
- auto& teles = playerLocations[player->GetGUID()];
- auto itr = teles.find(action);
- if (itr != teles.end())
- {
- teles.erase(itr);
- CharacterDatabase.PExecute("DELETE FROM player_teleports WHERE `guid` = %u and `id` = %u", player->GetGUID().GetCounter(), action);
- }
- OnGossipSelect(player, creature, 1, sender);
- return true;
- }
- OnGossipHello(player, creature);
- return true;
- }
- bool OnGossipSelectCode(Player* player, Creature* creature, uint32 sender, uint32 action, const char* code) override
- {
- player->PlayerTalkClass->ClearMenus();
- if (sender == 1 && action == 1)
- {
- auto& teles = playerLocations[player->GetGUID()];
- if (teles.size() < maxteles)
- {
- std::string escaped(code);
- CharacterDatabase.EscapeString(escaped);
- uint32 id = 0;
- for (auto& tele : teles)
- {
- if (tele.first > id)
- break;
- ++id;
- }
- Data data = { code, player->GetWorldLocation() };
- playerLocations[player->GetGUID()][id] = data;
- CharacterDatabase.PExecute("INSERT INTO player_teleports (`guid`, `id`, `name`, `map`, `x`, `y`, `z`, `o`) VALUES (%u, %u, \'%s\', %u, %f, %f, %f, %f)",
- player->GetGUID().GetCounter(), id, escaped.c_str(), data.loc.GetMapId(), data.loc.GetPositionX(), data.loc.GetPositionY(), data.loc.GetPositionZ(), data.loc.GetOrientation());
- player->GetSession()->SendAreaTriggerMessage("Position saved");
- }
- else
- {
- player->GetSession()->SendNotification("You have to delete a teleport to add a new one");
- }
- }
- OnGossipHello(player, creature);
- return true;
- }
- };
- void AddSC_myscript()
- {
- new myscriptclass0();
- new myscriptclass1();
- new myscriptclass2();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement