I noticed BBim and Adidishen were having trouble (adi might have gotten this to work already) getting items to correctly appear in the inventory.
The problem is not that difficult once you figure out the pattern on how to do this. First let me show you the GM command in its natural habitat.
I haven't yet added in the mods for the items so its more of a item and amount command thus far.Code:if(_stricmp(command,"item")==0){ int itemid = strval(strtok_s(NULL, " ",&next_token)); int amount = strval(strtok_s(NULL, " ",&next_token)); PlayerInventory* inventory = player->inv; ItemBank *itembank = ItemBanks::getItemBankById(itemid); Item* item = new Item(); item->setItemId(itembank->id); // check to see whether the amount is greater than the max amount in the itembank if(amount > itembank->maxItems) { item->setAmount(itembank->maxItems); } else { item->setAmount(amount); } item->setSlot(inventory->getFirstUnunsedSlot()); int usedSlot = item->getSlot(); inventory->addUsedSlot(usedSlot); inventory->removeUnusedSlot(usedSlot); inventory->addItem(item); int newSlot = item->getSlot(); InventoryPacket::addItem(player, item, newSlot); return true; }
So lemme break it down for you.
Input the following code in the chat box: !item 85 1
Code:int itemid = strval(strtok_s(NULL, " ",&next_token));
this is the id of the item.
Code:int amount = strval(strtok_s(NULL, " ",&next_token));
this is the number of items you want.
call in the player's inventory to work with.Code:PlayerInventory* inventory = player->inv;
get an item bank from the ID.Code:ItemBank *itembank = ItemBanks::getItemBankById(itemid);
Make a new item.Code:Item* item = new Item();
set the item id from the itembank. then it checks to see whether you try and beat the system of making more items than needed in the same slot (i,e, 200 swords). If its more than the max amount it sets the max amount to the amount of the item. Its a failsafe against item abuse.Code:item->setItemId(itembank->id); // check to see whether the amount is greater than the max amount in the itembank if(amount > itembank->maxItems) { item->setAmount(itembank->maxItems); } else { item->setAmount(amount); }
This is the crucial part. This returns the first unused slot available.Code:item->setSlot(inventory->getFirstUnunsedSlot());
Ok. Some don't know this but there are two arrays or vectors that hold some vital slot information. UsedSlot array has all the slot numbers in use including equipped slots. UnusedSlot is all the free slots available including equip slots. If you add one, you remove one and vice versa.Code:inventory->addUsedSlot(usedSlot); inventory->removeUnusedSlot(usedSlot);
This adds the item in the inventory for saving. Its not complete until you send the packet to the client.Code:inventory->addItem(item);
this is the final part of the command. this sends the packet to the client so your item can finally appear. Next I'll post the addItem packet. This is for the InventoryPacket.Code:int newSlot = item->getSlot(); InventoryPacket::addItem(player, item, newSlot);
Thanks to BBim and Adidishen for their POVs and help in the matter. This is just a simple packet which only has a id and a qty.Code:void InventoryPacket::addItem(Player* player, Item* item, int slot){ Packet pak = Packet(); pak.setPlayer(player); pak.addHeader(player, 0x03); // Add item to inventory command pak.addByte(0); pak.addInt(-1);//ff ff ff ff if(item->getItemId() == 0) { pak.addShort(21); } else { pak.addShort(item->getItemId()); } pak.addShort(0); pak.addInt(0); pak.addInt(0); pak.addShort(item->getAmount()); pak.addShort(0); pak.addInt(0); pak.addInt(0); pak.addShort(0); pak.addShort(0); pak.addInt(0); pak.addByte(0); pak.addShort(0); pak.addShort(0); pak.addInt(0); pak.addInt(0); pak.addBytes("000000000000000000000000000001"); pak.addByte(slot); pak.addShort(1); pak.completePacket(); pak.packetSend(); }



![[Share] DragonSource Item GM command v1.0](http://ragezone.com/hyper728.png)


