TrinityCore
Loading...
Searching...
No Matches
cs_cast.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18/* ScriptData
19Name: cast_commandscript
20%Complete: 100
21Comment: All cast related commands
22Category: commandscripts
23EndScriptData */
24
25#include "ScriptMgr.h"
26#include "Chat.h"
27#include "Creature.h"
28#include "Language.h"
29#include "Player.h"
30#include "RBAC.h"
31#include "SpellInfo.h"
32#include "SpellMgr.h"
33#include "WorldSession.h"
34
35using namespace Trinity::ChatCommands;
36
38{
39public:
40 cast_commandscript() : CommandScript("cast_commandscript") { }
41
43 {
44 static ChatCommandTable castCommandTable =
45 {
52 };
53 static ChatCommandTable commandTable =
54 {
55 { "cast", castCommandTable },
56 };
57 return commandTable;
58 }
59
60 static bool CheckSpellExistsAndIsValid(ChatHandler* handler, SpellInfo const* spell)
61 {
62 if (!spell)
63 {
65 handler->SetSentErrorMessage(true);
66 return false;
67 }
68
69 if (!SpellMgr::IsSpellValid(spell, handler->GetSession()->GetPlayer()))
70 {
72 handler->SetSentErrorMessage(true);
73 return false;
74 }
75 return true;
76 }
77
79 {
80 if (triggeredStr)
81 {
82 if (StringStartsWith("triggered", *triggeredStr)) // check if "triggered" starts with *triggeredStr (e.g. "trig", "trigger", etc.)
84 else
85 return std::nullopt;
86 }
87 return TRIGGERED_NONE;
88 }
89
90 static bool HandleCastCommand(ChatHandler* handler, SpellInfo const* spell, Optional<std::string> triggeredStr)
91 {
92 Unit* target = handler->getSelectedUnit();
93 if (!target)
94 {
96 handler->SetSentErrorMessage(true);
97 return false;
98 }
99
100 if (!CheckSpellExistsAndIsValid(handler, spell))
101 return false;
102
103 Optional<TriggerCastFlags> triggerFlags = GetTriggerFlags(triggeredStr);
104 if (!triggerFlags)
105 return false;
106
107 handler->GetSession()->GetPlayer()->CastSpell(target, spell->Id, *triggerFlags);
108
109 return true;
110 }
111
112 static bool HandleCastBackCommand(ChatHandler* handler, SpellInfo const* spell, Optional<std::string> triggeredStr)
113 {
114 Creature* caster = handler->getSelectedCreature();
115 if (!caster)
116 {
118 handler->SetSentErrorMessage(true);
119 return false;
120 }
121
122 if (!CheckSpellExistsAndIsValid(handler, spell))
123 return false;
124
125 Optional<TriggerCastFlags> triggerFlags = GetTriggerFlags(triggeredStr);
126 if (!triggerFlags)
127 return false;
128
129 caster->CastSpell(handler->GetSession()->GetPlayer(), spell->Id, *triggerFlags);
130
131 return true;
132 }
133
134 static bool HandleCastDistCommand(ChatHandler* handler, SpellInfo const* spell, float dist, Optional<std::string> triggeredStr)
135 {
136 if (!CheckSpellExistsAndIsValid(handler, spell))
137 return false;
138
139 Optional<TriggerCastFlags> triggerFlags = GetTriggerFlags(triggeredStr);
140 if (!triggerFlags)
141 return false;
142
143 float x, y, z;
144 handler->GetSession()->GetPlayer()->GetClosePoint(x, y, z, dist);
145 handler->GetSession()->GetPlayer()->CastSpell(Position{ x, y, z }, spell->Id, *triggerFlags);
146
147 return true;
148 }
149
150 static bool HandleCastSelfCommand(ChatHandler* handler, SpellInfo const* spell, Optional<std::string> triggeredStr)
151 {
152 Unit* target = handler->getSelectedUnit();
153 if (!target)
154 {
156 handler->SetSentErrorMessage(true);
157 return false;
158 }
159
160 if (!CheckSpellExistsAndIsValid(handler, spell))
161 return false;
162
163 Optional<TriggerCastFlags> triggerFlags = GetTriggerFlags(triggeredStr);
164 if (!triggerFlags)
165 return false;
166
167 target->CastSpell(target, spell->Id, *triggerFlags);
168
169 return true;
170 }
171
172 static bool HandleCastTargetCommad(ChatHandler* handler, SpellInfo const* spell, Optional<std::string> triggeredStr)
173 {
174 Creature* caster = handler->getSelectedCreature();
175 if (!caster)
176 {
178 handler->SetSentErrorMessage(true);
179 return false;
180 }
181
182 if (!caster->GetVictim())
183 {
185 handler->SetSentErrorMessage(true);
186 return false;
187 }
188
189 if (!CheckSpellExistsAndIsValid(handler, spell))
190 return false;
191
192 Optional<TriggerCastFlags> triggerFlags = GetTriggerFlags(triggeredStr);
193 if (!triggerFlags)
194 return false;
195
196 caster->CastSpell(caster->GetVictim(), spell->Id, *triggerFlags);
197
198 return true;
199 }
200
201 static bool HandleCastDestCommand(ChatHandler* handler, SpellInfo const* spell, float x, float y, float z, Optional<std::string> triggeredStr)
202 {
203 Unit* caster = handler->getSelectedUnit();
204 if (!caster)
205 {
207 handler->SetSentErrorMessage(true);
208 return false;
209 }
210
211 if (!CheckSpellExistsAndIsValid(handler, spell))
212 return false;
213
214 Optional<TriggerCastFlags> triggerFlags = GetTriggerFlags(triggeredStr);
215 if (!triggerFlags)
216 return false;
217
218 caster->CastSpell(Position{ x, y, z }, spell->Id, *triggerFlags);
219
220 return true;
221 }
222};
223
@ LANG_SELECTED_TARGET_NOT_HAVE_VICTIM
Definition Language.h:665
@ LANG_COMMAND_NOSPELLFOUND
Definition Language.h:508
@ LANG_SELECT_CHAR_OR_CREATURE
Definition Language.h:31
@ LANG_COMMAND_SPELL_BROKEN
Definition Language.h:551
std::optional< T > Optional
Optional helper class to wrap optional values within.
Definition Optional.h:25
Role Based Access Control related classes definition.
@ TRIGGERED_NONE
@ TRIGGERED_FULL_DEBUG_MASK
Will ignore most target checks (mostly DBC target checks)
bool StringStartsWith(std::string_view haystack, std::string_view needle)
Definition Util.h:363
Unit * getSelectedUnit()
Definition Chat.cpp:314
WorldSession * GetSession()
Definition Chat.h:46
Creature * getSelectedCreature()
Definition Chat.cpp:338
void SetSentErrorMessage(bool val)
Definition Chat.h:134
void PSendSysMessage(char const *fmt, Args &&... args)
Definition Chat.h:69
virtual void SendSysMessage(std::string_view str, bool escapeCharacters=false)
Definition Chat.cpp:101
uint32 Id
Definition SpellInfo.h:289
static bool IsSpellValid(SpellInfo const *spellInfo, Player *player=nullptr, bool msg=true)
Some checks for spells, to prevent adding deprecated/broken spells for trainers, spell book,...
Definition SpellMgr.cpp:71
Definition Unit.h:769
Unit * GetVictim() const
Definition Unit.h:859
void GetClosePoint(float &x, float &y, float &z, float size, float distance2d=0, float relAngle=0) const
Definition Object.cpp:3244
SpellCastResult CastSpell(CastSpellTargetArg const &targets, uint32 spellId, CastSpellExtraArgs const &args={ })
Definition Object.cpp:2832
Player * GetPlayer() const
static bool CheckSpellExistsAndIsValid(ChatHandler *handler, SpellInfo const *spell)
Definition cs_cast.cpp:60
static bool HandleCastDestCommand(ChatHandler *handler, SpellInfo const *spell, float x, float y, float z, Optional< std::string > triggeredStr)
Definition cs_cast.cpp:201
static Optional< TriggerCastFlags > GetTriggerFlags(Optional< std::string > triggeredStr)
Definition cs_cast.cpp:78
static bool HandleCastSelfCommand(ChatHandler *handler, SpellInfo const *spell, Optional< std::string > triggeredStr)
Definition cs_cast.cpp:150
static bool HandleCastBackCommand(ChatHandler *handler, SpellInfo const *spell, Optional< std::string > triggeredStr)
Definition cs_cast.cpp:112
static bool HandleCastTargetCommad(ChatHandler *handler, SpellInfo const *spell, Optional< std::string > triggeredStr)
Definition cs_cast.cpp:172
static bool HandleCastDistCommand(ChatHandler *handler, SpellInfo const *spell, float dist, Optional< std::string > triggeredStr)
Definition cs_cast.cpp:134
ChatCommandTable GetCommands() const override
Definition cs_cast.cpp:42
static bool HandleCastCommand(ChatHandler *handler, SpellInfo const *spell, Optional< std::string > triggeredStr)
Definition cs_cast.cpp:90
void AddSC_cast_commandscript()
Definition cs_cast.cpp:224
std::vector< ChatCommandBuilder > ChatCommandTable
Definition ChatCommand.h:50
@ RBAC_PERM_COMMAND_CAST_SELF
Definition RBAC.h:186
@ RBAC_PERM_COMMAND_CAST_DEST
Definition RBAC.h:188
@ RBAC_PERM_COMMAND_CAST
Definition RBAC.h:183
@ RBAC_PERM_COMMAND_CAST_TARGET
Definition RBAC.h:187
@ RBAC_PERM_COMMAND_CAST_BACK
Definition RBAC.h:184
@ RBAC_PERM_COMMAND_CAST_DIST
Definition RBAC.h:185