Modcraft - The community dedicated to quality WoW modding!
Wrath of the Lich King Modding => Serverside Modding => Topic started by: bizzlesnaff on June 27, 2015, 08:44:21 pm
-
Hey guys ;)
Atm I'm trying to create my own command (trinitycore btw). I just try to say ingame .showaccount (while target any player) and the result should be a text which is in a custom database and linked with the id of the player..
Well, here is my code:
RBAC.h
RBAC_PERM_COMMAND_CUSTOMINFO = 799,
cs_misc.cpp
{ "showaccount", rbac::RBAC_PERM_COMMAND_CUSTOMINFO, false, &HandleNewCommand, "", NULL },
static bool HandleNewCommand(ChatHandler * handler, char const* args)
{
Player* target;
ObjectGuid targetGuid;
std::string targetName;
QueryResult resulta = CharacterDatabase.PQuery("SELECT Info FROM characterinfo WHERE acc='%s'", targetGuid);
handler->PSendSysMessage("Info: %s", resulta);
return true;
}
I copied this code from some tutorials, and other scripts together, so i dont wonder that it doesn't work. But it compile fine, just if I type ingame ".showaccount" the server crashed up, but with no error log...just...shutting down or something
someone an idea what I've made wrong?
sorry for my english...thats such a dump language :)
-
Did you add the command to the rbac_permissions and rbac_linked_permissions table?
-
Ah yeah of course, forgot to write it. I did every step of this tutorial..
https://www.youtube.com/watch?v=scqujqegNh0 (https://www.youtube.com/watch?v=scqujqegNh0" onclick="window.open(this.href);return false;)
-
little push ;)?
-
QueryResult resulta = CharacterDatabase.PQuery("SELECT Info FROM characterinfo WHERE acc='%s'", targetGuid);
%s means null terminated string, but a guid is a integer value. Try %u instead of %s. %u means unsigned integer.
EDIT:
Lol, didn't saw that.
You can't just print a query as a string, like you did here:
handler->PSendSysMessage("Info: %s", resulta);
Here's a short snippet of a script, which i wrote:
QueryResult result = WorldDatabase.Query("SELECT * FROM hm_waves");
if (result)
{
do
{
Field *fields = result->Fetch();
int waveid = fields[0].GetInt32();
creature.id = fields[1].GetInt32();
} while (result->NextRow());
}
If the field info is a string, you have to use .GetString(); instead of GetInt32();. The array index is the number of the field in the query. You only request one field with your query, so it should be std::string infotext = fields[0].GetString();
And you don't have to use the do-while-loop, because you probably only expect one result.
-
Sorry for my long abstinence. I wasn't home, thus no response yet.
You query with targetGuid being NULL. This is impossible to work.
RBAC.h
RBAC_PERM_COMMAND_CUSTOMINFO = 799,
cs_misc.cpp
{ "showaccount", rbac::RBAC_PERM_COMMAND_CUSTOMINFO, false, &HandleNewCommand, "", NULL },
static bool HandleNewCommand(ChatHandler* handler, char const* args)
{
Player* target;
ObjectGuid parseGUID = ObjectGuid::Create<HighGuid::Player>(strtoull(args, nullptr, 10));
std::string targetName;
if (ObjectMgr::GetPlayerNameByGUID(parseGUID, targetName))
{
target = ObjectAccessor::FindPlayer(parseGUID);
targetGuid = parseGUID;
}
else if (!handler->extractPlayerTarget((char*)args, &target, &targetGuid, &targetName))
return false;
QueryResult resulta = CharacterDatabase.PQuery("SELECT Info FROM characterinfo WHERE acc=%u", targetGuid);
if (resulta)
{
handler->PSendSysMessage("Info: %s", (*resulta)[0].GetString());
return true;
}
return false;
}
This uses a guid, a target or handler (Command user). I based the way to get a target by copying what I used (and how it later was modified) from .pinfo. Please read further into other commands to see what you need. If you have questions, there is a TrinityCore IRC. I am on it as well.
I also took the liberty to change the topic name to be more approperiate.
Edit: Just notice that Info is a string, thus edited the fetched result.
EDIT 2:
Repost, accidently deleted this post (...).
-
Thank you both ;)
I'll try it tomorrow. Never thought, that a simple command could be so difficult ;)
-
Well i tried it today again, but it doesn't work for me ;(
I'll wait some weeks and learn more, maybe then I know how to fix it ;)
Again, thank you for your support ;)
-
Maybe this helps?
static bool HandleNewCommand(ChatHandler* handler, char const* args)
{
// get world session
WorldSession* session = handler->GetSession();
// make sure it exists. It doesnt if this command is called from the console
if (!session)
return false;
// get the player that invoked this command
Player* player = session->GetPlayer();
if (!player)
return false;
// get the player's target and make sure its a player
Player* target = player->GetSelectedPlayer();
if (!target)
return false;
// make a database query with the account ID
// dont use strings to identify things :) And you can get the name from auth database by the account id anyways if needed
QueryResult result = CharacterDatabase.PQuery("SELECT `info` FROM `characterinfo` WHERE `accountid` = %u", target->GetSession()->GetAccountId());
if (!result) // always make sure a row was returned before actually using it
{
handler->SendSysMessage("Info: no info");
}
else
{
// get the info string from the query. the selected values are indexed starting from 0, so info is at position 0
std::string info = result->Fetch()[0].GetString();
handler->PSendSysMessage("Info: %s", info.c_str()); // using c_str() to convert the std::string into const char* which is what PSendSysMessage, printf and other need.
}
// command succeeded, return true to not show syntax info or error message
return true;
}
Usually wrong database queries call abort and close the server. Better get those database types and columns right.
-
Wow that works aboslut perfect...thank you very much!
-
ah, forgot the fetch->result[0]. Oh well, as long as it works