root/trunk/src/game/Level3.cpp @ 163

Revision 163, 175.6 kB (checked in by yumileroy, 17 years ago)

[svn] added commands : pet create, tp, learn, unlearn. gobject activate and playall. thanx to dythzer.

Original author: KingPin?
Date: 2008-11-04 13:32:08-06:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "Common.h"
22#include "Database/DatabaseEnv.h"
23#include "WorldPacket.h"
24#include "WorldSession.h"
25#include "World.h"
26#include "ObjectMgr.h"
27#include "AccountMgr.h"
28#include "PlayerDump.h"
29#include "SpellMgr.h"
30#include "Player.h"
31#include "Opcodes.h"
32#include "GameObject.h"
33#include "Chat.h"
34#include "Log.h"
35#include "Guild.h"
36#include "ObjectAccessor.h"
37#include "MapManager.h"
38#include "SpellAuras.h"
39#include "ScriptCalls.h"
40#include "Language.h"
41#include "GridNotifiersImpl.h"
42#include "CellImpl.h"
43#include "Weather.h"
44#include "PointMovementGenerator.h"
45#include "TargetedMovementGenerator.h"
46#include "SkillDiscovery.h"
47#include "SkillExtraItems.h"
48#include "SystemConfig.h"
49#include "Config/ConfigEnv.h"
50#include "Util.h"
51#include "ItemEnchantmentMgr.h"
52#include "BattleGroundMgr.h"
53#include "InstanceSaveMgr.h"
54#include "InstanceData.h"
55#include "AccountMgr.h"
56
57//reload commands
58bool ChatHandler::HandleReloadCommand(const char* arg)
59{
60    // this is error catcher for wrong table name in .reload commands
61    PSendSysMessage("Db table with name starting from '%s' not found and can't be reloaded.",arg);
62    SetSentErrorMessage(true);
63    return false;
64}
65
66bool ChatHandler::HandleReloadAllCommand(const char*)
67{
68    HandleReloadAreaTriggerTeleportCommand("");
69    HandleReloadSkillFishingBaseLevelCommand("");
70
71    HandleReloadAllAreaCommand("");
72    HandleReloadAllLootCommand("");
73    HandleReloadAllNpcCommand("");
74    HandleReloadAllQuestCommand("");
75    HandleReloadAllSpellCommand("");
76    HandleReloadAllItemCommand("");
77    HandleReloadAllLocalesCommand("");
78
79    HandleReloadCommandCommand("");
80    HandleReloadReservedNameCommand("");
81    HandleReloadTrinityStringCommand("");
82    HandleReloadGameTeleCommand("");
83    return true;
84}
85
86bool ChatHandler::HandleReloadAllAreaCommand(const char*)
87{
88    //HandleReloadQuestAreaTriggersCommand(""); -- reloaded in HandleReloadAllQuestCommand
89    HandleReloadAreaTriggerTeleportCommand("");
90    HandleReloadAreaTriggerTavernCommand("");
91    HandleReloadGameGraveyardZoneCommand("");
92    return true;
93}
94
95bool ChatHandler::HandleReloadAllLootCommand(const char*)
96{
97    sLog.outString( "Re-Loading Loot Tables..." );
98    LoadLootTables();
99    SendGlobalSysMessage("DB tables `*_loot_template` reloaded.");
100    return true;
101}
102
103bool ChatHandler::HandleReloadAllNpcCommand(const char* /*args*/)
104{
105    HandleReloadNpcGossipCommand("a");
106    HandleReloadNpcTrainerCommand("a");
107    HandleReloadNpcVendorCommand("a");
108    return true;
109}
110
111bool ChatHandler::HandleReloadAllQuestCommand(const char* /*args*/)
112{
113    HandleReloadQuestAreaTriggersCommand("a");
114    HandleReloadQuestTemplateCommand("a");
115
116    sLog.outString( "Re-Loading Quests Relations..." );
117    objmgr.LoadQuestRelations();
118    SendGlobalSysMessage("DB tables `*_questrelation` and `*_involvedrelation` reloaded.");
119    return true;
120}
121
122bool ChatHandler::HandleReloadAllScriptsCommand(const char*)
123{
124    if(sWorld.IsScriptScheduled())
125    {
126        PSendSysMessage("DB scripts used currently, please attempt reload later.");
127        SetSentErrorMessage(true);
128        return false;
129    }
130
131    sLog.outString( "Re-Loading Scripts..." );
132    HandleReloadGameObjectScriptsCommand("a");
133    HandleReloadEventScriptsCommand("a");
134    HandleReloadQuestEndScriptsCommand("a");
135    HandleReloadQuestStartScriptsCommand("a");
136    HandleReloadSpellScriptsCommand("a");
137    SendGlobalSysMessage("DB tables `*_scripts` reloaded.");
138    return true;
139}
140
141bool ChatHandler::HandleReloadAllSpellCommand(const char*)
142{
143    HandleReloadSkillDiscoveryTemplateCommand("a");
144    HandleReloadSkillExtraItemTemplateCommand("a");
145    HandleReloadSpellAffectCommand("a");
146    HandleReloadSpellChainCommand("a");
147    HandleReloadSpellElixirCommand("a");
148    HandleReloadSpellLearnSpellCommand("a");
149    HandleReloadSpellProcEventCommand("a");
150    HandleReloadSpellScriptTargetCommand("a");
151    HandleReloadSpellTargetPositionCommand("a");
152    HandleReloadSpellThreatsCommand("a");
153    HandleReloadSpellPetAurasCommand("a");
154    HandleReloadSpellDisabledCommand("a");
155    return true;
156}
157
158bool ChatHandler::HandleReloadAllItemCommand(const char*)
159{
160    HandleReloadPageTextsCommand("a");
161    HandleReloadItemEnchantementsCommand("a");
162    return true;
163}
164
165bool ChatHandler::HandleReloadAllLocalesCommand(const char* args)
166{
167    HandleReloadLocalesCreatureCommand("a");
168    HandleReloadLocalesGameobjectCommand("a");
169    HandleReloadLocalesItemCommand("a");
170    HandleReloadLocalesNpcTextCommand("a");
171    HandleReloadLocalesPageTextCommand("a");
172    HandleReloadLocalesQuestCommand("a");
173    return true;
174}
175
176bool ChatHandler::HandleReloadConfigCommand(const char* arg)
177{
178    sLog.outString( "Re-Loading config settings..." );
179    sWorld.LoadConfigSettings(true);
180    SendGlobalSysMessage("World config settings reloaded.");
181    return true;
182}
183
184bool ChatHandler::HandleReloadAreaTriggerTavernCommand(const char*)
185{
186    sLog.outString( "Re-Loading Tavern Area Triggers..." );
187    objmgr.LoadTavernAreaTriggers();
188    SendGlobalSysMessage("DB table `areatrigger_tavern` reloaded.");
189    return true;
190}
191
192bool ChatHandler::HandleReloadAreaTriggerTeleportCommand(const char*)
193{
194    sLog.outString( "Re-Loading AreaTrigger teleport definitions..." );
195    objmgr.LoadAreaTriggerTeleports();
196    SendGlobalSysMessage("DB table `areatrigger_teleport` reloaded.");
197    return true;
198}
199
200bool ChatHandler::HandleReloadCommandCommand(const char*)
201{
202    load_command_table = true;
203    SendGlobalSysMessage("DB table `command` will be reloaded at next chat command use.");
204    return true;
205}
206
207bool ChatHandler::HandleReloadCreatureQuestRelationsCommand(const char*)
208{
209    sLog.outString( "Loading Quests Relations... (`creature_questrelation`)" );
210    objmgr.LoadCreatureQuestRelations();
211    SendGlobalSysMessage("DB table `creature_questrelation` (creature quest givers) reloaded.");
212    return true;
213}
214
215bool ChatHandler::HandleReloadCreatureQuestInvRelationsCommand(const char*)
216{
217    sLog.outString( "Loading Quests Relations... (`creature_involvedrelation`)" );
218    objmgr.LoadCreatureInvolvedRelations();
219    SendGlobalSysMessage("DB table `creature_involvedrelation` (creature quest takers) reloaded.");
220    return true;
221}
222
223bool ChatHandler::HandleReloadGOQuestRelationsCommand(const char*)
224{
225    sLog.outString( "Loading Quests Relations... (`gameobject_questrelation`)" );
226    objmgr.LoadGameobjectQuestRelations();
227    SendGlobalSysMessage("DB table `gameobject_questrelation` (gameobject quest givers) reloaded.");
228    return true;
229}
230
231bool ChatHandler::HandleReloadGOQuestInvRelationsCommand(const char*)
232{
233    sLog.outString( "Loading Quests Relations... (`gameobject_involvedrelation`)" );
234    objmgr.LoadGameobjectInvolvedRelations();
235    SendGlobalSysMessage("DB table `gameobject_involvedrelation` (gameobject quest takers) reloaded.");
236    return true;
237}
238
239bool ChatHandler::HandleReloadQuestAreaTriggersCommand(const char*)
240{
241    sLog.outString( "Re-Loading Quest Area Triggers..." );
242    objmgr.LoadQuestAreaTriggers();
243    SendGlobalSysMessage("DB table `areatrigger_involvedrelation` (quest area triggers) reloaded.");
244    return true;
245}
246
247bool ChatHandler::HandleReloadQuestTemplateCommand(const char*)
248{
249    sLog.outString( "Re-Loading Quest Templates..." );
250    objmgr.LoadQuests();
251    SendGlobalSysMessage("DB table `quest_template` (quest definitions) reloaded.");
252    return true;
253}
254
255bool ChatHandler::HandleReloadLootTemplatesCreatureCommand(const char*)
256{
257    sLog.outString( "Re-Loading Loot Tables... (`creature_loot_template`)" );
258    LoadLootTemplates_Creature();
259    LootTemplates_Creature.CheckLootRefs();
260    SendGlobalSysMessage("DB table `creature_loot_template` reloaded.");
261    return true;
262}
263
264bool ChatHandler::HandleReloadLootTemplatesDisenchantCommand(const char*)
265{
266    sLog.outString( "Re-Loading Loot Tables... (`disenchant_loot_template`)" );
267    LoadLootTemplates_Disenchant();
268    LootTemplates_Disenchant.CheckLootRefs();
269    SendGlobalSysMessage("DB table `disenchant_loot_template` reloaded.");
270    return true;
271}
272
273bool ChatHandler::HandleReloadLootTemplatesFishingCommand(const char*)
274{
275    sLog.outString( "Re-Loading Loot Tables... (`fishing_loot_template`)" );
276    LoadLootTemplates_Fishing();
277    LootTemplates_Fishing.CheckLootRefs();
278    SendGlobalSysMessage("DB table `fishing_loot_template` reloaded.");
279    return true;
280}
281
282bool ChatHandler::HandleReloadLootTemplatesGameobjectCommand(const char*)
283{
284    sLog.outString( "Re-Loading Loot Tables... (`gameobject_loot_template`)" );
285    LoadLootTemplates_Gameobject();
286    LootTemplates_Gameobject.CheckLootRefs();
287    SendGlobalSysMessage("DB table `gameobject_loot_template` reloaded.");
288    return true;
289}
290
291bool ChatHandler::HandleReloadLootTemplatesItemCommand(const char*)
292{
293    sLog.outString( "Re-Loading Loot Tables... (`item_loot_template`)" );
294    LoadLootTemplates_Item();
295    LootTemplates_Item.CheckLootRefs();
296    SendGlobalSysMessage("DB table `item_loot_template` reloaded.");
297    return true;
298}
299
300bool ChatHandler::HandleReloadLootTemplatesPickpocketingCommand(const char*)
301{
302    sLog.outString( "Re-Loading Loot Tables... (`pickpocketing_loot_template`)" );
303    LoadLootTemplates_Pickpocketing();
304    LootTemplates_Pickpocketing.CheckLootRefs();
305    SendGlobalSysMessage("DB table `pickpocketing_loot_template` reloaded.");
306    return true;
307}
308
309bool ChatHandler::HandleReloadLootTemplatesProspectingCommand(const char*)
310{
311    sLog.outString( "Re-Loading Loot Tables... (`prospecting_loot_template`)" );
312    LoadLootTemplates_Prospecting();
313    LootTemplates_Prospecting.CheckLootRefs();
314    SendGlobalSysMessage("DB table `prospecting_loot_template` reloaded.");
315    return true;
316}
317
318bool ChatHandler::HandleReloadLootTemplatesQuestMailCommand(const char*)
319{
320    sLog.outString( "Re-Loading Loot Tables... (`quest_mail_loot_template`)" );
321    LoadLootTemplates_QuestMail();
322    LootTemplates_QuestMail.CheckLootRefs();
323    SendGlobalSysMessage("DB table `quest_mail_loot_template` reloaded.");
324    return true;
325}
326
327bool ChatHandler::HandleReloadLootTemplatesReferenceCommand(const char*)
328{
329    sLog.outString( "Re-Loading Loot Tables... (`reference_loot_template`)" );
330    LoadLootTemplates_Reference();
331    SendGlobalSysMessage("DB table `reference_loot_template` reloaded.");
332    return true;
333}
334
335bool ChatHandler::HandleReloadLootTemplatesSkinningCommand(const char*)
336{
337    sLog.outString( "Re-Loading Loot Tables... (`skinning_loot_template`)" );
338    LoadLootTemplates_Skinning();
339    LootTemplates_Skinning.CheckLootRefs();
340    SendGlobalSysMessage("DB table `skinning_loot_template` reloaded.");
341    return true;
342}
343
344bool ChatHandler::HandleReloadTrinityStringCommand(const char*)
345{
346    sLog.outString( "Re-Loading trinity_string Table!" );
347    objmgr.LoadTrinityStrings();
348    SendGlobalSysMessage("DB table `trinity_string` reloaded.");
349    return true;
350}
351
352bool ChatHandler::HandleReloadNpcGossipCommand(const char*)
353{
354    sLog.outString( "Re-Loading `npc_gossip` Table!" );
355    objmgr.LoadNpcTextId();
356    SendGlobalSysMessage("DB table `npc_gossip` reloaded.");
357    return true;
358}
359
360bool ChatHandler::HandleReloadNpcTrainerCommand(const char*)
361{
362    sLog.outString( "Re-Loading `npc_trainer` Table!" );
363    objmgr.LoadTrainerSpell();
364    SendGlobalSysMessage("DB table `npc_trainer` reloaded.");
365    return true;
366}
367
368bool ChatHandler::HandleReloadNpcVendorCommand(const char*)
369{
370    sLog.outString( "Re-Loading `npc_vendor` Table!" );
371    objmgr.LoadVendors();
372    SendGlobalSysMessage("DB table `npc_vendor` reloaded.");
373    return true;
374}
375
376bool ChatHandler::HandleReloadReservedNameCommand(const char*)
377{
378    sLog.outString( "Loading ReservedNames... (`reserved_name`)" );
379    objmgr.LoadReservedPlayersNames();
380    SendGlobalSysMessage("DB table `reserved_name` (player reserved names) reloaded.");
381    return true;
382}
383
384bool ChatHandler::HandleReloadSkillDiscoveryTemplateCommand(const char* /*args*/)
385{
386    sLog.outString( "Re-Loading Skill Discovery Table..." );
387    LoadSkillDiscoveryTable();
388    SendGlobalSysMessage("DB table `skill_discovery_template` (recipes discovered at crafting) reloaded.");
389    return true;
390}
391
392bool ChatHandler::HandleReloadSkillExtraItemTemplateCommand(const char* /*args*/)
393{
394    sLog.outString( "Re-Loading Skill Extra Item Table..." );
395    LoadSkillExtraItemTable();
396    SendGlobalSysMessage("DB table `skill_extra_item_template` (extra item creation when crafting) reloaded.");
397    return true;
398}
399
400bool ChatHandler::HandleReloadSkillFishingBaseLevelCommand(const char* /*args*/)
401{
402    sLog.outString( "Re-Loading Skill Fishing base level requirements..." );
403    objmgr.LoadFishingBaseSkillLevel();
404    SendGlobalSysMessage("DB table `skill_fishing_base_level` (fishing base level for zone/subzone) reloaded.");
405    return true;
406}
407
408bool ChatHandler::HandleReloadSpellAffectCommand(const char*)
409{
410    sLog.outString( "Re-Loading SpellAffect definitions..." );
411    spellmgr.LoadSpellAffects();
412    SendGlobalSysMessage("DB table `spell_affect` (spell mods apply requirements) reloaded.");
413    return true;
414}
415
416bool ChatHandler::HandleReloadSpellChainCommand(const char*)
417{
418    sLog.outString( "Re-Loading Spell Chain Data... " );
419    spellmgr.LoadSpellChains();
420    SendGlobalSysMessage("DB table `spell_chain` (spell ranks) reloaded.");
421    return true;
422}
423
424bool ChatHandler::HandleReloadSpellElixirCommand(const char*)
425{
426    sLog.outString( "Re-Loading Spell Elixir types..." );
427    spellmgr.LoadSpellElixirs();
428    SendGlobalSysMessage("DB table `spell_elixir` (spell exlixir types) reloaded.");
429    return true;
430}
431
432bool ChatHandler::HandleReloadSpellLearnSpellCommand(const char*)
433{
434    sLog.outString( "Re-Loading Spell Learn Spells..." );
435    spellmgr.LoadSpellLearnSpells();
436    SendGlobalSysMessage("DB table `spell_learn_spell` reloaded.");
437    return true;
438}
439
440bool ChatHandler::HandleReloadSpellProcEventCommand(const char*)
441{
442    sLog.outString( "Re-Loading Spell Proc Event conditions..." );
443    spellmgr.LoadSpellProcEvents();
444    SendGlobalSysMessage("DB table `spell_proc_event` (spell proc trigger requirements) reloaded.");
445    return true;
446}
447
448bool ChatHandler::HandleReloadSpellScriptTargetCommand(const char*)
449{
450    sLog.outString( "Re-Loading SpellsScriptTarget..." );
451    spellmgr.LoadSpellScriptTarget();
452    SendGlobalSysMessage("DB table `spell_script_target` (spell targets selection in case specific creature/GO requirements) reloaded.");
453    return true;
454}
455
456bool ChatHandler::HandleReloadSpellTargetPositionCommand(const char*)
457{
458    sLog.outString( "Re-Loading Spell target coordinates..." );
459    spellmgr.LoadSpellTargetPositions();
460    SendGlobalSysMessage("DB table `spell_target_position` (destination coordinates for spell targets) reloaded.");
461    return true;
462}
463
464bool ChatHandler::HandleReloadSpellThreatsCommand(const char*)
465{
466    sLog.outString( "Re-Loading Aggro Spells Definitions...");
467    spellmgr.LoadSpellThreats();
468    SendGlobalSysMessage("DB table `spell_threat` (spell aggro definitions) reloaded.");
469    return true;
470}
471
472bool ChatHandler::HandleReloadSpellPetAurasCommand(const char*)
473{
474    sLog.outString( "Re-Loading Spell pet auras...");
475    spellmgr.LoadSpellPetAuras();
476    SendGlobalSysMessage("DB table `spell_pet_auras` reloaded.");
477    return true;
478}
479
480bool ChatHandler::HandleReloadPageTextsCommand(const char*)
481{
482    sLog.outString( "Re-Loading Page Texts..." );
483    objmgr.LoadPageTexts();
484    SendGlobalSysMessage("DB table `page_texts` reloaded.");
485    return true;
486}
487
488bool ChatHandler::HandleReloadItemEnchantementsCommand(const char*)
489{
490    sLog.outString( "Re-Loading Item Random Enchantments Table..." );
491    LoadRandomEnchantmentsTable();
492    SendGlobalSysMessage("DB table `item_enchantment_template` reloaded.");
493    return true;
494}
495
496bool ChatHandler::HandleReloadGameObjectScriptsCommand(const char* arg)
497{
498    if(sWorld.IsScriptScheduled())
499    {
500        SendSysMessage("DB scripts used currently, please attempt reload later.");
501        SetSentErrorMessage(true);
502        return false;
503    }
504
505    if(*arg!='a')
506        sLog.outString( "Re-Loading Scripts from `gameobject_scripts`...");
507
508    objmgr.LoadGameObjectScripts();
509
510    if(*arg!='a')
511        SendGlobalSysMessage("DB table `gameobject_scripts` reloaded.");
512
513    return true;
514}
515
516bool ChatHandler::HandleReloadEventScriptsCommand(const char* arg)
517{
518    if(sWorld.IsScriptScheduled())
519    {
520        SendSysMessage("DB scripts used currently, please attempt reload later.");
521        SetSentErrorMessage(true);
522        return false;
523    }
524
525    if(*arg!='a')
526        sLog.outString( "Re-Loading Scripts from `event_scripts`...");
527
528    objmgr.LoadEventScripts();
529
530    if(*arg!='a')
531        SendGlobalSysMessage("DB table `event_scripts` reloaded.");
532
533    return true;
534}
535
536bool ChatHandler::HandleReloadQuestEndScriptsCommand(const char* arg)
537{
538    if(sWorld.IsScriptScheduled())
539    {
540        SendSysMessage("DB scripts used currently, please attempt reload later.");
541        SetSentErrorMessage(true);
542        return false;
543    }
544
545    if(*arg!='a')
546        sLog.outString( "Re-Loading Scripts from `quest_end_scripts`...");
547
548    objmgr.LoadQuestEndScripts();
549
550    if(*arg!='a')
551        SendGlobalSysMessage("DB table `quest_end_scripts` reloaded.");
552
553    return true;
554}
555
556bool ChatHandler::HandleReloadQuestStartScriptsCommand(const char* arg)
557{
558    if(sWorld.IsScriptScheduled())
559    {
560        SendSysMessage("DB scripts used currently, please attempt reload later.");
561        SetSentErrorMessage(true);
562        return false;
563    }
564
565    if(*arg!='a')
566        sLog.outString( "Re-Loading Scripts from `quest_start_scripts`...");
567
568    objmgr.LoadQuestStartScripts();
569
570    if(*arg!='a')
571        SendGlobalSysMessage("DB table `quest_start_scripts` reloaded.");
572
573    return true;
574}
575
576bool ChatHandler::HandleReloadSpellScriptsCommand(const char* arg)
577{
578    if(sWorld.IsScriptScheduled())
579    {
580        SendSysMessage("DB scripts used currently, please attempt reload later.");
581        SetSentErrorMessage(true);
582        return false;
583    }
584
585    if(*arg!='a')
586        sLog.outString( "Re-Loading Scripts from `spell_scripts`...");
587
588    objmgr.LoadSpellScripts();
589
590    if(*arg!='a')
591        SendGlobalSysMessage("DB table `spell_scripts` reloaded.");
592
593    return true;
594}
595
596bool ChatHandler::HandleReloadGameGraveyardZoneCommand(const char* /*arg*/)
597{
598    sLog.outString( "Re-Loading Graveyard-zone links...");
599
600    objmgr.LoadGraveyardZones();
601
602    SendGlobalSysMessage("DB table `game_graveyard_zone` reloaded.");
603
604    return true;
605}
606
607bool ChatHandler::HandleReloadGameTeleCommand(const char* /*arg*/)
608{
609    sLog.outString( "Re-Loading Game Tele coordinates...");
610
611    objmgr.LoadGameTele();
612
613    SendGlobalSysMessage("DB table `game_tele` reloaded.");
614
615    return true;
616}
617
618bool ChatHandler::HandleReloadSpellDisabledCommand(const char* /*arg*/)
619{
620    sLog.outString( "Re-Loading spell disabled table...");
621
622    objmgr.LoadSpellDisabledEntrys();
623
624    SendGlobalSysMessage("DB table `spell_disabled` reloaded.");
625
626    return true;
627}
628
629bool ChatHandler::HandleReloadLocalesCreatureCommand(const char* /*arg*/)
630{
631    sLog.outString( "Re-Loading Locales Creature ...");
632    objmgr.LoadCreatureLocales();
633    SendGlobalSysMessage("DB table `locales_creature` reloaded.");
634    return true;
635}
636
637bool ChatHandler::HandleReloadLocalesGameobjectCommand(const char* /*arg*/)
638{
639    sLog.outString( "Re-Loading Locales Gameobject ... ");
640    objmgr.LoadGameObjectLocales();
641    SendGlobalSysMessage("DB table `locales_gameobject` reloaded.");
642    return true;
643}
644
645bool ChatHandler::HandleReloadLocalesItemCommand(const char* /*arg*/)
646{
647    sLog.outString( "Re-Loading Locales Item ... ");
648    objmgr.LoadItemLocales();
649    SendGlobalSysMessage("DB table `locales_item` reloaded.");
650    return true;
651}
652
653bool ChatHandler::HandleReloadLocalesNpcTextCommand(const char* /*arg*/)
654{
655    sLog.outString( "Re-Loading Locales NPC Text ... ");
656    objmgr.LoadNpcTextLocales();
657    SendGlobalSysMessage("DB table `locales_npc_text` reloaded.");
658    return true;
659}
660
661bool ChatHandler::HandleReloadLocalesPageTextCommand(const char* /*arg*/)
662{
663    sLog.outString( "Re-Loading Locales Page Text ... ");
664    objmgr.LoadPageTextLocales();
665    SendGlobalSysMessage("DB table `locales_page_text` reloaded.");
666    return true;
667}
668
669bool ChatHandler::HandleReloadLocalesQuestCommand(const char* /*arg*/)
670{
671    sLog.outString( "Re-Loading Locales Quest ... ");
672    objmgr.LoadQuestLocales();
673    SendGlobalSysMessage("DB table `locales_quest` reloaded.");
674    return true;
675}
676
677bool ChatHandler::HandleLoadScriptsCommand(const char* args)
678{
679    if(!LoadScriptingModule(args)) return true;
680
681    sWorld.SendWorldText(LANG_SCRIPTS_RELOADED);
682    return true;
683}
684
685bool ChatHandler::HandleAccountSetGmLevelCommand(const char* args)
686{
687    char* arg1 = strtok((char*)args, " ");
688    if( !arg1 )
689        return false;
690
691    char* arg2 = strtok(NULL, " ");
692
693    std::string targetAccountName;
694    uint32 targetAccountId = 0;
695    uint32 targetSecurity = 0;
696
697        /// only target player different from self allowed (if targetPlayer!=NULL then not console)
698    Player* targetPlayer = getSelectedPlayer();
699    if(targetPlayer && m_session->GetPlayer()!=targetPlayer)
700    {
701        /// wrong command syntax or unexpected targeting
702                if(arg2)
703                        return false;
704
705        targetAccountId = targetPlayer->GetSession()->GetAccountId();
706        targetSecurity = targetPlayer->GetSession()->GetSecurity();
707        if(!accmgr.GetName(targetAccountId,targetAccountName))
708                {
709                        PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,targetAccountName.c_str());
710                        SetSentErrorMessage(true);
711                        return false;
712                }
713    }
714    else
715    {
716        targetAccountName = arg1;
717        if(!AccountMgr::normilizeString(targetAccountName))
718        {
719            PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,targetAccountName.c_str());
720            SetSentErrorMessage(true);
721            return false;
722        }
723
724        targetAccountId = accmgr.GetId(targetAccountName);
725                targetSecurity = accmgr.GetSecurity(targetAccountId);
726    }
727
728    int32 gm = (int32)atoi(arg2);
729    if ( gm < SEC_PLAYER || gm > SEC_ADMINISTRATOR )
730    {
731        SendSysMessage(LANG_BAD_VALUE);
732        SetSentErrorMessage(true);
733        return false;
734    }
735
736    /// m_session==NULL only for console
737        uint32 plSecurity = m_session ? m_session->GetSecurity() : SEC_CONSOLE;
738
739        /// can set security level only for target with less security and to less security that we have
740        /// This is also reject self apply in fact
741        if(targetSecurity >= plSecurity || uint32(gm) >= plSecurity )
742    {
743        SendSysMessage(LANG_YOURS_SECURITY_IS_LOW);
744        SetSentErrorMessage(true);
745        return false;
746    }
747
748    if(targetPlayer)
749    {
750        ChatHandler(targetPlayer).PSendSysMessage(LANG_YOURS_SECURITY_CHANGED,GetName(), gm);
751        targetPlayer->GetSession()->SetSecurity(gm);
752    }
753
754    PSendSysMessage(LANG_YOU_CHANGE_SECURITY, targetAccountName.c_str(), gm);
755    loginDatabase.PExecute("UPDATE account SET gmlevel = '%i' WHERE id = '%u'", gm, targetAccountId);
756
757    return true;
758}
759
760/// Set password for account
761bool ChatHandler::HandleAccountSetPasswordCommand(const char* args)
762{
763        if(!*args)
764                return false;
765
766        ///- Get the command line arguments
767        char *szAccount = strtok ((char*)args," ");
768        char *szPassword1 =  strtok (NULL," ");
769        char *szPassword2 =  strtok (NULL," ");
770
771        if (!szAccount||!szPassword1 || !szPassword2)
772                return false;
773
774        std::string account_name = szAccount;
775        if(!AccountMgr::normilizeString(account_name))
776        {
777                PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
778                SetSentErrorMessage(true);
779                return false;
780        }
781
782        uint32 targetAccountId = accmgr.GetId(account_name);
783        if (!targetAccountId)
784        {
785                PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
786                SetSentErrorMessage(true);
787                return false;
788        }
789
790        uint32 targetSecurity = accmgr.GetSecurity(targetAccountId);
791
792        /// m_session==NULL only for console
793        uint32 plSecurity = m_session ? m_session->GetSecurity() : SEC_CONSOLE;
794
795        /// can set password only for target with less security
796        /// This is also reject self apply in fact
797        if (targetSecurity >= plSecurity)
798        {
799                SendSysMessage (LANG_YOURS_SECURITY_IS_LOW);
800                SetSentErrorMessage (true);
801                return false;
802        }
803
804        if (strcmp(szPassword1,szPassword2))
805        {
806                SendSysMessage (LANG_NEW_PASSWORDS_NOT_MATCH);
807                SetSentErrorMessage (true);
808                return false;
809        }
810
811        AccountOpResult result = accmgr.ChangePassword(targetAccountId, szPassword1);
812
813        switch(result)
814        {
815                case AOR_OK:
816                        SendSysMessage(LANG_COMMAND_PASSWORD);
817                        break;
818                case AOR_NAME_NOT_EXIST:
819                        PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
820                        SetSentErrorMessage(true);
821                        return false;
822                case AOR_PASS_TOO_LONG:
823                        SendSysMessage(LANG_PASSWORD_TOO_LONG);
824                        SetSentErrorMessage(true);
825                        return false;
826                default:
827                        SendSysMessage(LANG_COMMAND_NOTCHANGEPASSWORD);
828                        SetSentErrorMessage(true);
829                        return false;
830        }
831
832        return true;
833}
834
835bool ChatHandler::HandleAllowMovementCommand(const char* /*args*/)
836{
837    if(sWorld.getAllowMovement())
838    {
839        sWorld.SetAllowMovement(false);
840        SendSysMessage(LANG_CREATURE_MOVE_DISABLED);
841    }
842    else
843    {
844        sWorld.SetAllowMovement(true);
845        SendSysMessage(LANG_CREATURE_MOVE_ENABLED);
846    }
847    return true;
848}
849
850bool ChatHandler::HandleMaxSkillCommand(const char* /*args*/)
851{
852    Player* SelectedPlayer = getSelectedPlayer();
853    if(!SelectedPlayer)
854    {
855        SendSysMessage(LANG_NO_CHAR_SELECTED);
856        SetSentErrorMessage(true);
857        return false;
858    }
859
860    // each skills that have max skill value dependent from level seted to current level max skill value
861    SelectedPlayer->UpdateSkillsToMaxSkillsForLevel();
862    return true;
863}
864
865bool ChatHandler::HandleSetSkillCommand(const char* args)
866{
867    // number or [name] Shift-click form |color|Hskill:skill_id|h[name]|h|r
868    char* skill_p = extractKeyFromLink((char*)args,"Hskill");
869    if(!skill_p)
870        return false;
871
872    char *level_p = strtok (NULL, " ");
873
874    if( !level_p)
875        return false;
876
877    char *max_p   = strtok (NULL, " ");
878
879    int32 skill = atoi(skill_p);
880
881    if (skill <= 0)
882    {
883        PSendSysMessage(LANG_INVALID_SKILL_ID, skill);
884        SetSentErrorMessage(true);
885        return false;
886    }
887
888    int32 level = atol (level_p);
889
890    Player * target = getSelectedPlayer();
891    if(!target)
892    {
893        SendSysMessage(LANG_NO_CHAR_SELECTED);
894        SetSentErrorMessage(true);
895        return false;
896    }
897
898    SkillLineEntry const* sl = sSkillLineStore.LookupEntry(skill);
899    if(!sl)
900    {
901        PSendSysMessage(LANG_INVALID_SKILL_ID, skill);
902        SetSentErrorMessage(true);
903        return false;
904    }
905
906    if(!target->GetSkillValue(skill))
907    {
908        PSendSysMessage(LANG_SET_SKILL_ERROR, target->GetName(), skill, sl->name[0]);
909        SetSentErrorMessage(true);
910        return false;
911    }
912
913    int32 max   = max_p ? atol (max_p) : target->GetPureMaxSkillValue(skill);
914
915    if( level <= 0 || level > max || max <= 0 )
916        return false;
917
918    target->SetSkill(skill, level, max);
919    PSendSysMessage(LANG_SET_SKILL, skill, sl->name[0], target->GetName(), level, max);
920
921    return true;
922}
923
924bool ChatHandler::HandleUnLearnCommand(const char* args)
925{
926    if (!*args)
927        return false;
928
929
930    // number or [name] Shift-click form |color|Hspell:spell_id|h[name]|h|r
931    uint32 min_id = extractSpellIdFromLink((char*)args);
932    if(!min_id)
933        return false;
934
935    // number or [name] Shift-click form |color|Hspell:spell_id|h[name]|h|r
936    char* tail = strtok(NULL,"");
937
938    uint32 max_id = extractSpellIdFromLink(tail);
939
940    if (!max_id)
941    {
942        // number or [name] Shift-click form |color|Hspell:spell_id|h[name]|h|r
943        max_id =  min_id+1;
944    }
945    else
946    {
947        if (max_id < min_id)
948            std::swap(min_id,max_id);
949
950        max_id=max_id+1;
951    }
952
953    Player* target = getSelectedPlayer();
954    if(!target)
955    {
956        SendSysMessage(LANG_NO_CHAR_SELECTED);
957        SetSentErrorMessage(true);
958        return false;
959    }
960
961    for(uint32 spell=min_id;spell<max_id;spell++)
962    {
963        if (target->HasSpell(spell))
964            target->removeSpell(spell);
965        else
966            SendSysMessage(LANG_FORGET_SPELL);
967    }
968
969    return true;
970}
971
972bool ChatHandler::HandleCooldownCommand(const char* args)
973{
974    Player* target = getSelectedPlayer();
975    if(!target)
976    {
977        SendSysMessage(LANG_PLAYER_NOT_FOUND);
978        SetSentErrorMessage(true);
979        return false;
980    }
981
982    if (!*args)
983    {
984        target->RemoveAllSpellCooldown();
985        PSendSysMessage(LANG_REMOVEALL_COOLDOWN, target->GetName());
986    }
987    else
988    {
989        // number or [name] Shift-click form |color|Hspell:spell_id|h[name]|h|r or Htalent form
990        uint32 spell_id = extractSpellIdFromLink((char*)args);
991        if(!spell_id)
992            return false;
993
994        if(!sSpellStore.LookupEntry(spell_id))
995        {
996            PSendSysMessage(LANG_UNKNOWN_SPELL, target==m_session->GetPlayer() ? GetTrinityString(LANG_YOU) : target->GetName());
997            SetSentErrorMessage(true);
998            return false;
999        }
1000
1001        WorldPacket data( SMSG_CLEAR_COOLDOWN, (4+8) );
1002        data << uint32(spell_id);
1003        data << uint64(target->GetGUID());
1004        target->GetSession()->SendPacket(&data);
1005        target->RemoveSpellCooldown(spell_id);
1006        PSendSysMessage(LANG_REMOVE_COOLDOWN, spell_id, target==m_session->GetPlayer() ? GetTrinityString(LANG_YOU) : target->GetName());
1007    }
1008    return true;
1009}
1010
1011bool ChatHandler::HandleLearnAllCommand(const char* /*args*/)
1012{
1013    static const char *allSpellList[] =
1014    {
1015        "3365",
1016        "6233",
1017        "6247",
1018        "6246",
1019        "6477",
1020        "6478",
1021        "22810",
1022        "8386",
1023        "21651",
1024        "21652",
1025        "522",
1026        "7266",
1027        "8597",
1028        "2479",
1029        "22027",
1030        "6603",
1031        "5019",
1032        "133",
1033        "168",
1034        "227",
1035        "5009",
1036        "9078",
1037        "668",
1038        "203",
1039        "20599",
1040        "20600",
1041        "81",
1042        "20597",
1043        "20598",
1044        "20864",
1045        "1459",
1046        "5504",
1047        "587",
1048        "5143",
1049        "118",
1050        "5505",
1051        "597",
1052        "604",
1053        "1449",
1054        "1460",
1055        "2855",
1056        "1008",
1057        "475",
1058        "5506",
1059        "1463",
1060        "12824",
1061        "8437",
1062        "990",
1063        "5145",
1064        "8450",
1065        "1461",
1066        "759",
1067        "8494",
1068        "8455",
1069        "8438",
1070        "6127",
1071        "8416",
1072        "6129",
1073        "8451",
1074        "8495",
1075        "8439",
1076        "3552",
1077        "8417",
1078        "10138",
1079        "12825",
1080        "10169",
1081        "10156",
1082        "10144",
1083        "10191",
1084        "10201",
1085        "10211",
1086        "10053",
1087        "10173",
1088        "10139",
1089        "10145",
1090        "10192",
1091        "10170",
1092        "10202",
1093        "10054",
1094        "10174",
1095        "10193",
1096        "12826",
1097        "2136",
1098        "143",
1099        "145",
1100        "2137",
1101        "2120",
1102        "3140",
1103        "543",
1104        "2138",
1105        "2948",
1106        "8400",
1107        "2121",
1108        "8444",
1109        "8412",
1110        "8457",
1111        "8401",
1112        "8422",
1113        "8445",
1114        "8402",
1115        "8413",
1116        "8458",
1117        "8423",
1118        "8446",
1119        "10148",
1120        "10197",
1121        "10205",
1122        "10149",
1123        "10215",
1124        "10223",
1125        "10206",
1126        "10199",
1127        "10150",
1128        "10216",
1129        "10207",
1130        "10225",
1131        "10151",
1132        "116",
1133        "205",
1134        "7300",
1135        "122",
1136        "837",
1137        "10",
1138        "7301",
1139        "7322",
1140        "6143",
1141        "120",
1142        "865",
1143        "8406",
1144        "6141",
1145        "7302",
1146        "8461",
1147        "8407",
1148        "8492",
1149        "8427",
1150        "8408",
1151        "6131",
1152        "7320",
1153        "10159",
1154        "8462",
1155        "10185",
1156        "10179",
1157        "10160",
1158        "10180",
1159        "10219",
1160        "10186",
1161        "10177",
1162        "10230",
1163        "10181",
1164        "10161",
1165        "10187",
1166        "10220",
1167        "2018",
1168        "2663",
1169        "12260",
1170        "2660",
1171        "3115",
1172        "3326",
1173        "2665",
1174        "3116",
1175        "2738",
1176        "3293",
1177        "2661",
1178        "3319",
1179        "2662",
1180        "9983",
1181        "8880",
1182        "2737",
1183        "2739",
1184        "7408",
1185        "3320",
1186        "2666",
1187        "3323",
1188        "3324",
1189        "3294",
1190        "22723",
1191        "23219",
1192        "23220",
1193        "23221",
1194        "23228",
1195        "23338",
1196        "10788",
1197        "10790",
1198        "5611",
1199        "5016",
1200        "5609",
1201        "2060",
1202        "10963",
1203        "10964",
1204        "10965",
1205        "22593",
1206        "22594",
1207        "596",
1208        "996",
1209        "499",
1210        "768",
1211        "17002",
1212        "1448",
1213        "1082",
1214        "16979",
1215        "1079",
1216        "5215",
1217        "20484",
1218        "5221",
1219        "15590",
1220        "17007",
1221        "6795",
1222        "6807",
1223        "5487",
1224        "1446",
1225        "1066",
1226        "5421",
1227        "3139",
1228        "779",
1229        "6811",
1230        "6808",
1231        "1445",
1232        "5216",
1233        "1737",
1234        "5222",
1235        "5217",
1236        "1432",
1237        "6812",
1238        "9492",
1239        "5210",
1240        "3030",
1241        "1441",
1242        "783",
1243        "6801",
1244        "20739",
1245        "8944",
1246        "9491",
1247        "22569",
1248        "5226",
1249        "6786",
1250        "1433",
1251        "8973",
1252        "1828",
1253        "9495",
1254        "9006",
1255        "6794",
1256        "8993",
1257        "5203",
1258        "16914",
1259        "6784",
1260        "9635",
1261        "22830",
1262        "20722",
1263        "9748",
1264        "6790",
1265        "9753",
1266        "9493",
1267        "9752",
1268        "9831",
1269        "9825",
1270        "9822",
1271        "5204",
1272        "5401",
1273        "22831",
1274        "6793",
1275        "9845",
1276        "17401",
1277        "9882",
1278        "9868",
1279        "20749",
1280        "9893",
1281        "9899",
1282        "9895",
1283        "9832",
1284        "9902",
1285        "9909",
1286        "22832",
1287        "9828",
1288        "9851",
1289        "9883",
1290        "9869",
1291        "17406",
1292        "17402",
1293        "9914",
1294        "20750",
1295        "9897",
1296        "9848",
1297        "3127",
1298        "107",
1299        "204",
1300        "9116",
1301        "2457",
1302        "78",
1303        "18848",
1304        "331",
1305        "403",
1306        "2098",
1307        "1752",
1308        "11278",
1309        "11288",
1310        "11284",
1311        "6461",
1312        "2344",
1313        "2345",
1314        "6463",
1315        "2346",
1316        "2352",
1317        "775",
1318        "1434",
1319        "1612",
1320        "71",
1321        "2468",
1322        "2458",
1323        "2467",
1324        "7164",
1325        "7178",
1326        "7367",
1327        "7376",
1328        "7381",
1329        "21156",
1330        "5209",
1331        "3029",
1332        "5201",
1333        "9849",
1334        "9850",
1335        "20719",
1336        "22568",
1337        "22827",
1338        "22828",
1339        "22829",
1340        "6809",
1341        "8972",
1342        "9005",
1343        "9823",
1344        "9827",
1345        "6783",
1346        "9913",
1347        "6785",
1348        "6787",
1349        "9866",
1350        "9867",
1351        "9894",
1352        "9896",
1353        "6800",
1354        "8992",
1355        "9829",
1356        "9830",
1357        "780",
1358        "769",
1359        "6749",
1360        "6750",
1361        "9755",
1362        "9754",
1363        "9908",
1364        "20745",
1365        "20742",
1366        "20747",
1367        "20748",
1368        "9746",
1369        "9745",
1370        "9880",
1371        "9881",
1372        "5391",
1373        "842",
1374        "3025",
1375        "3031",
1376        "3287",
1377        "3329",
1378        "1945",
1379        "3559",
1380        "4933",
1381        "4934",
1382        "4935",
1383        "4936",
1384        "5142",
1385        "5390",
1386        "5392",
1387        "5404",
1388        "5420",
1389        "6405",
1390        "7293",
1391        "7965",
1392        "8041",
1393        "8153",
1394        "9033",
1395        "9034",
1396        //"9036", problems with ghost state
1397        "16421",
1398        "21653",
1399        "22660",
1400        "5225",
1401        "9846",
1402        "2426",
1403        "5916",
1404        "6634",
1405        //"6718", phasing stealth, annoing for learn all case.
1406        "6719",
1407        "8822",
1408        "9591",
1409        "9590",
1410        "10032",
1411        "17746",
1412        "17747",
1413        "8203",
1414        "11392",
1415        "12495",
1416        "16380",
1417        "23452",
1418        "4079",
1419        "4996",
1420        "4997",
1421        "4998",
1422        "4999",
1423        "5000",
1424        "6348",
1425        "6349",
1426        "6481",
1427        "6482",
1428        "6483",
1429        "6484",
1430        "11362",
1431        "11410",
1432        "11409",
1433        "12510",
1434        "12509",
1435        "12885",
1436        "13142",
1437        "21463",
1438        "23460",
1439        "11421",
1440        "11416",
1441        "11418",
1442        "1851",
1443        "10059",
1444        "11423",
1445        "11417",
1446        "11422",
1447        "11419",
1448        "11424",
1449        "11420",
1450        "27",
1451        "31",
1452        "33",
1453        "34",
1454        "35",
1455        "15125",
1456        "21127",
1457        "22950",
1458        "1180",
1459        "201",
1460        "12593",
1461        "12842",
1462        "16770",
1463        "6057",
1464        "12051",
1465        "18468",
1466        "12606",
1467        "12605",
1468        "18466",
1469        "12502",
1470        "12043",
1471        "15060",
1472        "12042",
1473        "12341",
1474        "12848",
1475        "12344",
1476        "12353",
1477        "18460",
1478        "11366",
1479        "12350",
1480        "12352",
1481        "13043",
1482        "11368",
1483        "11113",
1484        "12400",
1485        "11129",
1486        "16766",
1487        "12573",
1488        "15053",
1489        "12580",
1490        "12475",
1491        "12472",
1492        "12953",
1493        "12488",
1494        "11189",
1495        "12985",
1496        "12519",
1497        "16758",
1498        "11958",
1499        "12490",
1500        "11426",
1501        "3565",
1502        "3562",
1503        "18960",
1504        "3567",
1505        "3561",
1506        "3566",
1507        "3563",
1508        "1953",
1509        "2139",
1510        "12505",
1511        "13018",
1512        "12522",
1513        "12523",
1514        "5146",
1515        "5144",
1516        "5148",
1517        "8419",
1518        "8418",
1519        "10213",
1520        "10212",
1521        "10157",
1522        "12524",
1523        "13019",
1524        "12525",
1525        "13020",
1526        "12526",
1527        "13021",
1528        "18809",
1529        "13031",
1530        "13032",
1531        "13033",
1532        "4036",
1533        "3920",
1534        "3919",
1535        "3918",
1536        "7430",
1537        "3922",
1538        "3923",
1539        "7411",
1540        "7418",
1541        "7421",
1542        "13262",
1543        "7412",
1544        "7415",
1545        "7413",
1546        "7416",
1547        "13920",
1548        "13921",
1549        "7745",
1550        "7779",
1551        "7428",
1552        "7457",
1553        "7857",
1554        "7748",
1555        "7426",
1556        "13421",
1557        "7454",
1558        "13378",
1559        "7788",
1560        "14807",
1561        "14293",
1562        "7795",
1563        "6296",
1564        "20608",
1565        "755",
1566        "444",
1567        "427",
1568        "428",
1569        "442",
1570        "447",
1571        "3578",
1572        "3581",
1573        "19027",
1574        "3580",
1575        "665",
1576        "3579",
1577        "3577",
1578        "6755",
1579        "3576",
1580        "2575",
1581        "2577",
1582        "2578",
1583        "2579",
1584        "2580",
1585        "2656",
1586        "2657",
1587        "2576",
1588        "3564",
1589        "10248",
1590        "8388",
1591        "2659",
1592        "14891",
1593        "3308",
1594        "3307",
1595        "10097",
1596        "2658",
1597        "3569",
1598        "16153",
1599        "3304",
1600        "10098",
1601        "4037",
1602        "3929",
1603        "3931",
1604        "3926",
1605        "3924",
1606        "3930",
1607        "3977",
1608        "3925",
1609        "136",
1610        "228",
1611        "5487",
1612        "43",
1613        "202",
1614        "0"
1615    };
1616
1617    int loop = 0;
1618    while(strcmp(allSpellList[loop], "0"))
1619    {
1620        uint32 spell = atol((char*)allSpellList[loop++]);
1621
1622        if (m_session->GetPlayer()->HasSpell(spell))
1623            continue;
1624
1625        SpellEntry const* spellInfo = sSpellStore.LookupEntry(spell);
1626        if(!spellInfo || !SpellMgr::IsSpellValid(spellInfo,m_session->GetPlayer()))
1627        {
1628            PSendSysMessage(LANG_COMMAND_SPELL_BROKEN,spell);
1629            continue;
1630        }
1631
1632        m_session->GetPlayer()->learnSpell(spell);
1633    }
1634
1635    SendSysMessage(LANG_COMMAND_LEARN_MANY_SPELLS);
1636
1637    return true;
1638}
1639
1640bool ChatHandler::HandleLearnAllGMCommand(const char* /*args*/)
1641{
1642    static const char *gmSpellList[] =
1643    {
1644        "24347",                                            // Become A Fish, No Breath Bar
1645        "35132",                                            // Visual Boom
1646        "38488",                                            // Attack 4000-8000 AOE
1647        "38795",                                            // Attack 2000 AOE + Slow Down 90%
1648        "15712",                                            // Attack 200
1649        "1852",                                             // GM Spell Silence
1650        "31899",                                            // Kill
1651        "31924",                                            // Kill
1652        "29878",                                            // Kill My Self
1653        "26644",                                            // More Kill
1654
1655        "28550",                                            //Invisible 24
1656        "23452",                                            //Invisible + Target
1657        "0"
1658    };
1659
1660    uint16 gmSpellIter = 0;
1661    while( strcmp(gmSpellList[gmSpellIter], "0") )
1662    {
1663        uint32 spell = atol((char*)gmSpellList[gmSpellIter++]);
1664
1665        SpellEntry const* spellInfo = sSpellStore.LookupEntry(spell);
1666        if(!spellInfo || !SpellMgr::IsSpellValid(spellInfo,m_session->GetPlayer()))
1667        {
1668            PSendSysMessage(LANG_COMMAND_SPELL_BROKEN,spell);
1669            continue;
1670        }
1671
1672        m_session->GetPlayer()->learnSpell(spell);
1673    }
1674
1675    SendSysMessage(LANG_LEARNING_GM_SKILLS);
1676    return true;
1677}
1678
1679bool ChatHandler::HandleLearnAllMyClassCommand(const char* /*args*/)
1680{
1681    HandleLearnAllMySpellsCommand("");
1682    HandleLearnAllMyTalentsCommand("");
1683    return true;
1684}
1685
1686bool ChatHandler::HandleLearnAllMySpellsCommand(const char* /*args*/)
1687{
1688    ChrClassesEntry const* clsEntry = sChrClassesStore.LookupEntry(m_session->GetPlayer()->getClass());
1689    if(!clsEntry)
1690        return true;
1691    uint32 family = clsEntry->spellfamily;
1692
1693    for (uint32 i = 0; i < sSpellStore.GetNumRows(); i++)
1694    {
1695        SpellEntry const *spellInfo = sSpellStore.LookupEntry(i);
1696        if(!spellInfo)
1697            continue;
1698
1699        // skip wrong class/race skills
1700        if(!m_session->GetPlayer()->IsSpellFitByClassAndRace(spellInfo->Id))
1701            continue;
1702
1703        // skip other spell families
1704        if( spellInfo->SpellFamilyName != family)
1705            continue;
1706
1707        //TODO: skip triggered spells
1708
1709        // skip spells with first rank learned as talent (and all talents then also)
1710        uint32 first_rank = spellmgr.GetFirstSpellInChain(spellInfo->Id);
1711        if(GetTalentSpellCost(first_rank) > 0 )
1712            continue;
1713
1714        // skip broken spells
1715        if(!SpellMgr::IsSpellValid(spellInfo,m_session->GetPlayer(),false))
1716            continue;
1717
1718        m_session->GetPlayer()->learnSpell(i);
1719    }
1720
1721    SendSysMessage(LANG_COMMAND_LEARN_CLASS_SPELLS);
1722    return true;
1723}
1724
1725static void learnAllHighRanks(Player* player, uint32 spellid)
1726{
1727    SpellChainMapNext const& nextMap = spellmgr.GetSpellChainNext();
1728    for(SpellChainMapNext::const_iterator itr = nextMap.lower_bound(spellid); itr != nextMap.upper_bound(spellid); ++itr)
1729    {
1730        player->learnSpell(itr->second);
1731        learnAllHighRanks(player,itr->second);
1732    }
1733}
1734
1735bool ChatHandler::HandleLearnAllMyTalentsCommand(const char* /*args*/)
1736{
1737    Player* player = m_session->GetPlayer();
1738    uint32 classMask = player->getClassMask();
1739
1740    for (uint32 i = 0; i < sTalentStore.GetNumRows(); i++)
1741    {
1742        TalentEntry const *talentInfo = sTalentStore.LookupEntry(i);
1743        if(!talentInfo)
1744            continue;
1745
1746        TalentTabEntry const *talentTabInfo = sTalentTabStore.LookupEntry( talentInfo->TalentTab );
1747        if(!talentTabInfo)
1748            continue;
1749
1750        if( (classMask & talentTabInfo->ClassMask) == 0 )
1751            continue;
1752
1753        // search highest talent rank
1754        uint32 spellid = 0;
1755        int rank = 4;
1756        for(; rank >= 0; --rank)
1757        {
1758            if(talentInfo->RankID[rank]!=0)
1759            {
1760                spellid = talentInfo->RankID[rank];
1761                break;
1762            }
1763        }
1764
1765        if(!spellid)                                        // ??? none spells in telent
1766            continue;
1767
1768        SpellEntry const* spellInfo = sSpellStore.LookupEntry(spellid);
1769        if(!spellInfo || !SpellMgr::IsSpellValid(spellInfo,m_session->GetPlayer(),false))
1770            continue;
1771
1772        // learn highest rank of talent
1773        player->learnSpell(spellid);
1774
1775        // and learn all non-talent spell ranks (recursive by tree)
1776        learnAllHighRanks(player,spellid);
1777    }
1778
1779    SendSysMessage(LANG_COMMAND_LEARN_CLASS_TALENTS);
1780    return true;
1781}
1782
1783bool ChatHandler::HandleLearnAllLangCommand(const char* /*args*/)
1784{
1785    // skipping UNIVERSAL language (0)
1786    for(int i = 1; i < LANGUAGES_COUNT; ++i)
1787        m_session->GetPlayer()->learnSpell(lang_description[i].spell_id);
1788
1789    SendSysMessage(LANG_COMMAND_LEARN_ALL_LANG);
1790    return true;
1791}
1792
1793bool ChatHandler::HandleLearnAllDefaultCommand(const char* args)
1794{
1795    char* pName = strtok((char*)args, "");
1796    Player *player = NULL;
1797    if (pName)
1798    {
1799        std::string name = pName;
1800
1801        if(!normalizePlayerName(name))
1802        {
1803            SendSysMessage(LANG_PLAYER_NOT_FOUND);
1804            SetSentErrorMessage(true);
1805            return false;
1806        }
1807
1808        player = objmgr.GetPlayer(name.c_str());
1809    }
1810    else
1811        player = getSelectedPlayer();
1812
1813    if(!player)
1814    {
1815        SendSysMessage(LANG_NO_CHAR_SELECTED);
1816        SetSentErrorMessage(true);
1817        return false;
1818    }
1819
1820    player->learnDefaultSpells();
1821    player->learnQuestRewardedSpells();
1822
1823    PSendSysMessage(LANG_COMMAND_LEARN_ALL_DEFAULT_AND_QUEST,player->GetName());
1824    return true;
1825}
1826
1827bool ChatHandler::HandleLearnCommand(const char* args)
1828{
1829    Player* targetPlayer = getSelectedPlayer();
1830
1831    if(!targetPlayer)
1832    {
1833        SendSysMessage(LANG_PLAYER_NOT_FOUND);
1834        SetSentErrorMessage(true);
1835        return false;
1836    }
1837
1838    // number or [name] Shift-click form |color|Hspell:spell_id|h[name]|h|r or Htalent form
1839    uint32 spell = extractSpellIdFromLink((char*)args);
1840    if(!spell || !sSpellStore.LookupEntry(spell))
1841        return false;
1842
1843    if (targetPlayer->HasSpell(spell))
1844    {
1845        if(targetPlayer == m_session->GetPlayer())
1846            SendSysMessage(LANG_YOU_KNOWN_SPELL);
1847        else
1848            PSendSysMessage(LANG_TARGET_KNOWN_SPELL,targetPlayer->GetName());
1849        SetSentErrorMessage(true);
1850        return false;
1851    }
1852
1853    SpellEntry const* spellInfo = sSpellStore.LookupEntry(spell);
1854    if(!spellInfo || !SpellMgr::IsSpellValid(spellInfo,m_session->GetPlayer()))
1855    {
1856        PSendSysMessage(LANG_COMMAND_SPELL_BROKEN,spell);
1857        SetSentErrorMessage(true);
1858        return false;
1859    }
1860
1861    targetPlayer->learnSpell(spell);
1862
1863    return true;
1864}
1865
1866bool ChatHandler::HandleAddItemCommand(const char* args)
1867{
1868    if (!*args)
1869        return false;
1870
1871    uint32 itemId = 0;
1872
1873    if(args[0]=='[')                                        // [name] manual form
1874    {
1875        char* citemName = citemName = strtok((char*)args, "]");
1876
1877        if(citemName && citemName[0])
1878        {
1879            std::string itemName = citemName+1;
1880            WorldDatabase.escape_string(itemName);
1881            QueryResult *result = WorldDatabase.PQuery("SELECT entry FROM item_template WHERE name = '%s'", itemName.c_str());
1882            if (!result)
1883            {
1884                PSendSysMessage(LANG_COMMAND_COULDNOTFIND, citemName+1);
1885                SetSentErrorMessage(true);
1886                return false;
1887            }
1888            itemId = result->Fetch()->GetUInt16();
1889            delete result;
1890        }
1891        else
1892            return false;
1893    }
1894    else                                                    // item_id or [name] Shift-click form |color|Hitem:item_id:0:0:0|h[name]|h|r
1895    {
1896        char* cId = extractKeyFromLink((char*)args,"Hitem");
1897        if(!cId)
1898            return false;
1899        itemId = atol(cId);
1900    }
1901
1902    char* ccount = strtok(NULL, " ");
1903
1904    int32 count = 1;
1905
1906    if (ccount)
1907        count = strtol(ccount, NULL, 10);
1908
1909    if (count == 0)
1910        count = 1;
1911
1912    Player* pl = m_session->GetPlayer();
1913    Player* plTarget = getSelectedPlayer();
1914    if(!plTarget)
1915        plTarget = pl;
1916
1917    sLog.outDetail(GetTrinityString(LANG_ADDITEM), itemId, count);
1918
1919    ItemPrototype const *pProto = objmgr.GetItemPrototype(itemId);
1920    if(!pProto)
1921    {
1922        PSendSysMessage(LANG_COMMAND_ITEMIDINVALID, itemId);
1923        SetSentErrorMessage(true);
1924        return false;
1925    }
1926
1927    //Subtract
1928    if (count < 0)
1929    {
1930        plTarget->DestroyItemCount(itemId, -count, true, false);
1931        PSendSysMessage(LANG_REMOVEITEM, itemId, -count, plTarget->GetName());
1932        return true;
1933    }
1934
1935    //Adding items
1936    uint32 noSpaceForCount = 0;
1937
1938    // check space and find places
1939    ItemPosCountVec dest;
1940    uint8 msg = plTarget->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, itemId, count, &noSpaceForCount );
1941    if( msg != EQUIP_ERR_OK )                               // convert to possible store amount
1942        count -= noSpaceForCount;
1943
1944    if( count == 0 || dest.empty())                         // can't add any
1945    {
1946        PSendSysMessage(LANG_ITEM_CANNOT_CREATE, itemId, noSpaceForCount );
1947        SetSentErrorMessage(true);
1948        return false;
1949    }
1950
1951    Item* item = plTarget->StoreNewItem( dest, itemId, true, Item::GenerateItemRandomPropertyId(itemId));
1952
1953    // remove binding (let GM give it to another player later)
1954    if(pl==plTarget)
1955        for(ItemPosCountVec::const_iterator itr = dest.begin(); itr != dest.end(); ++itr)
1956            if(Item* item1 = pl->GetItemByPos(itr->pos))
1957                item1->SetBinding( false );
1958
1959    if(count > 0 && item)
1960    {
1961        pl->SendNewItem(item,count,false,true);
1962        if(pl!=plTarget)
1963            plTarget->SendNewItem(item,count,true,false);
1964    }
1965
1966    if(noSpaceForCount > 0)
1967        PSendSysMessage(LANG_ITEM_CANNOT_CREATE, itemId, noSpaceForCount);
1968
1969    return true;
1970}
1971
1972bool ChatHandler::HandleAddItemSetCommand(const char* args)
1973{
1974    if (!*args)
1975        return false;
1976
1977    char* cId = extractKeyFromLink((char*)args,"Hitemset"); // number or [name] Shift-click form |color|Hitemset:itemset_id|h[name]|h|r
1978    if (!cId)
1979        return false;
1980
1981    uint32 itemsetId = atol(cId);
1982
1983    // prevent generation all items with itemset field value '0'
1984    if (itemsetId == 0)
1985    {
1986        PSendSysMessage(LANG_NO_ITEMS_FROM_ITEMSET_FOUND,itemsetId);
1987        SetSentErrorMessage(true);
1988        return false;
1989    }
1990
1991    Player* pl = m_session->GetPlayer();
1992    Player* plTarget = getSelectedPlayer();
1993    if(!plTarget)
1994        plTarget = pl;
1995
1996    sLog.outDetail(GetTrinityString(LANG_ADDITEMSET), itemsetId);
1997
1998    QueryResult *result = WorldDatabase.PQuery("SELECT entry FROM item_template WHERE itemset = %u",itemsetId);
1999
2000    if(!result)
2001    {
2002        PSendSysMessage(LANG_NO_ITEMS_FROM_ITEMSET_FOUND,itemsetId);
2003
2004        SetSentErrorMessage(true);
2005        return false;
2006    }
2007
2008    do
2009    {
2010        Field *fields = result->Fetch();
2011        uint32 itemId = fields[0].GetUInt32();
2012
2013        ItemPosCountVec dest;
2014        uint8 msg = plTarget->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, itemId, 1 );
2015        if( msg == EQUIP_ERR_OK )
2016        {
2017            Item* item = plTarget->StoreNewItem( dest, itemId, true);
2018
2019            // remove binding (let GM give it to another player later)
2020            if(pl==plTarget)
2021                item->SetBinding( false );
2022
2023            pl->SendNewItem(item,1,false,true);
2024            if(pl!=plTarget)
2025                plTarget->SendNewItem(item,1,true,false);
2026        }
2027        else
2028        {
2029            pl->SendEquipError( msg, NULL, NULL );
2030            PSendSysMessage(LANG_ITEM_CANNOT_CREATE, itemId, 1);
2031        }
2032
2033    }while( result->NextRow() );
2034
2035    delete result;
2036
2037    return true;
2038}
2039
2040bool ChatHandler::HandleListItemCommand(const char* args)
2041{
2042    if(!*args)
2043        return false;
2044
2045    char* cId = extractKeyFromLink((char*)args,"Hitem");
2046    if(!cId)
2047        return false;
2048
2049    uint32 item_id = atol(cId);
2050        if(!item_id)
2051        {
2052                PSendSysMessage(LANG_COMMAND_ITEMIDINVALID, item_id);
2053                SetSentErrorMessage(true);
2054                return false;
2055        }
2056
2057        ItemPrototype const* itemProto = objmgr.GetItemPrototype(item_id);
2058    if(!itemProto)
2059    {
2060        PSendSysMessage(LANG_COMMAND_ITEMIDINVALID, item_id);
2061        SetSentErrorMessage(true);
2062        return false;
2063    }
2064
2065    char* c_count = strtok(NULL, " ");
2066    int count = c_count ? atol(c_count) : 10;
2067
2068    if(count < 0)
2069        return false;
2070
2071    QueryResult *result;
2072
2073    // inventory case
2074    uint32 inv_count = 0;
2075    result=CharacterDatabase.PQuery("SELECT COUNT(item_template) FROM character_inventory WHERE item_template='%u'",item_id);
2076    if(result)
2077    {
2078        inv_count = (*result)[0].GetUInt32();
2079        delete result;
2080    }
2081
2082    result=CharacterDatabase.PQuery(
2083    //          0        1             2             3        4                  5
2084        "SELECT ci.item, cibag.slot AS bag, ci.slot, ci.guid, characters.account,characters.name "
2085        "FROM character_inventory AS ci LEFT JOIN character_inventory AS cibag ON (cibag.item=ci.bag),characters "
2086        "WHERE ci.item_template='%u' AND ci.guid = characters.guid LIMIT %u ",
2087        item_id,uint32(count));
2088
2089    if(result)
2090    {
2091        do
2092        {
2093            Field *fields = result->Fetch();
2094            uint32 item_guid = fields[0].GetUInt32();
2095            uint32 item_bag = fields[1].GetUInt32();
2096            uint32 item_slot = fields[2].GetUInt32();
2097            uint32 owner_guid = fields[3].GetUInt32();
2098            uint32 owner_acc = fields[4].GetUInt32();
2099            std::string owner_name = fields[5].GetCppString();
2100
2101            char const* item_pos = 0;
2102            if(Player::IsEquipmentPos(item_bag,item_slot))
2103                item_pos = "[equipped]";
2104            else if(Player::IsInventoryPos(item_bag,item_slot))
2105                item_pos = "[in inventory]";
2106            else if(Player::IsBankPos(item_bag,item_slot))
2107                item_pos = "[in bank]";
2108            else
2109                item_pos = "";
2110
2111            PSendSysMessage(LANG_ITEMLIST_SLOT,
2112                item_guid,owner_name.c_str(),owner_guid,owner_acc,item_pos);
2113        } while (result->NextRow());
2114
2115        int64 res_count = result->GetRowCount();
2116
2117        delete result;
2118
2119        if(count > res_count)
2120            count-=res_count;
2121        else if(count)
2122            count = 0;
2123    }
2124
2125    // mail case
2126    uint32 mail_count = 0;
2127    result=CharacterDatabase.PQuery("SELECT COUNT(item_template) FROM mail_items WHERE item_template='%u'", item_id);
2128    if(result)
2129    {
2130        mail_count = (*result)[0].GetUInt32();
2131        delete result;
2132    }
2133
2134    if(count > 0)
2135    {
2136        result=CharacterDatabase.PQuery(
2137        //          0                     1            2              3               4            5               6
2138            "SELECT mail_items.item_guid, mail.sender, mail.receiver, char_s.account, char_s.name, char_r.account, char_r.name "
2139            "FROM mail,mail_items,characters as char_s,characters as char_r "
2140            "WHERE mail_items.item_template='%u' AND char_s.guid = mail.sender AND char_r.guid = mail.receiver AND mail.id=mail_items.mail_id LIMIT %u",
2141            item_id,uint32(count));
2142    }
2143    else
2144        result = NULL;
2145
2146    if(result)
2147    {
2148        do
2149        {
2150            Field *fields = result->Fetch();
2151            uint32 item_guid        = fields[0].GetUInt32();
2152            uint32 item_s           = fields[1].GetUInt32();
2153            uint32 item_r           = fields[2].GetUInt32();
2154            uint32 item_s_acc       = fields[3].GetUInt32();
2155            std::string item_s_name = fields[4].GetCppString();
2156            uint32 item_r_acc       = fields[5].GetUInt32();
2157            std::string item_r_name = fields[6].GetCppString();
2158
2159            char const* item_pos = "[in mail]";
2160
2161            PSendSysMessage(LANG_ITEMLIST_MAIL,
2162                item_guid,item_s_name.c_str(),item_s,item_s_acc,item_r_name.c_str(),item_r,item_r_acc,item_pos);
2163        } while (result->NextRow());
2164
2165        int64 res_count = result->GetRowCount();
2166
2167        delete result;
2168
2169        if(count > res_count)
2170            count-=res_count;
2171        else if(count)
2172            count = 0;
2173    }
2174
2175    // auction case
2176    uint32 auc_count = 0;
2177    result=CharacterDatabase.PQuery("SELECT COUNT(item_template) FROM auctionhouse WHERE item_template='%u'",item_id);
2178    if(result)
2179    {
2180        auc_count = (*result)[0].GetUInt32();
2181        delete result;
2182    }
2183
2184    if(count > 0)
2185    {
2186        result=CharacterDatabase.PQuery(
2187        //           0                      1                       2                   3
2188            "SELECT  auctionhouse.itemguid, auctionhouse.itemowner, characters.account, characters.name "
2189            "FROM auctionhouse,characters WHERE auctionhouse.item_template='%u' AND characters.guid = auctionhouse.itemowner LIMIT %u",
2190            item_id,uint32(count));
2191    }
2192    else
2193        result = NULL;
2194
2195    if(result)
2196    {
2197        do
2198        {
2199            Field *fields = result->Fetch();
2200            uint32 item_guid       = fields[0].GetUInt32();
2201            uint32 owner           = fields[1].GetUInt32();
2202            uint32 owner_acc       = fields[2].GetUInt32();
2203            std::string owner_name = fields[3].GetCppString();
2204
2205            char const* item_pos = "[in auction]";
2206
2207            PSendSysMessage(LANG_ITEMLIST_AUCTION, item_guid, owner_name.c_str(), owner, owner_acc,item_pos);
2208        } while (result->NextRow());
2209
2210        delete result;
2211    }
2212
2213    // guild bank case
2214        uint32 guild_count = 0;
2215        result=CharacterDatabase.PQuery("SELECT COUNT(item_entry) FROM guild_bank_item WHERE item_entry='%u'",item_id);
2216        if(result)
2217        {
2218                guild_count = (*result)[0].GetUInt32();
2219                delete result;
2220        }
2221
2222        result=CharacterDatabase.PQuery(
2223                //      0             1           2
2224                "SELECT gi.item_guid, gi.guildid, guild.name "
2225                "FROM guild_bank_item AS gi, guild WHERE gi.item_entry='%u' AND gi.guildid = guild.guildid LIMIT %u ",
2226                item_id,uint32(count));
2227
2228        if(result)
2229        {
2230                do
2231                {
2232                        Field *fields = result->Fetch();
2233                        uint32 item_guid = fields[0].GetUInt32();
2234                        uint32 guild_guid = fields[1].GetUInt32();
2235                        std::string guild_name = fields[2].GetCppString();
2236
2237                        char const* item_pos = "[in guild bank]";
2238
2239                        PSendSysMessage(LANG_ITEMLIST_GUILD,item_guid,guild_name.c_str(),guild_guid,item_pos);
2240                } while (result->NextRow());
2241
2242                int64 res_count = result->GetRowCount();
2243
2244                delete result;
2245
2246                if(count > res_count)
2247                        count-=res_count;
2248                else if(count)
2249                        count = 0;
2250        }
2251
2252        if(inv_count+mail_count+auc_count+guild_count == 0)
2253    {
2254        SendSysMessage(LANG_COMMAND_NOITEMFOUND);
2255        SetSentErrorMessage(true);
2256        return false;
2257    }
2258
2259    PSendSysMessage(LANG_COMMAND_LISTITEMMESSAGE,item_id,inv_count+mail_count+auc_count+guild_count,inv_count,mail_count,auc_count,guild_count);
2260
2261    return true;
2262}
2263
2264bool ChatHandler::HandleListObjectCommand(const char* args)
2265{
2266    if(!*args)
2267        return false;
2268
2269    // number or [name] Shift-click form |color|Hgameobject_entry:go_id|h[name]|h|r
2270    char* cId = extractKeyFromLink((char*)args,"Hgameobject_entry");
2271    if(!cId)
2272        return false;
2273
2274    uint32 go_id = atol(cId);
2275        if(!go_id)
2276        {
2277                PSendSysMessage(LANG_COMMAND_LISTOBJINVALIDID, go_id);
2278                SetSentErrorMessage(true);
2279                return false;
2280        }
2281
2282    GameObjectInfo const * gInfo = objmgr.GetGameObjectInfo(go_id);
2283        if(!gInfo)
2284    {
2285        PSendSysMessage(LANG_COMMAND_LISTOBJINVALIDID, go_id);
2286        SetSentErrorMessage(true);
2287        return false;
2288    }
2289
2290    char* c_count = strtok(NULL, " ");
2291    int count = c_count ? atol(c_count) : 10;
2292
2293    if(count < 0)
2294        return false;
2295
2296    QueryResult *result;
2297
2298    uint32 obj_count = 0;
2299    result=WorldDatabase.PQuery("SELECT COUNT(guid) FROM gameobject WHERE id='%u'",go_id);
2300    if(result)
2301    {
2302        obj_count = (*result)[0].GetUInt32();
2303        delete result;
2304    }
2305
2306    if(m_session)
2307        {
2308                Player* pl = m_session->GetPlayer();
2309                result = WorldDatabase.PQuery("SELECT guid, position_x, position_y, position_z, map, (POW(position_x - '%f', 2) + POW(position_y - '%f', 2) + POW(position_z - '%f', 2)) AS order_ FROM gameobject WHERE id = '%u' ORDER BY order_ ASC LIMIT %u",
2310                        pl->GetPositionX(), pl->GetPositionY(), pl->GetPositionZ(),go_id,uint32(count));
2311        }
2312        else
2313                result = WorldDatabase.PQuery("SELECT guid, position_x, position_y, position_z, map FROM gameobject WHERE id = '%u' LIMIT %u",
2314                go_id,uint32(count));
2315
2316    if (result)
2317    {
2318        do
2319        {
2320            Field *fields = result->Fetch();
2321            uint32 guid = fields[0].GetUInt32();
2322            float x = fields[1].GetFloat();
2323            float y = fields[2].GetFloat();
2324            float z = fields[3].GetFloat();
2325            int mapid = fields[4].GetUInt16();
2326
2327            if (m_session)
2328                                PSendSysMessage(LANG_GO_LIST_CHAT, guid, guid, gInfo->name, x, y, z, mapid);
2329                        else
2330                                PSendSysMessage(LANG_GO_LIST_CONSOLE, guid, gInfo->name, x, y, z, mapid);
2331        } while (result->NextRow());
2332
2333        delete result;
2334    }
2335
2336    PSendSysMessage(LANG_COMMAND_LISTOBJMESSAGE,go_id,obj_count);
2337    return true;
2338}
2339
2340bool ChatHandler::HandleNearObjectCommand(const char* args)
2341{
2342    float distance = (!*args) ? 10 : atol(args);
2343    uint32 count = 0;
2344
2345    Player* pl = m_session->GetPlayer();
2346    QueryResult *result = WorldDatabase.PQuery("SELECT guid, id, position_x, position_y, position_z, map, "
2347        "(POW(position_x - '%f', 2) + POW(position_y - '%f', 2) + POW(position_z - '%f', 2)) AS order_ "
2348        "FROM gameobject WHERE map='%u' AND (POW(position_x - '%f', 2) + POW(position_y - '%f', 2) + POW(position_z - '%f', 2)) <= '%f' ORDER BY order_",
2349        pl->GetPositionX(), pl->GetPositionY(), pl->GetPositionZ(),
2350        pl->GetMapId(),pl->GetPositionX(), pl->GetPositionY(), pl->GetPositionZ(),distance*distance);
2351
2352    if (result)
2353    {
2354        do
2355        {
2356            Field *fields = result->Fetch();
2357            uint32 guid = fields[0].GetUInt32();
2358            uint32 entry = fields[1].GetUInt32();
2359            float x = fields[2].GetFloat();
2360            float y = fields[3].GetFloat();
2361            float z = fields[4].GetFloat();
2362            int mapid = fields[5].GetUInt16();
2363
2364            GameObjectInfo const * gInfo = objmgr.GetGameObjectInfo(entry);
2365
2366            if(!gInfo)
2367                continue;
2368
2369            PSendSysMessage(LANG_GO_LIST_CHAT, guid, guid, gInfo->name, x, y, z, mapid);
2370
2371            ++count;
2372        } while (result->NextRow());
2373
2374        delete result;
2375    }
2376
2377    PSendSysMessage(LANG_COMMAND_NEAROBJMESSAGE,distance,count);
2378    return true;
2379}
2380
2381bool ChatHandler::HandleObjectStateCommand(const char* args)
2382{
2383    // number or [name] Shift-click form |color|Hgameobject:go_id|h[name]|h|r
2384    char* cId = extractKeyFromLink((char*)args, "Hgameobject");
2385    if(!cId)
2386        return false;
2387
2388    uint32 lowguid = atoi(cId);
2389    if(!lowguid)
2390        return false;
2391
2392    GameObject* gobj = NULL;
2393
2394    if(GameObjectData const* goData = objmgr.GetGOData(lowguid))
2395        gobj = GetObjectGlobalyWithGuidOrNearWithDbGuid(lowguid, goData->id);
2396
2397    if(!gobj)
2398    {
2399        PSendSysMessage(LANG_COMMAND_OBJNOTFOUND, lowguid);
2400        SetSentErrorMessage(true);
2401        return false;
2402    }
2403
2404    char* cstate = strtok(NULL, " ");
2405    if(!cstate)
2406        return false;
2407
2408    int32 state = atoi(cstate);
2409    if(state < 0)
2410        gobj->SendObjectDeSpawnAnim(gobj->GetGUID());
2411    else
2412        gobj->SetGoState(state);
2413
2414    return true;
2415
2416    return true;
2417}
2418
2419bool ChatHandler::HandleListCreatureCommand(const char* args)
2420{
2421    if(!*args)
2422        return false;
2423
2424    // number or [name] Shift-click form |color|Hcreature_entry:creature_id|h[name]|h|r
2425    char* cId = extractKeyFromLink((char*)args,"Hcreature_entry");
2426    if(!cId)
2427        return false;
2428
2429    uint32 cr_id = atol(cId);
2430        if(!cr_id)
2431        {
2432                PSendSysMessage(LANG_COMMAND_INVALIDCREATUREID, cr_id);
2433                SetSentErrorMessage(true);
2434                return false;
2435        }
2436
2437    CreatureInfo const* cInfo = objmgr.GetCreatureTemplate(cr_id);
2438        if(!cInfo)
2439    {
2440        PSendSysMessage(LANG_COMMAND_INVALIDCREATUREID, cr_id);
2441        SetSentErrorMessage(true);
2442        return false;
2443    }
2444
2445    char* c_count = strtok(NULL, " ");
2446    int count = c_count ? atol(c_count) : 10;
2447
2448    if(count < 0)
2449        return false;
2450
2451    QueryResult *result;
2452
2453    uint32 cr_count = 0;
2454    result=WorldDatabase.PQuery("SELECT COUNT(guid) FROM creature WHERE id='%u'",cr_id);
2455    if(result)
2456    {
2457        cr_count = (*result)[0].GetUInt32();
2458        delete result;
2459    }
2460
2461    if(m_session)
2462        {
2463                Player* pl = m_session->GetPlayer();
2464                result = WorldDatabase.PQuery("SELECT guid, position_x, position_y, position_z, map, (POW(position_x - '%f', 2) + POW(position_y - '%f', 2) + POW(position_z - '%f', 2)) AS order_ FROM creature WHERE id = '%u' ORDER BY order_ ASC LIMIT %u",
2465                        pl->GetPositionX(), pl->GetPositionY(), pl->GetPositionZ(), cr_id,uint32(count));
2466        }
2467        else
2468                result = WorldDatabase.PQuery("SELECT guid, position_x, position_y, position_z, map FROM creature WHERE id = '%u' LIMIT %u",
2469                        cr_id,uint32(count));
2470
2471    if (result)
2472    {
2473        do
2474        {
2475            Field *fields = result->Fetch();
2476            uint32 guid = fields[0].GetUInt32();
2477            float x = fields[1].GetFloat();
2478            float y = fields[2].GetFloat();
2479            float z = fields[3].GetFloat();
2480            int mapid = fields[4].GetUInt16();
2481
2482            if  (m_session)
2483                                PSendSysMessage(LANG_CREATURE_LIST_CHAT, guid, guid, cInfo->Name, x, y, z, mapid);
2484                        else
2485                                PSendSysMessage(LANG_CREATURE_LIST_CONSOLE, guid, cInfo->Name, x, y, z, mapid);
2486        } while (result->NextRow());
2487
2488        delete result;
2489    }
2490
2491    PSendSysMessage(LANG_COMMAND_LISTCREATUREMESSAGE,cr_id,cr_count);
2492    return true;
2493}
2494
2495bool ChatHandler::HandleLookupItemCommand(const char* args)
2496{
2497    if(!*args)
2498        return false;
2499
2500    std::string namepart = args;
2501    std::wstring wnamepart;
2502
2503    // converting string that we try to find to lower case
2504    if(!Utf8toWStr(namepart,wnamepart))
2505        return false;
2506
2507    wstrToLower(wnamepart);
2508
2509    uint32 counter = 0;
2510
2511    // Search in `item_template`
2512    for (uint32 id = 0; id < sItemStorage.MaxEntry; id++)
2513    {
2514        ItemPrototype const *pProto = sItemStorage.LookupEntry<ItemPrototype >(id);
2515        if(!pProto)
2516            continue;
2517
2518        int loc_idx = m_session ? m_session->GetSessionDbLocaleIndex() : objmgr.GetDBCLocaleIndex();
2519        if ( loc_idx >= 0 )
2520        {
2521            ItemLocale const *il = objmgr.GetItemLocale(pProto->ItemId);
2522            if (il)
2523            {
2524                if (il->Name.size() > loc_idx && !il->Name[loc_idx].empty())
2525                {
2526                    std::string name = il->Name[loc_idx];
2527
2528                    if (Utf8FitTo(name, wnamepart))
2529                    {
2530                        if (m_session)
2531                                                        PSendSysMessage(LANG_ITEM_LIST_CHAT, id, id, name.c_str());
2532                                                else
2533                                                        PSendSysMessage(LANG_ITEM_LIST_CONSOLE, id, name.c_str());
2534                        ++counter;
2535                        continue;
2536                    }
2537                }
2538            }
2539        }
2540
2541        std::string name = pProto->Name1;
2542        if(name.empty())
2543            continue;
2544
2545        if (Utf8FitTo(name, wnamepart))
2546        {
2547            if (m_session)
2548                                PSendSysMessage(LANG_ITEM_LIST_CHAT, id, id, name.c_str());
2549                        else
2550                                PSendSysMessage(LANG_ITEM_LIST_CONSOLE, id, name.c_str());
2551            ++counter;
2552        }
2553    }
2554
2555    if (counter==0)
2556        SendSysMessage(LANG_COMMAND_NOITEMFOUND);
2557
2558    return true;
2559}
2560
2561bool ChatHandler::HandleLookupItemSetCommand(const char* args)
2562{
2563    if(!*args)
2564        return false;
2565
2566    std::string namepart = args;
2567    std::wstring wnamepart;
2568
2569    if(!Utf8toWStr(namepart,wnamepart))
2570        return false;
2571
2572    // converting string that we try to find to lower case
2573    wstrToLower( wnamepart );
2574
2575    uint32 counter = 0;                                     // Counter for figure out that we found smth.
2576
2577    // Search in ItemSet.dbc
2578    for (uint32 id = 0; id < sItemSetStore.GetNumRows(); id++)
2579    {
2580        ItemSetEntry const *set = sItemSetStore.LookupEntry(id);
2581        if(set)
2582        {
2583            int loc = m_session ? m_session->GetSessionDbcLocale() : sWorld.GetDefaultDbcLocale();
2584                        std::string name = set->name[loc];
2585            if(name.empty())
2586                continue;
2587
2588            if (!Utf8FitTo(name, wnamepart))
2589            {
2590                loc = 0;
2591                for(; loc < MAX_LOCALE; ++loc)
2592                {
2593                    if(m_session && loc==m_session->GetSessionDbcLocale())
2594                        continue;
2595
2596                    name = set->name[loc];
2597                    if(name.empty())
2598                        continue;
2599
2600                    if (Utf8FitTo(name, wnamepart))
2601                        break;
2602                }
2603            }
2604
2605            if(loc < MAX_LOCALE)
2606            {
2607                // send item set in "id - [namedlink locale]" format
2608                if (m_session)
2609                                        PSendSysMessage(LANG_ITEMSET_LIST_CHAT,id,id,name.c_str(),localeNames[loc]);
2610                                else
2611                                        PSendSysMessage(LANG_ITEMSET_LIST_CONSOLE,id,name.c_str(),localeNames[loc]);
2612                ++counter;
2613            }
2614        }
2615    }
2616    if (counter == 0)                                       // if counter == 0 then we found nth
2617        SendSysMessage(LANG_COMMAND_NOITEMSETFOUND);
2618    return true;
2619}
2620
2621bool ChatHandler::HandleLookupSkillCommand(const char* args)
2622{
2623        if (!*args)
2624        return false;
2625
2626        // can be NULL in console call
2627        Player* target = getSelectedPlayer();
2628
2629    std::string namepart = args;
2630    std::wstring wnamepart;
2631
2632    if(!Utf8toWStr(namepart,wnamepart))
2633        return false;
2634
2635    // converting string that we try to find to lower case
2636    wstrToLower( wnamepart );
2637
2638    uint32 counter = 0;                                     // Counter for figure out that we found smth.
2639
2640    // Search in SkillLine.dbc
2641    for (uint32 id = 0; id < sSkillLineStore.GetNumRows(); id++)
2642    {
2643        SkillLineEntry const *skillInfo = sSkillLineStore.LookupEntry(id);
2644        if(skillInfo)
2645        {
2646            int loc = m_session ? m_session->GetSessionDbcLocale() : sWorld.GetDefaultDbcLocale();
2647            std::string name = skillInfo->name[loc];
2648            if(name.empty())
2649                continue;
2650
2651            if (!Utf8FitTo(name, wnamepart))
2652            {
2653                loc = 0;
2654                for(; loc < MAX_LOCALE; ++loc)
2655                {
2656                    if(m_session && loc==m_session->GetSessionDbcLocale())
2657                        continue;
2658
2659                    name = skillInfo->name[loc];
2660                    if(name.empty())
2661                        continue;
2662
2663                    if (Utf8FitTo(name, wnamepart))
2664                        break;
2665                }
2666            }
2667
2668            if(loc < MAX_LOCALE)
2669            {
2670                                char const* knownStr = "";
2671                                if(target && target->HasSkill(id))
2672                                        knownStr = GetTrinityString(LANG_KNOWN);
2673
2674                // send skill in "id - [namedlink locale]" format
2675                if (m_session)
2676                                        PSendSysMessage(LANG_SKILL_LIST_CHAT,id,id,name.c_str(),localeNames[loc],knownStr);
2677                                else
2678                                        PSendSysMessage(LANG_SKILL_LIST_CONSOLE,id,name.c_str(),localeNames[loc],knownStr);
2679
2680                ++counter;
2681            }
2682        }
2683    }
2684    if (counter == 0)                                       // if counter == 0 then we found nth
2685        SendSysMessage(LANG_COMMAND_NOSKILLFOUND);
2686    return true;
2687}
2688
2689bool ChatHandler::HandleLookupSpellCommand(const char* args)
2690{
2691        if (!*args)
2692        return false;
2693
2694        // can be NULL at console call
2695        Player* target = getSelectedPlayer();
2696
2697    std::string namepart = args;
2698    std::wstring wnamepart;
2699
2700    if(!Utf8toWStr(namepart,wnamepart))
2701        return false;
2702
2703    // converting string that we try to find to lower case
2704    wstrToLower( wnamepart );
2705
2706    uint32 counter = 0;                                     // Counter for figure out that we found smth.
2707
2708    // Search in Spell.dbc
2709    for (uint32 id = 0; id < sSpellStore.GetNumRows(); id++)
2710    {
2711        SpellEntry const *spellInfo = sSpellStore.LookupEntry(id);
2712        if(spellInfo)
2713        {
2714            int loc = m_session ? m_session->GetSessionDbcLocale() : sWorld.GetDefaultDbcLocale();
2715            std::string name = spellInfo->SpellName[loc];
2716            if(name.empty())
2717                continue;
2718
2719            if (!Utf8FitTo(name, wnamepart))
2720            {
2721                loc = 0;
2722                for(; loc < MAX_LOCALE; ++loc)
2723                {
2724                    if(m_session && loc==m_session->GetSessionDbcLocale())
2725                        continue;
2726
2727                    name = spellInfo->SpellName[loc];
2728                    if(name.empty())
2729                        continue;
2730
2731                    if (Utf8FitTo(name, wnamepart))
2732                        break;
2733                }
2734            }
2735
2736            if(loc < MAX_LOCALE)
2737            {
2738                bool known = target && target->HasSpell(id);
2739                bool learn = (spellInfo->Effect[0] == SPELL_EFFECT_LEARN_SPELL);
2740
2741                uint32 telentCost = GetTalentSpellCost(id);
2742
2743                bool talent = (telentCost > 0);
2744                bool passive = IsPassiveSpell(id);
2745                bool active = target && (target->HasAura(id,0) || target->HasAura(id,1) || target->HasAura(id,2));
2746
2747                // unit32 used to prevent interpreting uint8 as char at output
2748                // find rank of learned spell for learning spell, or talent rank
2749                uint32 rank = telentCost ? telentCost : spellmgr.GetSpellRank(learn ? spellInfo->EffectTriggerSpell[0] : id);
2750
2751                // send spell in "id - [name, rank N] [talent] [passive] [learn] [known]" format
2752                std::ostringstream ss;
2753                if (m_session)
2754                                        ss << id << " - |cffffffff|Hspell:" << id << "|h[" << name;
2755                                else
2756                                        ss << id << " - " << name;
2757
2758                // include rank in link name
2759                if(rank)
2760                    ss << GetTrinityString(LANG_SPELL_RANK) << rank;
2761
2762                if (m_session)
2763                                        ss << " " << localeNames[loc] << "]|h|r";
2764                                else
2765                                        ss << " " << localeNames[loc];
2766
2767                if(talent)
2768                    ss << GetTrinityString(LANG_TALENT);
2769                if(passive)
2770                    ss << GetTrinityString(LANG_PASSIVE);
2771                if(learn)
2772                    ss << GetTrinityString(LANG_LEARN);
2773                if(known)
2774                    ss << GetTrinityString(LANG_KNOWN);
2775                if(active)
2776                    ss << GetTrinityString(LANG_ACTIVE);
2777
2778                SendSysMessage(ss.str().c_str());
2779
2780                ++counter;
2781            }
2782        }
2783    }
2784    if (counter == 0)                                       // if counter == 0 then we found nth
2785        SendSysMessage(LANG_COMMAND_NOSPELLFOUND);
2786    return true;
2787}
2788
2789bool ChatHandler::HandleLookupQuestCommand(const char* args)
2790{
2791        if (!*args)
2792        return false;
2793
2794        // can be NULL at console call
2795        Player* target = getSelectedPlayer();
2796
2797    std::string namepart = args;
2798    std::wstring wnamepart;
2799
2800    // converting string that we try to find to lower case
2801    if(!Utf8toWStr(namepart,wnamepart))
2802        return false;
2803
2804    wstrToLower(wnamepart);
2805
2806    uint32 counter = 0 ;
2807
2808    ObjectMgr::QuestMap const& qTemplates = objmgr.GetQuestTemplates();
2809    for (ObjectMgr::QuestMap::const_iterator iter = qTemplates.begin(); iter != qTemplates.end(); ++iter)
2810    {
2811        Quest * qinfo = iter->second;
2812
2813        int loc_idx = m_session ? m_session->GetSessionDbLocaleIndex() : objmgr.GetDBCLocaleIndex();
2814        if ( loc_idx >= 0 )
2815        {
2816            QuestLocale const *il = objmgr.GetQuestLocale(qinfo->GetQuestId());
2817            if (il)
2818            {
2819                if (il->Title.size() > loc_idx && !il->Title[loc_idx].empty())
2820                {
2821                    std::string title = il->Title[loc_idx];
2822
2823                    if (Utf8FitTo(title, wnamepart))
2824                    {
2825                        char const* statusStr = "";
2826
2827                                                if(target)
2828                        {
2829                            QuestStatus status = target->GetQuestStatus(qinfo->GetQuestId());
2830
2831                                                        if(status == QUEST_STATUS_COMPLETE)
2832                                                        {
2833                                                                if(target->GetQuestRewardStatus(qinfo->GetQuestId()))
2834                                                                        statusStr = GetTrinityString(LANG_COMMAND_QUEST_REWARDED);
2835                                                                else
2836                                                                        statusStr = GetTrinityString(LANG_COMMAND_QUEST_COMPLETE);
2837                                                        }
2838                                                        else if(status == QUEST_STATUS_INCOMPLETE)
2839                                                                statusStr = GetTrinityString(LANG_COMMAND_QUEST_ACTIVE);
2840                        }
2841                       
2842                                                if (m_session)
2843                                                        PSendSysMessage(LANG_QUEST_LIST_CHAT,qinfo->GetQuestId(),qinfo->GetQuestId(),title.c_str(),statusStr);
2844                                                else
2845                                                        PSendSysMessage(LANG_QUEST_LIST_CONSOLE,qinfo->GetQuestId(),title.c_str(),statusStr);
2846
2847                        ++counter;
2848                        continue;
2849                    }
2850                }
2851            }
2852        }
2853
2854        std::string title = qinfo->GetTitle();
2855        if(title.empty())
2856            continue;
2857
2858        if (Utf8FitTo(title, wnamepart))
2859        {
2860            char const* statusStr = "";
2861           
2862                        if(target)
2863            {
2864                QuestStatus status = target->GetQuestStatus(qinfo->GetQuestId());
2865
2866                                if(status == QUEST_STATUS_COMPLETE)
2867                                {
2868                                        if(target->GetQuestRewardStatus(qinfo->GetQuestId()))
2869                                                statusStr = GetTrinityString(LANG_COMMAND_QUEST_REWARDED);
2870                                        else
2871                                                statusStr = GetTrinityString(LANG_COMMAND_QUEST_COMPLETE);
2872                                }
2873                                else if(status == QUEST_STATUS_INCOMPLETE)
2874                                        statusStr = GetTrinityString(LANG_COMMAND_QUEST_ACTIVE);
2875            }
2876           
2877                        if (m_session)
2878                                PSendSysMessage(LANG_QUEST_LIST_CHAT,qinfo->GetQuestId(),qinfo->GetQuestId(),title.c_str(),statusStr);
2879                        else
2880                                PSendSysMessage(LANG_QUEST_LIST_CONSOLE,qinfo->GetQuestId(),title.c_str(),statusStr);
2881
2882            ++counter;
2883        }
2884    }
2885
2886    if (counter==0)
2887        SendSysMessage(LANG_COMMAND_NOQUESTFOUND);
2888
2889    return true;
2890}
2891
2892bool ChatHandler::HandleLookupCreatureCommand(const char* args)
2893{
2894    if (!*args)
2895        return false;
2896
2897    std::string namepart = args;
2898    std::wstring wnamepart;
2899
2900    // converting string that we try to find to lower case
2901    if (!Utf8toWStr (namepart,wnamepart))
2902        return false;
2903
2904    wstrToLower (wnamepart);
2905
2906    uint32 counter = 0;
2907
2908    for (uint32 id = 0; id< sCreatureStorage.MaxEntry; ++id)
2909    {
2910        CreatureInfo const* cInfo = sCreatureStorage.LookupEntry<CreatureInfo> (id);
2911        if(!cInfo)
2912            continue;
2913
2914        int loc_idx = m_session ? m_session->GetSessionDbLocaleIndex() : objmgr.GetDBCLocaleIndex();
2915                if (loc_idx >= 0)
2916        {
2917            CreatureLocale const *cl = objmgr.GetCreatureLocale (id);
2918            if (cl)
2919            {
2920                if (cl->Name.size() > loc_idx && !cl->Name[loc_idx].empty ())
2921                {
2922                    std::string name = cl->Name[loc_idx];
2923
2924                    if (Utf8FitTo (name, wnamepart))
2925                    {
2926                        if (m_session)
2927                                                        PSendSysMessage (LANG_CREATURE_ENTRY_LIST_CHAT, id, id, name.c_str ());
2928                                                else
2929                                                        PSendSysMessage (LANG_CREATURE_ENTRY_LIST_CONSOLE, id, name.c_str ());
2930
2931                        ++counter;
2932                        continue;
2933                    }
2934                }
2935            }
2936        }
2937
2938        std::string name = cInfo->Name;
2939        if (name.empty ())
2940            continue;
2941
2942        if (Utf8FitTo(name, wnamepart))
2943        {
2944            if (m_session)
2945                                PSendSysMessage (LANG_CREATURE_ENTRY_LIST_CHAT, id, id, name.c_str ());
2946                        else
2947                                PSendSysMessage (LANG_CREATURE_ENTRY_LIST_CONSOLE, id, name.c_str ());
2948            ++counter;
2949        }
2950    }
2951
2952    if (counter==0)
2953        SendSysMessage (LANG_COMMAND_NOCREATUREFOUND);
2954
2955    return true;
2956}
2957
2958bool ChatHandler::HandleLookupObjectCommand(const char* args)
2959{
2960    if(!*args)
2961        return false;
2962
2963    std::string namepart = args;
2964    std::wstring wnamepart;
2965
2966    // converting string that we try to find to lower case
2967    if(!Utf8toWStr(namepart,wnamepart))
2968        return false;
2969
2970    wstrToLower(wnamepart);
2971
2972    uint32 counter = 0;
2973
2974    for (uint32 id = 0; id< sGOStorage.MaxEntry; id++ )
2975    {
2976        GameObjectInfo const* gInfo = sGOStorage.LookupEntry<GameObjectInfo>(id);
2977        if(!gInfo)
2978            continue;
2979
2980        int loc_idx = m_session ? m_session->GetSessionDbLocaleIndex() : objmgr.GetDBCLocaleIndex();
2981        if ( loc_idx >= 0 )
2982        {
2983            GameObjectLocale const *gl = objmgr.GetGameObjectLocale(id);
2984            if (gl)
2985            {
2986                if (gl->Name.size() > loc_idx && !gl->Name[loc_idx].empty())
2987                {
2988                    std::string name = gl->Name[loc_idx];
2989
2990                    if (Utf8FitTo(name, wnamepart))
2991                    {
2992                        if (m_session)
2993                                                        PSendSysMessage(LANG_GO_ENTRY_LIST_CHAT, id, id, name.c_str());
2994                                                else
2995                                                        PSendSysMessage(LANG_GO_ENTRY_LIST_CONSOLE, id, name.c_str());
2996                        ++counter;
2997                        continue;
2998                    }
2999                }
3000            }
3001        }
3002
3003        std::string name = gInfo->name;
3004        if(name.empty())
3005            continue;
3006
3007        if(Utf8FitTo(name, wnamepart))
3008        {
3009            if (m_session)
3010                                PSendSysMessage(LANG_GO_ENTRY_LIST_CHAT, id, id, name.c_str());
3011                        else
3012                                PSendSysMessage(LANG_GO_ENTRY_LIST_CONSOLE, id, name.c_str());
3013            ++counter;
3014        }
3015    }
3016
3017    if(counter==0)
3018        SendSysMessage(LANG_COMMAND_NOGAMEOBJECTFOUND);
3019
3020    return true;
3021}
3022
3023/** \brief GM command level 3 - Create a guild.
3024 *
3025 * This command allows a GM (level 3) to create a guild.
3026 *
3027 * The "args" parameter contains the name of the guild leader
3028 * and then the name of the guild.
3029 *
3030 */
3031bool ChatHandler::HandleGuildCreateCommand(const char* args)
3032{
3033
3034    if (!*args)
3035        return false;
3036
3037    char *lname = strtok ((char*)args, " ");
3038        char *gname = strtok (NULL, "");
3039
3040    if (!lname)
3041        return false;
3042   
3043        if (!gname)
3044    {
3045        SendSysMessage (LANG_INSERT_GUILD_NAME);
3046                SetSentErrorMessage (true);
3047        return false;
3048    }
3049
3050    std::string guildname = gname;
3051
3052    Player* player = ObjectAccessor::Instance ().FindPlayerByName (lname);
3053        if (!player)
3054    {
3055        SendSysMessage (LANG_PLAYER_NOT_FOUND);
3056                SetSentErrorMessage (true);
3057        return false;
3058    }
3059
3060    if (player->GetGuildId())
3061    {
3062        SendSysMessage (LANG_PLAYER_IN_GUILD);
3063                return true;
3064        }
3065
3066        Guild *guild = new Guild;
3067        if (!guild->create (player->GetGUID (),guildname))
3068        {
3069                delete guild;
3070                SendSysMessage (LANG_GUILD_NOT_CREATED);
3071                SetSentErrorMessage (true);
3072                return false;
3073    }
3074   
3075        objmgr.AddGuild (guild);
3076    return true;
3077}
3078
3079bool ChatHandler::HandleGuildInviteCommand(const char *args)
3080{
3081    if (!*args)
3082        return false;
3083
3084    char* par1 = strtok ((char*)args, " ");
3085    char* par2 = strtok (NULL, "");
3086    if(!par1 || !par2)
3087        return false;
3088
3089    std::string glName = par2;
3090    Guild* targetGuild = objmgr.GetGuildByName (glName);
3091        if (!targetGuild)
3092        return false;
3093
3094    std::string plName = par1;
3095    if (!normalizePlayerName (plName))
3096    {
3097        SendSysMessage (LANG_PLAYER_NOT_FOUND);
3098                SetSentErrorMessage (true);
3099        return false;
3100    }
3101
3102    uint64 plGuid = 0;
3103    if (Player* targetPlayer = ObjectAccessor::Instance ().FindPlayerByName (plName.c_str ()))
3104                plGuid = targetPlayer->GetGUID ();
3105    else
3106        plGuid = objmgr.GetPlayerGUIDByName (plName.c_str ());
3107
3108    if (!plGuid)
3109        false;
3110
3111    // players's guild membership checked in AddMember before add
3112    if (!targetGuild->AddMember (plGuid,targetGuild->GetLowestRank ()))
3113        return false;
3114
3115    return true;
3116}
3117
3118bool ChatHandler::HandleGuildUninviteCommand(const char *args)
3119{
3120    if (!*args)
3121        return false;
3122
3123    char* par1 = strtok ((char*)args, " ");
3124    if(!par1)
3125        return false;
3126
3127    std::string plName = par1;
3128    if (!normalizePlayerName (plName))
3129    {
3130        SendSysMessage (LANG_PLAYER_NOT_FOUND);
3131                SetSentErrorMessage (true);
3132        return false;
3133    }
3134
3135    uint64 plGuid = 0;
3136    uint32 glId   = 0;
3137    if (Player* targetPlayer = ObjectAccessor::Instance ().FindPlayerByName (plName.c_str ()))
3138    {
3139        plGuid = targetPlayer->GetGUID ();
3140                glId   = targetPlayer->GetGuildId ();
3141    }
3142    else
3143    {
3144        plGuid = objmgr.GetPlayerGUIDByName (plName.c_str ());
3145                glId = Player::GetGuildIdFromDB (plGuid);
3146    }
3147
3148    if (!plGuid || !glId)
3149        return false;
3150
3151    Guild* targetGuild = objmgr.GetGuildById (glId);
3152        if (!targetGuild)
3153        return false;
3154
3155    targetGuild->DelMember (plGuid);
3156
3157    return true;
3158}
3159
3160bool ChatHandler::HandleGuildRankCommand(const char *args)
3161{
3162    if (!*args)
3163        return false;
3164
3165    char* par1 = strtok ((char*)args, " ");
3166        char* par2 = strtok (NULL, " ");
3167        if (!par1 || !par2)
3168        return false;
3169
3170    std::string plName = par1;
3171    if (!normalizePlayerName (plName))
3172    {
3173        SendSysMessage (LANG_PLAYER_NOT_FOUND);
3174                SetSentErrorMessage (true);
3175        return false;
3176    }
3177
3178    uint64 plGuid = 0;
3179    uint32 glId   = 0;
3180    if (Player* targetPlayer = ObjectAccessor::Instance ().FindPlayerByName (plName.c_str ()))
3181    {
3182        plGuid = targetPlayer->GetGUID ();
3183                glId   = targetPlayer->GetGuildId ();
3184    }
3185    else
3186    {
3187        plGuid = objmgr.GetPlayerGUIDByName (plName.c_str ());
3188                glId = Player::GetGuildIdFromDB (plGuid);
3189    }
3190
3191    if (!plGuid || !glId)
3192        return false;
3193
3194    Guild* targetGuild = objmgr.GetGuildById (glId);
3195        if (!targetGuild)
3196        return false;
3197
3198    uint32 newrank = uint32 (atoi (par2));
3199        if (newrank > targetGuild->GetLowestRank ())
3200        return false;
3201
3202    targetGuild->ChangeRank (plGuid,newrank);
3203
3204    return true;
3205}
3206
3207bool ChatHandler::HandleGuildDeleteCommand(const char* args)
3208{
3209    if (!*args)
3210        return false;
3211
3212    char* par1 = strtok ((char*)args, " ");
3213        if (!par1)
3214        return false;
3215
3216    std::string gld = par1;
3217
3218    Guild* targetGuild = objmgr.GetGuildByName (gld);
3219        if (!targetGuild)
3220        return false;
3221
3222    targetGuild->Disband ();
3223
3224    return true;
3225}
3226
3227bool ChatHandler::HandleGetDistanceCommand(const char* /*args*/)
3228{
3229    Unit* pUnit = getSelectedUnit();
3230
3231    if(!pUnit)
3232    {
3233        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
3234        SetSentErrorMessage(true);
3235        return false;
3236    }
3237
3238    PSendSysMessage(LANG_DISTANCE, m_session->GetPlayer()->GetDistance(pUnit),m_session->GetPlayer()->GetDistance2d(pUnit));
3239
3240    return true;
3241}
3242
3243// FIX-ME!!!
3244
3245bool ChatHandler::HandleAddWeaponCommand(const char* /*args*/)
3246{
3247    /*if (!*args)
3248        return false;
3249
3250    uint64 guid = m_session->GetPlayer()->GetSelection();
3251    if (guid == 0)
3252    {
3253        SendSysMessage(LANG_NO_SELECTION);
3254        return true;
3255    }
3256
3257    Creature *pCreature = ObjectAccessor::GetCreature(*m_session->GetPlayer(), guid);
3258
3259    if(!pCreature)
3260    {
3261        SendSysMessage(LANG_SELECT_CREATURE);
3262        return true;
3263    }
3264
3265    char* pSlotID = strtok((char*)args, " ");
3266    if (!pSlotID)
3267        return false;
3268
3269    char* pItemID = strtok(NULL, " ");
3270    if (!pItemID)
3271        return false;
3272
3273    uint32 ItemID = atoi(pItemID);
3274    uint32 SlotID = atoi(pSlotID);
3275
3276    ItemPrototype* tmpItem = objmgr.GetItemPrototype(ItemID);
3277
3278    bool added = false;
3279    if(tmpItem)
3280    {
3281        switch(SlotID)
3282        {
3283            case 1:
3284                pCreature->SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_DISPLAY, ItemID);
3285                added = true;
3286                break;
3287            case 2:
3288                pCreature->SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_DISPLAY_01, ItemID);
3289                added = true;
3290                break;
3291            case 3:
3292                pCreature->SetUInt32Value(UNIT_VIRTUAL_ITEM_SLOT_DISPLAY_02, ItemID);
3293                added = true;
3294                break;
3295            default:
3296                PSendSysMessage(LANG_ITEM_SLOT_NOT_EXIST,SlotID);
3297                added = false;
3298                break;
3299        }
3300        if(added)
3301        {
3302            PSendSysMessage(LANG_ITEM_ADDED_TO_SLOT,ItemID,tmpItem->Name1,SlotID);
3303        }
3304    }
3305    else
3306    {
3307        PSendSysMessage(LANG_ITEM_NOT_FOUND,ItemID);
3308        return true;
3309    }
3310    */
3311    return true;
3312}
3313
3314bool ChatHandler::HandleDieCommand(const char* /*args*/)
3315{
3316    Unit* target = getSelectedUnit();
3317
3318    if(!target || !m_session->GetPlayer()->GetSelection())
3319    {
3320        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
3321        SetSentErrorMessage(true);
3322        return false;
3323    }
3324
3325    if( target->isAlive() )
3326    {
3327        m_session->GetPlayer()->DealDamage(target, target->GetHealth(), NULL, DIRECT_DAMAGE, SPELL_SCHOOL_MASK_NORMAL, NULL, false);
3328    }
3329
3330    return true;
3331}
3332
3333bool ChatHandler::HandleDamageCommand(const char * args)
3334{
3335    if (!*args)
3336        return false;
3337
3338    Unit* target = getSelectedUnit();
3339
3340    if(!target || !m_session->GetPlayer()->GetSelection())
3341    {
3342        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
3343        SetSentErrorMessage(true);
3344        return false;
3345    }
3346
3347    if( !target->isAlive() )
3348        return true;
3349
3350    char* damageStr = strtok((char*)args, " ");
3351    if(!damageStr)
3352        return false;
3353
3354    int32 damage = atoi((char*)damageStr);
3355    if(damage <=0)
3356        return true;
3357
3358    char* schoolStr = strtok((char*)NULL, " ");
3359
3360    // flat melee damage without resistence/etc reduction
3361    if(!schoolStr)
3362    {
3363        m_session->GetPlayer()->DealDamage(target, damage, NULL, DIRECT_DAMAGE, SPELL_SCHOOL_MASK_NORMAL, NULL, false);
3364        m_session->GetPlayer()->SendAttackStateUpdate (HITINFO_NORMALSWING2, target, 1, SPELL_SCHOOL_MASK_NORMAL, damage, 0, 0, VICTIMSTATE_NORMAL, 0);
3365        return true;
3366    }
3367
3368    uint32 school = schoolStr ? atoi((char*)schoolStr) : SPELL_SCHOOL_NORMAL;
3369    if(school >= MAX_SPELL_SCHOOL)
3370        return false;
3371
3372    SpellSchoolMask schoolmask = SpellSchoolMask(1 << school);
3373
3374    if ( schoolmask & SPELL_SCHOOL_MASK_NORMAL )
3375        damage = m_session->GetPlayer()->CalcArmorReducedDamage(target, damage);
3376
3377    char* spellStr = strtok((char*)NULL, " ");
3378
3379    // melee damage by specific school
3380    if(!spellStr)
3381    {
3382        uint32 absorb = 0;
3383        uint32 resist = 0;
3384
3385        m_session->GetPlayer()->CalcAbsorbResist(target,schoolmask, SPELL_DIRECT_DAMAGE, damage, &absorb, &resist);
3386
3387        if (damage <= absorb + resist)
3388            return true;
3389
3390        damage -= absorb + resist;
3391
3392        m_session->GetPlayer()->DealDamage(target, damage, NULL, DIRECT_DAMAGE, schoolmask, NULL, false);
3393        m_session->GetPlayer()->SendAttackStateUpdate (HITINFO_NORMALSWING2, target, 1, schoolmask, damage, absorb, resist, VICTIMSTATE_NORMAL, 0);
3394        return true;
3395    }
3396
3397    // non-melee damage
3398
3399    // number or [name] Shift-click form |color|Hspell:spell_id|h[name]|h|r or Htalent form
3400    uint32 spellid = extractSpellIdFromLink((char*)args);
3401    if(!spellid || !sSpellStore.LookupEntry(spellid))
3402        return false;
3403
3404    m_session->GetPlayer()->SpellNonMeleeDamageLog(target, spellid, damage, false);
3405    return true;
3406}
3407
3408bool ChatHandler::HandleModifyArenaCommand(const char * args)
3409{
3410    if (!*args)
3411        return false;
3412
3413    Player *target = getSelectedPlayer();
3414    if(!target)
3415    {
3416        SendSysMessage(LANG_PLAYER_NOT_FOUND);
3417        SetSentErrorMessage(true);
3418        return false;
3419    }
3420
3421    int32 amount = (uint32)atoi(args);
3422
3423    target->ModifyArenaPoints(amount);
3424
3425    PSendSysMessage(LANG_COMMAND_MODIFY_ARENA, target->GetName(), target->GetArenaPoints());
3426
3427    return true;
3428}
3429
3430bool ChatHandler::HandleReviveCommand(const char* args)
3431{
3432    Player* SelectedPlayer = NULL;
3433
3434    if (*args)
3435    {
3436        std::string name = args;
3437        if(!normalizePlayerName(name))
3438        {
3439            SendSysMessage(LANG_PLAYER_NOT_FOUND);
3440            SetSentErrorMessage(true);
3441            return false;
3442        }
3443
3444        SelectedPlayer = objmgr.GetPlayer(name.c_str());
3445    }
3446    else
3447        SelectedPlayer = getSelectedPlayer();
3448
3449    if(!SelectedPlayer)
3450    {
3451        SendSysMessage(LANG_NO_CHAR_SELECTED);
3452        SetSentErrorMessage(true);
3453        return false;
3454    }
3455
3456    SelectedPlayer->ResurrectPlayer(0.5f);
3457    SelectedPlayer->SpawnCorpseBones();
3458    SelectedPlayer->SaveToDB();
3459    return true;
3460}
3461
3462bool ChatHandler::HandleAuraCommand(const char* args)
3463{
3464    char* px = strtok((char*)args, " ");
3465    if (!px)
3466        return false;
3467
3468    Unit *target = getSelectedUnit();
3469    if(!target)
3470    {
3471        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
3472        SetSentErrorMessage(true);
3473        return false;
3474    }
3475
3476    uint32 spellID = (uint32)atoi(px);
3477    SpellEntry const *spellInfo = sSpellStore.LookupEntry( spellID );
3478    if(spellInfo)
3479    {
3480        for(uint32 i = 0;i<3;i++)
3481        {
3482            uint8 eff = spellInfo->Effect[i];
3483            if (eff>=TOTAL_SPELL_EFFECTS)
3484                continue;
3485            if( IsAreaAuraEffect(eff)           ||
3486                eff == SPELL_EFFECT_APPLY_AURA  ||
3487                eff == SPELL_EFFECT_PERSISTENT_AREA_AURA )
3488            {
3489                Aura *Aur = CreateAura(spellInfo, i, NULL, target);
3490                target->AddAura(Aur);
3491            }
3492        }
3493    }
3494
3495    return true;
3496}
3497
3498bool ChatHandler::HandleUnAuraCommand(const char* args)
3499{
3500    char* px = strtok((char*)args, " ");
3501    if (!px)
3502        return false;
3503
3504    Unit *target = getSelectedUnit();
3505    if(!target)
3506    {
3507        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
3508        SetSentErrorMessage(true);
3509        return false;
3510    }
3511
3512    std::string argstr = args;
3513    if (argstr == "all")
3514    {
3515        target->RemoveAllAuras();
3516        return true;
3517    }
3518
3519    uint32 spellID = (uint32)atoi(px);
3520    target->RemoveAurasDueToSpell(spellID);
3521
3522    return true;
3523}
3524
3525bool ChatHandler::HandleLinkGraveCommand(const char* args)
3526{
3527    if(!*args)
3528        return false;
3529
3530    char* px = strtok((char*)args, " ");
3531    if (!px)
3532        return false;
3533
3534    uint32 g_id = (uint32)atoi(px);
3535
3536    uint32 g_team;
3537
3538    char* px2 = strtok(NULL, " ");
3539
3540    if (!px2)
3541        g_team = 0;
3542    else if (strncmp(px2,"horde",6)==0)
3543        g_team = HORDE;
3544    else if (strncmp(px2,"alliance",9)==0)
3545        g_team = ALLIANCE;
3546    else
3547        return false;
3548
3549    WorldSafeLocsEntry const* graveyard =  sWorldSafeLocsStore.LookupEntry(g_id);
3550
3551    if(!graveyard )
3552    {
3553        PSendSysMessage(LANG_COMMAND_GRAVEYARDNOEXIST, g_id);
3554        SetSentErrorMessage(true);
3555        return false;
3556    }
3557
3558    Player* player = m_session->GetPlayer();
3559
3560    uint32 zoneId = player->GetZoneId();
3561
3562    AreaTableEntry const *areaEntry = GetAreaEntryByAreaID(zoneId);
3563    if(!areaEntry || areaEntry->zone !=0 )
3564    {
3565        PSendSysMessage(LANG_COMMAND_GRAVEYARDWRONGZONE, g_id,zoneId);
3566        SetSentErrorMessage(true);
3567        return false;
3568    }
3569
3570    if(graveyard->map_id != areaEntry->mapid && g_team != 0)
3571    {
3572        SendSysMessage(LANG_COMMAND_GRAVEYARDWRONGTEAM);
3573        SetSentErrorMessage(true);
3574        return false;
3575    }
3576
3577    if(objmgr.AddGraveYardLink(g_id,player->GetZoneId(),g_team))
3578        PSendSysMessage(LANG_COMMAND_GRAVEYARDLINKED, g_id,zoneId);
3579    else
3580        PSendSysMessage(LANG_COMMAND_GRAVEYARDALRLINKED, g_id,zoneId);
3581
3582    return true;
3583}
3584
3585bool ChatHandler::HandleNearGraveCommand(const char* args)
3586{
3587    uint32 g_team;
3588
3589    size_t argslen = strlen(args);
3590
3591    if(!*args)
3592        g_team = 0;
3593    else if (strncmp((char*)args,"horde",argslen)==0)
3594        g_team = HORDE;
3595    else if (strncmp((char*)args,"alliance",argslen)==0)
3596        g_team = ALLIANCE;
3597    else
3598        return false;
3599
3600    Player* player = m_session->GetPlayer();
3601
3602    WorldSafeLocsEntry const* graveyard = objmgr.GetClosestGraveYard(
3603        player->GetPositionX(), player->GetPositionY(), player->GetPositionZ(),player->GetMapId(),g_team);
3604
3605    if(graveyard)
3606    {
3607        uint32 g_id = graveyard->ID;
3608
3609        GraveYardData const* data = objmgr.FindGraveYardData(g_id,player->GetZoneId());
3610        if (!data)
3611        {
3612            PSendSysMessage(LANG_COMMAND_GRAVEYARDERROR,g_id);
3613            SetSentErrorMessage(true);
3614            return false;
3615        }
3616
3617        g_team = data->team;
3618
3619        std::string team_name = GetTrinityString(LANG_COMMAND_GRAVEYARD_NOTEAM);
3620
3621        if(g_team == 0)
3622            team_name = GetTrinityString(LANG_COMMAND_GRAVEYARD_ANY);
3623        else if(g_team == HORDE)
3624            team_name = GetTrinityString(LANG_COMMAND_GRAVEYARD_HORDE);
3625        else if(g_team == ALLIANCE)
3626            team_name = GetTrinityString(LANG_COMMAND_GRAVEYARD_ALLIANCE);
3627
3628        PSendSysMessage(LANG_COMMAND_GRAVEYARDNEAREST, g_id,team_name.c_str(),player->GetZoneId());
3629    }
3630    else
3631    {
3632        std::string team_name;
3633
3634        if(g_team == 0)
3635            team_name = GetTrinityString(LANG_COMMAND_GRAVEYARD_ANY);
3636        else if(g_team == HORDE)
3637            team_name = GetTrinityString(LANG_COMMAND_GRAVEYARD_HORDE);
3638        else if(g_team == ALLIANCE)
3639            team_name = GetTrinityString(LANG_COMMAND_GRAVEYARD_ALLIANCE);
3640
3641        if(g_team == ~uint32(0))
3642            PSendSysMessage(LANG_COMMAND_ZONENOGRAVEYARDS, player->GetZoneId());
3643        else
3644            PSendSysMessage(LANG_COMMAND_ZONENOGRAFACTION, player->GetZoneId(),team_name.c_str());
3645    }
3646
3647    return true;
3648}
3649
3650//play npc emote
3651bool ChatHandler::HandlePlayEmoteCommand(const char* args)
3652{
3653    uint32 emote = atoi((char*)args);
3654
3655    Creature* target = getSelectedCreature();
3656    if(!target)
3657    {
3658        SendSysMessage(LANG_SELECT_CREATURE);
3659        SetSentErrorMessage(true);
3660        return false;
3661    }
3662
3663    target->SetUInt32Value(UNIT_NPC_EMOTESTATE,emote);
3664
3665    return true;
3666}
3667
3668bool ChatHandler::HandleNpcInfoCommand(const char* /*args*/)
3669{
3670    Creature* target = getSelectedCreature();
3671
3672    if(!target)
3673    {
3674        SendSysMessage(LANG_SELECT_CREATURE);
3675        SetSentErrorMessage(true);
3676        return false;
3677    }
3678
3679    uint32 faction = target->getFaction();
3680    uint32 npcflags = target->GetUInt32Value(UNIT_NPC_FLAGS);
3681    uint32 displayid = target->GetDisplayId();
3682    uint32 nativeid = target->GetNativeDisplayId();
3683    uint32 Entry = target->GetEntry();
3684    CreatureInfo const* cInfo = target->GetCreatureInfo();
3685
3686    int32 curRespawnDelay = target->GetRespawnTimeEx()-time(NULL);
3687    if(curRespawnDelay < 0)
3688        curRespawnDelay = 0;
3689    std::string curRespawnDelayStr = secsToTimeString(curRespawnDelay,true);
3690    std::string defRespawnDelayStr = secsToTimeString(target->GetRespawnDelay(),true);
3691
3692    PSendSysMessage(LANG_NPCINFO_CHAR,  target->GetDBTableGUIDLow(), faction, npcflags, Entry, displayid, nativeid);
3693    PSendSysMessage(LANG_NPCINFO_LEVEL, target->getLevel());
3694    PSendSysMessage(LANG_NPCINFO_HEALTH,target->GetCreateHealth(), target->GetMaxHealth(), target->GetHealth());
3695    PSendSysMessage(LANG_NPCINFO_FLAGS, target->GetUInt32Value(UNIT_FIELD_FLAGS), target->GetUInt32Value(UNIT_DYNAMIC_FLAGS), target->getFaction());
3696    PSendSysMessage(LANG_COMMAND_RAWPAWNTIMES, defRespawnDelayStr.c_str(),curRespawnDelayStr.c_str());
3697    PSendSysMessage(LANG_NPCINFO_LOOT,  cInfo->lootid,cInfo->pickpocketLootId,cInfo->SkinLootId);
3698    PSendSysMessage(LANG_NPCINFO_DUNGEON_ID, target->GetInstanceId());
3699    PSendSysMessage(LANG_NPCINFO_POSITION,float(target->GetPositionX()), float(target->GetPositionY()), float(target->GetPositionZ()));
3700
3701    if ((npcflags & UNIT_NPC_FLAG_VENDOR) )
3702    {
3703        SendSysMessage(LANG_NPCINFO_VENDOR);
3704    }
3705    if ((npcflags & UNIT_NPC_FLAG_TRAINER) )
3706    {
3707        SendSysMessage(LANG_NPCINFO_TRAINER);
3708    }
3709
3710    return true;
3711}
3712
3713bool ChatHandler::HandleExploreCheatCommand(const char* args)
3714{
3715    if (!*args)
3716        return false;
3717
3718    int flag = atoi((char*)args);
3719
3720    Player *chr = getSelectedPlayer();
3721    if (chr == NULL)
3722    {
3723        SendSysMessage(LANG_NO_CHAR_SELECTED);
3724        SetSentErrorMessage(true);
3725        return false;
3726    }
3727
3728    if (flag != 0)
3729    {
3730        PSendSysMessage(LANG_YOU_SET_EXPLORE_ALL, chr->GetName());
3731        if (needReportToTarget(chr))
3732            ChatHandler(chr).PSendSysMessage(LANG_YOURS_EXPLORE_SET_ALL,GetName());
3733    }
3734    else
3735    {
3736        PSendSysMessage(LANG_YOU_SET_EXPLORE_NOTHING, chr->GetName());
3737        if (needReportToTarget(chr))
3738            ChatHandler(chr).PSendSysMessage(LANG_YOURS_EXPLORE_SET_NOTHING,GetName());
3739    }
3740
3741    for (uint8 i=0; i<128; i++)
3742    {
3743        if (flag != 0)
3744        {
3745            m_session->GetPlayer()->SetFlag(PLAYER_EXPLORED_ZONES_1+i,0xFFFFFFFF);
3746        }
3747        else
3748        {
3749            m_session->GetPlayer()->SetFlag(PLAYER_EXPLORED_ZONES_1+i,0);
3750        }
3751    }
3752
3753    return true;
3754}
3755
3756bool ChatHandler::HandleHoverCommand(const char* args)
3757{
3758    char* px = strtok((char*)args, " ");
3759    uint32 flag;
3760    if (!px)
3761        flag = 1;
3762    else
3763        flag = atoi(px);
3764
3765    m_session->GetPlayer()->SetHover(flag);
3766
3767    if (flag)
3768        SendSysMessage(LANG_HOVER_ENABLED);
3769    else
3770        SendSysMessage(LANG_HOVER_DISABLED);
3771
3772    return true;
3773}
3774
3775bool ChatHandler::HandleWaterwalkCommand(const char* args)
3776{
3777    if(!args)
3778        return false;
3779
3780    Player *player = getSelectedPlayer();
3781    if(!player)
3782    {
3783        PSendSysMessage(LANG_NO_CHAR_SELECTED);
3784        SetSentErrorMessage(true);
3785        return false;
3786    }
3787       
3788    if (strncmp(args, "on", 3) == 0)
3789        player->SetMovement(MOVE_WATER_WALK);               // ON
3790    else if (strncmp(args, "off", 4) == 0)
3791        player->SetMovement(MOVE_LAND_WALK);                // OFF
3792    else
3793    {
3794        SendSysMessage(LANG_USE_BOL);
3795        return false;
3796    }
3797
3798    PSendSysMessage(LANG_YOU_SET_WATERWALK, args, player->GetName());
3799    if(needReportToTarget(player))
3800        ChatHandler(player).PSendSysMessage(LANG_YOUR_WATERWALK_SET, args, GetName());
3801    return true;
3802
3803}
3804
3805bool ChatHandler::HandleLevelUpCommand(const char* args)
3806{
3807    char* px = strtok((char*)args, " ");
3808    char* py = strtok((char*)NULL, " ");
3809
3810    // command format parsing
3811    char* pname = (char*)NULL;
3812    int addlevel = 1;
3813
3814    if(px && py)                                            // .levelup name level
3815    {
3816        addlevel = atoi(py);
3817        pname = px;
3818    }
3819    else if(px && !py)                                      // .levelup name OR .levelup level
3820    {
3821        if(isalpha(px[0]))                                  // .levelup name
3822            pname = px;
3823        else                                                // .levelup level
3824            addlevel = atoi(px);
3825    }
3826    // else .levelup - nothing do for prepering
3827
3828    // player
3829    Player *chr = NULL;
3830    uint64 chr_guid = 0;
3831
3832    std::string name;
3833
3834    if(pname)                                               // player by name
3835    {
3836        name = pname;
3837        if(!normalizePlayerName(name))
3838        {
3839            SendSysMessage(LANG_PLAYER_NOT_FOUND);
3840            SetSentErrorMessage(true);
3841            return false;
3842        }
3843
3844        chr = objmgr.GetPlayer(name.c_str());
3845        if(!chr)                                            // not in game
3846        {
3847            chr_guid = objmgr.GetPlayerGUIDByName(name);
3848            if (chr_guid == 0)
3849            {
3850                SendSysMessage(LANG_PLAYER_NOT_FOUND);
3851                SetSentErrorMessage(true);
3852                return false;
3853            }
3854        }
3855    }
3856    else                                                    // player by selection
3857    {
3858        chr = getSelectedPlayer();
3859
3860        if (chr == NULL)
3861        {
3862            SendSysMessage(LANG_NO_CHAR_SELECTED);
3863            SetSentErrorMessage(true);
3864            return false;
3865        }
3866
3867        name = chr->GetName();
3868    }
3869
3870    assert(chr || chr_guid);
3871
3872    int32 oldlevel = chr ? chr->getLevel() : Player::GetUInt32ValueFromDB(UNIT_FIELD_LEVEL,chr_guid);
3873    int32 newlevel = oldlevel + addlevel;
3874    if(newlevel < 1)
3875        newlevel = 1;
3876    if(newlevel > 255)                                      // hardcoded maximum level
3877        newlevel = 255;
3878
3879    if(chr)
3880    {
3881        chr->GiveLevel(newlevel);
3882        chr->InitTalentForLevel();
3883        chr->SetUInt32Value(PLAYER_XP,0);
3884
3885        if(oldlevel == newlevel)
3886            ChatHandler(chr).SendSysMessage(LANG_YOURS_LEVEL_PROGRESS_RESET);
3887        else
3888        if(oldlevel < newlevel)
3889            ChatHandler(chr).PSendSysMessage(LANG_YOURS_LEVEL_UP,newlevel-oldlevel);
3890        else
3891        if(oldlevel > newlevel)
3892            ChatHandler(chr).PSendSysMessage(LANG_YOURS_LEVEL_DOWN,newlevel-oldlevel);
3893    }
3894    else
3895    {
3896        // update levle and XP at level, all other will be updated at loading
3897        Tokens values;
3898        Player::LoadValuesArrayFromDB(values,chr_guid);
3899        Player::SetUInt32ValueInArray(values,UNIT_FIELD_LEVEL,newlevel);
3900        Player::SetUInt32ValueInArray(values,PLAYER_XP,0);
3901        Player::SaveValuesArrayInDB(values,chr_guid);
3902    }
3903
3904    if(m_session->GetPlayer() != chr)                       // including chr==NULL
3905        PSendSysMessage(LANG_YOU_CHANGE_LVL,name.c_str(),newlevel);
3906    return true;
3907}
3908
3909bool ChatHandler::HandleShowAreaCommand(const char* args)
3910{
3911    if (!*args)
3912        return false;
3913
3914    int area = atoi((char*)args);
3915
3916    Player *chr = getSelectedPlayer();
3917    if (chr == NULL)
3918    {
3919        SendSysMessage(LANG_NO_CHAR_SELECTED);
3920        SetSentErrorMessage(true);
3921        return false;
3922    }
3923
3924    int offset = area / 32;
3925    uint32 val = (uint32)(1 << (area % 32));
3926
3927    if(offset >= 128)
3928    {
3929        SendSysMessage(LANG_BAD_VALUE);
3930        SetSentErrorMessage(true);
3931        return false;
3932    }
3933
3934    uint32 currFields = chr->GetUInt32Value(PLAYER_EXPLORED_ZONES_1 + offset);
3935    chr->SetUInt32Value(PLAYER_EXPLORED_ZONES_1 + offset, (uint32)(currFields | val));
3936
3937    SendSysMessage(LANG_EXPLORE_AREA);
3938    return true;
3939}
3940
3941bool ChatHandler::HandleHideAreaCommand(const char* args)
3942{
3943    if (!*args)
3944        return false;
3945
3946    int area = atoi((char*)args);
3947
3948    Player *chr = getSelectedPlayer();
3949    if (chr == NULL)
3950    {
3951        SendSysMessage(LANG_NO_CHAR_SELECTED);
3952        SetSentErrorMessage(true);
3953        return false;
3954    }
3955
3956    int offset = area / 32;
3957    uint32 val = (uint32)(1 << (area % 32));
3958
3959    if(offset >= 128)
3960    {
3961        SendSysMessage(LANG_BAD_VALUE);
3962        SetSentErrorMessage(true);
3963        return false;
3964    }
3965
3966    uint32 currFields = chr->GetUInt32Value(PLAYER_EXPLORED_ZONES_1 + offset);
3967    chr->SetUInt32Value(PLAYER_EXPLORED_ZONES_1 + offset, (uint32)(currFields ^ val));
3968
3969    SendSysMessage(LANG_UNEXPLORE_AREA);
3970    return true;
3971}
3972
3973bool ChatHandler::HandleUpdate(const char* args)
3974{
3975    if(!*args)
3976        return false;
3977
3978    uint32 updateIndex;
3979    uint32 value;
3980
3981    char* pUpdateIndex = strtok((char*)args, " ");
3982
3983    Unit* chr = getSelectedUnit();
3984    if (chr == NULL)
3985    {
3986        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
3987        SetSentErrorMessage(true);
3988        return false;
3989    }
3990
3991    if(!pUpdateIndex)
3992    {
3993        return true;
3994    }
3995    updateIndex = atoi(pUpdateIndex);
3996    //check updateIndex
3997    if(chr->GetTypeId() == TYPEID_PLAYER)
3998    {
3999        if (updateIndex>=PLAYER_END) return true;
4000    }
4001    else
4002    {
4003        if (updateIndex>=UNIT_END) return true;
4004    }
4005
4006    char*  pvalue = strtok(NULL, " ");
4007    if (!pvalue)
4008    {
4009        value=chr->GetUInt32Value(updateIndex);
4010
4011        PSendSysMessage(LANG_UPDATE, chr->GetGUIDLow(),updateIndex,value);
4012        return true;
4013    }
4014
4015    value=atoi(pvalue);
4016
4017    PSendSysMessage(LANG_UPDATE_CHANGE, chr->GetGUIDLow(),updateIndex,value);
4018
4019    chr->SetUInt32Value(updateIndex,value);
4020
4021    return true;
4022}
4023
4024bool ChatHandler::HandleBankCommand(const char* /*args*/)
4025{
4026    m_session->SendShowBank( m_session->GetPlayer()->GetGUID() );
4027
4028    return true;
4029}
4030
4031bool ChatHandler::HandleChangeWeather(const char* args)
4032{
4033    if(!*args)
4034        return false;
4035
4036    //Weather is OFF
4037    if (!sWorld.getConfig(CONFIG_WEATHER))
4038    {
4039        SendSysMessage(LANG_WEATHER_DISABLED);
4040        SetSentErrorMessage(true);
4041        return false;
4042    }
4043
4044    //*Change the weather of a cell
4045    char* px = strtok((char*)args, " ");
4046    char* py = strtok(NULL, " ");
4047
4048    if (!px || !py)
4049        return false;
4050
4051    uint32 type = (uint32)atoi(px);                         //0 to 3, 0: fine, 1: rain, 2: snow, 3: sand
4052    float grade = (float)atof(py);                          //0 to 1, sending -1 is instand good weather
4053
4054    Player *player = m_session->GetPlayer();
4055    uint32 zoneid = player->GetZoneId();
4056
4057    Weather* wth = sWorld.FindWeather(zoneid);
4058
4059    if(!wth)
4060        wth = sWorld.AddWeather(zoneid);
4061    if(!wth)
4062    {
4063        SendSysMessage(LANG_NO_WEATHER);
4064        SetSentErrorMessage(true);
4065        return false;
4066    }
4067
4068    wth->SetWeather(WeatherType(type), grade);
4069
4070    return true;
4071}
4072
4073bool ChatHandler::HandleSetValue(const char* args)
4074{
4075    if(!*args)
4076        return false;
4077
4078    char* px = strtok((char*)args, " ");
4079    char* py = strtok(NULL, " ");
4080    char* pz = strtok(NULL, " ");
4081
4082    if (!px || !py)
4083        return false;
4084
4085    Unit* target = getSelectedUnit();
4086    if(!target)
4087    {
4088        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
4089        SetSentErrorMessage(true);
4090        return false;
4091    }
4092
4093    uint64 guid = target->GetGUID();
4094
4095    uint32 Opcode = (uint32)atoi(px);
4096    if(Opcode >= target->GetValuesCount())
4097    {
4098        PSendSysMessage(LANG_TOO_BIG_INDEX, Opcode, GUID_LOPART(guid), target->GetValuesCount());
4099        return false;
4100    }
4101    uint32 iValue;
4102    float fValue;
4103    bool isint32 = true;
4104    if(pz)
4105        isint32 = (bool)atoi(pz);
4106    if(isint32)
4107    {
4108        iValue = (uint32)atoi(py);
4109        sLog.outDebug(GetTrinityString(LANG_SET_UINT), GUID_LOPART(guid), Opcode, iValue);
4110        target->SetUInt32Value( Opcode , iValue );
4111        PSendSysMessage(LANG_SET_UINT_FIELD, GUID_LOPART(guid), Opcode,iValue);
4112    }
4113    else
4114    {
4115        fValue = (float)atof(py);
4116        sLog.outDebug(GetTrinityString(LANG_SET_FLOAT), GUID_LOPART(guid), Opcode, fValue);
4117        target->SetFloatValue( Opcode , fValue );
4118        PSendSysMessage(LANG_SET_FLOAT_FIELD, GUID_LOPART(guid), Opcode,fValue);
4119    }
4120
4121    return true;
4122}
4123
4124bool ChatHandler::HandleGetValue(const char* args)
4125{
4126    if(!*args)
4127        return false;
4128
4129    char* px = strtok((char*)args, " ");
4130    char* pz = strtok(NULL, " ");
4131
4132    if (!px)
4133        return false;
4134
4135    Unit* target = getSelectedUnit();
4136    if(!target)
4137    {
4138        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
4139        SetSentErrorMessage(true);
4140        return false;
4141    }
4142
4143    uint64 guid = target->GetGUID();
4144
4145    uint32 Opcode = (uint32)atoi(px);
4146    if(Opcode >= target->GetValuesCount())
4147    {
4148        PSendSysMessage(LANG_TOO_BIG_INDEX, Opcode, GUID_LOPART(guid), target->GetValuesCount());
4149        return false;
4150    }
4151    uint32 iValue;
4152    float fValue;
4153    bool isint32 = true;
4154    if(pz)
4155        isint32 = (bool)atoi(pz);
4156
4157    if(isint32)
4158    {
4159        iValue = target->GetUInt32Value( Opcode );
4160        sLog.outDebug(GetTrinityString(LANG_GET_UINT), GUID_LOPART(guid), Opcode, iValue);
4161        PSendSysMessage(LANG_GET_UINT_FIELD, GUID_LOPART(guid), Opcode,    iValue);
4162    }
4163    else
4164    {
4165        fValue = target->GetFloatValue( Opcode );
4166        sLog.outDebug(GetTrinityString(LANG_GET_FLOAT), GUID_LOPART(guid), Opcode, fValue);
4167        PSendSysMessage(LANG_GET_FLOAT_FIELD, GUID_LOPART(guid), Opcode, fValue);
4168    }
4169
4170    return true;
4171}
4172
4173bool ChatHandler::HandleSet32Bit(const char* args)
4174{
4175    if(!*args)
4176        return false;
4177
4178    char* px = strtok((char*)args, " ");
4179    char* py = strtok(NULL, " ");
4180
4181    if (!px || !py)
4182        return false;
4183
4184    uint32 Opcode = (uint32)atoi(px);
4185    uint32 Value = (uint32)atoi(py);
4186    if (Value > 32)                                         //uint32 = 32 bits
4187        return false;
4188
4189    sLog.outDebug(GetTrinityString(LANG_SET_32BIT), Opcode, Value);
4190
4191    m_session->GetPlayer( )->SetUInt32Value( Opcode , 2^Value );
4192
4193    PSendSysMessage(LANG_SET_32BIT_FIELD, Opcode,1);
4194    return true;
4195}
4196
4197bool ChatHandler::HandleMod32Value(const char* args)
4198{
4199    if(!*args)
4200        return false;
4201
4202    char* px = strtok((char*)args, " ");
4203    char* py = strtok(NULL, " ");
4204
4205    if (!px || !py)
4206        return false;
4207
4208    uint32 Opcode = (uint32)atoi(px);
4209    int Value = atoi(py);
4210
4211    if(Opcode >= m_session->GetPlayer()->GetValuesCount())
4212    {
4213        PSendSysMessage(LANG_TOO_BIG_INDEX, Opcode, m_session->GetPlayer()->GetGUIDLow(), m_session->GetPlayer( )->GetValuesCount());
4214        return false;
4215    }
4216
4217    sLog.outDebug(GetTrinityString(LANG_CHANGE_32BIT), Opcode, Value);
4218
4219    int CurrentValue = (int)m_session->GetPlayer( )->GetUInt32Value( Opcode );
4220
4221    CurrentValue += Value;
4222    m_session->GetPlayer( )->SetUInt32Value( Opcode , (uint32)CurrentValue );
4223
4224    PSendSysMessage(LANG_CHANGE_32BIT_FIELD, Opcode,CurrentValue);
4225
4226    return true;
4227}
4228
4229bool ChatHandler::HandleAddTeleCommand(const char * args)
4230{
4231    if(!*args)
4232        return false;
4233
4234    Player *player=m_session->GetPlayer();
4235    if (!player)
4236        return false;
4237
4238    std::string name = args;
4239
4240    if(objmgr.GetGameTele(name))
4241    {
4242        SendSysMessage(LANG_COMMAND_TP_ALREADYEXIST);
4243        SetSentErrorMessage(true);
4244        return false;
4245    }
4246
4247    GameTele tele;
4248    tele.position_x  = player->GetPositionX();
4249    tele.position_y  = player->GetPositionY();
4250    tele.position_z  = player->GetPositionZ();
4251    tele.orientation = player->GetOrientation();
4252    tele.mapId       = player->GetMapId();
4253    tele.name        = name;
4254
4255    if(objmgr.AddGameTele(tele))
4256    {
4257        SendSysMessage(LANG_COMMAND_TP_ADDED);
4258    }
4259    else
4260    {
4261        SendSysMessage(LANG_COMMAND_TP_ADDEDERR);
4262        SetSentErrorMessage(true);
4263        return false;
4264    }
4265
4266    return true;
4267}
4268
4269bool ChatHandler::HandleDelTeleCommand(const char * args)
4270{
4271    if(!*args)
4272        return false;
4273
4274    std::string name = args;
4275
4276    if(!objmgr.DeleteGameTele(name))
4277    {
4278        SendSysMessage(LANG_COMMAND_TELE_NOTFOUND);
4279        SetSentErrorMessage(true);
4280        return false;
4281    }
4282
4283    SendSysMessage(LANG_COMMAND_TP_DELETED);
4284    return true;
4285}
4286
4287bool ChatHandler::HandleListAurasCommand (const char * /*args*/)
4288{
4289    Unit *unit = getSelectedUnit();
4290    if(!unit)
4291    {
4292        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
4293        SetSentErrorMessage(true);
4294        return false;
4295    }
4296
4297    char const* talentStr = GetTrinityString(LANG_TALENT);
4298    char const* passiveStr = GetTrinityString(LANG_PASSIVE);
4299
4300    Unit::AuraMap const& uAuras = unit->GetAuras();
4301    PSendSysMessage(LANG_COMMAND_TARGET_LISTAURAS, uAuras.size());
4302    for (Unit::AuraMap::const_iterator itr = uAuras.begin(); itr != uAuras.end(); ++itr)
4303    {
4304        bool talent = GetTalentSpellCost(itr->second->GetId()) > 0;
4305        PSendSysMessage(LANG_COMMAND_TARGET_AURADETAIL, itr->second->GetId(), itr->second->GetEffIndex(),
4306            itr->second->GetModifier()->m_auraname, itr->second->GetAuraDuration(), itr->second->GetAuraMaxDuration(),
4307            itr->second->GetSpellProto()->SpellName[m_session->GetSessionDbcLocale()],
4308            (itr->second->IsPassive() ? passiveStr : ""),(talent ? talentStr : ""),
4309            IS_PLAYER_GUID(itr->second->GetCasterGUID()) ? "player" : "creature",GUID_LOPART(itr->second->GetCasterGUID()));
4310    }
4311    for (int i = 0; i < TOTAL_AURAS; i++)
4312    {
4313        Unit::AuraList const& uAuraList = unit->GetAurasByType(AuraType(i));
4314        if (uAuraList.empty()) continue;
4315        PSendSysMessage(LANG_COMMAND_TARGET_LISTAURATYPE, uAuraList.size(), i);
4316        for (Unit::AuraList::const_iterator itr = uAuraList.begin(); itr != uAuraList.end(); ++itr)
4317        {
4318            bool talent = GetTalentSpellCost((*itr)->GetId()) > 0;
4319            PSendSysMessage(LANG_COMMAND_TARGET_AURASIMPLE, (*itr)->GetId(), (*itr)->GetEffIndex(),
4320                (*itr)->GetSpellProto()->SpellName[m_session->GetSessionDbcLocale()],((*itr)->IsPassive() ? passiveStr : ""),(talent ? talentStr : ""),
4321                IS_PLAYER_GUID((*itr)->GetCasterGUID()) ? "player" : "creature",GUID_LOPART((*itr)->GetCasterGUID()));
4322        }
4323    }
4324    return true;
4325}
4326
4327bool ChatHandler::HandleResetHonorCommand (const char * args)
4328{
4329    char* pName = strtok((char*)args, "");
4330    Player *player = NULL;
4331    if (pName)
4332    {
4333        std::string name = pName;
4334        if(!normalizePlayerName(name))
4335        {
4336            SendSysMessage(LANG_PLAYER_NOT_FOUND);
4337            SetSentErrorMessage(true);
4338            return false;
4339        }
4340
4341        uint64 guid = objmgr.GetPlayerGUIDByName(name.c_str());
4342        player = objmgr.GetPlayer(guid);
4343    }
4344    else
4345        player = getSelectedPlayer();
4346
4347    if(!player)
4348    {
4349        SendSysMessage(LANG_NO_CHAR_SELECTED);
4350        return true;
4351    }
4352
4353    player->SetUInt32Value(PLAYER_FIELD_KILLS, 0);
4354    player->SetUInt32Value(PLAYER_FIELD_LIFETIME_HONORBALE_KILLS, 0);
4355    player->SetUInt32Value(PLAYER_FIELD_HONOR_CURRENCY, 0);
4356    player->SetUInt32Value(PLAYER_FIELD_TODAY_CONTRIBUTION, 0);
4357    player->SetUInt32Value(PLAYER_FIELD_YESTERDAY_CONTRIBUTION, 0);
4358
4359    return true;
4360}
4361
4362static bool HandleResetStatsOrLevelHelper(Player* player)
4363{
4364    PlayerInfo const *info = objmgr.GetPlayerInfo(player->getRace(), player->getClass());
4365    if(!info) return false;
4366
4367    ChrClassesEntry const* cEntry = sChrClassesStore.LookupEntry(player->getClass());
4368    if(!cEntry)
4369    {
4370        sLog.outError("Class %u not found in DBC (Wrong DBC files?)",player->getClass());
4371        return false;
4372    }
4373
4374    uint8 powertype = cEntry->powerType;
4375
4376    uint32 unitfield;
4377    if(powertype == POWER_RAGE)
4378        unitfield = 0x1100EE00;
4379    else if(powertype == POWER_ENERGY)
4380        unitfield = 0x00000000;
4381    else if(powertype == POWER_MANA)
4382        unitfield = 0x0000EE00;
4383    else
4384    {
4385        sLog.outError("Invalid default powertype %u for player (class %u)",powertype,player->getClass());
4386        return false;
4387    }
4388
4389    // reset m_form if no aura
4390    if(!player->HasAuraType(SPELL_AURA_MOD_SHAPESHIFT))
4391        player->m_form = FORM_NONE;
4392
4393    player->SetFloatValue(UNIT_FIELD_BOUNDINGRADIUS, DEFAULT_WORLD_OBJECT_SIZE );
4394    player->SetFloatValue(UNIT_FIELD_COMBATREACH, 1.5f   );
4395
4396    player->setFactionForRace(player->getRace());
4397
4398    player->SetUInt32Value(UNIT_FIELD_BYTES_0, ( ( player->getRace() ) | ( player->getClass() << 8 ) | ( player->getGender() << 16 ) | ( powertype << 24 ) ) );
4399
4400    // reset only if player not in some form;
4401    if(player->m_form==FORM_NONE)
4402    {
4403        switch(player->getGender())
4404        {
4405            case GENDER_FEMALE:
4406                player->SetDisplayId(info->displayId_f);
4407                player->SetNativeDisplayId(info->displayId_f);
4408                break;
4409            case GENDER_MALE:
4410                player->SetDisplayId(info->displayId_m);
4411                player->SetNativeDisplayId(info->displayId_m);
4412                break;
4413            default:
4414                break;
4415        }
4416    }
4417
4418    // set UNIT_FIELD_BYTES_1 to init state but preserve m_form value
4419    player->SetUInt32Value(UNIT_FIELD_BYTES_1, unitfield);
4420    player->SetByteValue(UNIT_FIELD_BYTES_2, 1, UNIT_BYTE2_FLAG_UNK3 | UNIT_BYTE2_FLAG_UNK5 );
4421    player->SetByteValue(UNIT_FIELD_BYTES_2, 3, player->m_form);
4422
4423    player->SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
4424
4425    //-1 is default value
4426    player->SetUInt32Value(PLAYER_FIELD_WATCHED_FACTION_INDEX, uint32(-1));
4427
4428    //player->SetUInt32Value(PLAYER_FIELD_BYTES, 0xEEE00000 );
4429    return true;
4430}
4431
4432bool ChatHandler::HandleResetLevelCommand(const char * args)
4433{
4434    char* pName = strtok((char*)args, "");
4435    Player *player = NULL;
4436    if (pName)
4437    {
4438        std::string name = pName;
4439        if(!normalizePlayerName(name))
4440        {
4441            SendSysMessage(LANG_PLAYER_NOT_FOUND);
4442            SetSentErrorMessage(true);
4443            return false;
4444        }
4445
4446        uint64 guid = objmgr.GetPlayerGUIDByName(name.c_str());
4447        player = objmgr.GetPlayer(guid);
4448    }
4449    else
4450        player = getSelectedPlayer();
4451
4452    if(!player)
4453    {
4454        SendSysMessage(LANG_NO_CHAR_SELECTED);
4455        SetSentErrorMessage(true);
4456        return false;
4457    }
4458
4459    if(!HandleResetStatsOrLevelHelper(player))
4460        return false;
4461
4462    player->SetLevel(1);
4463    player->InitStatsForLevel(true);
4464    player->InitTaxiNodesForLevel();
4465    player->InitTalentForLevel();
4466    player->SetUInt32Value(PLAYER_XP,0);
4467
4468    // reset level to summoned pet
4469    Pet* pet = player->GetPet();
4470    if(pet && pet->getPetType()==SUMMON_PET)
4471        pet->InitStatsForLevel(1);
4472
4473    return true;
4474}
4475
4476bool ChatHandler::HandleResetStatsCommand(const char * args)
4477{
4478    char* pName = strtok((char*)args, "");
4479    Player *player = NULL;
4480    if (pName)
4481    {
4482        std::string name = pName;
4483        if(!normalizePlayerName(name))
4484        {
4485            SendSysMessage(LANG_PLAYER_NOT_FOUND);
4486            SetSentErrorMessage(true);
4487            return false;
4488        }
4489
4490        uint64 guid = objmgr.GetPlayerGUIDByName(name.c_str());
4491        player = objmgr.GetPlayer(guid);
4492    }
4493    else
4494        player = getSelectedPlayer();
4495
4496    if(!player)
4497    {
4498        SendSysMessage(LANG_NO_CHAR_SELECTED);
4499        SetSentErrorMessage(true);
4500        return false;
4501    }
4502
4503    if(!HandleResetStatsOrLevelHelper(player))
4504        return false;
4505
4506    player->InitStatsForLevel(true);
4507    player->InitTaxiNodesForLevel();
4508    player->InitTalentForLevel();
4509
4510    return true;
4511}
4512
4513bool ChatHandler::HandleResetSpellsCommand(const char * args)
4514{
4515    char* pName = strtok((char*)args, "");
4516    Player *player = NULL;
4517    uint64 playerGUID = 0;
4518    if (pName)
4519    {
4520        std::string name = pName;
4521
4522        if(!normalizePlayerName(name))
4523        {
4524            SendSysMessage(LANG_PLAYER_NOT_FOUND);
4525            SetSentErrorMessage(true);
4526            return false;
4527        }
4528
4529        player = objmgr.GetPlayer(name.c_str());
4530        if(!player)
4531            playerGUID = objmgr.GetPlayerGUIDByName(name.c_str());
4532    }
4533    else
4534        player = getSelectedPlayer();
4535
4536    if(!player && !playerGUID)
4537    {
4538        SendSysMessage(LANG_NO_CHAR_SELECTED);
4539        SetSentErrorMessage(true);
4540        return false;
4541    }
4542
4543    if(player)
4544    {
4545        player->resetSpells();
4546
4547        ChatHandler(player).SendSysMessage(LANG_RESET_SPELLS);
4548
4549        if(m_session->GetPlayer()!=player)
4550            PSendSysMessage(LANG_RESET_SPELLS_ONLINE,player->GetName());
4551    }
4552    else
4553    {
4554        CharacterDatabase.PExecute("UPDATE characters SET at_login = at_login | '%u' WHERE guid = '%u'",uint32(AT_LOGIN_RESET_SPELLS), GUID_LOPART(playerGUID));
4555        PSendSysMessage(LANG_RESET_SPELLS_OFFLINE,pName);
4556    }
4557
4558    return true;
4559}
4560
4561bool ChatHandler::HandleResetTalentsCommand(const char * args)
4562{
4563    char* pName = strtok((char*)args, "");
4564    Player *player = NULL;
4565    uint64 playerGUID = 0;
4566    if (pName)
4567    {
4568        std::string name = pName;
4569        if(!normalizePlayerName(name))
4570        {
4571            SendSysMessage(LANG_PLAYER_NOT_FOUND);
4572            SetSentErrorMessage(true);
4573            return false;
4574        }
4575
4576        player = objmgr.GetPlayer(name.c_str());
4577        if(!player)
4578            playerGUID = objmgr.GetPlayerGUIDByName(name.c_str());
4579    }
4580    else
4581        player = getSelectedPlayer();
4582
4583    if(!player && !playerGUID)
4584    {
4585        SendSysMessage(LANG_NO_CHAR_SELECTED);
4586        SetSentErrorMessage(true);
4587        return false;
4588    }
4589
4590    if(player)
4591    {
4592        player->resetTalents(true);
4593
4594        ChatHandler(player).SendSysMessage(LANG_RESET_TALENTS);
4595
4596        if(m_session->GetPlayer()!=player)
4597            PSendSysMessage(LANG_RESET_TALENTS_ONLINE,player->GetName());
4598    }
4599    else
4600    {
4601        CharacterDatabase.PExecute("UPDATE characters SET at_login = at_login | '%u' WHERE guid = '%u'",uint32(AT_LOGIN_RESET_TALENTS), GUID_LOPART(playerGUID) );
4602        PSendSysMessage(LANG_RESET_TALENTS_OFFLINE,pName);
4603    }
4604
4605    return true;
4606}
4607
4608bool ChatHandler::HandleResetAllCommand(const char * args)
4609{
4610    if(!*args)
4611        return false;
4612
4613    std::string casename = args;
4614
4615    AtLoginFlags atLogin;
4616
4617    // Command specially created as single command to prevent using short case names
4618    if(casename=="spells")
4619    {
4620        atLogin = AT_LOGIN_RESET_SPELLS;
4621        sWorld.SendWorldText(LANG_RESETALL_SPELLS);
4622    }
4623    else if(casename=="talents")
4624    {
4625        atLogin = AT_LOGIN_RESET_TALENTS;
4626        sWorld.SendWorldText(LANG_RESETALL_TALENTS);
4627    }
4628    else
4629    {
4630        PSendSysMessage(LANG_RESETALL_UNKNOWN_CASE,args);
4631        SetSentErrorMessage(true);
4632        return false;
4633    }
4634
4635    CharacterDatabase.PExecute("UPDATE characters SET at_login = at_login | '%u'",atLogin);
4636    HashMapHolder<Player>::MapType const& plist = ObjectAccessor::Instance().GetPlayers();
4637    for(HashMapHolder<Player>::MapType::const_iterator itr = plist.begin(); itr != plist.end(); ++itr)
4638        itr->second->SetAtLoginFlag(atLogin);
4639
4640    return true;
4641}
4642
4643bool ChatHandler::HandleShutDownCommand(const char* args)
4644{
4645    if(!*args)
4646        return false;
4647
4648    if(std::string(args)=="cancel")
4649    {
4650        sWorld.ShutdownCancel();
4651    }
4652    else
4653    {
4654        int32 time = atoi(args);
4655
4656        ///- Prevent interpret wrong arg value as 0 secs shutdown time
4657        if(time == 0 && (args[0]!='0' || args[1]!='\0') || time < 0)
4658            return false;
4659
4660        sWorld.ShutdownServ(time);
4661    }
4662    return true;
4663}
4664
4665bool ChatHandler::HandleRestartCommand(const char* args)
4666{
4667    if(!*args)
4668        return false;
4669
4670    if(std::string(args)=="cancel")
4671    {
4672        sWorld.ShutdownCancel();
4673    }
4674    else
4675    {
4676        int32 time = atoi(args);
4677
4678        ///- Prevent interpret wrong arg value as 0 secs shutdown time
4679        if(time == 0 && (args[0]!='0' || args[1]!='\0') || time < 0)
4680            return false;
4681
4682        sWorld.ShutdownServ(time, SHUTDOWN_MASK_RESTART);
4683    }
4684    return true;
4685}
4686
4687bool ChatHandler::HandleIdleRestartCommand(const char* args)
4688{
4689    if(!*args)
4690        return false;
4691
4692    if(std::string(args)=="cancel")
4693    {
4694        sWorld.ShutdownCancel();
4695    }
4696    else
4697    {
4698        int32 time = atoi(args);
4699
4700        ///- Prevent interpret wrong arg value as 0 secs shutdown time
4701        if(time == 0 && (args[0]!='0' || args[1]!='\0') || time < 0)
4702            return false;
4703
4704        sWorld.ShutdownServ(time,SHUTDOWN_MASK_RESTART+SHUTDOWN_MASK_IDLE);
4705    }
4706    return true;
4707}
4708
4709bool ChatHandler::HandleIdleShutDownCommand(const char* args)
4710{
4711    if(!*args)
4712        return false;
4713
4714    if(std::string(args)=="cancel")
4715    {
4716        sWorld.ShutdownCancel();
4717    }
4718    else
4719    {
4720        int32 time = atoi(args);
4721
4722        ///- Prevent interpret wrong arg value as 0 secs shutdown time
4723        if(time == 0 && (args[0]!='0' || args[1]!='\0') || time < 0)
4724            return false;
4725
4726        sWorld.ShutdownServ(time,SHUTDOWN_MASK_IDLE);
4727    }
4728    return true;
4729}
4730
4731bool ChatHandler::HandleAddQuest(const char* args)
4732{
4733    Player* player = getSelectedPlayer();
4734    if(!player)
4735    {
4736        SendSysMessage(LANG_NO_CHAR_SELECTED);
4737        SetSentErrorMessage(true);
4738        return false;
4739    }
4740
4741    // .addquest #entry'
4742    // number or [name] Shift-click form |color|Hquest:quest_id|h[name]|h|r
4743    char* cId = extractKeyFromLink((char*)args,"Hquest");
4744    if(!cId)
4745        return false;
4746
4747    uint32 entry = atol(cId);
4748
4749    Quest const* pQuest = objmgr.GetQuestTemplate(entry);
4750
4751    if(!pQuest)
4752    {
4753        PSendSysMessage(LANG_COMMAND_QUEST_NOTFOUND,entry);
4754        SetSentErrorMessage(true);
4755        return false;
4756    }
4757
4758    // check item starting quest (it can work incorrectly if added without item in inventory)
4759    QueryResult *result = WorldDatabase.PQuery("SELECT entry FROM item_template WHERE startquest = '%u' LIMIT 1",entry);
4760    if(result)
4761    {
4762        Field* fields = result->Fetch();
4763        uint32 item_id = fields[0].GetUInt32();
4764        delete result;
4765
4766        PSendSysMessage(LANG_COMMAND_QUEST_STARTFROMITEM, entry,item_id);
4767        SetSentErrorMessage(true);
4768        return false;
4769    }
4770
4771    // ok, normal (creature/GO starting) quest
4772    if( player->CanAddQuest( pQuest, true ) )
4773    {
4774        player->AddQuest( pQuest, NULL );
4775
4776        if ( player->CanCompleteQuest( entry ) )
4777            player->CompleteQuest( entry );
4778    }
4779
4780    return true;
4781}
4782
4783bool ChatHandler::HandleRemoveQuest(const char* args)
4784{
4785    Player* player = getSelectedPlayer();
4786    if(!player)
4787    {
4788        SendSysMessage(LANG_NO_CHAR_SELECTED);
4789        SetSentErrorMessage(true);
4790        return false;
4791    }
4792
4793    // .removequest #entry'
4794    // number or [name] Shift-click form |color|Hquest:quest_id|h[name]|h|r
4795    char* cId = extractKeyFromLink((char*)args,"Hquest");
4796    if(!cId)
4797        return false;
4798
4799    uint32 entry = atol(cId);
4800
4801    Quest const* pQuest = objmgr.GetQuestTemplate(entry);
4802
4803    if(!pQuest)
4804    {
4805        PSendSysMessage(LANG_COMMAND_QUEST_NOTFOUND, entry);
4806        SetSentErrorMessage(true);
4807        return false;
4808    }
4809
4810    // remove all quest entries for 'entry' from quest log
4811    for(uint8 slot = 0; slot < MAX_QUEST_LOG_SIZE; ++slot )
4812    {
4813        uint32 quest = player->GetQuestSlotQuestId(slot);
4814        if(quest==entry)
4815        {
4816            player->SetQuestSlot(slot,0);
4817
4818            // we ignore unequippable quest items in this case, its' still be equipped
4819            player->TakeQuestSourceItem( quest, false );
4820        }
4821    }
4822
4823    // set quest status to not started (will updated in DB at next save)
4824    player->SetQuestStatus( entry, QUEST_STATUS_NONE);
4825
4826    // reset rewarded for restart repeatable quest
4827    player->getQuestStatusMap()[entry].m_rewarded = false;
4828
4829    SendSysMessage(LANG_COMMAND_QUEST_REMOVED);
4830    return true;
4831}
4832
4833bool ChatHandler::HandleCompleteQuest(const char* args)
4834{
4835    Player* player = getSelectedPlayer();
4836    if(!player)
4837    {
4838        SendSysMessage(LANG_NO_CHAR_SELECTED);
4839        SetSentErrorMessage(true);
4840        return false;
4841    }
4842
4843    // .quest complete #entry
4844    // number or [name] Shift-click form |color|Hquest:quest_id|h[name]|h|r
4845    char* cId = extractKeyFromLink((char*)args,"Hquest");
4846    if(!cId)
4847        return false;
4848
4849    uint32 entry = atol(cId);
4850
4851    Quest const* pQuest = objmgr.GetQuestTemplate(entry);
4852
4853    // If player doesn't have the quest
4854    if(!pQuest || player->GetQuestStatus(entry) == QUEST_STATUS_NONE)
4855    {
4856        PSendSysMessage(LANG_COMMAND_QUEST_NOTFOUND, entry);
4857        SetSentErrorMessage(true);
4858        return false;
4859    }
4860
4861    // Add quest items for quests that require items
4862    for(uint8 x = 0; x < QUEST_OBJECTIVES_COUNT; ++x)
4863    {
4864        uint32 id = pQuest->ReqItemId[x];
4865        uint32 count = pQuest->ReqItemCount[x];
4866        if(!id || !count)
4867            continue;
4868
4869        uint32 curItemCount = player->GetItemCount(id,true);
4870
4871        ItemPosCountVec dest;
4872        uint8 msg = player->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, id, count-curItemCount );
4873        if( msg == EQUIP_ERR_OK )
4874        {
4875            Item* item = player->StoreNewItem( dest, id, true);
4876            player->SendNewItem(item,count-curItemCount,true,false);
4877        }
4878    }
4879
4880    // All creature/GO slain/casted (not required, but otherwise it will display "Creature slain 0/10")
4881    for(uint8 i = 0; i < QUEST_OBJECTIVES_COUNT; i++)
4882    {
4883        uint32 creature = pQuest->ReqCreatureOrGOId[i];
4884        uint32 creaturecount = pQuest->ReqCreatureOrGOCount[i];
4885
4886        if(uint32 spell_id = pQuest->ReqSpell[i])
4887        {
4888            for(uint16 z = 0; z < creaturecount; ++z)
4889                player->CastedCreatureOrGO(creature,0,spell_id);
4890        }
4891        else if(creature > 0)
4892        {
4893            for(uint16 z = 0; z < creaturecount; ++z)
4894                player->KilledMonster(creature,0);
4895        }
4896        else if(creature < 0)
4897        {
4898            for(uint16 z = 0; z < creaturecount; ++z)
4899                player->CastedCreatureOrGO(creature,0,0);
4900        }
4901    }
4902
4903    // If the quest requires reputation to complete
4904    if(uint32 repFaction = pQuest->GetRepObjectiveFaction())
4905    {
4906        uint32 repValue = pQuest->GetRepObjectiveValue();
4907        uint32 curRep = player->GetReputation(repFaction);
4908        if(curRep < repValue)
4909        {
4910            FactionEntry const *factionEntry = sFactionStore.LookupEntry(repFaction);
4911            player->SetFactionReputation(factionEntry,repValue);
4912        }
4913    }
4914
4915    // If the quest requires money
4916    int32 ReqOrRewMoney = pQuest->GetRewOrReqMoney();
4917    if(ReqOrRewMoney < 0)
4918        player->ModifyMoney(-ReqOrRewMoney);
4919
4920    player->CompleteQuest(entry);
4921    return true;
4922}
4923
4924bool ChatHandler::HandleBanAccountCommand(const char* args)
4925{
4926    return HandleBanHelper(BAN_ACCOUNT,args);
4927}
4928
4929bool ChatHandler::HandleBanCharacterCommand(const char* args)
4930{
4931        return HandleBanHelper(BAN_CHARACTER,args);
4932}
4933
4934bool ChatHandler::HandleBanIPCommand(const char* args)
4935{
4936        return HandleBanHelper(BAN_IP,args);
4937}
4938
4939bool ChatHandler::HandleBanHelper(BanMode mode, const char* args)
4940{
4941        if(!args)
4942        return false;
4943
4944    char* cnameOrIP = strtok ((char*)args, " ");
4945        if (!cnameOrIP)
4946        return false;
4947
4948    std::string nameOrIP = cnameOrIP;
4949
4950        char* duration = strtok (NULL," ");
4951    if(!duration || !atoi(duration))
4952        return false;
4953
4954    char* reason = strtok (NULL,"");
4955    if(!reason)
4956        return false;
4957
4958    switch(mode)
4959        {
4960                case BAN_ACCOUNT:
4961                        if(!AccountMgr::normilizeString(nameOrIP))
4962                        {
4963                                PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,nameOrIP.c_str());
4964                                SetSentErrorMessage(true);
4965                                return false;
4966                        }
4967                        break;
4968                case BAN_CHARACTER:
4969                        if(!normalizePlayerName(nameOrIP))
4970                        {
4971                                SendSysMessage(LANG_PLAYER_NOT_FOUND);
4972                                SetSentErrorMessage(true);
4973                                return false;
4974                        }
4975                        break;
4976                case BAN_IP:
4977                        if(!IsIPAddress(nameOrIP.c_str()))
4978                                return false;
4979                        break;
4980        }
4981
4982        switch(sWorld.BanAccount(mode, nameOrIP, duration, reason,m_session ? m_session->GetPlayerName() : ""))
4983    {
4984        case BAN_SUCCESS:
4985            if(atoi(duration)>0)
4986                PSendSysMessage(LANG_BAN_YOUBANNED,nameOrIP.c_str(),secsToTimeString(TimeStringToSecs(duration),true).c_str(),reason);
4987            else
4988                PSendSysMessage(LANG_BAN_YOUPERMBANNED,nameOrIP.c_str(),reason);
4989            break;
4990        case BAN_SYNTAX_ERROR:
4991            return false;
4992        case BAN_NOTFOUND:
4993           switch(mode)
4994                   {
4995                           default:
4996                                   PSendSysMessage(LANG_BAN_NOTFOUND,"account",nameOrIP.c_str());
4997                                   break;
4998                           case BAN_CHARACTER:
4999                                   PSendSysMessage(LANG_BAN_NOTFOUND,"character",nameOrIP.c_str());
5000                                   break;
5001                           case BAN_IP:
5002                                   PSendSysMessage(LANG_BAN_NOTFOUND,"ip",nameOrIP.c_str());
5003                                   break;
5004
5005                   }
5006                   SetSentErrorMessage(true);
5007                   return false;
5008    }
5009
5010    return true;
5011}
5012
5013bool ChatHandler::HandleUnBanAccountCommand(const char* args)
5014{
5015    return HandleUnBanHelper(BAN_ACCOUNT,args);
5016}
5017
5018bool ChatHandler::HandleUnBanCharacterCommand(const char* args)
5019{
5020        return HandleUnBanHelper(BAN_CHARACTER,args);
5021}
5022
5023bool ChatHandler::HandleUnBanIPCommand(const char* args)
5024{
5025        return HandleUnBanHelper(BAN_IP,args);
5026}
5027
5028bool ChatHandler::HandleUnBanHelper(BanMode mode, const char* args)
5029{
5030    if(!args)
5031        return false;
5032
5033    char* cnameOrIP = strtok ((char*)args, " ");
5034        if(!cnameOrIP)
5035        return false;
5036
5037    std::string nameOrIP = cnameOrIP;
5038    switch(mode)
5039    {
5040        case BAN_ACCOUNT:
5041                        if(!AccountMgr::normilizeString(nameOrIP))
5042            {
5043                PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,nameOrIP.c_str());
5044                                SetSentErrorMessage(true);
5045                                return false;
5046                        }
5047                        break;
5048                case BAN_CHARACTER:
5049                        if(!normalizePlayerName(nameOrIP))
5050            {
5051                SendSysMessage(LANG_PLAYER_NOT_FOUND);
5052                SetSentErrorMessage(true);
5053                return false;
5054            }
5055                        break;
5056                case BAN_IP:
5057                        if(!IsIPAddress(nameOrIP.c_str()))
5058                                return false;
5059                        break;
5060        }
5061
5062        if(sWorld.RemoveBanAccount(mode,nameOrIP))
5063                PSendSysMessage(LANG_UNBAN_UNBANNED,nameOrIP.c_str());
5064        else
5065                PSendSysMessage(LANG_UNBAN_ERROR,nameOrIP.c_str());
5066
5067        return true;
5068}
5069
5070bool ChatHandler::HandleBanInfoAccountCommand(const char* args)
5071{
5072        if(!args)
5073                return false;
5074
5075        char* cname = strtok((char*)args, "");
5076        if(!cname)
5077                return false;
5078
5079        std::string account_name = cname;
5080        if(!AccountMgr::normilizeString(account_name))
5081        {
5082                PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
5083                SetSentErrorMessage(true);
5084                return false;
5085    }
5086   
5087        uint32 accountid = accmgr.GetId(account_name);
5088        if(!accountid)
5089    {
5090        PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
5091                return true;
5092    }
5093   
5094        return HandleBanInfoHelper(accountid,account_name.c_str());
5095}
5096
5097bool ChatHandler::HandleBanInfoCharacterCommand(const char* args)
5098{
5099    if(!args)
5100        return false;
5101
5102    char* cname = strtok ((char*)args, "");
5103        if(!cname)
5104        return false;
5105
5106    std::string name = cname;
5107        if(!normalizePlayerName(name))
5108        {
5109                SendSysMessage(LANG_PLAYER_NOT_FOUND);
5110                SetSentErrorMessage(true);
5111                return false;
5112        }
5113
5114    uint32 accountid = objmgr.GetPlayerAccountIdByPlayerName(name);
5115        if(!accountid)
5116    {
5117        SendSysMessage(LANG_PLAYER_NOT_FOUND);
5118                SetSentErrorMessage(true);
5119                return false;
5120        }
5121
5122        std::string accountname;
5123        if(!accmgr.GetName(accountid,accountname))
5124        {
5125                PSendSysMessage(LANG_BANINFO_NOCHARACTER);
5126        return true;
5127    }
5128   
5129        return HandleBanInfoHelper(accountid,accountname.c_str());
5130}
5131
5132bool ChatHandler::HandleBanInfoHelper(uint32 accountid, char const* accountname)
5133{
5134        QueryResult *result = loginDatabase.PQuery("SELECT FROM_UNIXTIME(bandate), unbandate-bandate, active, unbandate,banreason,bannedby FROM account_banned WHERE id = '%u' ORDER BY bandate ASC",accountid);
5135        if(!result)
5136    {
5137        PSendSysMessage(LANG_BANINFO_NOACCOUNTBAN, accountname);
5138                return true;
5139    }
5140   
5141        PSendSysMessage(LANG_BANINFO_BANHISTORY,accountname);
5142        do
5143    {
5144        Field* fields = result->Fetch();
5145
5146                time_t unbandate = time_t(fields[3].GetUInt64());
5147                bool active = false;
5148                if(fields[2].GetBool() && (fields[1].GetUInt64() == (uint64)0 ||unbandate >= time(NULL)) )
5149                        active = true;
5150                bool permanent = (fields[1].GetUInt64() == (uint64)0);
5151                std::string bantime = permanent?GetTrinityString(LANG_BANINFO_INFINITE):secsToTimeString(fields[1].GetUInt64(), true);
5152                PSendSysMessage(LANG_BANINFO_HISTORYENTRY,
5153                        fields[0].GetString(), bantime.c_str(), active ? GetTrinityString(LANG_BANINFO_YES):GetTrinityString(LANG_BANINFO_NO), fields[4].GetString(), fields[5].GetString());
5154        }while (result->NextRow());
5155
5156        delete result;
5157        return true;
5158}
5159
5160bool ChatHandler::HandleBanInfoIPCommand(const char* args)
5161{
5162        if(!args)
5163                return false;
5164
5165        char* cIP = strtok ((char*)args, "");
5166        if(!cIP)
5167                return false;
5168
5169        if (!IsIPAddress(cIP))
5170                return false;
5171
5172        std::string IP = cIP;
5173
5174        loginDatabase.escape_string(IP);
5175        QueryResult *result = loginDatabase.PQuery("SELECT ip, FROM_UNIXTIME(bandate), FROM_UNIXTIME(unbandate), unbandate-UNIX_TIMESTAMP(), banreason,bannedby,unbandate-bandate FROM ip_banned WHERE ip = '%s'",IP.c_str());
5176        if(!result)
5177        {
5178                PSendSysMessage(LANG_BANINFO_NOIP);
5179                return true;
5180    }
5181   
5182        Field *fields = result->Fetch();
5183        bool permanent = !fields[6].GetUInt64();
5184        PSendSysMessage(LANG_BANINFO_IPENTRY,
5185                fields[0].GetString(), fields[1].GetString(), permanent ? GetTrinityString(LANG_BANINFO_NEVER):fields[2].GetString(),
5186                permanent ? GetTrinityString(LANG_BANINFO_INFINITE):secsToTimeString(fields[3].GetUInt64(), true).c_str(), fields[4].GetString(), fields[5].GetString());
5187        delete result;
5188        return true;
5189}
5190
5191bool ChatHandler::HandleBanListCharacterCommand(const char* args)
5192{
5193        loginDatabase.Execute("DELETE FROM ip_banned WHERE unbandate<=UNIX_TIMESTAMP() AND unbandate<>bandate");
5194
5195        char* cFilter = strtok ((char*)args, " ");
5196        if(!cFilter)
5197        return false;
5198
5199        std::string filter = cFilter;
5200        loginDatabase.escape_string(filter);
5201        QueryResult* result = CharacterDatabase.PQuery("SELECT account FROM characters WHERE name "_LIKE_" "_CONCAT3_("'%%'","'%s'","'%%'"),filter.c_str());
5202        if (!result)
5203        {
5204                PSendSysMessage(LANG_BANLIST_NOCHARACTER);
5205                return true;
5206        }
5207
5208        return HandleBanListHelper(result);
5209}
5210
5211bool ChatHandler::HandleBanListAccountCommand(const char* args)
5212{
5213        loginDatabase.Execute("DELETE FROM ip_banned WHERE unbandate<=UNIX_TIMESTAMP() AND unbandate<>bandate");
5214
5215        char* cFilter = strtok((char*)args, " ");
5216        std::string filter = cFilter ? cFilter : "";
5217        loginDatabase.escape_string(filter);
5218
5219        QueryResult* result;
5220
5221        if(filter.empty())
5222        {
5223                 result = loginDatabase.Query("SELECT account.id, username FROM account, account_banned"
5224                         " WHERE account.id = account_banned.id AND active = 1 GROUP BY account.id");
5225        }
5226        else
5227        {
5228                result = loginDatabase.PQuery("SELECT account.id, username FROM account, account_banned"
5229                        " WHERE account.id = account_banned.id AND active = 1 AND username "_LIKE_" "_CONCAT3_("'%%'","'%s'","'%%'")" GROUP BY account.id",
5230                        filter.c_str());
5231        }
5232
5233        if (!result)
5234        {
5235                PSendSysMessage(LANG_BANLIST_NOACCOUNT);
5236                return true;
5237        }
5238
5239        return HandleBanListHelper(result);
5240}
5241
5242bool ChatHandler::HandleBanListHelper(QueryResult* result)
5243{
5244    PSendSysMessage(LANG_BANLIST_MATCHINGACCOUNT);
5245   
5246        // Chat short output
5247        if(m_session)
5248    {
5249        do
5250                {
5251                        Field* fields = result->Fetch();
5252                        uint32 accountid = fields[0].GetUInt32();
5253
5254                        QueryResult* banresult = loginDatabase.PQuery("SELECT account.username FROM account,account_banned WHERE account_banned.id='%u' AND account_banned.id=account.id",accountid);
5255                        if(banresult)
5256                        {
5257                                Field* fields2 = banresult->Fetch();
5258                                PSendSysMessage("%s",fields2[0].GetString());
5259                                delete banresult;
5260                        }
5261                } while (result->NextRow());
5262        }
5263        // Console wide output
5264        else
5265        {
5266                SendSysMessage(LANG_BANLIST_ACCOUNTS);
5267                SendSysMessage("===============================================================================");
5268                SendSysMessage(LANG_BANLIST_ACCOUNTS_HEADER);
5269                do
5270                {
5271                        SendSysMessage("-------------------------------------------------------------------------------");
5272                        Field *fields = result->Fetch();
5273                        uint32 account_id = fields[0].GetUInt32 ();
5274
5275                        std::string account_name;
5276
5277                        // "account" case, name can be get in same quary
5278                        if(result->GetFieldCount() > 1)
5279                                account_name = fields[1].GetCppString();
5280                        // "character" case, name need extract from another DB
5281                        else
5282                                accmgr.GetName (account_id,account_name);
5283
5284                        // No SQL injection. id is uint32.
5285                        QueryResult *banInfo = loginDatabase.PQuery("SELECT bandate,unbandate,bannedby,banreason FROM account_banned WHERE id = %u ORDER BY unbandate", account_id);
5286                        if (banInfo)
5287                        {
5288                                Field *fields2 = banInfo->Fetch();
5289                                do
5290                                {
5291                                        time_t t_ban = fields2[0].GetUInt64();
5292                                        tm* aTm_ban = localtime(&t_ban);
5293
5294                                        if (fields2[0].GetUInt64() == fields2[1].GetUInt64())
5295                                        {
5296                                                PSendSysMessage("|%-15.15s|%02d-%02d-%02d %02d:%02d|   permanent  |%-15.15s|%-15.15s|",
5297                                                        account_name.c_str(),aTm_ban->tm_year%100, aTm_ban->tm_mon+1, aTm_ban->tm_mday, aTm_ban->tm_hour, aTm_ban->tm_min,
5298                                                        fields2[2].GetString(),fields2[3].GetString());
5299                                        }
5300                                        else
5301                                        {
5302                                                time_t t_unban = fields2[1].GetUInt64();
5303                                                tm* aTm_unban = localtime(&t_unban);
5304                                                PSendSysMessage("|%-15.15s|%02d-%02d-%02d %02d:%02d|%02d-%02d-%02d %02d:%02d|%-15.15s|%-15.15s|",
5305                                                        account_name.c_str(),aTm_ban->tm_year%100, aTm_ban->tm_mon+1, aTm_ban->tm_mday, aTm_ban->tm_hour, aTm_ban->tm_min,
5306                                                        aTm_unban->tm_year%100, aTm_unban->tm_mon+1, aTm_unban->tm_mday, aTm_unban->tm_hour, aTm_unban->tm_min,
5307                                                        fields2[2].GetString(),fields2[3].GetString());
5308                                        }
5309                                }while ( banInfo->NextRow() );
5310                                delete banInfo;
5311                        }
5312                }while( result->NextRow() );
5313                SendSysMessage("===============================================================================");
5314        }
5315
5316        delete result;
5317        return true;
5318}
5319
5320bool ChatHandler::HandleBanListIPCommand(const char* args)
5321{
5322        loginDatabase.Execute("DELETE FROM ip_banned WHERE unbandate<=UNIX_TIMESTAMP() AND unbandate<>bandate");
5323
5324        char* cFilter = strtok((char*)args, " ");
5325        std::string filter = cFilter ? cFilter : "";
5326        loginDatabase.escape_string(filter);
5327
5328        QueryResult* result;
5329
5330        if(filter.empty())
5331        {
5332                result = loginDatabase.Query ("SELECT ip,bandate,unbandate,bannedby,banreason FROM ip_banned"
5333                        " WHERE (bandate=unbandate OR unbandate>UNIX_TIMESTAMP())"
5334                        " ORDER BY unbandate" );
5335        }
5336        else
5337        {
5338                result = loginDatabase.PQuery( "SELECT ip,bandate,unbandate,bannedby,banreason FROM ip_banned"
5339                        " WHERE (bandate=unbandate OR unbandate>UNIX_TIMESTAMP()) AND ip "_LIKE_" "_CONCAT3_("'%%'","'%s'","'%%'")
5340                        " ORDER BY unbandate",filter.c_str() );
5341        }
5342
5343        if(!result)
5344        {
5345                PSendSysMessage(LANG_BANLIST_NOIP);
5346                return true;
5347        }
5348
5349        PSendSysMessage(LANG_BANLIST_MATCHINGIP);
5350        // Chat short output
5351        if(m_session)
5352        {
5353                do
5354                {
5355                        Field* fields = result->Fetch();
5356                        PSendSysMessage("%s",fields[0].GetString());
5357                }while (result->NextRow());
5358        }
5359        // Console wide output
5360        else
5361        {
5362                SendSysMessage(LANG_BANLIST_IPS);SendSysMessage("===============================================================================");
5363                SendSysMessage(LANG_BANLIST_IPS_HEADER);
5364                do
5365        {
5366            SendSysMessage("-------------------------------------------------------------------------------");
5367                        Field *fields = result->Fetch();
5368                        time_t t_ban = fields[1].GetUInt64();
5369                        tm* aTm_ban = localtime(&t_ban);
5370                        if ( fields[1].GetUInt64() == fields[2].GetUInt64() )
5371                        {
5372                                PSendSysMessage("|%-15.15s|%02d-%02d-%02d %02d:%02d|   permanent  |%-15.15s|%-15.15s|",
5373                                        fields[0].GetString(), aTm_ban->tm_year%100, aTm_ban->tm_mon+1, aTm_ban->tm_mday, aTm_ban->tm_hour, aTm_ban->tm_min,
5374                                        fields[3].GetString(), fields[4].GetString());
5375                        }
5376                        else
5377                        {
5378                                time_t t_unban = fields[2].GetUInt64();
5379                                tm* aTm_unban = localtime(&t_unban);
5380                                PSendSysMessage("|%-15.15s|%02d-%02d-%02d %02d:%02d|%02d-%02d-%02d %02d:%02d|%-15.15s|%-15.15s|",
5381                                        fields[0].GetString(), aTm_ban->tm_year%100, aTm_ban->tm_mon+1, aTm_ban->tm_mday, aTm_ban->tm_hour, aTm_ban->tm_min,
5382                                        aTm_unban->tm_year%100, aTm_unban->tm_mon+1, aTm_unban->tm_mday, aTm_unban->tm_hour, aTm_unban->tm_min,
5383                                        fields[3].GetString(), fields[4].GetString());
5384                        }
5385                }while( result->NextRow() );
5386                SendSysMessage("===============================================================================");
5387    }
5388
5389    delete result;
5390    return true;
5391}
5392
5393bool ChatHandler::HandleRespawnCommand(const char* /*args*/)
5394{
5395    Unit* target = getSelectedUnit();
5396
5397    if(target && target->GetTypeId() == TYPEID_UNIT && target->isDead())
5398    {
5399        ((Creature*)target)->Respawn();
5400        return true;
5401    }
5402
5403    Player* pl = m_session->GetPlayer();
5404
5405    CellPair p(Trinity::ComputeCellPair(pl->GetPositionX(), pl->GetPositionY()));
5406    Cell cell(p);
5407    cell.data.Part.reserved = ALL_DISTRICT;
5408    cell.SetNoCreate();
5409
5410    Trinity::RespawnDo u_do;
5411    Trinity::WorldObjectWorker<Trinity::RespawnDo> worker(u_do);
5412
5413    TypeContainerVisitor<Trinity::WorldObjectWorker<Trinity::RespawnDo>, GridTypeMapContainer > obj_worker(worker);
5414    CellLock<GridReadGuard> cell_lock(cell, p);
5415    cell_lock->Visit(cell_lock, obj_worker, *MapManager::Instance().GetMap(pl->GetMapId(), pl));
5416
5417    return true;
5418}
5419
5420bool ChatHandler::HandleFlyModeCommand(const char* args)
5421{
5422    if(!args)
5423        return false;
5424
5425    Unit *unit = getSelectedUnit();
5426    if (!unit || (unit->GetTypeId() != TYPEID_PLAYER))
5427        unit = m_session->GetPlayer();
5428
5429    WorldPacket data(12);
5430    if (strncmp(args, "on", 3) == 0)
5431        data.SetOpcode(SMSG_MOVE_SET_CAN_FLY);
5432    else if (strncmp(args, "off", 4) == 0)
5433        data.SetOpcode(SMSG_MOVE_UNSET_CAN_FLY);
5434    else
5435    {
5436        SendSysMessage(LANG_USE_BOL);
5437        return false;
5438    }
5439    data.append(unit->GetPackGUID());
5440    data << uint32(0);                                      // unknown
5441    unit->SendMessageToSet(&data, true);
5442    PSendSysMessage(LANG_COMMAND_FLYMODE_STATUS, unit->GetName(), args);
5443    return true;
5444}
5445
5446bool ChatHandler::HandleLoadPDumpCommand(const char *args)
5447{
5448    if (!args)
5449        return false;
5450
5451    char * file = strtok((char*)args, " ");
5452        if(!file)
5453                return false;
5454
5455        char * account = strtok(NULL, " ");
5456        if(!account)
5457                return false;
5458
5459        std::string account_name = account;
5460        if(!AccountMgr::normilizeString(account_name))
5461        {
5462                PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
5463                SetSentErrorMessage(true);
5464        return false;
5465        }
5466
5467    uint32 account_id = accmgr.GetId(account_name);
5468    if(!account_id)
5469    {
5470        account_id = atoi(account);                             // use original string
5471        if(!account_id)
5472        {
5473            PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
5474                        SetSentErrorMessage(true);
5475                        return false;
5476        }
5477        }
5478
5479        if(!accmgr.GetName(account_id,account_name))
5480        {
5481                PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
5482                SetSentErrorMessage(true);
5483                return false;
5484        }
5485
5486        char* guid_str = NULL;
5487        char* name_str = strtok(NULL, " ");
5488
5489        std::string name;
5490        if(name_str)
5491        {
5492                name = name_str;
5493                // normalize the name if specified and check if it exists
5494                if(!normalizePlayerName(name))
5495                {
5496                        PSendSysMessage(LANG_INVALID_CHARACTER_NAME);
5497                        SetSentErrorMessage(true);
5498                        return false;
5499                }
5500
5501                if(!ObjectMgr::IsValidName(name,true))
5502                {
5503                        PSendSysMessage(LANG_INVALID_CHARACTER_NAME);
5504                        SetSentErrorMessage(true);
5505            return false;
5506                }
5507
5508                guid_str = strtok(NULL, " ");
5509    }
5510
5511    uint32 guid = 0;
5512
5513    if(guid_str)
5514        {
5515                guid = atoi(guid_str);
5516                if(!guid)
5517                {
5518                        PSendSysMessage(LANG_INVALID_CHARACTER_GUID);
5519                        SetSentErrorMessage(true);
5520                        return false;
5521                }
5522               
5523                if(objmgr.GetPlayerAccountIdByGUID(guid))
5524                {
5525                        PSendSysMessage(LANG_CHARACTER_GUID_IN_USE,guid);
5526                        SetSentErrorMessage(true);
5527                        return false;
5528                }
5529        }
5530
5531        switch(PlayerDumpReader().LoadDump(file, account_id, name, guid))
5532        {
5533                case DUMP_SUCCESS:
5534                        PSendSysMessage(LANG_COMMAND_IMPORT_SUCCESS);
5535                        break;
5536                case DUMP_FILE_OPEN_ERROR:
5537                        PSendSysMessage(LANG_FILE_OPEN_FAIL,file);
5538                        SetSentErrorMessage(true);
5539                        return false;
5540                case DUMP_FILE_BROKEN:
5541                        PSendSysMessage(LANG_DUMP_BROKEN,file);
5542                        SetSentErrorMessage(true);
5543                        return false;
5544                case DUMP_TOO_MANY_CHARS:
5545                        PSendSysMessage(LANG_ACCOUNT_CHARACTER_LIST_FULL,account_name.c_str(),account_id);
5546                        SetSentErrorMessage(true);
5547                        return false;
5548                default:
5549                        PSendSysMessage(LANG_COMMAND_IMPORT_FAILED);
5550                        SetSentErrorMessage(true);
5551                        return false;
5552        }
5553
5554    return true;
5555}
5556
5557bool ChatHandler::HandleChangeEntryCommand(const char *args)
5558{
5559    if(!args)
5560        return false;
5561
5562    uint32 newEntryNum = atoi(args);
5563    if(!newEntryNum)
5564        return false;
5565
5566    Unit* unit = getSelectedUnit();
5567    if(!unit || unit->GetTypeId() != TYPEID_UNIT)
5568    {
5569        SendSysMessage(LANG_SELECT_CREATURE);
5570        SetSentErrorMessage(true);
5571        return false;
5572    }
5573    Creature* creature = (Creature*)unit;
5574    if(creature->UpdateEntry(newEntryNum))
5575        SendSysMessage(LANG_DONE);
5576    else
5577        SendSysMessage(LANG_ERROR);
5578    return true;
5579}
5580
5581bool ChatHandler::HandleWritePDumpCommand(const char *args)
5582{
5583    if(!args)
5584        return false;
5585
5586    char* file = strtok((char*)args, " ");
5587    char* p2 = strtok(NULL, " ");
5588
5589    if(!file || !p2)
5590        return false;
5591
5592    uint32 guid = objmgr.GetPlayerGUIDByName(p2);
5593    if(!guid)
5594        guid = atoi(p2);
5595
5596    if(!objmgr.GetPlayerAccountIdByGUID(guid))
5597        {
5598                PSendSysMessage(LANG_PLAYER_NOT_FOUND);
5599                SetSentErrorMessage(true);
5600                return false;
5601        }
5602
5603        switch(PlayerDumpWriter().WriteDump(file, guid))
5604        {
5605                case DUMP_SUCCESS:
5606                        PSendSysMessage(LANG_COMMAND_EXPORT_SUCCESS);
5607                        break;
5608                case DUMP_FILE_OPEN_ERROR:
5609                        PSendSysMessage(LANG_FILE_OPEN_FAIL,file);
5610                        SetSentErrorMessage(true);
5611                        return false;
5612                default:
5613                        PSendSysMessage(LANG_COMMAND_EXPORT_FAILED);
5614                        SetSentErrorMessage(true);
5615                        return false;
5616        }
5617
5618    return true;
5619}
5620
5621bool ChatHandler::HandleMovegensCommand(const char* /*args*/)
5622{
5623    Unit* unit = getSelectedUnit();
5624    if(!unit)
5625    {
5626        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
5627        SetSentErrorMessage(true);
5628        return false;
5629    }
5630
5631    PSendSysMessage(LANG_MOVEGENS_LIST,(unit->GetTypeId()==TYPEID_PLAYER ? "Player" : "Creature" ),unit->GetGUIDLow());
5632
5633    MotionMaster* mm = unit->GetMotionMaster();
5634    for(MotionMaster::const_iterator itr = mm->begin(); itr != mm->end(); ++itr)
5635    {
5636        switch((*itr)->GetMovementGeneratorType())
5637        {
5638            case IDLE_MOTION_TYPE:          SendSysMessage(LANG_MOVEGENS_IDLE);          break;
5639            case RANDOM_MOTION_TYPE:        SendSysMessage(LANG_MOVEGENS_RANDOM);        break;
5640            case WAYPOINT_MOTION_TYPE:      SendSysMessage(LANG_MOVEGENS_WAYPOINT);      break;
5641            case ANIMAL_RANDOM_MOTION_TYPE: SendSysMessage(LANG_MOVEGENS_ANIMAL_RANDOM); break;
5642            case CONFUSED_MOTION_TYPE:      SendSysMessage(LANG_MOVEGENS_CONFUSED);      break;
5643            case TARGETED_MOTION_TYPE:
5644            {
5645                if(unit->GetTypeId()==TYPEID_PLAYER)
5646                {
5647                    TargetedMovementGenerator<Player> const* mgen = static_cast<TargetedMovementGenerator<Player> const*>(*itr);
5648                    Unit* target = mgen->GetTarget();
5649                    if(target)
5650                        PSendSysMessage(LANG_MOVEGENS_TARGETED_PLAYER,target->GetName(),target->GetGUIDLow());
5651                    else
5652                        SendSysMessage(LANG_MOVEGENS_TARGETED_NULL);
5653                }
5654                else
5655                {
5656                    TargetedMovementGenerator<Creature> const* mgen = static_cast<TargetedMovementGenerator<Creature> const*>(*itr);
5657                    Unit* target = mgen->GetTarget();
5658                    if(target)
5659                        PSendSysMessage(LANG_MOVEGENS_TARGETED_CREATURE,target->GetName(),target->GetGUIDLow());
5660                    else
5661                        SendSysMessage(LANG_MOVEGENS_TARGETED_NULL);
5662                }
5663                break;
5664            }
5665            case HOME_MOTION_TYPE:
5666                if(unit->GetTypeId()==TYPEID_UNIT)
5667                {
5668                    float x,y,z;
5669                    (*itr)->GetDestination(x,y,z);
5670                    PSendSysMessage(LANG_MOVEGENS_HOME_CREATURE,x,y,z);
5671                }
5672                else
5673                    SendSysMessage(LANG_MOVEGENS_HOME_PLAYER);
5674                break;
5675            case FLIGHT_MOTION_TYPE:   SendSysMessage(LANG_MOVEGENS_FLIGHT);  break;
5676            case POINT_MOTION_TYPE:
5677            {
5678                float x,y,z;
5679                (*itr)->GetDestination(x,y,z);
5680                PSendSysMessage(LANG_MOVEGENS_POINT,x,y,z);
5681                break;
5682            }
5683            case FLEEING_MOTION_TYPE:  SendSysMessage(LANG_MOVEGENS_FEAR);    break;
5684            case DISTRACT_MOTION_TYPE: SendSysMessage(LANG_MOVEGENS_DISTRACT);  break;
5685            default:
5686                PSendSysMessage(LANG_MOVEGENS_UNKNOWN,(*itr)->GetMovementGeneratorType());
5687                break;
5688        }
5689    }
5690    return true;
5691}
5692
5693bool ChatHandler::HandlePLimitCommand(const char *args)
5694{
5695    if(*args)
5696    {
5697        char* param = strtok((char*)args, " ");
5698        if(!param)
5699            return false;
5700
5701        int l = strlen(param);
5702
5703        if(     strncmp(param,"player",l) == 0 )
5704            sWorld.SetPlayerLimit(-SEC_PLAYER);
5705        else if(strncmp(param,"moderator",l) == 0 )
5706            sWorld.SetPlayerLimit(-SEC_MODERATOR);
5707        else if(strncmp(param,"gamemaster",l) == 0 )
5708            sWorld.SetPlayerLimit(-SEC_GAMEMASTER);
5709        else if(strncmp(param,"administrator",l) == 0 )
5710            sWorld.SetPlayerLimit(-SEC_ADMINISTRATOR);
5711        else if(strncmp(param,"reset",l) == 0 )
5712            sWorld.SetPlayerLimit( sConfig.GetIntDefault("PlayerLimit", DEFAULT_PLAYER_LIMIT) );
5713        else
5714        {
5715            int val = atoi(param);
5716            if(val < -SEC_ADMINISTRATOR) val = -SEC_ADMINISTRATOR;
5717
5718            sWorld.SetPlayerLimit(val);
5719        }
5720
5721        // kick all low security level players
5722        if(sWorld.GetPlayerAmountLimit() > SEC_PLAYER)
5723            sWorld.KickAllLess(sWorld.GetPlayerSecurityLimit());
5724    }
5725
5726    uint32 pLimit = sWorld.GetPlayerAmountLimit();
5727    AccountTypes allowedAccountType = sWorld.GetPlayerSecurityLimit();
5728    char const* secName = "";
5729    switch(allowedAccountType)
5730    {
5731        case SEC_PLAYER:        secName = "Player";        break;
5732        case SEC_MODERATOR:     secName = "Moderator";     break;
5733        case SEC_GAMEMASTER:    secName = "Gamemaster";    break;
5734        case SEC_ADMINISTRATOR: secName = "Administrator"; break;
5735        default:                secName = "<unknown>";     break;
5736    }
5737
5738    PSendSysMessage("Player limits: amount %u, min. security level %s.",pLimit,secName);
5739
5740    return true;
5741}
5742
5743bool ChatHandler::HandleCastCommand(const char* args)
5744{
5745    if(!*args)
5746        return false;
5747
5748    Unit* target = getSelectedUnit();
5749
5750    if(!target)
5751    {
5752        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
5753        SetSentErrorMessage(true);
5754        return false;
5755    }
5756
5757    // number or [name] Shift-click form |color|Hspell:spell_id|h[name]|h|r or Htalent form
5758    uint32 spell = extractSpellIdFromLink((char*)args);
5759    if(!spell)
5760        return false;
5761
5762    SpellEntry const* spellInfo = sSpellStore.LookupEntry(spell);
5763    if(!spellInfo)
5764        return false;
5765
5766    if(!SpellMgr::IsSpellValid(spellInfo,m_session->GetPlayer()))
5767    {
5768        PSendSysMessage(LANG_COMMAND_SPELL_BROKEN,spell);
5769        SetSentErrorMessage(true);
5770        return false;
5771    }
5772
5773    char* trig_str = strtok(NULL, " ");
5774    if(trig_str)
5775    {
5776        int l = strlen(trig_str);
5777        if(strncmp(trig_str,"triggered",l) != 0 )
5778            return false;
5779    }
5780
5781    bool triggered = (trig_str != NULL);
5782
5783    m_session->GetPlayer()->CastSpell(target,spell,triggered);
5784
5785    return true;
5786}
5787
5788bool ChatHandler::HandleCastBackCommand(const char* args)
5789{
5790    Creature* caster = getSelectedCreature();
5791
5792    if(!caster)
5793    {
5794        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
5795        SetSentErrorMessage(true);
5796        return false;
5797    }
5798
5799    // number or [name] Shift-click form |color|Hspell:spell_id|h[name]|h|r
5800    // number or [name] Shift-click form |color|Hspell:spell_id|h[name]|h|r or Htalent form
5801    uint32 spell = extractSpellIdFromLink((char*)args);
5802    if(!spell || !sSpellStore.LookupEntry(spell))
5803        return false;
5804
5805    char* trig_str = strtok(NULL, " ");
5806    if(trig_str)
5807    {
5808        int l = strlen(trig_str);
5809        if(strncmp(trig_str,"triggered",l) != 0 )
5810            return false;
5811    }
5812
5813    bool triggered = (trig_str != NULL);
5814
5815    // update orientation at server
5816    caster->SetOrientation(caster->GetAngle(m_session->GetPlayer()));
5817
5818    // and client
5819    WorldPacket data;
5820    caster->BuildHeartBeatMsg(&data);
5821    caster->SendMessageToSet(&data,true);
5822
5823    caster->CastSpell(m_session->GetPlayer(),spell,false);
5824
5825    return true;
5826}
5827
5828bool ChatHandler::HandleCastDistCommand(const char* args)
5829{
5830    if(!*args)
5831        return false;
5832
5833
5834    // number or [name] Shift-click form |color|Hspell:spell_id|h[name]|h|r or Htalent form
5835    uint32 spell = extractSpellIdFromLink((char*)args);
5836    if(!spell)
5837        return false;
5838
5839    SpellEntry const* spellInfo = sSpellStore.LookupEntry(spell);
5840    if(!spellInfo)
5841        return false;
5842
5843    if(!SpellMgr::IsSpellValid(spellInfo,m_session->GetPlayer()))
5844    {
5845        PSendSysMessage(LANG_COMMAND_SPELL_BROKEN,spell);
5846        SetSentErrorMessage(true);
5847        return false;
5848    }
5849
5850    char *distStr = strtok(NULL, " ");
5851
5852    float dist = 0;
5853
5854    if(distStr)
5855        sscanf(distStr, "%f", &dist);
5856
5857    char* trig_str = strtok(NULL, " ");
5858    if(trig_str)
5859    {
5860        int l = strlen(trig_str);
5861        if(strncmp(trig_str,"triggered",l) != 0 )
5862            return false;
5863    }
5864
5865    bool triggered = (trig_str != NULL);
5866
5867    float x,y,z;
5868    m_session->GetPlayer()->GetClosePoint(x,y,z,dist);
5869
5870    m_session->GetPlayer()->CastSpell(x,y,z,spell,triggered);
5871    return true;
5872}
5873
5874bool ChatHandler::HandleCastTargetCommand(const char* args)
5875{
5876    Creature* caster = getSelectedCreature();
5877
5878    if(!caster)
5879    {
5880        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
5881        SetSentErrorMessage(true);
5882        return false;
5883    }
5884
5885    if(!caster->getVictim())
5886    {
5887        SendSysMessage(LANG_SELECTED_TARGET_NOT_HAVE_VICTIM);
5888        SetSentErrorMessage(true);
5889        return false;
5890    }
5891
5892    // number or [name] Shift-click form |color|Hspell:spell_id|h[name]|h|r or Htalent form
5893    uint32 spell = extractSpellIdFromLink((char*)args);
5894    if(!spell || !sSpellStore.LookupEntry(spell))
5895        return false;
5896
5897    char* trig_str = strtok(NULL, " ");
5898    if(trig_str)
5899    {
5900        int l = strlen(trig_str);
5901        if(strncmp(trig_str,"triggered",l) != 0 )
5902            return false;
5903    }
5904
5905    bool triggered = (trig_str != NULL);
5906
5907    // update orientation at server
5908    caster->SetOrientation(caster->GetAngle(m_session->GetPlayer()));
5909
5910    // and client
5911    WorldPacket data;
5912    caster->BuildHeartBeatMsg(&data);
5913    caster->SendMessageToSet(&data,true);
5914
5915    caster->CastSpell(caster->getVictim(),spell,false);
5916
5917    return true;
5918}
5919
5920/*
5921ComeToMe command REQUIRED for 3rd party scripting library to have access to PointMovementGenerator
5922Without this function 3rd party scripting library will get linking errors (unresolved external)
5923when attempting to use the PointMovementGenerator
5924*/
5925bool ChatHandler::HandleComeToMeCommand(const char *args)
5926{
5927    Creature* caster = getSelectedCreature();
5928
5929    if(!caster)
5930    {
5931        SendSysMessage(LANG_SELECT_CREATURE);
5932        SetSentErrorMessage(true);
5933        return false;
5934    }
5935
5936    char* newFlagStr = strtok((char*)args, " ");
5937
5938    if(!newFlagStr)
5939        return false;
5940
5941    uint32 newFlags = atoi(newFlagStr);
5942
5943    caster->SetUnitMovementFlags(newFlags);
5944
5945    Player* pl = m_session->GetPlayer();
5946
5947    caster->GetMotionMaster()->MovePoint(0, pl->GetPositionX(), pl->GetPositionY(), pl->GetPositionZ());
5948    return true;
5949}
5950
5951bool ChatHandler::HandleCastSelfCommand(const char* args)
5952{
5953    if(!*args)
5954        return false;
5955
5956    Unit* target = getSelectedUnit();
5957
5958    if(!target)
5959    {
5960        SendSysMessage(LANG_SELECT_CHAR_OR_CREATURE);
5961        SetSentErrorMessage(true);
5962        return false;
5963    }
5964
5965    // number or [name] Shift-click form |color|Hspell:spell_id|h[name]|h|r or Htalent form
5966    uint32 spell = extractSpellIdFromLink((char*)args);
5967    if(!spell)
5968        return false;
5969
5970    SpellEntry const* spellInfo = sSpellStore.LookupEntry(spell);
5971    if(!spellInfo)
5972        return false;
5973
5974    if(!SpellMgr::IsSpellValid(spellInfo,m_session->GetPlayer()))
5975    {
5976        PSendSysMessage(LANG_COMMAND_SPELL_BROKEN,spell);
5977        SetSentErrorMessage(true);
5978        return false;
5979    }
5980
5981    target->CastSpell(target,spell,false);
5982
5983    return true;
5984}
5985
5986std::string GetTimeString(uint32 time)
5987{
5988    uint16 days = time / DAY, hours = (time % DAY) / HOUR, minute = (time % HOUR) / MINUTE;
5989    std::ostringstream ss;
5990    if(days) ss << days << "d ";
5991    if(hours) ss << hours << "h ";
5992    ss << minute << "m";
5993    return ss.str();
5994}
5995
5996bool ChatHandler::HandleInstanceListBindsCommand(const char* /*args*/)
5997{
5998    Player* player = getSelectedPlayer();
5999    if (!player) player = m_session->GetPlayer();
6000    uint32 counter = 0;
6001    for(uint8 i = 0; i < TOTAL_DIFFICULTIES; i++)
6002    {
6003        Player::BoundInstancesMap &binds = player->GetBoundInstances(i);
6004        for(Player::BoundInstancesMap::iterator itr = binds.begin(); itr != binds.end(); ++itr)
6005        {
6006            InstanceSave *save = itr->second.save;
6007            std::string timeleft = GetTimeString(save->GetResetTime() - time(NULL));
6008            PSendSysMessage("map: %d inst: %d perm: %s diff: %s canReset: %s TTR: %s", itr->first, save->GetInstanceId(), itr->second.perm ? "yes" : "no",  save->GetDifficulty() == DIFFICULTY_NORMAL ? "normal" : "heroic", save->CanReset() ? "yes" : "no", timeleft.c_str());
6009            counter++;
6010        }
6011    }
6012    PSendSysMessage("player binds: %d", counter);
6013    counter = 0;
6014    Group *group = player->GetGroup();
6015    if(group)
6016    {
6017        for(uint8 i = 0; i < TOTAL_DIFFICULTIES; i++)
6018        {
6019            Group::BoundInstancesMap &binds = group->GetBoundInstances(i);
6020            for(Group::BoundInstancesMap::iterator itr = binds.begin(); itr != binds.end(); ++itr)
6021            {
6022                InstanceSave *save = itr->second.save;
6023                std::string timeleft = GetTimeString(save->GetResetTime() - time(NULL));
6024                PSendSysMessage("map: %d inst: %d perm: %s diff: %s canReset: %s TTR: %s", itr->first, save->GetInstanceId(), itr->second.perm ? "yes" : "no",  save->GetDifficulty() == DIFFICULTY_NORMAL ? "normal" : "heroic", save->CanReset() ? "yes" : "no", timeleft.c_str());
6025                counter++;
6026            }
6027        }
6028    }
6029    PSendSysMessage("group binds: %d", counter);
6030
6031    return true;
6032}
6033
6034bool ChatHandler::HandleInstanceUnbindCommand(const char* args)
6035{
6036    if(!*args)
6037        return false;
6038
6039    std::string cmd = args;
6040    if(cmd == "all")
6041    {
6042        Player* player = getSelectedPlayer();
6043        if (!player) player = m_session->GetPlayer();
6044        uint32 counter = 0;
6045        for(uint8 i = 0; i < TOTAL_DIFFICULTIES; i++)
6046        {
6047            Player::BoundInstancesMap &binds = player->GetBoundInstances(i);
6048            for(Player::BoundInstancesMap::iterator itr = binds.begin(); itr != binds.end();)
6049            {
6050                if(itr->first != player->GetMapId())
6051                {
6052                    InstanceSave *save = itr->second.save;
6053                    std::string timeleft = GetTimeString(save->GetResetTime() - time(NULL));
6054                    PSendSysMessage("unbinding map: %d inst: %d perm: %s diff: %s canReset: %s TTR: %s", itr->first, save->GetInstanceId(), itr->second.perm ? "yes" : "no",  save->GetDifficulty() == DIFFICULTY_NORMAL ? "normal" : "heroic", save->CanReset() ? "yes" : "no", timeleft.c_str());
6055                    player->UnbindInstance(itr, i);
6056                    counter++;
6057                }
6058                else
6059                    ++itr;
6060            }
6061        }
6062        PSendSysMessage("instances unbound: %d", counter);
6063    }
6064    return true;
6065}
6066
6067bool ChatHandler::HandleInstanceStatsCommand(const char* /*args*/)
6068{
6069    PSendSysMessage("instances loaded: %d", MapManager::Instance().GetNumInstances());
6070    PSendSysMessage("players in instances: %d", MapManager::Instance().GetNumPlayersInInstances());
6071    PSendSysMessage("instance saves: %d", sInstanceSaveManager.GetNumInstanceSaves());
6072    PSendSysMessage("players bound: %d", sInstanceSaveManager.GetNumBoundPlayersTotal());
6073    PSendSysMessage("groups bound: %d", sInstanceSaveManager.GetNumBoundGroupsTotal());
6074    return true;
6075}
6076
6077bool ChatHandler::HandleInstanceSaveDataCommand(const char * /*args*/)
6078{
6079    Player* pl = m_session->GetPlayer();
6080
6081    Map* map = pl->GetMap();
6082    if (!map->IsDungeon())
6083    {
6084        PSendSysMessage("Map is not a dungeon.");
6085        SetSentErrorMessage(true);
6086        return false;
6087    }
6088
6089    if (!((InstanceMap*)map)->GetInstanceData())
6090    {
6091        PSendSysMessage("Map has no instance data.");
6092        SetSentErrorMessage(true);
6093        return false;
6094    }
6095
6096    ((InstanceMap*)map)->GetInstanceData()->SaveToDB();
6097    return true;
6098}
6099
6100/// Display the list of GMs
6101bool ChatHandler::HandleGMListFullCommand(const char* /*args*/)
6102{
6103        ///- Get the accounts with GM Level >0
6104        QueryResult *result = loginDatabase.Query( "SELECT username,gmlevel FROM account WHERE gmlevel > 0" );
6105        if(result)
6106        {
6107                SendSysMessage(LANG_GMLIST);
6108                SendSysMessage("========================");
6109                SendSysMessage(LANG_GMLIST_HEADER);
6110                SendSysMessage("========================");
6111
6112                ///- Circle through them. Display username and GM level
6113                do
6114                {
6115                        Field *fields = result->Fetch();
6116                        PSendSysMessage("|%15s|%6s|", fields[0].GetString(),fields[1].GetString());
6117                } while( result->NextRow() );
6118
6119                PSendSysMessage("========================");
6120                delete result;
6121        }
6122        else
6123                PSendSysMessage(LANG_GMLIST_EMPTY);
6124        return true;
6125}
6126
6127/// Define the 'Message of the day' for the realm
6128bool ChatHandler::HandleServerSetMotdCommand(const char* args)
6129{
6130        sWorld.SetMotd(args);
6131        PSendSysMessage(LANG_MOTD_NEW, args);
6132        return true;
6133}
6134
6135/// Set/Unset the expansion level for an account
6136bool ChatHandler::HandleAccountSetAddonCommand(const char* args)
6137{
6138        ///- Get the command line arguments
6139        char *szAcc = strtok((char*)args," ");
6140        char *szExp = strtok(NULL," ");
6141
6142        if(!szAcc)
6143                return false;
6144
6145        std::string account_name;
6146        uint32 account_id;
6147
6148        if(!szExp)
6149        {
6150                Player* player = getSelectedPlayer();
6151                if(!player)
6152                        return false;
6153
6154                account_id = player->GetSession()->GetAccountId();
6155                accmgr.GetName(account_id,account_name);
6156                szExp = szAcc;
6157        }
6158        else
6159        {
6160                ///- Convert Account name to Upper Format
6161                account_name = szAcc;
6162                if(!AccountMgr::normilizeString(account_name))
6163                {
6164                        PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
6165                        SetSentErrorMessage(true);
6166                        return false;
6167                }
6168
6169                account_id = accmgr.GetId(account_name);
6170                if(!account_id)
6171                {
6172                        PSendSysMessage(LANG_ACCOUNT_NOT_EXIST,account_name.c_str());
6173                        SetSentErrorMessage(true);
6174                        return false;
6175                }
6176        }
6177
6178        int lev=atoi(szExp);                                    //get int anyway (0 if error)
6179        if(lev < 0)
6180                return false;
6181
6182        // No SQL injection
6183        loginDatabase.PExecute("UPDATE account SET expansion = '%d' WHERE id = '%u'",lev,account_id);
6184        PSendSysMessage(LANG_ACCOUNT_SETADDON,account_name.c_str(),account_id,lev);
6185        return true;
6186}
6187
6188/// Send a message to a player in game
6189bool ChatHandler::HandleSendMessageCommand(const char* args)
6190{
6191        ///- Get the command line arguments
6192        char* name_str = strtok((char*)args, " ");
6193        char* msg_str = strtok(NULL, "");
6194
6195        if(!name_str || !msg_str)
6196                return false;
6197
6198        std::string name = name_str;
6199
6200        if(!normalizePlayerName(name))
6201                return false;
6202
6203        ///- Find the player and check that he is not logging out.
6204        Player *rPlayer = objmgr.GetPlayer(name.c_str());
6205        if(!rPlayer)
6206        {
6207                SendSysMessage(LANG_PLAYER_NOT_FOUND);
6208                SetSentErrorMessage(true);
6209                return false;
6210        }
6211
6212        if(rPlayer->GetSession()->isLogingOut())
6213        {
6214                SendSysMessage(LANG_PLAYER_NOT_FOUND);
6215                SetSentErrorMessage(true);
6216                return false;
6217        }
6218
6219        ///- Send the message
6220        //Use SendAreaTriggerMessage for fastest delivery.
6221        rPlayer->GetSession()->SendAreaTriggerMessage("%s", msg_str);
6222        rPlayer->GetSession()->SendAreaTriggerMessage("|cffff0000[Message from administrator]:|r");
6223
6224        //Confirmation message
6225        PSendSysMessage(LANG_SENDMESSAGE,name.c_str(),msg_str);
6226        return true;
6227}
6228
6229bool ChatHandler::HandlePlayAllCommand(const char* args)
6230{
6231        if(!*args)
6232                return false;
6233
6234        uint32 soundId = atoi((char*)args);
6235
6236        if(!sSoundEntriesStore.LookupEntry(soundId))
6237        {
6238                PSendSysMessage(LANG_SOUND_NOT_EXIST, soundId);
6239                SetSentErrorMessage(true);
6240                return false;
6241        }
6242
6243        WorldPacket data(SMSG_PLAY_SOUND, 4);
6244        data << uint32(soundId) << m_session->GetPlayer()->GetGUID();
6245        sWorld.SendGlobalMessage(&data);
6246
6247        PSendSysMessage(LANG_COMMAND_PLAYED_TO_ALL, soundId);
6248        return true;
6249}
6250
6251bool ChatHandler::HandleModifyGenderCommand(const char *args)
6252{
6253        if(!*args)      return false;
6254        Player *player = getSelectedPlayer();
6255
6256        if(!player)
6257        {
6258                PSendSysMessage(LANG_NO_PLAYER);
6259                SetSentErrorMessage(true);
6260                return false;
6261        }
6262
6263        std::string gender = (char*)args;
6264        uint32 displayId = player->GetNativeDisplayId();
6265
6266        if(gender == "male") // MALE
6267        {
6268                if(player->getGender() == GENDER_MALE)
6269                {
6270                        PSendSysMessage("%s is already male", player->GetName());
6271                        SetSentErrorMessage(true);
6272                        return false;
6273                }
6274
6275                // Set gender
6276                player->SetByteValue(UNIT_FIELD_BYTES_0, 2, GENDER_MALE);
6277                // Change display ID
6278                player->SetDisplayId(player->getRace() == RACE_BLOODELF ? displayId+1 : displayId-1);
6279                player->SetNativeDisplayId(player->getRace() == RACE_BLOODELF ? displayId+1 : displayId-1);
6280
6281                ChatHandler(player).PSendSysMessage("Gender changed. You are now a man!");
6282                PSendSysMessage("Gender changed for %s", player->GetName());
6283                return true;
6284        }
6285        else if(gender == "female") // FEMALE
6286        {
6287                if(player->getGender() == GENDER_FEMALE)
6288                {
6289                        PSendSysMessage("%s is already female", player->GetName());
6290                        SetSentErrorMessage(true);
6291                        return false;
6292                }
6293
6294                // Set gender
6295                player->SetByteValue(UNIT_FIELD_BYTES_0, 2, GENDER_FEMALE);
6296                // Change display ID
6297                player->SetDisplayId(player->getRace() == RACE_BLOODELF ? displayId-1 : displayId+1);
6298                player->SetNativeDisplayId(player->getRace() == RACE_BLOODELF ? displayId-1 : displayId+1);
6299
6300                ChatHandler(player).PSendSysMessage("Gender changed. You are now a woman!");
6301                PSendSysMessage("Gender changed for %s", player->GetName());
6302                return true;
6303        }
6304        else
6305        {
6306                PSendSysMessage("You must use male or female as gender.");
6307                SetSentErrorMessage(true);
6308                return false;
6309        }
6310
6311        return true;
6312}
6313
6314bool ChatHandler::HandleFreezeCommand(const char *args)
6315{
6316    std::string name;
6317    Player* player;
6318    char* TargetName = strtok((char*)args, " "); //get entered name
6319    if (!TargetName) //if no name entered use target
6320    {
6321        player = getSelectedPlayer();
6322        if (player) //prevent crash with creature as target
6323        {   
6324           name = player->GetName();
6325           normalizePlayerName(name);
6326        }
6327    }
6328    else // if name entered
6329    {
6330        name = TargetName;
6331        normalizePlayerName(name);
6332        player = objmgr.GetPlayer(name.c_str()); //get player by name
6333    }
6334   
6335    if (!player)
6336    {
6337        SendSysMessage(LANG_COMMAND_FREEZE_WRONG);
6338        return true;
6339    }
6340
6341    if (player==m_session->GetPlayer())
6342    {
6343        SendSysMessage(LANG_COMMAND_FREEZE_ERROR);
6344        return true;
6345    }
6346
6347    //effect
6348    if ((player) && (!(player==m_session->GetPlayer())))
6349    {
6350        PSendSysMessage(LANG_COMMAND_FREEZE,name.c_str());
6351
6352        //stop combat + make player unattackable + duel stop + stop some spells
6353        player->setFaction(35);
6354        player->CombatStop();
6355        if(player->IsNonMeleeSpellCasted(true))
6356        player->InterruptNonMeleeSpells(true);
6357        player->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE);
6358        player->SetUInt32Value(PLAYER_DUEL_TEAM, 1);
6359
6360        //if player class = hunter || warlock remove pet if alive
6361        if((player->getClass() == CLASS_HUNTER) || (player->getClass() == CLASS_WARLOCK))
6362        {
6363            if(Pet* pet = player->GetPet())
6364            {
6365                pet->SavePetToDB(PET_SAVE_AS_CURRENT);
6366                // not let dismiss dead pet
6367                if(pet && pet->isAlive())
6368                player->RemovePet(pet,PET_SAVE_NOT_IN_SLOT);
6369            }
6370        }
6371
6372        //stop movement and disable spells
6373        uint32 spellID = 9454;
6374        //m_session->GetPlayer()->CastSpell(player,spellID,false);
6375        SpellEntry const *spellInfo = sSpellStore.LookupEntry( spellID );
6376        if(spellInfo) //TODO: Change the duration of the aura to -1 instead of 5000000
6377        {
6378            for(uint32 i = 0;i<3;i++)
6379            {
6380                uint8 eff = spellInfo->Effect[i];
6381                if (eff>=TOTAL_SPELL_EFFECTS)
6382                    continue;
6383                if( eff == SPELL_EFFECT_APPLY_AREA_AURA_PARTY || eff == SPELL_EFFECT_APPLY_AURA ||
6384                    eff == SPELL_EFFECT_PERSISTENT_AREA_AURA || eff == SPELL_EFFECT_APPLY_AREA_AURA_FRIEND || 
6385                    eff == SPELL_EFFECT_APPLY_AREA_AURA_ENEMY)
6386                {
6387                    Aura *Aur = CreateAura(spellInfo, i, NULL, player);
6388                    player->AddAura(Aur);
6389               }
6390            }
6391        }
6392
6393        //save player
6394        player->SaveToDB();
6395    }
6396    return true;
6397}
6398
6399bool ChatHandler::HandleUnFreezeCommand(const char *args)
6400{
6401    std::string name;
6402    Player* player;
6403    char* TargetName = strtok((char*)args, " "); //get entered name
6404    if (!TargetName) //if no name entered use target
6405    {
6406        player = getSelectedPlayer();
6407        if (player) //prevent crash with creature as target
6408        {   
6409           name = player->GetName();
6410        }
6411    }
6412
6413    else // if name entered
6414    {
6415        name = TargetName;
6416        normalizePlayerName(name);
6417        player = objmgr.GetPlayer(name.c_str()); //get player by name
6418    }
6419
6420    //effect
6421    if (player)
6422    {
6423        PSendSysMessage(LANG_COMMAND_UNFREEZE,name.c_str());
6424
6425        //Reset player faction + allow combat + allow duels
6426        player->setFactionForRace(player->getRace());
6427        player->RemoveFlag (UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE);
6428
6429        //allow movement and spells
6430        uint32 spellID = 9454;
6431        player->RemoveAurasDueToSpell(spellID);
6432
6433        //save player
6434        player->SaveToDB();
6435    }
6436
6437    if (!player)
6438    {
6439        if (TargetName)
6440        {       
6441            //check for offline players
6442            QueryResult *result = CharacterDatabase.PQuery("SELECT characters.guid FROM `characters` WHERE characters.name = '%s'",name.c_str());
6443            if(!result)
6444            {
6445                SendSysMessage(LANG_COMMAND_FREEZE_WRONG);
6446                return true;
6447            }
6448            //if player found: delete his freeze aura
6449            Field *fields=result->Fetch();
6450            uint64 pguid = fields[0].GetUInt64();
6451            delete result;
6452            CharacterDatabase.PQuery("DELETE FROM `character_aura` WHERE character_aura.spell = 9454 AND character_aura.guid = '%u'",pguid);
6453            PSendSysMessage(LANG_COMMAND_UNFREEZE,name.c_str());
6454            return true;
6455        }
6456        else
6457        {
6458            SendSysMessage(LANG_COMMAND_FREEZE_WRONG);
6459            return true;
6460        }
6461    }
6462
6463    return true;
6464}
6465
6466bool ChatHandler::HandleListFreezeCommand(const char* args)
6467{
6468    //Get names from DB
6469    QueryResult *result = CharacterDatabase.PQuery("SELECT characters.name FROM `characters` LEFT JOIN `character_aura` ON (characters.guid = character_aura.guid) WHERE character_aura.spell = 9454");
6470    if(!result)
6471    {
6472        SendSysMessage(LANG_COMMAND_NO_FROZEN_PLAYERS);
6473        return true;
6474    }
6475    //Header of the names
6476    PSendSysMessage(LANG_COMMAND_LIST_FREEZE);
6477   
6478    //Output of the results
6479        do
6480    {
6481        Field *fields = result->Fetch();
6482        std::string fplayers = fields[0].GetCppString();
6483        PSendSysMessage(LANG_COMMAND_FROZEN_PLAYERS,fplayers.c_str());
6484    } while (result->NextRow());
6485
6486    delete result;
6487    return true;
6488}
6489
6490bool ChatHandler::HandleFlushArenaPointsCommand(const char * /*args*/)
6491{
6492    sBattleGroundMgr.DistributeArenaPoints();
6493    return true;
6494}
6495
6496bool ChatHandler::HandleGroupLeaderCommand(const char* args)
6497{
6498    Player* plr  = NULL;
6499    Group* group = NULL;
6500    uint64 guid  = 0;
6501    char* cname  = strtok((char*)args, " ");
6502
6503    if(GetPlayerGroupAndGUIDByName(cname, plr, group, guid))
6504        if(group && group->GetLeaderGUID() != guid)
6505            group->ChangeLeader(guid);
6506
6507    return true;
6508}
6509
6510bool ChatHandler::HandleGroupDisbandCommand(const char* args)
6511{
6512    Player* plr  = NULL;
6513    Group* group = NULL;
6514    uint64 guid  = 0;
6515    char* cname  = strtok((char*)args, " ");
6516
6517    if(GetPlayerGroupAndGUIDByName(cname, plr, group, guid))
6518        if(group)
6519            group->Disband();
6520
6521    return true;
6522}
6523
6524bool ChatHandler::HandleGroupRemoveCommand(const char* args)
6525{
6526    Player* plr  = NULL;
6527    Group* group = NULL;
6528    uint64 guid  = 0;
6529    char* cname  = strtok((char*)args, " ");
6530
6531    if(GetPlayerGroupAndGUIDByName(cname, plr, group, guid, true))
6532        if(group)
6533            group->RemoveMember(guid, 0);
6534
6535    return true;
6536}
Note: See TracBrowser for help on using the browser.