From 4cd49d7eca5f8fd53eb98577a1f218a5086704bb Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 5 Apr 2021 01:38:43 +0100 Subject: Fix sending incorrect date values on world change Yak shave: make more things use cTickTime. Fix a couple of incorrect modulo-on-millisecond-value by making them use WorldTickAge. --- src/BlockEntities/BeaconEntity.cpp | 6 ++++-- src/BlockEntities/BrewingstandEntity.cpp | 4 ++-- src/BlockEntities/FurnaceEntity.cpp | 4 ++-- src/BlockEntities/HopperEntity.cpp | 27 ++++++++++++++++----------- src/BlockEntities/HopperEntity.h | 13 ++++++------- src/BlockEntities/MobSpawnerEntity.cpp | 6 ++++-- 6 files changed, 34 insertions(+), 26 deletions(-) (limited to 'src/BlockEntities') diff --git a/src/BlockEntities/BeaconEntity.cpp b/src/BlockEntities/BeaconEntity.cpp index e59d31f52..6a7916dd1 100644 --- a/src/BlockEntities/BeaconEntity.cpp +++ b/src/BlockEntities/BeaconEntity.cpp @@ -296,8 +296,10 @@ void cBeaconEntity::SendTo(cClientHandle & a_Client) bool cBeaconEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) { - // Update the beacon every 4 seconds - if ((GetWorld()->GetWorldAge() % 80) == 0) + using namespace std::chrono_literals; + + // Update the beacon every 4 seconds: + if ((GetWorld()->GetWorldTickAge() % 4s) == 0s) { UpdateBeacon(); GiveEffects(); diff --git a/src/BlockEntities/BrewingstandEntity.cpp b/src/BlockEntities/BrewingstandEntity.cpp index 44a077bc0..bdac1b327 100644 --- a/src/BlockEntities/BrewingstandEntity.cpp +++ b/src/BlockEntities/BrewingstandEntity.cpp @@ -288,8 +288,8 @@ void cBrewingstandEntity::OnSlotChanged(cItemGrid * a_ItemGrid, int a_SlotNum) void cBrewingstandEntity::UpdateProgressBars(bool a_ForceUpdate) { - /** Sending an update every 3th tick, using a higher value lets look the progressbar ugly */ - if (!a_ForceUpdate && (m_World->GetWorldAge() % 3 != 0)) + // Send an update every 3rd tick, using a higher value makes the progressbar look ugly: + if (!a_ForceUpdate && ((m_World->GetWorldTickAge() % 3_tick) != 0_tick)) { return; } diff --git a/src/BlockEntities/FurnaceEntity.cpp b/src/BlockEntities/FurnaceEntity.cpp index 6b2e7bbac..d36b19791 100644 --- a/src/BlockEntities/FurnaceEntity.cpp +++ b/src/BlockEntities/FurnaceEntity.cpp @@ -388,8 +388,8 @@ bool cFurnaceEntity::CanCookInputToOutput(void) const void cFurnaceEntity::UpdateProgressBars(bool a_ForceUpdate) { - // In order to preserve bandwidth, an update is sent only every 10th tick - if (!a_ForceUpdate && (m_World->GetWorldAge() % 10 != 0)) + // In order to preserve bandwidth, an update is sent only every 10th tick: + if (!a_ForceUpdate && ((m_World->GetWorldTickAge() % 10_tick) != 0_tick)) { return; } diff --git a/src/BlockEntities/HopperEntity.cpp b/src/BlockEntities/HopperEntity.cpp index 9c9f229e4..58a5fd565 100644 --- a/src/BlockEntities/HopperEntity.cpp +++ b/src/BlockEntities/HopperEntity.cpp @@ -17,6 +17,13 @@ +// How many ticks at minimum between two item transfers to or from the hopper. +#define TICKS_PER_TRANSFER 8_tick + + + + + cHopperEntity::cHopperEntity(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World): Super(a_BlockType, a_BlockMeta, a_Pos, ContentsWidth, ContentsHeight, a_World), m_LastMoveItemsInTick(0), @@ -76,14 +83,14 @@ void cHopperEntity::CopyFrom(const cBlockEntity & a_Src) bool cHopperEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) { UNUSED(a_Dt); - Int64 CurrentTick = a_Chunk.GetWorld()->GetWorldAge(); bool isDirty = false; if (!m_Locked) { - isDirty = MoveItemsIn (a_Chunk, CurrentTick) || isDirty; - isDirty = MovePickupsIn(a_Chunk, CurrentTick) || isDirty; - isDirty = MoveItemsOut (a_Chunk, CurrentTick) || isDirty; + const auto CurrentTick = a_Chunk.GetWorld()->GetWorldAge(); + isDirty = MoveItemsIn(a_Chunk, CurrentTick) || isDirty; + isDirty = MovePickupsIn(a_Chunk) || isDirty; + isDirty = MoveItemsOut(a_Chunk, CurrentTick) || isDirty; } return isDirty; } @@ -147,7 +154,7 @@ void cHopperEntity::OpenNewWindow(void) -bool cHopperEntity::MoveItemsIn(cChunk & a_Chunk, Int64 a_CurrentTick) +bool cHopperEntity::MoveItemsIn(cChunk & a_Chunk, const cTickTimeLong a_CurrentTick) { if (m_Pos.y >= cChunkDef::Height) { @@ -155,7 +162,7 @@ bool cHopperEntity::MoveItemsIn(cChunk & a_Chunk, Int64 a_CurrentTick) return false; } - if (a_CurrentTick - m_LastMoveItemsInTick < TICKS_PER_TRANSFER) + if ((a_CurrentTick - m_LastMoveItemsInTick) < TICKS_PER_TRANSFER) { // Too early after the previous transfer return false; @@ -201,10 +208,8 @@ bool cHopperEntity::MoveItemsIn(cChunk & a_Chunk, Int64 a_CurrentTick) -bool cHopperEntity::MovePickupsIn(cChunk & a_Chunk, Int64 a_CurrentTick) +bool cHopperEntity::MovePickupsIn(cChunk & a_Chunk) { - UNUSED(a_CurrentTick); - class cHopperPickupSearchCallback { public: @@ -290,9 +295,9 @@ bool cHopperEntity::MovePickupsIn(cChunk & a_Chunk, Int64 a_CurrentTick) -bool cHopperEntity::MoveItemsOut(cChunk & a_Chunk, Int64 a_CurrentTick) +bool cHopperEntity::MoveItemsOut(cChunk & a_Chunk, const cTickTimeLong a_CurrentTick) { - if (a_CurrentTick - m_LastMoveItemsOutTick < TICKS_PER_TRANSFER) + if ((a_CurrentTick - m_LastMoveItemsOutTick) < TICKS_PER_TRANSFER) { // Too early after the previous transfer return false; diff --git a/src/BlockEntities/HopperEntity.h b/src/BlockEntities/HopperEntity.h index 366eb8e9e..8eb0196a3 100644 --- a/src/BlockEntities/HopperEntity.h +++ b/src/BlockEntities/HopperEntity.h @@ -30,8 +30,7 @@ public: enum { ContentsHeight = 1, - ContentsWidth = 5, - TICKS_PER_TRANSFER = 8, ///< How many ticks at minimum between two item transfers to or from the hopper + ContentsWidth = 5 } ; // tolua_end @@ -48,8 +47,8 @@ public: protected: - Int64 m_LastMoveItemsInTick; - Int64 m_LastMoveItemsOutTick; + cTickTimeLong m_LastMoveItemsInTick; + cTickTimeLong m_LastMoveItemsOutTick; // cBlockEntity overrides: virtual void CopyFrom(const cBlockEntity & a_Src) override; @@ -61,13 +60,13 @@ protected: void OpenNewWindow(void); /** Moves items from the container above it into this hopper. Returns true if the contents have changed. */ - bool MoveItemsIn(cChunk & a_Chunk, Int64 a_CurrentTick); + bool MoveItemsIn(cChunk & a_Chunk, cTickTimeLong a_CurrentTick); /** Moves pickups from above this hopper into it. Returns true if the contents have changed. */ - bool MovePickupsIn(cChunk & a_Chunk, Int64 a_CurrentTick); + bool MovePickupsIn(cChunk & a_Chunk); /** Moves items out from this hopper into the destination. Returns true if the contents have changed. */ - bool MoveItemsOut(cChunk & a_Chunk, Int64 a_CurrentTick); + bool MoveItemsOut(cChunk & a_Chunk, cTickTimeLong a_CurrentTick); /** Moves items from a chest (dblchest) above the hopper into this hopper. Returns true if contents have changed. */ bool MoveItemsFromChest(cChunk & a_Chunk); diff --git a/src/BlockEntities/MobSpawnerEntity.cpp b/src/BlockEntities/MobSpawnerEntity.cpp index 8e5585b89..5778fb706 100644 --- a/src/BlockEntities/MobSpawnerEntity.cpp +++ b/src/BlockEntities/MobSpawnerEntity.cpp @@ -92,8 +92,10 @@ void cMobSpawnerEntity::UpdateActiveState(void) bool cMobSpawnerEntity::Tick(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) { - // Update the active flag every 5 seconds - if ((m_World->GetWorldAge() % 100) == 0) + using namespace std::chrono_literals; + + // Update the active flag every 5 seconds: + if ((m_World->GetWorldTickAge() % 5s) == 0s) { UpdateActiveState(); } -- cgit v1.2.3