From c747b4911ed4b1833d7f400d62d1835aba704278 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Wed, 22 Mar 2023 10:20:16 +0000 Subject: Removed C style arrays from the BlockEntity folder (#5005) * Removed C style arrays from the BlockEntity folder * Update for loops with size_t where there are no item grids involved * Fixed some casts in BrewingstandEntity.cpp --------- Co-authored-by: bibo38 Co-authored-by: x12xx12x <44411062+12xx12@users.noreply.github.com> --- src/BlockEntities/BrewingstandEntity.cpp | 30 ++++++++++++------------------ src/BlockEntities/BrewingstandEntity.h | 6 +++--- src/BlockEntities/DropSpenserEntity.cpp | 6 +++--- src/BlockEntities/SignEntity.cpp | 15 +++++++-------- src/BlockEntities/SignEntity.h | 6 +++--- 5 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/BlockEntities/BrewingstandEntity.cpp b/src/BlockEntities/BrewingstandEntity.cpp index ff7b49821..12f8d5a36 100644 --- a/src/BlockEntities/BrewingstandEntity.cpp +++ b/src/BlockEntities/BrewingstandEntity.cpp @@ -29,14 +29,8 @@ void cBrewingstandEntity::CopyFrom(const cBlockEntity & a_Src) Super::CopyFrom(a_Src); auto & src = static_cast(a_Src); m_IsBrewing = src.m_IsBrewing; - for (size_t i = 0; i < ARRAYCOUNT(m_CurrentBrewingRecipes); ++i) - { - m_CurrentBrewingRecipes[i] = src.m_CurrentBrewingRecipes[i]; - } - for (size_t i = 0; i < ARRAYCOUNT(m_Results); ++i) - { - m_Results[i] = src.m_Results[i]; - } + m_CurrentBrewingRecipes = src.m_CurrentBrewingRecipes; + m_Results = src.m_Results; m_TimeBrewed = src.m_TimeBrewed; m_RemainingFuel = src.m_RemainingFuel; } @@ -117,15 +111,15 @@ bool cBrewingstandEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) // Loop over all bottle slots and update available bottles const cBrewingRecipes::cRecipe * Recipe = nullptr; - for (int i = 0; i < 3; i++) + for (std::size_t i = 0; i < 3; i++) { - if (m_Contents.GetSlot(i).IsEmpty() || (m_CurrentBrewingRecipes[i] == nullptr)) + if (m_Contents.GetSlot(static_cast(i)).IsEmpty() || (m_CurrentBrewingRecipes[i] == nullptr)) { continue; } Recipe = m_CurrentBrewingRecipes[i]; - m_Contents.SetSlot(i, Recipe->Output.CopyOne()); + m_Contents.SetSlot(static_cast(i), Recipe->Output.CopyOne()); } // Brewing process completed @@ -234,9 +228,9 @@ void cBrewingstandEntity::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) cBrewingRecipes * BR = cRoot::Get()->GetBrewingRecipes(); const cBrewingRecipes::cRecipe * Recipe = nullptr; bool Stop = true; - for (int i = 0; i < 3; i++) + for (std::size_t i = 0; i < 3; i++) { - if (GetSlot(i).IsEmpty()) + if (GetSlot(static_cast(i)).IsEmpty()) { m_CurrentBrewingRecipes[i] = nullptr; m_Results[i].Clear(); @@ -246,14 +240,14 @@ void cBrewingstandEntity::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) if (m_CurrentBrewingRecipes[i] != nullptr) { Recipe = m_CurrentBrewingRecipes[i]; - if (Recipe->Ingredient.IsEqual(GetSlot(bsIngredient)) && Recipe->Input.IsEqual(GetSlot(i))) + if (Recipe->Ingredient.IsEqual(GetSlot(bsIngredient)) && Recipe->Input.IsEqual(GetSlot(static_cast(i)))) { Stop = false; continue; } } - Recipe = BR->GetRecipeFrom(m_Contents.GetSlot(i), m_Contents.GetSlot(bsIngredient)); + Recipe = BR->GetRecipeFrom(m_Contents.GetSlot(static_cast(i)), m_Contents.GetSlot(bsIngredient)); if (Recipe != nullptr) { // Found a brewing recipe for the items @@ -323,13 +317,13 @@ void cBrewingstandEntity::LoadRecipes(void) cBrewingRecipes * BR = cRoot::Get()->GetBrewingRecipes(); const cBrewingRecipes::cRecipe * Recipe = nullptr; - for (int i = 0; i < 3; i++) + for (std::size_t i = 0; i < 3; i++) { - if (GetSlot(i).IsEmpty()) + if (GetSlot(static_cast(i)).IsEmpty()) { continue; } - Recipe = BR->GetRecipeFrom(GetSlot(i), GetSlot(bsIngredient)); + Recipe = BR->GetRecipeFrom(GetSlot(static_cast(i)), GetSlot(bsIngredient)); if (Recipe != nullptr) { m_CurrentBrewingRecipes[i] = Recipe; diff --git a/src/BlockEntities/BrewingstandEntity.h b/src/BlockEntities/BrewingstandEntity.h index 67a6da1a3..66eaa920d 100644 --- a/src/BlockEntities/BrewingstandEntity.h +++ b/src/BlockEntities/BrewingstandEntity.h @@ -78,7 +78,7 @@ public: const cItem & GetFuelSlot(void) const { return GetSlot(bsFuel); } /** Get the expected result item for the given slot number */ - const cItem & GetResultItem(int a_SlotNumber) { return m_Results[a_SlotNumber]; } + const cItem & GetResultItem(size_t a_SlotNumber) { return m_Results[a_SlotNumber]; } /** Sets the item in the left bottle slot */ void SetLeftBottleSlot(const cItem & a_Item) { SetSlot(bsLeftBottle, a_Item); } @@ -118,10 +118,10 @@ protected: const short m_NeedBrewingTime = 400; /** Store the current brewing recipes */ - const cBrewingRecipes::cRecipe * m_CurrentBrewingRecipes[3] = {}; + std::array m_CurrentBrewingRecipes = {}; /** Result items for the bottle inputs */ - cItem m_Results[3]; + std::array m_Results; /** Amount of ticks that the current item has been brewed */ short m_TimeBrewed; diff --git a/src/BlockEntities/DropSpenserEntity.cpp b/src/BlockEntities/DropSpenserEntity.cpp index b067e1081..6c0df0606 100644 --- a/src/BlockEntities/DropSpenserEntity.cpp +++ b/src/BlockEntities/DropSpenserEntity.cpp @@ -47,8 +47,8 @@ void cDropSpenserEntity::AddDropSpenserDir(Vector3i & a_RelCoord, NIBBLETYPE a_D void cDropSpenserEntity::DropSpense(cChunk & a_Chunk) { // Pick one of the occupied slots: - int OccupiedSlots[9]; - int SlotsCnt = 0; + std::array OccupiedSlots; + size_t SlotsCnt = 0; for (int i = m_Contents.GetNumSlots() - 1; i >= 0; i--) { if (!m_Contents.GetSlot(i).IsEmpty()) @@ -65,7 +65,7 @@ void cDropSpenserEntity::DropSpense(cChunk & a_Chunk) return; } - const int RandomSlot = m_World->GetTickRandomNumber(SlotsCnt - 1); + const size_t RandomSlot = GetRandomProvider().RandInt(SlotsCnt - 1); const int SpenseSlot = OccupiedSlots[RandomSlot]; if (cPluginManager::Get()->CallHookDropSpense(*m_World, *this, SpenseSlot)) diff --git a/src/BlockEntities/SignEntity.cpp b/src/BlockEntities/SignEntity.cpp index a2609d234..fc96e06f5 100644 --- a/src/BlockEntities/SignEntity.cpp +++ b/src/BlockEntities/SignEntity.cpp @@ -27,10 +27,7 @@ void cSignEntity::CopyFrom(const cBlockEntity & a_Src) { Super::CopyFrom(a_Src); auto & src = static_cast(a_Src); - for (size_t i = 0; i < ARRAYCOUNT(m_Line); ++i) - { - m_Line[i] = src.m_Line[i]; - } + m_Line = src.m_Line; } @@ -59,13 +56,14 @@ void cSignEntity::SetLines(const AString & a_Line1, const AString & a_Line2, con -void cSignEntity::SetLine(int a_Index, const AString & a_Line) +void cSignEntity::SetLine(size_t a_Index, const AString & a_Line) { - if ((a_Index < 0) || (a_Index >= static_cast(ARRAYCOUNT(m_Line)))) + if (a_Index >= m_Line.size()) { LOGWARNING("%s: setting a non-existent line %d (value \"%s\"", __FUNCTION__, a_Index, a_Line.c_str()); return; } + m_Line[a_Index] = a_Line; } @@ -73,13 +71,14 @@ void cSignEntity::SetLine(int a_Index, const AString & a_Line) -AString cSignEntity::GetLine(int a_Index) const +AString cSignEntity::GetLine(size_t a_Index) const { - if ((a_Index < 0) || (a_Index >= static_cast(ARRAYCOUNT(m_Line)))) + if (a_Index >= m_Line.size()) { LOGWARNING("%s: requesting a non-existent line %d", __FUNCTION__, a_Index); return ""; } + return m_Line[a_Index]; } diff --git a/src/BlockEntities/SignEntity.h b/src/BlockEntities/SignEntity.h index b5abd9f51..4cc69ee22 100644 --- a/src/BlockEntities/SignEntity.h +++ b/src/BlockEntities/SignEntity.h @@ -34,10 +34,10 @@ public: // tolua_export void SetLines(const AString & a_Line1, const AString & a_Line2, const AString & a_Line3, const AString & a_Line4); /** Sets individual line (zero-based index) */ - void SetLine(int a_Index, const AString & a_Line); + void SetLine(size_t a_Index, const AString & a_Line); /** Retrieves individual line (zero-based index) */ - AString GetLine(int a_Index) const; + AString GetLine(size_t a_Index) const; // tolua_end @@ -48,5 +48,5 @@ public: // tolua_export private: - AString m_Line[4]; + std::array m_Line; } ; // tolua_export -- cgit v1.2.3