Forum > Serverside Modding

[SOLVED] [C++] Command crashes on usage

<< < (2/3) > >>

Ascathos:
Sorry for my long abstinence. I wasn't home, thus no response yet.
You query with targetGuid being NULL. This is impossible to work.


--- 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 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;
   }
--- End code ---

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 (...).

bizzlesnaff:
Thank you both ;)
I'll try it tomorrow. Never thought, that a simple command could be so difficult ;)

bizzlesnaff:
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 ;)

Rochet2:
Maybe this helps?


--- Code: ---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;
}
--- End code ---


Usually wrong database queries call abort and close the server. Better get those database types and columns right.

bizzlesnaff:
Wow that works aboslut perfect...thank you very much!

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version