diff options
author | Mattes D <github@xoft.cz> | 2014-02-15 15:06:23 +0100 |
---|---|---|
committer | Mattes D <github@xoft.cz> | 2014-02-15 15:06:23 +0100 |
commit | 48eafee823cb979ded833b5311c0f5c37a8ca0f2 (patch) | |
tree | fad70a53bd337fa152f661e32422da5b9699096e /src/BlockEntities/HopperEntity.cpp | |
parent | Merge pull request #677 from mc-server/BroadcastParticleEffect (diff) | |
parent | Fixed typographical error (diff) | |
download | cuberite-48eafee823cb979ded833b5311c0f5c37a8ca0f2.tar cuberite-48eafee823cb979ded833b5311c0f5c37a8ca0f2.tar.gz cuberite-48eafee823cb979ded833b5311c0f5c37a8ca0f2.tar.bz2 cuberite-48eafee823cb979ded833b5311c0f5c37a8ca0f2.tar.lz cuberite-48eafee823cb979ded833b5311c0f5c37a8ca0f2.tar.xz cuberite-48eafee823cb979ded833b5311c0f5c37a8ca0f2.tar.zst cuberite-48eafee823cb979ded833b5311c0f5c37a8ca0f2.zip |
Diffstat (limited to 'src/BlockEntities/HopperEntity.cpp')
-rw-r--r-- | src/BlockEntities/HopperEntity.cpp | 85 |
1 files changed, 83 insertions, 2 deletions
diff --git a/src/BlockEntities/HopperEntity.cpp b/src/BlockEntities/HopperEntity.cpp index 2255cad64..31b23ac99 100644 --- a/src/BlockEntities/HopperEntity.cpp +++ b/src/BlockEntities/HopperEntity.cpp @@ -7,10 +7,12 @@ #include "HopperEntity.h" #include "../Chunk.h" #include "../Entities/Player.h" +#include "../Entities/Pickup.h" #include "../Bindings/PluginManager.h" #include "ChestEntity.h" #include "DropSpenserEntity.h" #include "FurnaceEntity.h" +#include "../BoundingBox.h" @@ -190,8 +192,87 @@ bool cHopperEntity::MoveItemsIn(cChunk & a_Chunk, Int64 a_CurrentTick) /// Moves pickups from above this hopper into it. Returns true if the contents have changed. bool cHopperEntity::MovePickupsIn(cChunk & a_Chunk, Int64 a_CurrentTick) { - // TODO - return false; + UNUSED(a_CurrentTick); + + class cHopperPickupSearchCallback : + public cEntityCallback + { + public: + cHopperPickupSearchCallback(const Vector3i & a_Pos, cItemGrid & a_Contents) : + m_Pos(a_Pos), + m_bFoundPickupsAbove(false), + m_Contents(a_Contents) + { + } + + virtual bool Item(cEntity * a_Entity) override + { + ASSERT(a_Entity != NULL); + + if (!a_Entity->IsPickup() || a_Entity->IsDestroyed()) + { + return false; + } + + Vector3f EntityPos = a_Entity->GetPosition(); + Vector3f BlockPos(m_Pos.x + 0.5f, (float)m_Pos.y + 1, m_Pos.z + 0.5f); // One block above hopper, and search from center outwards + float Distance = (EntityPos - BlockPos).Length(); + + if (Distance < 0.5) + { + if (TrySuckPickupIn((cPickup *)a_Entity)) + { + return false; + } + } + + return false; + } + + bool TrySuckPickupIn(cPickup * a_Pickup) + { + for (int i = 0; i < ContentsWidth * ContentsHeight; i++) + { + if (m_Contents.IsSlotEmpty(i)) + { + m_bFoundPickupsAbove = true; + m_Contents.SetSlot(i, a_Pickup->GetItem()); + a_Pickup->Destroy(); // Kill pickup + + return true; + } + else if (m_Contents.GetSlot(i).IsEqual(a_Pickup->GetItem()) && !m_Contents.GetSlot(i).IsFullStack()) + { + m_bFoundPickupsAbove = true; + + int PreviousCount = m_Contents.GetSlot(i).m_ItemCount; + a_Pickup->GetItem().m_ItemCount -= m_Contents.ChangeSlotCount(i, a_Pickup->GetItem().m_ItemCount) - PreviousCount; // Set count to however many items were added + + if (a_Pickup->GetItem().IsEmpty()) + { + a_Pickup->Destroy(); // Kill pickup if all items were added + } + return true; + } + } + return false; + } + + bool FoundPickupsAbove(void) const + { + return m_bFoundPickupsAbove; + } + + protected: + Vector3i m_Pos; + bool m_bFoundPickupsAbove; + cItemGrid & m_Contents; + }; + + cHopperPickupSearchCallback HopperPickupSearchCallback(Vector3i(GetPosX(), GetPosY(), GetPosZ()), m_Contents); + a_Chunk.ForEachEntity(HopperPickupSearchCallback); + + return HopperPickupSearchCallback.FoundPickupsAbove(); } |