Skip to content

Commit

Permalink
Off-hand/shield slot functional, save and load slot, bow + arrow func…
Browse files Browse the repository at this point in the history
…tional (cuberite#3725)

Fixes cuberite#3714.
  • Loading branch information
spekdrum authored and madmaxoft committed May 24, 2017
1 parent 8a890cf commit 84bdba3
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 9 deletions.
29 changes: 29 additions & 0 deletions Server/Plugins/APIDump/APIDesc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7970,6 +7970,16 @@ These ItemGrids are available in the API and can be manipulated by the plugins,
},
Notes = "Returns the specified hotbar slot contents. Note that the returned item is read-only",
},
GetShieldSlot =
{
Returns =
{
{
Type = "cItem",
},
},
Notes = "Returns current item in shield slot.",
},
GetInventoryGrid =
{
Returns =
Expand Down Expand Up @@ -8177,6 +8187,17 @@ These ItemGrids are available in the API and can be manipulated by the plugins,
},
Notes = "Sets the specified hotbar slot contents",
},
SetShieldSlot =
{
Params =
{
{
Name = "Item",
Type = "cItem",
},
},
Notes = "Sets the shield slot content",
},
SetInventorySlot =
{
Params =
Expand Down Expand Up @@ -8234,6 +8255,14 @@ These ItemGrids are available in the API and can be manipulated by the plugins,
{
Notes = "Starting slot number of the main inventory part",
},
invShieldCount =
{
Notes = "Number of slots in the Shield part",
},
invShieldOffset =
{
Notes = "Starting slot number of the Shield part",
},
invNumSlots =
{
Notes = "Total number of slots in a cInventory",
Expand Down
10 changes: 10 additions & 0 deletions src/ClientHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,11 @@ void cClientHandle::HandleLeftClick(int a_BlockX, int a_BlockY, int a_BlockZ, eB
// A plugin doesn't agree with the action. The plugin itself is responsible for handling the consequences (possible inventory mismatch)
return;
}
// When bow is in off-hand / shield slot
if (m_Player->GetInventory().GetShieldSlot().m_ItemType == E_ITEM_BOW)
{
ItemHandler = cItemHandler::GetItemHandler(m_Player->GetInventory().GetShieldSlot());
}
ItemHandler->OnItemShoot(m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
}
return;
Expand Down Expand Up @@ -1524,6 +1529,11 @@ void cClientHandle::HandleRightClick(int a_BlockX, int a_BlockY, int a_BlockZ, e
ItemHandler->OnItemUse(World, m_Player, PluginInterface, Equipped, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace);
PlgMgr->CallHookPlayerUsedItem(*m_Player, a_BlockX, a_BlockY, a_BlockZ, a_BlockFace, a_CursorX, a_CursorY, a_CursorZ);
}
// Charge bow when it's in slot off-hand / shield
if ((a_BlockFace == BLOCK_FACE_NONE) && (m_Player->GetInventory().GetShieldSlot().m_ItemType == E_ITEM_BOW))
{
m_Player->StartChargingBow();
}
}


Expand Down
57 changes: 50 additions & 7 deletions src/Inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cInventory::cInventory(cPlayer & a_Owner) :
m_ArmorSlots (1, 4), // 1 x 4 slots
m_InventorySlots(9, 3), // 9 x 3 slots
m_HotbarSlots (9, 1), // 9 x 1 slots
m_ShieldSlots (1, 1), // 1 x 1 slots
m_Owner(a_Owner)
{
// Ask each ItemGrid to report changes to us:
Expand All @@ -40,6 +41,7 @@ void cInventory::Clear(void)
m_ArmorSlots.Clear();
m_InventorySlots.Clear();
m_HotbarSlots.Clear();
m_ShieldSlots.Clear();
}


Expand Down Expand Up @@ -178,7 +180,12 @@ int cInventory::AddItems(cItems & a_ItemStackList, bool a_AllowNewStacks)

int cInventory::RemoveItem(const cItem & a_ItemStack)
{
int RemovedItems = m_HotbarSlots.RemoveItem(a_ItemStack);
int RemovedItems = m_ShieldSlots.RemoveItem(a_ItemStack);

if (RemovedItems < a_ItemStack.m_ItemCount)
{
RemovedItems += m_HotbarSlots.RemoveItem(a_ItemStack);
}

if (RemovedItems < a_ItemStack.m_ItemCount)
{
Expand Down Expand Up @@ -214,7 +221,8 @@ int cInventory::HowManyItems(const cItem & a_Item)
return
m_ArmorSlots.HowManyItems(a_Item) +
m_InventorySlots.HowManyItems(a_Item) +
m_HotbarSlots.HowManyItems(a_Item);
m_HotbarSlots.HowManyItems(a_Item) +
m_ShieldSlots.HowManyItems(a_Item);
}


Expand Down Expand Up @@ -280,6 +288,15 @@ void cInventory::SetHotbarSlot(int a_HotBarSlotNum, const cItem & a_Item)



void cInventory::SetShieldSlot(const cItem & a_Item)
{
m_ShieldSlots.SetSlot(0, a_Item);
}





void cInventory::SendEquippedSlot()
{
int EquippedSlotNum = cInventory::invArmorCount + cInventory::invInventoryCount + GetEquippedSlotNum();
Expand Down Expand Up @@ -354,6 +371,15 @@ const cItem & cInventory::GetHotbarSlot(int a_SlotNum) const



const cItem & cInventory::GetShieldSlot() const
{
return m_ShieldSlots.GetSlot(0);
}





const cItem & cInventory::GetEquippedItem(void) const
{
return GetHotbarSlot(m_EquippedSlotNum);
Expand Down Expand Up @@ -617,13 +643,18 @@ void cInventory::SaveToJson(Json::Value & a_Value)
a_Value.append(JSON_Item);
}

// The hotbar is the last:
// The hotbar:
for (int i = 0; i < invHotbarCount; i++)
{
Json::Value JSON_Item;
m_HotbarSlots.GetSlot(i).GetJson(JSON_Item);
a_Value.append(JSON_Item);
}

// Shield slot is the last
Json::Value JSON_Item;
m_ShieldSlots.GetSlot(0).GetJson(JSON_Item);
a_Value.append(JSON_Item);
}


Expand Down Expand Up @@ -678,8 +709,14 @@ const cItemGrid * cInventory::GetGridForSlotNum(int a_SlotNum, int & a_GridSlotN
a_GridSlotNum = a_SlotNum;
return &m_InventorySlots;
}
a_GridSlotNum = a_SlotNum - invInventoryCount;
return &m_HotbarSlots;
a_SlotNum -= invInventoryCount;
if (a_SlotNum < invHotbarCount)
{
a_GridSlotNum = a_SlotNum;
return &m_HotbarSlots;
}
a_GridSlotNum = a_SlotNum - invHotbarCount;
return &m_ShieldSlots;
}


Expand All @@ -701,8 +738,14 @@ cItemGrid * cInventory::GetGridForSlotNum(int a_SlotNum, int & a_GridSlotNum)
a_GridSlotNum = a_SlotNum;
return &m_InventorySlots;
}
a_GridSlotNum = a_SlotNum - invInventoryCount;
return &m_HotbarSlots;
a_SlotNum -= invInventoryCount;
if (a_SlotNum < invHotbarCount)
{
a_GridSlotNum = a_SlotNum;
return &m_HotbarSlots;
}
a_GridSlotNum = a_SlotNum - invHotbarCount;
return &m_ShieldSlots;
}


Expand Down
21 changes: 19 additions & 2 deletions src/Inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@ class cInventory :
invArmorCount = 4,
invInventoryCount = 9 * 3,
invHotbarCount = 9,
invShieldCount = 1, // Number of slots in shield slots grid

invArmorOffset = 0,
invInventoryOffset = invArmorOffset + invArmorCount,
invHotbarOffset = invInventoryOffset + invInventoryCount,
invNumSlots = invHotbarOffset + invHotbarCount
invShieldOffset = invHotbarOffset + invHotbarCount, // Offset where shield slots start
invNumSlots = invShieldOffset + invShieldCount
} ;

// tolua_end
Expand Down Expand Up @@ -120,17 +122,31 @@ class cInventory :

// tolua_begin

/** Returns current item in a_SlotNum slot */
const cItem & GetSlot(int a_SlotNum) const;
/** Returns current item in a_ArmorSlotNum in armor slots */
const cItem & GetArmorSlot(int a_ArmorSlotNum) const;
/** Returns current item in a_ArmorSlotNum in inventory slots */
const cItem & GetInventorySlot(int a_InventorySlotNum) const;
/** Returns current item in a_ArmorSlotNum in hotbar slots */
const cItem & GetHotbarSlot(int a_HotBarSlotNum) const;
/** Returns current item in shield slot */
const cItem & GetShieldSlot() const;
/** Returns current equiped item */
const cItem & GetEquippedItem(void) const;
/** Puts a_Item item in a_SlotNum slot number */
void SetSlot(int a_SlotNum, const cItem & a_Item);
/** Puts a_Item item in a_ArmorSlotNum slot number in armor slots */
void SetArmorSlot(int a_ArmorSlotNum, const cItem & a_Item);
/** Puts a_Item item in a_InventorySlotNum slot number in inventory slots */
void SetInventorySlot(int a_InventorySlotNum, const cItem & a_Item);
/** Puts a_Item item in a_HotBarSlotNum slot number in hotbar slots */
void SetHotbarSlot(int a_HotBarSlotNum, const cItem & a_Item);

/** Sets current item in shield slot */
void SetShieldSlot(const cItem & a_Item);
/** Sets equiped item to the a_SlotNum slot number */
void SetEquippedSlotNum(int a_SlotNum);
/** Returns slot number of equiped item */
int GetEquippedSlotNum(void) { return m_EquippedSlotNum; }

/** Adds (or subtracts, if a_AddToCount is negative) to the count of items in the specified slot.
Expand Down Expand Up @@ -170,6 +186,7 @@ class cInventory :
cItemGrid m_ArmorSlots;
cItemGrid m_InventorySlots;
cItemGrid m_HotbarSlots;
cItemGrid m_ShieldSlots;

int m_EquippedSlotNum;

Expand Down
1 change: 1 addition & 0 deletions src/UI/InventoryWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cInventoryWindow::cInventoryWindow(cPlayer & a_Player) :
m_SlotAreas.push_back(new cSlotAreaArmor(*this));
m_SlotAreas.push_back(new cSlotAreaInventory(*this));
m_SlotAreas.push_back(new cSlotAreaHotBar(*this));
m_SlotAreas.push_back(new cSlotAreaShield(*this));
}


Expand Down
17 changes: 17 additions & 0 deletions src/UI/SlotArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,23 @@ class cSlotAreaHotBar :



/** Handles the shield of each player */
class cSlotAreaShield :
public cSlotAreaInventoryBase
{
typedef cSlotAreaInventoryBase super;

public:
cSlotAreaShield(cWindow & a_ParentWindow) :
cSlotAreaInventoryBase(cInventory::invShieldCount, cInventory::invShieldOffset, a_ParentWindow)
{
}
};





/** Handles the armor area of the player's inventory */
class cSlotAreaArmor :
public cSlotAreaInventoryBase
Expand Down

0 comments on commit 84bdba3

Please sign in to comment.