Skip to content

Commit f29478c

Browse files
KinglykrabAkkadius
andauthored
[Commands] Add #zonevariable Command (#4882)
* [Commands] Add #zonevariable Command * Update zonevariable.cpp * Argument safety --------- Co-authored-by: Chris Miles <[email protected]>
1 parent 888a88f commit f29478c

File tree

8 files changed

+116
-8
lines changed

8 files changed

+116
-8
lines changed

zone/command.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ int command_init(void)
246246
command_add("zoneinstance", "[Instance ID] [X] [Y] [Z] - Teleport to specified Instance by ID (coordinates are optional)", AccountStatus::Guide, command_zone_instance) ||
247247
command_add("zoneshard", "[zone] [instance_id] - Teleport explicitly to a zone shard", AccountStatus::Player, command_zone_shard) ||
248248
command_add("zoneshutdown", "[instance|zone] [Instance ID|Zone ID|Zone Short Name] - Shut down a zone server by Instance ID, Zone ID, or Zone Short Name", AccountStatus::GMLeadAdmin, command_zoneshutdown) ||
249+
command_add("zonevariable", "[clear|delete|set|view] - Modify zone variables for your current zone", AccountStatus::GMAdmin, command_zonevariable) ||
249250
command_add("zsave", " Saves zheader to the database", AccountStatus::QuestTroupe, command_zsave)
250251
) {
251252
command_deinit();
@@ -928,6 +929,7 @@ void command_bot(Client *c, const Seperator *sep)
928929
#include "gm_commands/zone.cpp"
929930
#include "gm_commands/zonebootup.cpp"
930931
#include "gm_commands/zoneshutdown.cpp"
932+
#include "gm_commands/zonevariable.cpp"
931933
#include "gm_commands/zone_instance.cpp"
932934
#include "gm_commands/zone_shard.cpp"
933935
#include "gm_commands/zsave.cpp"

zone/command.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ void command_zone_instance(Client *c, const Seperator *sep);
198198
void command_zone_shard(Client *c, const Seperator *sep);
199199
void command_zonebootup(Client *c, const Seperator *sep);
200200
void command_zoneshutdown(Client *c, const Seperator *sep);
201+
void command_zonevariable(Client *c, const Seperator *sep);
201202
void command_zsave(Client *c, const Seperator *sep);
202203

203204
#include "bot.h"

zone/gm_commands/zonevariable.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include "../client.h"
2+
3+
void command_zonevariable(Client *c, const Seperator *sep)
4+
{
5+
const uint16 arguments = sep->argnum;
6+
7+
if (!arguments) {
8+
c->Message(Chat::White, "Usage: #zonevariable clear - Clear all zone variables");
9+
c->Message(Chat::White, "Usage: #zonevariable delete [Variable Name] - Delete a zone variable");
10+
c->Message(Chat::White, "Usage: #zonevariable set [Variable Name] [Variable Value] - Set a zone variable");
11+
c->Message(Chat::White, "Usage: #zonevariable view [Variable Name] - View a zone variable");
12+
c->Message(Chat::White, "Note: You can have spaces in variable names and values by wrapping in double quotes like this");
13+
c->Message(Chat::White, "Example: #zonevariable set \"Test Variable\" \"Test Value\"");
14+
c->Message(Chat::White, "Note: Variable Value is optional and can be set to empty by not providing a value");
15+
return;
16+
}
17+
18+
const char* action = arguments >= 1 ? sep->arg[1] : "";
19+
const bool is_clear = !strcasecmp(action, "clear");
20+
const bool is_delete = !strcasecmp(action, "delete");
21+
const bool is_set = !strcasecmp(action, "set");
22+
const bool is_view = !strcasecmp(action, "view");
23+
24+
if (!is_clear && !is_delete && !is_set && !is_view) {
25+
c->Message(Chat::White, "Usage: #zonevariable clear - Clear all zone variables");
26+
c->Message(Chat::White, "Usage: #zonevariable delete [Variable Name] - Delete a zone variable");
27+
c->Message(Chat::White, "Usage: #zonevariable set [Variable Name] [Variable Value] - Set a zone variable");
28+
c->Message(Chat::White, "Usage: #zonevariable view [Variable Name] - View a zone variable");
29+
c->Message(Chat::White, "Note: You can have spaces in variable names and values by wrapping in double quotes like this");
30+
c->Message(Chat::White, "Example: #zonevariable set \"Test Variable\" \"Test Value\"");
31+
c->Message(Chat::White, "Note: Variable Value is optional and can be set to empty by not providing a value");
32+
return;
33+
}
34+
35+
if (is_clear) {
36+
const bool cleared = zone->ClearVariables();
37+
c->Message(Chat::White, cleared ? "Cleared all zone variables." : "There are no zone variables to clear.");
38+
return;
39+
}
40+
41+
if (is_delete) {
42+
const std::string variable_name = arguments >= 2 ? sep->argplus[2] : "";
43+
if (variable_name.empty() || !zone->VariableExists(variable_name)) {
44+
c->Message(Chat::White, fmt::format("A zone variable named '{}' does not exist.", variable_name).c_str());
45+
return;
46+
}
47+
48+
zone->DeleteVariable(variable_name);
49+
c->Message(Chat::White, fmt::format("Deleted a zone variable named '{}'.", variable_name).c_str());
50+
return;
51+
}
52+
53+
if (is_set) {
54+
const std::string variable_name = arguments >= 2 ? sep->arg[2] : "";
55+
const std::string variable_value = arguments >= 3 ? sep->arg[3] : "";
56+
zone->SetVariable(variable_name, variable_value);
57+
c->Message(Chat::White, fmt::format("Set a zone variable named '{}' to a value of '{}'.", variable_name, variable_value).c_str());
58+
return;
59+
}
60+
61+
if (is_view) {
62+
const auto& l = zone->GetVariables();
63+
const std::string search_criteria = arguments >= 2 ? sep->argplus[2] : "";
64+
65+
uint32 variable_count = 0;
66+
uint32 variable_number = 1;
67+
68+
for (const auto& e : l) {
69+
if (search_criteria.empty() || Strings::Contains(Strings::ToLower(e), Strings::ToLower(search_criteria))) {
70+
c->Message(Chat::White, fmt::format(
71+
"Variable {} | Name: {} Value: {} | {}",
72+
variable_number,
73+
e,
74+
zone->GetVariable(e),
75+
Saylink::Silent(fmt::format("#zonevariable delete {}", e), "Delete")
76+
).c_str());
77+
78+
variable_count++;
79+
variable_number++;
80+
}
81+
}
82+
83+
if (!variable_count) {
84+
c->Message(Chat::White, fmt::format(
85+
"There are no zone variables{}.",
86+
(!search_criteria.empty() ? fmt::format(" matching '{}'", search_criteria) : "")
87+
).c_str());
88+
return;
89+
}
90+
91+
c->Message(Chat::White, fmt::format(
92+
"There {} {} zone variable{}{}, would you like to {} zone variables?",
93+
variable_count != 1 ? "are" : "is",
94+
variable_count,
95+
variable_count != 1 ? "s" : "",
96+
(!search_criteria.empty() ? fmt::format(" matching '{}'", search_criteria) : ""),
97+
Saylink::Silent("#zonevariable clear", "clear")
98+
).c_str());
99+
}
100+
}

zone/lua_zone.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,10 @@ std::string Lua_Zone::GetBucketRemaining(const std::string& bucket_name)
727727
return self->GetBucketRemaining(bucket_name);
728728
}
729729

730-
void Lua_Zone::ClearVariables()
730+
bool Lua_Zone::ClearVariables()
731731
{
732-
Lua_Safe_Call_Void();
733-
self->ClearVariables();
732+
Lua_Safe_Call_Bool();
733+
return self->ClearVariables();
734734
}
735735

736736
bool Lua_Zone::DeleteVariable(const std::string& variable_name)

zone/lua_zone.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class Lua_Zone : public Lua_Ptr<Zone>
141141
void SetInstanceTimeRemaining(uint32 time_remaining);
142142
void SetIsHotzone(bool is_hotzone);
143143
void ShowZoneGlobalLoot(Lua_Client c);
144-
void ClearVariables();
144+
bool ClearVariables();
145145
bool DeleteVariable(const std::string& variable_name);
146146
std::string GetVariable(const std::string& variable_name);
147147
luabind::object GetVariables(lua_State* L);

zone/perl_zone.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,9 @@ std::string Perl_Zone_GetBucketRemaining(Zone* self, const std::string bucket_na
561561
return self->GetBucketRemaining(bucket_name);
562562
}
563563

564-
void Perl_Zone_ClearVariables(Zone* self)
564+
bool Perl_Zone_ClearVariables(Zone* self)
565565
{
566-
self->ClearVariables();
566+
return self->ClearVariables();
567567
}
568568

569569
bool Perl_Zone_DeleteVariable(Zone* self, const std::string variable_name)

zone/zone.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3225,9 +3225,14 @@ void Zone::DisableRespawnTimers()
32253225
}
32263226
}
32273227

3228-
void Zone::ClearVariables()
3228+
bool Zone::ClearVariables()
32293229
{
3230+
if (m_zone_variables.empty()) {
3231+
return false;
3232+
}
3233+
32303234
m_zone_variables.clear();
3235+
return true;
32313236
}
32323237

32333238
bool Zone::DeleteVariable(const std::string& variable_name)

zone/zone.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ class Zone {
198198
int32 MobsAggroCount() { return aggroedmobs; }
199199
DynamicZone *GetDynamicZone();
200200

201-
void ClearVariables();
201+
bool ClearVariables();
202202
bool DeleteVariable(const std::string& variable_name);
203203
std::string GetVariable(const std::string& variable_name);
204204
std::vector<std::string> GetVariables();

0 commit comments

Comments
 (0)