SHOW:
|
|
- or go back to the newest paste.
| 1 | - | // Teleport location saver (not tested) |
| 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 "Creature.h" |
| 9 | + | |
| 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 | - | class myscriptclass2 : public CreatureScript |
| 79 | + | |
| 80 | ||
| 81 | class myscriptclass2 : public ItemScript | |
| 82 | - | myscriptclass2() : CreatureScript("myscriptclass2")
|
| 82 | + | |
| 83 | public: | |
| 84 | myscriptclass2() : ItemScript("myscriptclass2")
| |
| 85 | {
| |
| 86 | - | bool OnGossipHello(Player* player, Creature* creature) override |
| 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->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, creature->GetGUID()); |
| 92 | + | |
| 93 | player->ADD_GOSSIP_ITEM(3, "Delete Position", 1, 3); | |
| 94 | player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, item->GetGUID()); | |
| 95 | - | bool OnGossipSelect(Player* player, Creature* creature, uint32 sender, uint32 action) override |
| 95 | + | |
| 96 | } | |
| 97 | ||
| 98 | void OnGossipSelect(Player* player, Item* item, uint32 sender, uint32 action) override | |
| 99 | {
| |
| 100 | player->PlayerTalkClass->ClearMenus(); | |
| 101 | if (sender == 1) | |
| 102 | - | auto& teles = playerLocations[player->GetGUID()]; |
| 102 | + | |
| 103 | auto& teles = playerLocations[player->GetGUID()]; | |
| 104 | if (teles.empty()) | |
| 105 | {
| |
| 106 | player->GetSession()->SendNotification("You have not saved any locations");
| |
| 107 | - | player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, creature->GetGUID()); |
| 107 | + | OnUse(player, item, SpellCastTargets()); |
| 108 | - | return true; |
| 108 | + | return; |
| 109 | } | |
| 110 | if (action == 2) | |
| 111 | {
| |
| 112 | - | auto& teles = playerLocations[player->GetGUID()]; |
| 112 | + | |
| 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, creature->GetGUID()); |
| 117 | + | player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, item->GetGUID()); |
| 118 | - | return true; |
| 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 | - | OnGossipSelect(player, creature, 1, sender); |
| 127 | + | player->SEND_GOSSIP_MENU(DEFAULT_GOSSIP_MESSAGE, item->GetGUID()); |
| 128 | - | return true; |
| 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 | - | OnGossipSelect(player, creature, 1, sender); |
| 139 | + | |
| 140 | - | return true; |
| 140 | + | |
| 141 | {
| |
| 142 | auto& teles = playerLocations[player->GetGUID()]; | |
| 143 | - | OnGossipHello(player, creature); |
| 143 | + | |
| 144 | if (itr != teles.end()) | |
| 145 | {
| |
| 146 | teles.erase(itr); | |
| 147 | - | bool OnGossipSelectCode(Player* player, Creature* creature, uint32 sender, uint32 action, const char* code) override |
| 147 | + | |
| 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 | - | playerLocations[player->GetGUID()][id] = data; |
| 166 | + | |
| 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 | - | OnGossipHello(player, creature); |
| 176 | + | |
| 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 | } |