summaryrefslogtreecommitdiff
path: root/src/server/scripts/Commands/cs_inventory.cpp
blob: 09dc8838117a48338fde1c20b5d0d2a4a3e7a25b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by the
 * Free Software Foundation; either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "Chat.h"
#include "CommandScript.h"
#include "Language.h"
#include "Player.h"
#include "WorldSession.h"

constexpr std::array<const char*, MAX_ITEM_SUBCLASS_CONTAINER> bagSpecsToString =
{
    "normal",
    "soul",
    "herb",
    "enchanting",
    "engineering",
    "gem",
    "mining",
    "leatherworking",
    "inscription"
};

constexpr std::array<uint32, MAX_ITEM_SUBCLASS_CONTAINER> bagSpecsColors =
{
    0xfff0de18,     // YELLOW - Normal
    0xffa335ee,     // PURPLE - Souls
    0xff1eff00,     // GREEN - Herb
    0xffe37166,     // PINK - Enchanting
    0xffa68b30,     // BROWN - Engineering
    0xff0070dd,     // BLUE - Gem
    0xffc1c8c9,     // GREY - Mining
    0xfff5a925,     // ORANGE - Leatherworking
    0xff54504f      // DARK GREY - Inscription
};

//constexpr std::array<const char*, MAX_ITEM_SUBCLASS_CONTAINER> bagSpecsColorToString =
//{
//    "normal",
//    "soul",
//    "herb",
//    "enchanting",
//    "engineering",
//    "gem",
//    "mining",
//    "leatherworking",
//    "inscription"
//};

using namespace Acore::ChatCommands;

class inventory_commandscript : public CommandScript
{
public:
    inventory_commandscript() : CommandScript("inventory_commandscript") { }

    ChatCommandTable GetCommands() const override
    {
        static ChatCommandTable inventoryCommandTable =
        {
            { "count",      HandleInventoryCountCommand,   SEC_MODERATOR, Console::No }
        };

        static ChatCommandTable commandTable =
        {
            { "inventory",  inventoryCommandTable }
        };

        return commandTable;
    }

    static bool HandleInventoryCountCommand(ChatHandler* handler, Optional<PlayerIdentifier> player)
    {
        if (!player)
        {
            player = PlayerIdentifier::FromTargetOrSelf(handler);
        }

        if (!player)
        {
            handler->SendErrorMessage(LANG_PLAYER_NOT_FOUND);
            return false;
        }

        Player* target = player->GetConnectedPlayer();
        if (!target)
        {
            handler->SendErrorMessage(LANG_PLAYER_NOT_FOUND);
            return false;
        }

        std::array<uint32, MAX_ITEM_SUBCLASS_CONTAINER> freeSlotsInBags = { };
        uint32 freeSlotsForBags = 0;
        bool haveFreeSlot = false;

        // Check backpack
        for (uint8 slot = INVENTORY_SLOT_ITEM_START; slot < INVENTORY_SLOT_ITEM_END; ++slot)
        {
            if (!target->GetItemByPos(INVENTORY_SLOT_BAG_0, slot))
            {
                haveFreeSlot = true;
                ++freeSlotsInBags[ITEM_SUBCLASS_CONTAINER];
            }
        }

        // Check bags
        for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++)
        {
            if (Bag* bag = target->GetBagByPos(i))
            {
                if (ItemTemplate const* bagTemplate = bag->GetTemplate())
                {
                    if (bagTemplate->Class == ITEM_CLASS_CONTAINER || bagTemplate->Class == ITEM_CLASS_QUIVER)
                    {
                        haveFreeSlot = true;
                        freeSlotsInBags[bagTemplate->SubClass] += bag->GetFreeSlots();
                    }
                }
            }
            else
            {
                ++freeSlotsForBags;
            }
        }

        std::ostringstream str;

        if (haveFreeSlot)
        {
            str << "Player " << target->GetName() << " have ";
            bool initialize = true;

            for (uint8 i = ITEM_SUBCLASS_CONTAINER; i < MAX_ITEM_SUBCLASS_CONTAINER; ++i)
            {
                if (uint32 freeSlots = freeSlotsInBags[i])
                {
                    std::string bagSpecString = bagSpecsToString[i];
                    if (!initialize)
                    {
                        str << ", ";
                    }

                    str << "|c";
                    str << std::hex << bagSpecsColors[i] << std::dec;
                    str << freeSlots << " in " << bagSpecString << " bags|r";

                    initialize = false;
                }
            }
        }
        else
        {
            str << "Player " << target->GetName() << " does not have free slots in their bags";
        }

        if (freeSlotsForBags)
        {
            str << " and also has " << freeSlotsForBags << " free slots for bags";
        }

        str << ".";

        handler->SendSysMessage(str.str().c_str());

        return true;
    }
};

void AddSC_inventory_commandscript()
{
    new inventory_commandscript();
}