Cuberite
A lightweight, fast and extensible game server for Minecraft
Inventory.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "ItemGrid.h"
5 
6 
7 
8 
9 
10 namespace Json
11 {
12  class Value;
13 };
14 
15 class cPlayer;
16 
17 
18 
19 
20 // tolua_begin
21 
32 class cInventory :
33  // tolua_end
35  // tolua_begin
36 {
37 public:
38 
39  // Counts and offsets to individual parts of the inventory, as used by GetSlot() / SetSlot() / HowManyCanFit():
40  enum
41  {
45  invShieldCount = 1, // Number of slots in shield slots grid
46 
50  invShieldOffset = invHotbarOffset + invHotbarCount, // Offset where shield slots start
52  } ;
53 
54  // tolua_end
55 
56  cInventory(cPlayer & a_Owner);
57 
58  virtual ~cInventory() override {}
59 
60  // tolua_begin
61 
63  void Clear(void);
64 
66  int HowManyCanFit(const cItem & a_ItemStack, bool a_ConsiderEmptySlots = true);
67 
69  int HowManyCanFit(const cItem & a_ItemStack, int a_BeginSlotNum, int a_EndSlotNum, bool a_ConsiderEmptySlots = true);
70 
77  char AddItem(const cItem & a_ItemStack, bool a_AllowNewStacks = true);
78 
85  char AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks);
86 
89  int RemoveItem(const cItem & a_ItemStack);
90 
92  cItem * FindItem(const cItem & a_RecipeItem);
93 
95  bool RemoveOneEquippedItem(void);
96 
102  int ReplaceOneEquippedItem(const cItem & a_Item, bool a_TryOtherSlots = true);
103 
105  int HowManyItems(const cItem & a_Item);
106 
108  bool HasItems(const cItem & a_ItemStack);
109 
111  void SendEquippedSlot();
112 
114  cItemGrid & GetArmorGrid(void) { return m_ArmorSlots; }
115 
118 
121 
123  cPlayer & GetOwner(void) { return m_Owner; }
124 
126  void CopyToItems(cItems & a_Items);
127 
128  // tolua_end
129 
131  const cPlayer & GetOwner(void) const { return m_Owner; }
132 
133  // tolua_begin
134 
136  const cItem & GetSlot(int a_SlotNum) const;
138  const cItem & GetArmorSlot(int a_ArmorSlotNum) const;
140  const cItem & GetInventorySlot(int a_InventorySlotNum) const;
142  const cItem & GetHotbarSlot(int a_HotBarSlotNum) const;
144  const cItem & GetShieldSlot() const;
146  const cItem & GetEquippedItem(void) const;
148  void SetSlot(int a_SlotNum, const cItem & a_Item);
150  void SetArmorSlot(int a_ArmorSlotNum, const cItem & a_Item);
152  void SetInventorySlot(int a_InventorySlotNum, const cItem & a_Item);
154  void SetHotbarSlot(int a_HotBarSlotNum, const cItem & a_Item);
156  void SetShieldSlot(const cItem & a_Item);
158  void SetEquippedItem(const cItem & a_Item);
160  void SetEquippedSlotNum(int a_SlotNum);
162  int GetEquippedSlotNum(void) { return m_EquippedSlotNum; }
163 
168  char ChangeSlotCount(int a_SlotNum, char a_AddToCount);
169 
171  bool DamageItem(int a_SlotNum, short a_Amount);
172 
174  bool DamageEquippedItem(short a_Amount = 1);
175 
176  const cItem & GetEquippedHelmet (void) const { return m_ArmorSlots.GetSlot(0); }
177  const cItem & GetEquippedChestplate(void) const { return m_ArmorSlots.GetSlot(1); }
178  const cItem & GetEquippedLeggings (void) const { return m_ArmorSlots.GetSlot(2); }
179  const cItem & GetEquippedBoots (void) const { return m_ArmorSlots.GetSlot(3); }
180 
181  // tolua_end
182 
184  void SendSlot(int a_SlotNum);
185 
187  void UpdateItems(void);
188 
190  static int ArmorSlotNumToEntityEquipmentID(short a_ArmorSlotNum);
191 
192  void SaveToJson(Json::Value & a_Value);
193  bool LoadFromJson(Json::Value & a_Value);
194 
195 protected:
196  bool AddToBar(cItem & a_Item, const int a_Offset, const int a_Size, bool * a_bChangedSlots, int a_Mode = 0);
197 
202 
204 
206 
208  const cItemGrid * GetGridForSlotNum(int a_SlotNum, int & a_GridSlotNum) const;
209 
211  cItemGrid * GetGridForSlotNum(int a_SlotNum, int & a_GridSlotNum);
212 
213  // cItemGrid::cListener override:
214  virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) override;
215 }; // tolua_export
Definition: Inventory.h:11
Definition: Player.h:29
This class represents the player's inventory The slots are divided into three areas:
Definition: Inventory.h:36
char AddItem(const cItem &a_ItemStack, bool a_AllowNewStacks=true)
Adds as many items out of a_ItemStack as can fit.
Definition: Inventory.cpp:106
int m_EquippedSlotNum
Definition: Inventory.h:203
const cItem & GetHotbarSlot(int a_HotBarSlotNum) const
Returns current item in a_ArmorSlotNum in hotbar slots.
Definition: Inventory.cpp:440
bool LoadFromJson(Json::Value &a_Value)
Definition: Inventory.cpp:744
void UpdateItems(void)
Update items (e.g.
Definition: Inventory.cpp:686
const cItem & GetEquippedChestplate(void) const
Definition: Inventory.h:177
cItemGrid & GetHotbarGrid(void)
Returns the cItemGrid object representing the hotbar slots.
Definition: Inventory.h:120
cItemGrid m_ArmorSlots
Definition: Inventory.h:198
virtual void OnSlotChanged(cItemGrid *a_ItemGrid, int a_SlotNum) override
Called whenever a slot changes.
Definition: Inventory.cpp:835
cItemGrid m_InventorySlots
Definition: Inventory.h:199
virtual ~cInventory() override
Definition: Inventory.h:58
cInventory(cPlayer &a_Owner)
Definition: Inventory.cpp:20
void CopyToItems(cItems &a_Items)
Copies the non-empty slots into a_ItemStacks; preserves the original a_Items contents.
Definition: Inventory.cpp:549
static int ArmorSlotNumToEntityEquipmentID(short a_ArmorSlotNum)
Converts an armor slot number into the ID for the EntityEquipment packet.
Definition: Inventory.cpp:611
bool HasItems(const cItem &a_ItemStack)
Returns true if there are at least as many items of type a_ItemStack as in a_ItemStack.
Definition: Inventory.cpp:303
cItemGrid m_ShieldSlots
Definition: Inventory.h:201
bool DamageEquippedItem(short a_Amount=1)
Adds the specified damage to the currently held item; deletes the item and returns true if the item b...
Definition: Inventory.cpp:489
const cItem & GetArmorSlot(int a_ArmorSlotNum) const
Returns current item in a_ArmorSlotNum in armor slots.
Definition: Inventory.cpp:412
char AddItems(cItems &a_ItemStackList, bool a_AllowNewStacks)
Same as AddItem, but works on an entire list of item stacks.
Definition: Inventory.cpp:165
cItem * FindItem(const cItem &a_RecipeItem)
Finds an item based on ItemType and ItemDamage (<- defines the itemType, too)
Definition: Inventory.cpp:212
@ invNumSlots
Definition: Inventory.h:51
@ invArmorOffset
Definition: Inventory.h:47
@ invShieldCount
Definition: Inventory.h:45
@ invInventoryOffset
Definition: Inventory.h:48
@ invHotbarCount
Definition: Inventory.h:44
@ invShieldOffset
Definition: Inventory.h:50
@ invInventoryCount
Definition: Inventory.h:43
@ invHotbarOffset
Definition: Inventory.h:49
@ invArmorCount
Definition: Inventory.h:42
void SetEquippedSlotNum(int a_SlotNum)
Sets equiped item to the a_SlotNum slot number.
Definition: Inventory.cpp:472
void SaveToJson(Json::Value &a_Value)
Definition: Inventory.cpp:699
const cItem & GetEquippedBoots(void) const
Definition: Inventory.h:179
const cPlayer & GetOwner(void) const
Returns the player associated with this inventory (const version)
Definition: Inventory.h:131
cItemGrid m_HotbarSlots
Definition: Inventory.h:200
cItemGrid & GetInventoryGrid(void)
Returns the cItemGrid object representing the main inventory slots.
Definition: Inventory.h:117
void SendSlot(int a_SlotNum)
Sends the slot contents to the owner.
Definition: Inventory.cpp:560
cPlayer & GetOwner(void)
Returns the player associated with this inventory.
Definition: Inventory.h:123
void SetSlot(int a_SlotNum, const cItem &a_Item)
Puts a_Item item in a_SlotNum slot number.
Definition: Inventory.cpp:313
const cItem & GetSlot(int a_SlotNum) const
Returns current item in a_SlotNum slot.
Definition: Inventory.cpp:390
void SendEquippedSlot()
Sends the equipped item slot to the client.
Definition: Inventory.cpp:380
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.
Definition: Inventory.cpp:498
bool AddToBar(cItem &a_Item, const int a_Offset, const int a_Size, bool *a_bChangedSlots, int a_Mode=0)
void SetShieldSlot(const cItem &a_Item)
Sets current item in shield slot.
Definition: Inventory.cpp:362
int HowManyItems(const cItem &a_Item)
Returns the number of items of type a_Item that are stored.
Definition: Inventory.cpp:290
const cItem & GetEquippedHelmet(void) const
Definition: Inventory.h:176
void SetInventorySlot(int a_InventorySlotNum, const cItem &a_Item)
Puts a_Item item in a_InventorySlotNum slot number in inventory slots.
Definition: Inventory.cpp:344
void Clear(void)
Removes all items from the entire inventory.
Definition: Inventory.cpp:40
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 sta...
Definition: Inventory.cpp:247
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.
Definition: Inventory.cpp:514
const cItem & GetInventorySlot(int a_InventorySlotNum) const
Returns current item in a_ArmorSlotNum in inventory slots.
Definition: Inventory.cpp:426
void SetArmorSlot(int a_ArmorSlotNum, const cItem &a_Item)
Puts a_Item item in a_ArmorSlotNum slot number in armor slots.
Definition: Inventory.cpp:335
int GetEquippedSlotNum(void)
Returns slot number of equiped item.
Definition: Inventory.h:162
void SetHotbarSlot(int a_HotBarSlotNum, const cItem &a_Item)
Puts a_Item item in a_HotBarSlotNum slot number in hotbar slots.
Definition: Inventory.cpp:353
const cItemGrid * GetGridForSlotNum(int a_SlotNum, int &a_GridSlotNum) const
Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return nullptr for ...
Definition: Inventory.cpp:777
cPlayer & m_Owner
Definition: Inventory.h:205
bool RemoveOneEquippedItem(void)
Removes one item out of the currently equipped item stack, returns true if successful,...
Definition: Inventory.cpp:232
const cItem & GetEquippedItem(void) const
Returns current equiped item.
Definition: Inventory.cpp:463
int HowManyCanFit(const cItem &a_ItemStack, bool a_ConsiderEmptySlots=true)
Returns number of items out of a_ItemStack that can fit in the storage.
Definition: Inventory.cpp:52
int RemoveItem(const cItem &a_ItemStack)
Removes the specified item from the inventory, as many as possible, up to a_ItemStack....
Definition: Inventory.cpp:189
const cItem & GetShieldSlot() const
Returns current item in shield slot.
Definition: Inventory.cpp:454
cItemGrid & GetArmorGrid(void)
Returns the cItemGrid object representing the armor slots.
Definition: Inventory.h:114
void SetEquippedItem(const cItem &a_Item)
Sets current item in the equipped hotbar slot.
Definition: Inventory.cpp:371
const cItem & GetEquippedLeggings(void) const
Definition: Inventory.h:178
Definition: Item.h:37
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:215
const cItem & GetSlot(int a_X, int a_Y) const
Definition: ItemGrid.cpp:96
This class is used as a callback for when a slot changes.
Definition: ItemGrid.h:26