From cf87169737fdeeee7d4b160688bbed7194e46147 Mon Sep 17 00:00:00 2001 From: "madmaxoft@gmail.com" Date: Fri, 24 May 2013 07:30:39 +0000 Subject: Refactored cInventory to use cItemGrid for the actual Storage This makes the API more orthogonal and is easier to use in the plugins. Also changes in the inventory are now propagated to the needed places (armor updates to BroadcastEntityEquipment etc.) even when the inventory is changed by a plugin. git-svn-id: http://mc-server.googlecode.com/svn/trunk@1503 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/Inventory.h | 159 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 112 insertions(+), 47 deletions(-) (limited to 'source/Inventory.h') diff --git a/source/Inventory.h b/source/Inventory.h index 351d900dc..572a566e1 100644 --- a/source/Inventory.h +++ b/source/Inventory.h @@ -1,7 +1,7 @@ #pragma once -#include "Item.h" +#include "ItemGrid.h" @@ -18,36 +18,108 @@ class cPlayer; +// tolua_begin -class cInventory // tolua_export -{ // tolua_export +/** This class represents the player's inventory +The slots are divided into three areas: +- armor slots (1 x 4) +- inventory slots (9 x 3) +- hotbar slots (9 x 1) +The generic GetSlot(), SetSlot() and HowManyCanFit() functions take the index of the slots, +as if armor slots, inventory slots and then hotbar slots were put one after another. +You can use the invArmorOffset, invInventoryOffset and invHotbarOffset constants. +*/ + +class cInventory : + public cItemGrid::cListener +{ public: - cInventory(cPlayer & a_Owner); - ~cInventory(); + + // Counts and offsets to individual parts of the inventory, as used by GetSlot() / SetSlot() / HowManyCanFit(): + enum + { + invArmorCount = 4, + invInventoryCount = 9 * 3, + invHotbarCount = 9, + + invArmorOffset = 0, + invInventoryOffset = invArmorOffset + invArmorCount, + invHotbarOffset = invInventoryOffset + invInventoryCount, + invNumSlots = invHotbarOffset + invHotbarCount + } ; - void Clear(); // tolua_export + // tolua_end + + cInventory(cPlayer & a_Owner); + + // tolua_begin - // cItem * GetSlotsForType( int a_Type ); - // int GetSlotCountForType( int a_Type ); + /// Removes all items from the entire inventory + void Clear(void); - bool AddItem( cItem & a_Item ); // tolua_export - bool AddItemAnyAmount( cItem & a_Item ); // tolua_export - bool RemoveItem( cItem & a_Item ); // tolua_export + /// Returns number of items out of a_ItemStack that can fit in the storage + int HowManyCanFit(const cItem & a_ItemStack, bool a_ConsiderEmptySlots); - void SaveToJson(Json::Value & a_Value); - bool LoadFromJson(Json::Value & a_Value); + /// Returns how many items of the specified type would fit into the slot range specified + int HowManyCanFit(const cItem & a_ItemStack, int a_BeginSlotNum, int a_EndSlotNum, bool a_ConsiderEmptySlots); + + /** 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. + Returns the number of items that fit. + */ + int AddItem(const cItem & a_ItemStack, bool a_AllowNewStacks = true); + + /** 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. + */ + int AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks); + + /// Removes one item out of the currently equipped item stack, returns true if successful, false if empty-handed + bool RemoveOneEquippedItem(void); + + /// Returns the number of items of type a_Item that are stored + int HowManyItems(const cItem & a_Item); + + /// Returns true if there are at least as many items of type a_ItemStack as in a_ItemStack + bool HasItems(const cItem & a_ItemStack); + + /// Returns the cItemGrid object representing the armor slots + cItemGrid & GetArmorGrid(void) { return m_ArmorSlots; } + + /// Returns the cItemGrid object representing the main inventory slots + cItemGrid & GetInventoryGrid(void) { return m_InventorySlots; } + + /// Returns the cItemGrid object representing the hotbar slots + cItemGrid & GetHotbarGrid(void) { return m_HotbarSlots; } + + /// Returns the player associated with this inventory + cPlayer & GetOwner(void) { return m_Owner; } + + /// Copies the non-empty slots into a_ItemStacks; preserves the original a_Items contents + void CopyToItems(cItems & a_Items); + + // tolua_end void SendWholeInventory(cClientHandle & a_Client); - const cItem * GetSlots(void) const { return m_Slots; } - + /// Returns the player associated with this inventory (const version) + const cPlayer & GetOwner(void) const { return m_Owner; } + // tolua_begin const cItem & GetSlot(int a_SlotNum) const; - const cItem & GetHotBarSlot(int a_HotBarSlotNum) const; + const cItem & GetArmorSlot(int a_ArmorSlotNum) const; + const cItem & GetInventorySlot(int a_InventorySlotNum) const; + const cItem & GetHotbarSlot(int a_HotBarSlotNum) const; const cItem & GetEquippedItem(void) const; void SetSlot(int a_SlotNum, const cItem & a_Item); - void SetHotBarSlot(int a_HotBarSlotNum, const cItem & a_Item); + void SetArmorSlot(int a_ArmorSlotNum, const cItem & a_Item); + void SetInventorySlot(int a_InventorySlotNum, const cItem & a_Item); + void SetHotbarSlot(int a_HotBarSlotNum, const cItem & a_Item); void SetEquippedSlotNum(int a_SlotNum); int GetEquippedSlotNum(void) { return m_EquippedSlotNum; } @@ -58,48 +130,41 @@ public: /// Adds the specified damage to the currently held item; deletes the item and returns true if the item broke. bool DamageEquippedItem(short a_Amount = 1); - const cItem & GetEquippedHelmet (void) const { return m_Slots[c_ArmorOffset]; } - const cItem & GetEquippedChestplate(void) const { return m_Slots[c_ArmorOffset + 1]; } - const cItem & GetEquippedLeggings (void) const { return m_Slots[c_ArmorOffset + 2]; } - const cItem & GetEquippedBoots (void) const { return m_Slots[c_ArmorOffset + 3]; } + const cItem & GetEquippedHelmet (void) const { return m_ArmorSlots.GetSlot(0); } + const cItem & GetEquippedChestplate(void) const { return m_ArmorSlots.GetSlot(1); } + const cItem & GetEquippedLeggings (void) const { return m_ArmorSlots.GetSlot(2); } + const cItem & GetEquippedBoots (void) const { return m_ArmorSlots.GetSlot(3); } - // tolua_end + /// Sends the slot contents to the owner + void SendSlot(int a_SlotNum); - void SendSlot( int a_SlotNum ); // tolua_export - - /// Returns how many items of the specified type would fit into the slot range specified - int HowManyCanFit(short a_ItemType, short a_ItemDamage, int a_BeginSlot, int a_EndSlot); + // tolua_end - /// Moves items, fitting them into the slot range specified, up to a_Count items. Returns the number of items moved - int MoveItem(short a_ItemType, short a_ItemDamage, int a_Count, int a_BeginSlot, int a_EndSlot); - - static const unsigned int c_NumSlots = 45; - static const unsigned int c_MainSlots = 27; - static const unsigned int c_HotSlots = 9; - static const unsigned int c_CraftSlots = 4; - static const unsigned int c_ArmorSlots = 4; + /// Converts an armor slot number into the ID for the EntityEquipment packet + static int ArmorSlotNumToEntityEquipmentID(short a_ArmorSlotNum); - static const unsigned int c_CraftOffset = 0; - static const unsigned int c_ArmorOffset = 5; - static const unsigned int c_MainOffset = 9; - static const unsigned int c_HotOffset = 36; - - /// Converts a slot number into the ID for the EntityEquipment packet - static int SlotNumToEntityEquipmentID(short a_SlotNum); + void SaveToJson(Json::Value & a_Value); + bool LoadFromJson(Json::Value & a_Value); protected: bool AddToBar( cItem & a_Item, const int a_Offset, const int a_Size, bool* a_bChangedSlots, int a_Mode = 0 ); - - cItem m_Slots[c_NumSlots]; - cItem * m_MainSlots; - cItem * m_CraftSlots; - cItem * m_ArmorSlots; - cItem * m_HotSlots; + cItemGrid m_ArmorSlots; + cItemGrid m_InventorySlots; + cItemGrid m_HotbarSlots; int m_EquippedSlotNum; cPlayer & m_Owner; + + /// Returns the ItemGrid and the (grid-local) slot number for a (global) slot number; return NULL for invalid SlotNum + 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 NULL for invalid SlotNum + cItemGrid * GetGridForSlotNum(int a_SlotNum, int & a_GridSlotNum); + + // cItemGrid::cListener override: + virtual void OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) override; }; // tolua_export -- cgit v1.2.3