/*
 *  Passing a reference (not by value), because otherwise if insert fail, the item could be lost.
 */
void Item::Container::insert(ItemPtr &item, int amount)
{
  if ( !item->isStackable() ) amount = 1;
  Weight items_weight = amount * item->weight();

  if ( _weight_cap.cur + items_weight > _weight_cap.max )
  {
    qDebug() << "Cur cap [" << _weight_cap.cur << "] + items_weight [" << items_weight << "] < max_cap [" << _weight_cap.max << "]";
    throw error::container_insertion_error("Zbyt duży ciężar, aby pomieścić go w pojemniku.");
  }
  else
  {
    if (item->isStackable() && _items.find(item->ref()) != _items.end() )
    {
      AmountedItem& aitem = _items[item->ref()];
      aitem.amount += amount;
    }
    else
    {
      _items.insert(std::make_pair<dbRef, AmountedItem >(item->ref(), AmountedItem(item, amount)) );
    }

    set_modified();
  }
}
ItemPtr ProtocolGame::internalGetItem(InputMessage& msg, int id)
{
    if(id == 0)
        id = msg.getU16();

    ItemPtr item = Item::create(id);
    if(item->isStackable() || item->isFluidContainer() || item->isFluid())
        item->setData(msg.getU8());

    return item;
}