Cuberite
A lightweight, fast and extensible game server for Minecraft
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
cInventory Class Reference

This class represents the player's inventory The slots are divided into three areas: More...

#include <Inventory.h>

Inheritance diagram for cInventory:
Inheritance graph
[legend]
Collaboration diagram for cInventory:
Collaboration graph
[legend]

Public Types

enum  {
  invArmorCount = 4 , invInventoryCount = 9 * 3 , invHotbarCount = 9 , invShieldCount = 1 ,
  invArmorOffset = 0 , invInventoryOffset = invArmorOffset + invArmorCount , invHotbarOffset = invInventoryOffset + invInventoryCount , invShieldOffset = invHotbarOffset + invHotbarCount ,
  invNumSlots = invShieldOffset + invShieldCount
}
 

Public Member Functions

char AddItem (const cItem &a_ItemStack, bool a_AllowNewStacks=true)
 Adds as many items out of a_ItemStack as can fit. More...
 
char AddItems (cItems &a_ItemStackList, bool a_AllowNewStacks)
 Same as AddItem, but works on an entire list of item stacks. More...
 
char ChangeSlotCount (int a_SlotNum, char a_AddToCount)
 Adds (or subtracts, if a_AddToCount is negative) to the count of items in the specified slot. More...
 
 cInventory (cPlayer &a_Owner)
 
void Clear (void)
 Removes all items from the entire inventory. More...
 
void CopyToItems (cItems &a_Items)
 Copies the non-empty slots into a_ItemStacks; preserves the original a_Items contents. More...
 
bool DamageEquippedItem (short a_Amount=1)
 Adds the specified damage to the currently held item; deletes the item and returns true if the item broke. More...
 
bool DamageItem (int a_SlotNum, short a_Amount)
 Adds the specified damage to the specified item; deletes the item and returns true if the item broke. More...
 
cItemFindItem (const cItem &a_RecipeItem)
 Finds an item based on ItemType and ItemDamage (<- defines the itemType, too) More...
 
cItemGridGetArmorGrid (void)
 Returns the cItemGrid object representing the armor slots. More...
 
const cItemGetArmorSlot (int a_ArmorSlotNum) const
 Returns current item in a_ArmorSlotNum in armor slots. More...
 
const cItemGetEquippedBoots (void) const
 
const cItemGetEquippedChestplate (void) const
 
const cItemGetEquippedHelmet (void) const
 
const cItemGetEquippedItem (void) const
 Returns current equiped item. More...
 
const cItemGetEquippedLeggings (void) const
 
int GetEquippedSlotNum (void)
 Returns slot number of equiped item. More...
 
cItemGridGetHotbarGrid (void)
 Returns the cItemGrid object representing the hotbar slots. More...
 
const cItemGetHotbarSlot (int a_HotBarSlotNum) const
 Returns current item in a_ArmorSlotNum in hotbar slots. More...
 
cItemGridGetInventoryGrid (void)
 Returns the cItemGrid object representing the main inventory slots. More...
 
const cItemGetInventorySlot (int a_InventorySlotNum) const
 Returns current item in a_ArmorSlotNum in inventory slots. More...
 
cPlayerGetOwner (void)
 Returns the player associated with this inventory. More...
 
const cPlayerGetOwner (void) const
 Returns the player associated with this inventory (const version) More...
 
const cItemGetShieldSlot () const
 Returns current item in shield slot. More...
 
const cItemGetSlot (int a_SlotNum) const
 Returns current item in a_SlotNum slot. More...
 
bool HasItems (const cItem &a_ItemStack)
 Returns true if there are at least as many items of type a_ItemStack as in a_ItemStack. More...
 
int HowManyCanFit (const cItem &a_ItemStack, bool a_ConsiderEmptySlots=true)
 Returns number of items out of a_ItemStack that can fit in the storage. More...
 
int HowManyCanFit (const cItem &a_ItemStack, int a_BeginSlotNum, int a_EndSlotNum, bool a_ConsiderEmptySlots=true)
 Returns how many items of the specified type would fit into the slot range specified. More...
 
int HowManyItems (const cItem &a_Item)
 Returns the number of items of type a_Item that are stored. More...
 
bool LoadFromJson (Json::Value &a_Value)
 
int RemoveItem (const cItem &a_ItemStack)
 Removes the specified item from the inventory, as many as possible, up to a_ItemStack.m_ItemCount. More...
 
bool RemoveOneEquippedItem (void)
 Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed. More...
 
int ReplaceOneEquippedItem (const cItem &a_Item, bool a_TryOtherSlots=true)
 Removes one item from the the current equipped item stack, and attempts to add the specified item stack back to the same slot. More...
 
void SaveToJson (Json::Value &a_Value)
 
void SendEquippedSlot ()
 Sends the equipped item slot to the client. More...
 
void SendSlot (int a_SlotNum)
 Sends the slot contents to the owner. More...
 
void SetArmorSlot (int a_ArmorSlotNum, const cItem &a_Item)
 Puts a_Item item in a_ArmorSlotNum slot number in armor slots. More...
 
void SetEquippedItem (const cItem &a_Item)
 Sets current item in the equipped hotbar slot. More...
 
void SetEquippedSlotNum (int a_SlotNum)
 Sets equiped item to the a_SlotNum slot number. More...
 
void SetHotbarSlot (int a_HotBarSlotNum, const cItem &a_Item)
 Puts a_Item item in a_HotBarSlotNum slot number in hotbar slots. More...
 
void SetInventorySlot (int a_InventorySlotNum, const cItem &a_Item)
 Puts a_Item item in a_InventorySlotNum slot number in inventory slots. More...
 
void SetShieldSlot (const cItem &a_Item)
 Sets current item in shield slot. More...
 
void SetSlot (int a_SlotNum, const cItem &a_Item)
 Puts a_Item item in a_SlotNum slot number. More...
 
void UpdateItems (void)
 Update items (e.g. More...
 
virtual ~cInventory () override
 
- Public Member Functions inherited from cItemGrid::cListener
virtual ~cListener ()
 

Static Public Member Functions

static int ArmorSlotNumToEntityEquipmentID (short a_ArmorSlotNum)
 Converts an armor slot number into the ID for the EntityEquipment packet. More...
 

Protected Member Functions

bool AddToBar (cItem &a_Item, const int a_Offset, const int a_Size, bool *a_bChangedSlots, int a_Mode=0)
 
cItemGridGetGridForSlotNum (int a_SlotNum, int &a_GridSlotNum)
 Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return nullptr for invalid SlotNum. More...
 
const cItemGridGetGridForSlotNum (int a_SlotNum, int &a_GridSlotNum) const
 Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return nullptr for invalid SlotNum. More...
 
virtual void OnSlotChanged (cItemGrid *a_ItemGrid, int a_SlotNum) override
 Called whenever a slot changes. More...
 

Protected Attributes

cItemGrid m_ArmorSlots
 
int m_EquippedSlotNum
 
cItemGrid m_HotbarSlots
 
cItemGrid m_InventorySlots
 
cPlayerm_Owner
 
cItemGrid m_ShieldSlots
 

Detailed Description

This class represents the player's inventory The slots are divided into three areas:

Definition at line 32 of file Inventory.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
Enumerator
invArmorCount 
invInventoryCount 
invHotbarCount 
invShieldCount 
invArmorOffset 
invInventoryOffset 
invHotbarOffset 
invShieldOffset 
invNumSlots 

Definition at line 40 of file Inventory.h.

Constructor & Destructor Documentation

◆ cInventory()

cInventory::cInventory ( cPlayer a_Owner)

Definition at line 20 of file Inventory.cpp.

◆ ~cInventory()

virtual cInventory::~cInventory ( )
inlineoverridevirtual

Definition at line 58 of file Inventory.h.

Member Function Documentation

◆ AddItem()

char cInventory::AddItem ( const cItem a_ItemStack,
bool  a_AllowNewStacks = true 
)

Adds as many items out of a_ItemStack as can fit.

If a_AllowNewStacks is set to false, only existing stacks can be topped up; if a_AllowNewStacks is set to true, empty slots can be used for the rest. Fills existing stacks first and fills the hotbar before the main inventory. Returns the number of items that fit.

Definition at line 106 of file Inventory.cpp.

◆ AddItems()

char cInventory::AddItems ( cItems a_ItemStackList,
bool  a_AllowNewStacks 
)

Same as AddItem, but works on an entire list of item stacks.

The a_ItemStackList is modified to reflect the leftover items. If a_AllowNewStacks is set to false, only existing stacks can be topped up; if a_AllowNewStacks is set to true, empty slots can be used for the rest. Returns the total number of items that fit.

Definition at line 165 of file Inventory.cpp.

◆ AddToBar()

bool cInventory::AddToBar ( cItem a_Item,
const int  a_Offset,
const int  a_Size,
bool *  a_bChangedSlots,
int  a_Mode = 0 
)
protected

◆ ArmorSlotNumToEntityEquipmentID()

int cInventory::ArmorSlotNumToEntityEquipmentID ( short  a_ArmorSlotNum)
static

Converts an armor slot number into the ID for the EntityEquipment packet.

Definition at line 611 of file Inventory.cpp.

◆ ChangeSlotCount()

char cInventory::ChangeSlotCount ( int  a_SlotNum,
char  a_AddToCount 
)

Adds (or subtracts, if a_AddToCount is negative) to the count of items in the specified slot.

If the slot is empty, ignores the call. Returns the new count, or -1 if the slot number is invalid.

Definition at line 498 of file Inventory.cpp.

◆ Clear()

void cInventory::Clear ( void  )

Removes all items from the entire inventory.

Definition at line 40 of file Inventory.cpp.

◆ CopyToItems()

void cInventory::CopyToItems ( cItems a_Items)

Copies the non-empty slots into a_ItemStacks; preserves the original a_Items contents.

Definition at line 549 of file Inventory.cpp.

◆ DamageEquippedItem()

bool cInventory::DamageEquippedItem ( short  a_Amount = 1)

Adds the specified damage to the currently held item; deletes the item and returns true if the item broke.

Definition at line 489 of file Inventory.cpp.

◆ DamageItem()

bool cInventory::DamageItem ( int  a_SlotNum,
short  a_Amount 
)

Adds the specified damage to the specified item; deletes the item and returns true if the item broke.

Definition at line 514 of file Inventory.cpp.

◆ FindItem()

cItem * cInventory::FindItem ( const cItem a_RecipeItem)

Finds an item based on ItemType and ItemDamage (<- defines the itemType, too)

Definition at line 212 of file Inventory.cpp.

◆ GetArmorGrid()

cItemGrid& cInventory::GetArmorGrid ( void  )
inline

Returns the cItemGrid object representing the armor slots.

Definition at line 114 of file Inventory.h.

◆ GetArmorSlot()

const cItem & cInventory::GetArmorSlot ( int  a_ArmorSlotNum) const

Returns current item in a_ArmorSlotNum in armor slots.

Definition at line 412 of file Inventory.cpp.

◆ GetEquippedBoots()

const cItem& cInventory::GetEquippedBoots ( void  ) const
inline

Definition at line 179 of file Inventory.h.

◆ GetEquippedChestplate()

const cItem& cInventory::GetEquippedChestplate ( void  ) const
inline

Definition at line 177 of file Inventory.h.

◆ GetEquippedHelmet()

const cItem& cInventory::GetEquippedHelmet ( void  ) const
inline

Definition at line 176 of file Inventory.h.

◆ GetEquippedItem()

const cItem & cInventory::GetEquippedItem ( void  ) const

Returns current equiped item.

Definition at line 463 of file Inventory.cpp.

◆ GetEquippedLeggings()

const cItem& cInventory::GetEquippedLeggings ( void  ) const
inline

Definition at line 178 of file Inventory.h.

◆ GetEquippedSlotNum()

int cInventory::GetEquippedSlotNum ( void  )
inline

Returns slot number of equiped item.

Definition at line 162 of file Inventory.h.

◆ GetGridForSlotNum() [1/2]

cItemGrid * cInventory::GetGridForSlotNum ( int  a_SlotNum,
int &  a_GridSlotNum 
)
protected

Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return nullptr for invalid SlotNum.

Definition at line 806 of file Inventory.cpp.

◆ GetGridForSlotNum() [2/2]

const cItemGrid * cInventory::GetGridForSlotNum ( int  a_SlotNum,
int &  a_GridSlotNum 
) const
protected

Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return nullptr for invalid SlotNum.

Definition at line 777 of file Inventory.cpp.

◆ GetHotbarGrid()

cItemGrid& cInventory::GetHotbarGrid ( void  )
inline

Returns the cItemGrid object representing the hotbar slots.

Definition at line 120 of file Inventory.h.

◆ GetHotbarSlot()

const cItem & cInventory::GetHotbarSlot ( int  a_HotBarSlotNum) const

Returns current item in a_ArmorSlotNum in hotbar slots.

Definition at line 440 of file Inventory.cpp.

◆ GetInventoryGrid()

cItemGrid& cInventory::GetInventoryGrid ( void  )
inline

Returns the cItemGrid object representing the main inventory slots.

Definition at line 117 of file Inventory.h.

◆ GetInventorySlot()

const cItem & cInventory::GetInventorySlot ( int  a_InventorySlotNum) const

Returns current item in a_ArmorSlotNum in inventory slots.

Definition at line 426 of file Inventory.cpp.

◆ GetOwner() [1/2]

cPlayer& cInventory::GetOwner ( void  )
inline

Returns the player associated with this inventory.

Definition at line 123 of file Inventory.h.

◆ GetOwner() [2/2]

const cPlayer& cInventory::GetOwner ( void  ) const
inline

Returns the player associated with this inventory (const version)

Definition at line 131 of file Inventory.h.

◆ GetShieldSlot()

const cItem & cInventory::GetShieldSlot ( ) const

Returns current item in shield slot.

Definition at line 454 of file Inventory.cpp.

◆ GetSlot()

const cItem & cInventory::GetSlot ( int  a_SlotNum) const

Returns current item in a_SlotNum slot.

Definition at line 390 of file Inventory.cpp.

◆ HasItems()

bool cInventory::HasItems ( const cItem a_ItemStack)

Returns true if there are at least as many items of type a_ItemStack as in a_ItemStack.

Definition at line 303 of file Inventory.cpp.

◆ HowManyCanFit() [1/2]

int cInventory::HowManyCanFit ( const cItem a_ItemStack,
bool  a_ConsiderEmptySlots = true 
)

Returns number of items out of a_ItemStack that can fit in the storage.

Definition at line 52 of file Inventory.cpp.

◆ HowManyCanFit() [2/2]

int cInventory::HowManyCanFit ( const cItem a_ItemStack,
int  a_BeginSlotNum,
int  a_EndSlotNum,
bool  a_ConsiderEmptySlots = true 
)

Returns how many items of the specified type would fit into the slot range specified.

Definition at line 61 of file Inventory.cpp.

◆ HowManyItems()

int cInventory::HowManyItems ( const cItem a_Item)

Returns the number of items of type a_Item that are stored.

Definition at line 290 of file Inventory.cpp.

◆ LoadFromJson()

bool cInventory::LoadFromJson ( Json::Value &  a_Value)

Definition at line 744 of file Inventory.cpp.

◆ OnSlotChanged()

void cInventory::OnSlotChanged ( cItemGrid a_ItemGrid,
int  a_SlotNum 
)
overrideprotectedvirtual

Called whenever a slot changes.

Implements cItemGrid::cListener.

Definition at line 835 of file Inventory.cpp.

◆ RemoveItem()

int cInventory::RemoveItem ( const cItem a_ItemStack)

Removes the specified item from the inventory, as many as possible, up to a_ItemStack.m_ItemCount.

Returns the number of items that were removed.

Definition at line 189 of file Inventory.cpp.

◆ RemoveOneEquippedItem()

bool cInventory::RemoveOneEquippedItem ( void  )

Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed.

Definition at line 232 of file Inventory.cpp.

◆ ReplaceOneEquippedItem()

int cInventory::ReplaceOneEquippedItem ( const cItem a_Item,
bool  a_TryOtherSlots = true 
)

Removes one item from the the current equipped item stack, and attempts to add the specified item stack back to the same slot.

If it is not possible to place the item in the same slot, optionally (default true) tries to place the specified item elsewhere in the inventory. Returns the number of items successfully added. If the currently equipped slot is empty, its contents are simply set to the given Item.

Definition at line 247 of file Inventory.cpp.

◆ SaveToJson()

void cInventory::SaveToJson ( Json::Value &  a_Value)

Definition at line 699 of file Inventory.cpp.

◆ SendEquippedSlot()

void cInventory::SendEquippedSlot ( )

Sends the equipped item slot to the client.

Definition at line 380 of file Inventory.cpp.

◆ SendSlot()

void cInventory::SendSlot ( int  a_SlotNum)

Sends the slot contents to the owner.

Definition at line 560 of file Inventory.cpp.

◆ SetArmorSlot()

void cInventory::SetArmorSlot ( int  a_ArmorSlotNum,
const cItem a_Item 
)

Puts a_Item item in a_ArmorSlotNum slot number in armor slots.

Definition at line 335 of file Inventory.cpp.

◆ SetEquippedItem()

void cInventory::SetEquippedItem ( const cItem a_Item)

Sets current item in the equipped hotbar slot.

Definition at line 371 of file Inventory.cpp.

◆ SetEquippedSlotNum()

void cInventory::SetEquippedSlotNum ( int  a_SlotNum)

Sets equiped item to the a_SlotNum slot number.

Definition at line 472 of file Inventory.cpp.

◆ SetHotbarSlot()

void cInventory::SetHotbarSlot ( int  a_HotBarSlotNum,
const cItem a_Item 
)

Puts a_Item item in a_HotBarSlotNum slot number in hotbar slots.

Definition at line 353 of file Inventory.cpp.

◆ SetInventorySlot()

void cInventory::SetInventorySlot ( int  a_InventorySlotNum,
const cItem a_Item 
)

Puts a_Item item in a_InventorySlotNum slot number in inventory slots.

Definition at line 344 of file Inventory.cpp.

◆ SetShieldSlot()

void cInventory::SetShieldSlot ( const cItem a_Item)

Sets current item in shield slot.

Definition at line 362 of file Inventory.cpp.

◆ SetSlot()

void cInventory::SetSlot ( int  a_SlotNum,
const cItem a_Item 
)

Puts a_Item item in a_SlotNum slot number.

Definition at line 313 of file Inventory.cpp.

◆ UpdateItems()

void cInventory::UpdateItems ( void  )

Update items (e.g.

Maps)

Definition at line 686 of file Inventory.cpp.

Member Data Documentation

◆ m_ArmorSlots

cItemGrid cInventory::m_ArmorSlots
protected

Definition at line 198 of file Inventory.h.

◆ m_EquippedSlotNum

int cInventory::m_EquippedSlotNum
protected

Definition at line 203 of file Inventory.h.

◆ m_HotbarSlots

cItemGrid cInventory::m_HotbarSlots
protected

Definition at line 200 of file Inventory.h.

◆ m_InventorySlots

cItemGrid cInventory::m_InventorySlots
protected

Definition at line 199 of file Inventory.h.

◆ m_Owner

cPlayer& cInventory::m_Owner
protected

Definition at line 205 of file Inventory.h.

◆ m_ShieldSlots

cItemGrid cInventory::m_ShieldSlots
protected

Definition at line 201 of file Inventory.h.


The documentation for this class was generated from the following files: