AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
pet_commandscript Class Reference
Inheritance diagram for pet_commandscript:
CommandScript ScriptObject

Public Member Functions

 pet_commandscript ()
 
ChatCommandTable GetCommands () const override
 
- Public Member Functions inherited from ScriptObject
virtual bool IsDatabaseBound () const
 
virtual bool isAfterLoadScript () const
 
virtual void checkValidity ()
 
const std::string & GetName () const
 
uint16 GetTotalAvailableHooks ()
 

Static Public Member Functions

static bool HandlePetCreateCommand (ChatHandler *handler)
 
static bool HandlePetLearnCommand (ChatHandler *handler, SpellInfo const *spell)
 
static bool HandlePetDeleteCommand (ChatHandler *handler, PlayerIdentifier owner, uint32 petNumber)
 
static bool HandlePetListCommand (ChatHandler *handler, PlayerIdentifier owner)
 
static bool HandlePetRenameCommand (ChatHandler *handler, PlayerIdentifier owner, uint32 petNumber, std::string newName)
 
static bool HandlePetUnlearnCommand (ChatHandler *handler, SpellInfo const *spell)
 

Additional Inherited Members

- Protected Member Functions inherited from CommandScript
 CommandScript (const char *name)
 
- Protected Member Functions inherited from ScriptObject
 ScriptObject (const char *name, uint16 totalAvailableHooks=0)
 
virtual ~ScriptObject ()=default
 

Detailed Description

Constructor & Destructor Documentation

◆ pet_commandscript()

pet_commandscript::pet_commandscript ( )
inline
38: CommandScript("pet_commandscript") { }
Definition CommandScript.h:25

Member Function Documentation

◆ GetCommands()

ChatCommandTable pet_commandscript::GetCommands ( ) const
inlineoverridevirtual

Implements CommandScript.

41 {
42 static ChatCommandTable petCommandTable =
43 {
50 };
51
52 static ChatCommandTable commandTable =
53 {
54 { "pet", petCommandTable }
55 };
56
57 return commandTable;
58 }
static bool HandlePetRenameCommand(ChatHandler *handler, PlayerIdentifier owner, uint32 petNumber, std::string newName)
Definition cs_pet.cpp:233
static bool HandlePetLearnCommand(ChatHandler *handler, SpellInfo const *spell)
Definition cs_pet.cpp:94
static bool HandlePetDeleteCommand(ChatHandler *handler, PlayerIdentifier owner, uint32 petNumber)
Definition cs_pet.cpp:136
static bool HandlePetCreateCommand(ChatHandler *handler)
Definition cs_pet.cpp:60
static bool HandlePetUnlearnCommand(ChatHandler *handler, SpellInfo const *spell)
Definition cs_pet.cpp:306
static bool HandlePetListCommand(ChatHandler *handler, PlayerIdentifier owner)
Definition cs_pet.cpp:195
std::vector< ChatCommandBuilder > ChatCommandTable
Definition ChatCommand.h:46
@ RBAC_PERM_COMMAND_PET_DELETE
Definition RBAC.h:679
@ RBAC_PERM_COMMAND_PET_LEARN
Definition RBAC.h:312
@ RBAC_PERM_COMMAND_PET_CREATE
Definition RBAC.h:311
@ RBAC_PERM_COMMAND_PET_RENAME
Definition RBAC.h:686
@ RBAC_PERM_COMMAND_PET_LIST
Definition RBAC.h:678
@ RBAC_PERM_COMMAND_PET_UNLEARN
Definition RBAC.h:313

References HandlePetCreateCommand(), HandlePetDeleteCommand(), HandlePetLearnCommand(), HandlePetListCommand(), HandlePetRenameCommand(), HandlePetUnlearnCommand(), rbac::RBAC_PERM_COMMAND_PET_CREATE, rbac::RBAC_PERM_COMMAND_PET_DELETE, rbac::RBAC_PERM_COMMAND_PET_LEARN, rbac::RBAC_PERM_COMMAND_PET_LIST, rbac::RBAC_PERM_COMMAND_PET_RENAME, and rbac::RBAC_PERM_COMMAND_PET_UNLEARN.

◆ HandlePetCreateCommand()

static bool pet_commandscript::HandlePetCreateCommand ( ChatHandler handler)
inlinestatic
61 {
62 Player* player = handler->GetSession()->GetPlayer();
63 Creature* creatureTarget = handler->getSelectedCreature();
64
65 if (!creatureTarget || creatureTarget->IsPet() || creatureTarget->IsPlayer())
66 {
68 return false;
69 }
70
71 CreatureTemplate const* creatrueTemplate = sObjectMgr->GetCreatureTemplate(creatureTarget->GetEntry());
72 // Creatures with family 0 crashes the server
73 if (!creatrueTemplate->family)
74 {
75 handler->SendErrorMessage(LANG_CREATURE_NON_TAMEABLE, creatrueTemplate->Entry);
76 return false;
77 }
78
79 if (player->IsExistPet())
80 {
82 return false;
83 }
84
85 if (!player->CreatePet(creatureTarget))
86 {
87 handler->SendErrorMessage(LANG_CREATURE_NON_TAMEABLE, creatrueTemplate->Entry);
88 return false;
89 }
90
91 return true;
92 }
@ LANG_SELECT_CREATURE
Definition Language.h:32
@ LANG_CREATURE_NON_TAMEABLE
Definition Language.h:393
@ LANG_YOU_ALREADY_HAVE_PET
Definition Language.h:394
#define sObjectMgr
Definition ObjectMgr.h:1732
WorldSession * GetSession()
Definition Chat.h:242
void SendErrorMessage(uint32 entry)
Definition Chat.cpp:224
Creature * getSelectedCreature() const
Definition Chat.cpp:418
Definition Creature.h:47
bool IsPlayer() const
Definition Object.h:201
uint32 GetEntry() const
Definition Object.h:117
Definition Player.h:1084
bool IsExistPet()
Definition Player.cpp:9251
Pet * CreatePet(Creature *creatureTarget, uint32 spellID=0)
Definition Player.cpp:9257
bool IsPet() const
Definition Unit.h:797
Player * GetPlayer() const
Definition WorldSession.h:464
Definition CreatureData.h:187
uint32 Entry
Definition CreatureData.h:188
uint32 family
Definition CreatureData.h:217

References Player::CreatePet(), CreatureTemplate::Entry, CreatureTemplate::family, Object::GetEntry(), WorldSession::GetPlayer(), ChatHandler::getSelectedCreature(), ChatHandler::GetSession(), Player::IsExistPet(), Unit::IsPet(), Object::IsPlayer(), LANG_CREATURE_NON_TAMEABLE, LANG_SELECT_CREATURE, LANG_YOU_ALREADY_HAVE_PET, ChatHandler::SendErrorMessage(), and sObjectMgr.

Referenced by GetCommands().

◆ HandlePetDeleteCommand()

static bool pet_commandscript::HandlePetDeleteCommand ( ChatHandler handler,
PlayerIdentifier  owner,
uint32  petNumber 
)
inlinestatic
137 {
138 ObjectGuid ownerGuid = owner.GetGUID();
139
140 QueryResult result = CharacterDatabase.Query(
141 "SELECT entry, name FROM character_pet WHERE id = {} AND owner = {}",
142 petNumber, ownerGuid.GetCounter());
143
144 if (!result)
145 {
146 handler->SendErrorMessage(LANG_PET_DELETE_NOT_FOUND, petNumber, owner.GetName(),
147 ownerGuid.GetCounter());
148 return false;
149 }
150
151 if (Player* online = owner.GetConnectedPlayer())
152 {
153 if (Pet* activePet = online->GetPet())
154 {
155 if (activePet->GetCharmInfo() &&
156 activePet->GetCharmInfo()->GetPetNumber() == petNumber)
157 {
158 online->RemovePet(activePet, PET_SAVE_AS_DELETED);
159 }
160 }
161
162 // Drop the pet from the in-memory PetStable so the stable UI / call-pet
163 // logic does not reference a row that no longer exists in the database.
164 if (PetStable* stable = online->GetPetStable())
165 {
166 if (stable->CurrentPet && stable->CurrentPet->PetNumber == petNumber)
167 stable->CurrentPet.reset();
168
169 for (Optional<PetStable::PetInfo>& stabled : stable->StabledPets)
170 if (stabled && stabled->PetNumber == petNumber)
171 stabled.reset();
172
173 stable->UnslottedPets.erase(
174 std::remove_if(stable->UnslottedPets.begin(), stable->UnslottedPets.end(),
175 [petNumber](PetStable::PetInfo const& p) { return p.PetNumber == petNumber; }),
176 stable->UnslottedPets.end());
177 }
178 }
179
180 Field* fields = result->Fetch();
181 uint32 entry = fields[0].Get<uint32>();
182 std::string name = fields[1].Get<std::string>();
183
184 std::string creatureName = "<unknown>";
185 if (CreatureTemplate const* cInfo = sObjectMgr->GetCreatureTemplate(entry))
186 creatureName = cInfo->Name;
187
188 Pet::DeleteFromDB(petNumber);
189
190 handler->PSendSysMessage(LANG_PET_DELETE_SUCCESS, petNumber, name, entry, creatureName,
191 owner.GetName(), ownerGuid.GetCounter());
192 return true;
193 }
std::shared_ptr< ResultSet > QueryResult
Definition DatabaseEnvFwd.h:27
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
Definition DatabaseEnv.cpp:21
std::uint32_t uint32
Definition Define.h:107
@ LANG_PET_DELETE_SUCCESS
Definition Language.h:1495
@ LANG_PET_DELETE_NOT_FOUND
Definition Language.h:1494
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:24
@ PET_SAVE_AS_DELETED
Definition PetDefines.h:42
void PSendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition Chat.cpp:219
Class used to access individual fields of database query result.
Definition Field.h:98
std::enable_if_t< std::is_arithmetic_v< T >, T > Get() const
Definition Field.h:112
Definition ObjectGuid.h:118
LowType GetCounter() const
Definition ObjectGuid.h:145
Definition PetDefines.h:212
Definition Pet.h:41
void DeleteFromDB() override
Definition Pet.h:171
ObjectGuid GetGUID() const
Definition ChatCommandTags.h:181
std::string const & GetName() const
Definition ChatCommandTags.h:180
Player * GetConnectedPlayer() const
Definition ChatCommandTags.h:183
Definition PetDefines.h:215

References CharacterDatabase, Pet::DeleteFromDB(), Field::Get(), Acore::ChatCommands::PlayerIdentifier::GetConnectedPlayer(), ObjectGuid::GetCounter(), Acore::ChatCommands::PlayerIdentifier::GetGUID(), Acore::ChatCommands::PlayerIdentifier::GetName(), LANG_PET_DELETE_NOT_FOUND, LANG_PET_DELETE_SUCCESS, PET_SAVE_AS_DELETED, ChatHandler::PSendSysMessage(), ChatHandler::SendErrorMessage(), and sObjectMgr.

Referenced by GetCommands().

◆ HandlePetLearnCommand()

static bool pet_commandscript::HandlePetLearnCommand ( ChatHandler handler,
SpellInfo const *  spell 
)
inlinestatic
95 {
96 if (!spell)
97 {
99 return false;
100 }
101
102 if (!SpellMgr::IsSpellValid(spell))
103 {
104 handler->SendErrorMessage(LANG_COMMAND_SPELL_BROKEN, spell->Id);
105 return false;
106 }
107
108 Pet* pet = handler->GetSession()->GetPlayer()->GetPet();
109 if (!pet)
110 {
111 handler->SendErrorMessage("You have no pet");
112 return false;
113 }
114
115 SpellScriptsBounds bounds = sObjectMgr->GetSpellScriptsBounds(spell->Id);
116 uint32 spellDifficultyId = sSpellMgr->GetSpellDifficultyId(spell->Id);
117 if (bounds.first != bounds.second || spellDifficultyId)
118 {
119 handler->SendErrorMessage("Spell {} cannot be learnt using a command!", spell->Id);
120 return false;
121 }
122
123 // Check if pet already has it
124 if (pet->HasSpell(spell->Id))
125 {
126 handler->SendErrorMessage("Pet already has spell: {}", spell->Id);
127 return false;
128 }
129
130 pet->learnSpell(spell->Id);
131 handler->PSendSysMessage("Pet has learned spell {}", spell->Id);
132
133 return true;
134 }
@ LANG_COMMAND_NOSPELLFOUND
Definition Language.h:486
@ LANG_COMMAND_SPELL_BROKEN
Definition Language.h:529
std::pair< SpellScriptsContainer::iterator, SpellScriptsContainer::iterator > SpellScriptsBounds
Definition ObjectMgr.h:391
#define sSpellMgr
Definition SpellMgr.h:847
bool HasSpell(uint32 spell) const override
Definition Pet.cpp:2326
bool learnSpell(uint32 spell_id)
Definition Pet.cpp:1897
Pet * GetPet() const
Definition Player.cpp:8970
static bool IsSpellValid(SpellInfo const *spellInfo)
Definition SpellMgr.cpp:490

References Player::GetPet(), WorldSession::GetPlayer(), ChatHandler::GetSession(), Pet::HasSpell(), SpellInfo::Id, SpellMgr::IsSpellValid(), LANG_COMMAND_NOSPELLFOUND, LANG_COMMAND_SPELL_BROKEN, Pet::learnSpell(), ChatHandler::PSendSysMessage(), ChatHandler::SendErrorMessage(), sObjectMgr, and sSpellMgr.

Referenced by GetCommands().

◆ HandlePetListCommand()

static bool pet_commandscript::HandlePetListCommand ( ChatHandler handler,
PlayerIdentifier  owner 
)
inlinestatic
196 {
197 ObjectGuid ownerGuid = owner.GetGUID();
198
199 QueryResult result = CharacterDatabase.Query(
200 "SELECT id, entry, level, slot, name, PetType FROM character_pet "
201 "WHERE owner = {} ORDER BY slot, id",
202 ownerGuid.GetCounter());
203
204 if (!result)
205 {
206 handler->PSendSysMessage(LANG_PET_LIST_EMPTY, owner.GetName(), ownerGuid.GetCounter());
207 return true;
208 }
209
210 handler->PSendSysMessage(LANG_PET_LIST_HEADER, owner.GetName(), ownerGuid.GetCounter());
211
212 do
213 {
214 Field* fields = result->Fetch();
215 uint32 petNumber = fields[0].Get<uint32>();
216 uint32 entry = fields[1].Get<uint32>();
217 uint8 level = fields[2].Get<uint8>();
218 uint8 slot = fields[3].Get<uint8>();
219 std::string name = fields[4].Get<std::string>();
220 uint8 petType = fields[5].Get<uint8>();
221
222 std::string creatureName = "<unknown>";
223 if (CreatureTemplate const* cInfo = sObjectMgr->GetCreatureTemplate(entry))
224 creatureName = cInfo->Name;
225
226 handler->PSendSysMessage(LANG_PET_LIST_ENTRY, petNumber, uint32(slot), entry,
227 creatureName, name, uint32(level), uint32(petType));
228 } while (result->NextRow());
229
230 return true;
231 }
std::uint8_t uint8
Definition Define.h:109
@ LANG_PET_LIST_EMPTY
Definition Language.h:1491
@ LANG_PET_LIST_ENTRY
Definition Language.h:1490
@ LANG_PET_LIST_HEADER
Definition Language.h:1489

References CharacterDatabase, Field::Get(), ObjectGuid::GetCounter(), Acore::ChatCommands::PlayerIdentifier::GetGUID(), Acore::ChatCommands::PlayerIdentifier::GetName(), LANG_PET_LIST_EMPTY, LANG_PET_LIST_ENTRY, LANG_PET_LIST_HEADER, ChatHandler::PSendSysMessage(), and sObjectMgr.

Referenced by GetCommands().

◆ HandlePetRenameCommand()

static bool pet_commandscript::HandlePetRenameCommand ( ChatHandler handler,
PlayerIdentifier  owner,
uint32  petNumber,
std::string  newName 
)
inlinestatic
234 {
236 {
237 handler->SendErrorMessage(LANG_PET_RENAME_INVALID, newName);
238 return false;
239 }
240
241 ObjectGuid ownerGuid = owner.GetGUID();
242
243 QueryResult result = CharacterDatabase.Query("SELECT name FROM character_pet WHERE id = {} AND owner = {}", petNumber, ownerGuid.GetCounter());
244
245 if (!result)
246 {
247 handler->SendErrorMessage(LANG_PET_DELETE_NOT_FOUND, petNumber, owner.GetName(), ownerGuid.GetCounter());
248 return false;
249 }
250
251 std::string oldName = result->Fetch()[0].Get<std::string>();
252
254 stmt->SetData(0, newName);
255 stmt->SetData(1, ownerGuid.GetCounter());
256 stmt->SetData(2, petNumber);
257 CharacterDatabase.Execute(stmt);
258
259 if (Player* online = owner.GetConnectedPlayer())
260 {
261 if (Pet* activePet = online->GetPet())
262 {
263 if (activePet->GetCharmInfo() && activePet->GetCharmInfo()->GetPetNumber() == petNumber)
264 {
265 activePet->SetName(newName);
266 // Bump the name timestamp so nearby clients re-query and drop their cached pet name.
267 activePet->SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, uint32(GameTime::GetGameTime().count()));
268 activePet->RemoveByteFlag(UNIT_FIELD_BYTES_2, 2, UNIT_CAN_BE_RENAMED);
269 if (online->GetGroup())
270 online->SetGroupUpdateFlag(GROUP_UPDATE_FLAG_PET_NAME);
271 }
272 }
273
274 if (PetStable* stable = online->GetPetStable())
275 {
276 if (stable->CurrentPet && stable->CurrentPet->PetNumber == petNumber)
277 {
278 stable->CurrentPet->Name = newName;
279 stable->CurrentPet->WasRenamed = true;
280 }
281
282 for (Optional<PetStable::PetInfo>& stabled : stable->StabledPets)
283 {
284 if (stabled && stabled->PetNumber == petNumber)
285 {
286 stabled->Name = newName;
287 stabled->WasRenamed = true;
288 }
289 }
290
291 for (PetStable::PetInfo& unslotted : stable->UnslottedPets)
292 {
293 if (unslotted.PetNumber == petNumber)
294 {
295 unslotted.Name = newName;
296 unslotted.WasRenamed = true;
297 }
298 }
299 }
300 }
301
302 handler->PSendSysMessage(LANG_PET_RENAME_SUCCESS, petNumber, oldName, newName, owner.GetName(), ownerGuid.GetCounter());
303 return true;
304 }
@ CHAR_UPD_CHAR_PET_NAME
Definition CharacterDatabase.h:488
@ GROUP_UPDATE_FLAG_PET_NAME
Definition Group.h:111
@ LANG_PET_RENAME_INVALID
Definition Language.h:1519
@ LANG_PET_RENAME_SUCCESS
Definition Language.h:1520
@ PET_NAME_SUCCESS
Definition SharedDefines.h:3914
@ UNIT_CAN_BE_RENAMED
Definition UnitDefines.h:152
@ UNIT_FIELD_PET_NAME_TIMESTAMP
Definition UpdateFields.h:133
@ UNIT_FIELD_BYTES_2
Definition UpdateFields.h:161
static PetNameInvalidReason CheckPetName(std::string_view name)
Definition ObjectMgr.cpp:9481
Acore::Types::is_default< T > SetData(const uint8 index, T value)
Definition PreparedStatement.h:77
Definition PreparedStatement.h:157
Seconds GetGameTime()
Definition GameTime.cpp:38

References CHAR_UPD_CHAR_PET_NAME, CharacterDatabase, ObjectMgr::CheckPetName(), Acore::ChatCommands::PlayerIdentifier::GetConnectedPlayer(), ObjectGuid::GetCounter(), GameTime::GetGameTime(), Acore::ChatCommands::PlayerIdentifier::GetGUID(), Acore::ChatCommands::PlayerIdentifier::GetName(), GROUP_UPDATE_FLAG_PET_NAME, LANG_PET_DELETE_NOT_FOUND, LANG_PET_RENAME_INVALID, LANG_PET_RENAME_SUCCESS, PET_NAME_SUCCESS, ChatHandler::PSendSysMessage(), ChatHandler::SendErrorMessage(), PreparedStatementBase::SetData(), UNIT_CAN_BE_RENAMED, UNIT_FIELD_BYTES_2, and UNIT_FIELD_PET_NAME_TIMESTAMP.

Referenced by GetCommands().

◆ HandlePetUnlearnCommand()

static bool pet_commandscript::HandlePetUnlearnCommand ( ChatHandler handler,
SpellInfo const *  spell 
)
inlinestatic
307 {
308 if (!spell)
309 {
311 return false;
312 }
313
314 if (!SpellMgr::IsSpellValid(spell))
315 {
316 handler->SendErrorMessage(LANG_COMMAND_SPELL_BROKEN, spell->Id);
317 return false;
318 }
319
320 Pet* pet = handler->GetSession()->GetPlayer()->GetPet();
321 if (!pet)
322 {
323 handler->SendErrorMessage("You have no pet");
324 return false;
325 }
326
327 if (pet->HasSpell(spell->Id))
328 {
329 pet->removeSpell(spell->Id, false);
330 }
331 else
332 {
333 handler->PSendSysMessage("Pet doesn't have that spell");
334 }
335
336 return true;
337 }
bool removeSpell(uint32 spell_id, bool learn_prev, bool clear_ab=true)
Definition Pet.cpp:1970

References Player::GetPet(), WorldSession::GetPlayer(), ChatHandler::GetSession(), Pet::HasSpell(), SpellInfo::Id, SpellMgr::IsSpellValid(), LANG_COMMAND_NOSPELLFOUND, LANG_COMMAND_SPELL_BROKEN, ChatHandler::PSendSysMessage(), Pet::removeSpell(), and ChatHandler::SendErrorMessage().

Referenced by GetCommands().


The documentation for this class was generated from the following file: