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  {
42  invArmorCount = 4,
43  invInventoryCount = 9 * 3,
44  invHotbarCount = 9,
45  invShieldCount = 1, // Number of slots in shield slots grid
46 
47  invArmorOffset = 0,
48  invInventoryOffset = invArmorOffset + invArmorCount,
49  invHotbarOffset = invInventoryOffset + invInventoryCount,
50  invShieldOffset = invHotbarOffset + invHotbarCount, // Offset where shield slots start
51  invNumSlots = invShieldOffset + invShieldCount
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 
76  int AddItem(const cItem & a_ItemStack, bool a_AllowNewStacks = true);
77 
84  int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks);
85 
88  int RemoveItem(const cItem & a_ItemStack);
89 
91  bool RemoveOneEquippedItem(void);
92 
94  int HowManyItems(const cItem & a_Item);
95 
97  bool HasItems(const cItem & a_ItemStack);
98 
100  void SendEquippedSlot();
101 
103  cItemGrid & GetArmorGrid(void) { return m_ArmorSlots; }
104 
106  cItemGrid & GetInventoryGrid(void) { return m_InventorySlots; }
107 
109  cItemGrid & GetHotbarGrid(void) { return m_HotbarSlots; }
110 
112  cPlayer & GetOwner(void) { return m_Owner; }
113 
115  void CopyToItems(cItems & a_Items);
116 
117  // tolua_end
118 
120  const cPlayer & GetOwner(void) const { return m_Owner; }
121 
122  // tolua_begin
123 
125  const cItem & GetSlot(int a_SlotNum) const;
127  const cItem & GetArmorSlot(int a_ArmorSlotNum) const;
129  const cItem & GetInventorySlot(int a_InventorySlotNum) const;
131  const cItem & GetHotbarSlot(int a_HotBarSlotNum) const;
133  const cItem & GetShieldSlot() const;
135  const cItem & GetEquippedItem(void) const;
137  void SetSlot(int a_SlotNum, const cItem & a_Item);
139  void SetArmorSlot(int a_ArmorSlotNum, const cItem & a_Item);
141  void SetInventorySlot(int a_InventorySlotNum, const cItem & a_Item);
143  void SetHotbarSlot(int a_HotBarSlotNum, const cItem & a_Item);
145  void SetShieldSlot(const cItem & a_Item);
147  void SetEquippedSlotNum(int a_SlotNum);
149  int GetEquippedSlotNum(void) { return m_EquippedSlotNum; }
150 
155  int ChangeSlotCount(int a_SlotNum, int a_AddToCount);
156 
158  bool DamageItem(int a_SlotNum, short a_Amount);
159 
161  bool DamageEquippedItem(short a_Amount = 1);
162 
163  const cItem & GetEquippedHelmet (void) const { return m_ArmorSlots.GetSlot(0); }
164  const cItem & GetEquippedChestplate(void) const { return m_ArmorSlots.GetSlot(1); }
165  const cItem & GetEquippedLeggings (void) const { return m_ArmorSlots.GetSlot(2); }
166  const cItem & GetEquippedBoots (void) const { return m_ArmorSlots.GetSlot(3); }
167 
168  // tolua_end
169 
171  void SendSlot(int a_SlotNum);
172 
174  void UpdateItems(void);
175 
177  static int ArmorSlotNumToEntityEquipmentID(short a_ArmorSlotNum);
178 
179  void SaveToJson(Json::Value & a_Value);
180  bool LoadFromJson(Json::Value & a_Value);
181 
182 protected:
183  bool AddToBar(cItem & a_Item, const int a_Offset, const int a_Size, bool * a_bChangedSlots, int a_Mode = 0);
184 
189 
191 
193 
195  const cItemGrid * GetGridForSlotNum(int a_SlotNum, int & a_GridSlotNum) const;
196 
198  cItemGrid * GetGridForSlotNum(int a_SlotNum, int & a_GridSlotNum);
199 
200  // cItemGrid::cListener override:
201  virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) override;
202 }; // tolua_export
203 
204 
205 
206 
cItemGrid & GetInventoryGrid(void)
Returns the cItemGrid object representing the main inventory slots.
Definition: Inventory.h:106
const cItem & GetEquippedBoots(void) const
Definition: Inventory.h:166
cItemGrid m_InventorySlots
Definition: Inventory.h:186
cItemGrid & GetArmorGrid(void)
Returns the cItemGrid object representing the armor slots.
Definition: Inventory.h:103
int GetEquippedSlotNum(void)
Returns slot number of equiped item.
Definition: Inventory.h:149
Definition: Player.h:27
This class is used as a callback for when a slot changes.
Definition: ItemGrid.h:25
cPlayer & GetOwner(void)
Returns the player associated with this inventory.
Definition: Inventory.h:112
Definition: Inventory.h:10
virtual ~cInventory() override
Definition: Inventory.h:58
const cItem & GetEquippedLeggings(void) const
Definition: Inventory.h:165
const cItem & GetEquippedHelmet(void) const
Definition: Inventory.h:163
int m_EquippedSlotNum
Definition: Inventory.h:190
const cPlayer & GetOwner(void) const
Returns the player associated with this inventory (const version)
Definition: Inventory.h:120
cItemGrid m_ShieldSlots
Definition: Inventory.h:188
cPlayer & m_Owner
Definition: Inventory.h:192
cItemGrid m_HotbarSlots
Definition: Inventory.h:187
cItemGrid m_ArmorSlots
Definition: Inventory.h:185
cItemGrid & GetHotbarGrid(void)
Returns the cItemGrid object representing the hotbar slots.
Definition: Inventory.h:109
This class represents the player's inventory The slots are divided into three areas: ...
Definition: Inventory.h:32
Definition: Item.h:36
const cItem & GetEquippedChestplate(void) const
Definition: Inventory.h:164
This class bridges a vector of cItem for safe access via Lua.
Definition: Item.h:234