From 3210714679da6b3958fa2f3b7f9428552afbb8aa Mon Sep 17 00:00:00 2001 From: Dofes <91889957+Dofes@users.noreply.github.com> Date: Mon, 6 Jan 2025 17:51:29 +0800 Subject: [PATCH] refactor: refactor block state getter and setter --- src/mc/world/level/block/Block.h | 4 ++-- src/mc/world/level/block/BlockLegacy.h | 14 +++++++------- .../world/level/block/states/BlockStateInstance.h | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/mc/world/level/block/Block.h b/src/mc/world/level/block/Block.h index 07e6dae7e5..c5b8603c9f 100644 --- a/src/mc/world/level/block/Block.h +++ b/src/mc/world/level/block/Block.h @@ -92,12 +92,12 @@ class Block { public: template - T getState(uint64 id) const { + std::optional getState(uint64 id) const { return mLegacyBlock->get()->getState(id, mData); } template - T getState(BlockState const& state) const { + std::optional getState(BlockState const& state) const { return mLegacyBlock->get()->getState(state, mData); } diff --git a/src/mc/world/level/block/BlockLegacy.h b/src/mc/world/level/block/BlockLegacy.h index ccbe97421f..99c5e15c7c 100644 --- a/src/mc/world/level/block/BlockLegacy.h +++ b/src/mc/world/level/block/BlockLegacy.h @@ -334,7 +334,7 @@ class BlockLegacy { LLNDAPI static optional_ref tryGetFromRegistry(uint legacyBlockID); template - T getState(uint64 id, ushort data) const { + std::optional getState(uint64 id, ushort data) const { auto it = mStates->lower_bound(id); if (it == mStates->end() || it->first != id) { @@ -342,7 +342,7 @@ class BlockLegacy { if (result.has_value()) { return static_cast(result.value()); } else { - return T{}; + return std::nullopt; } } @@ -350,12 +350,13 @@ class BlockLegacy { } template - T getState(BlockState const& stateType, ushort data) const { + std::optional getState(BlockState const& stateType, ushort data) const { return getState(stateType.mID, data); } template - Block const* trySetState(uint64 id, T val, ushort data) { + requires(std::is_integral_v || std::is_enum_v) + optional_ref trySetState(uint64 id, T val, ushort data) { auto it = mStates->lower_bound(id); if (it != mStates->end() && it->first == id) { @@ -372,8 +373,7 @@ class BlockLegacy { } } - Block const* alteredStateBlock = _trySetStateFromAlteredStateCollection(id, static_cast(val), data); - if (alteredStateBlock) { + if (auto alteredStateBlock = _trySetStateFromAlteredStateCollection(id, static_cast(val), data)) { return alteredStateBlock; } @@ -385,7 +385,7 @@ class BlockLegacy { } template - Block const* trySetState(BlockState const& stateType, T val, ushort data) { + optional_ref trySetState(BlockState const& stateType, T val, ushort data) { return trySetState(stateType.mID, val, data); } diff --git a/src/mc/world/level/block/states/BlockStateInstance.h b/src/mc/world/level/block/states/BlockStateInstance.h index af857943bb..1e44008d4e 100644 --- a/src/mc/world/level/block/states/BlockStateInstance.h +++ b/src/mc/world/level/block/states/BlockStateInstance.h @@ -21,8 +21,8 @@ class BlockStateInstance { public: template + requires(std::is_integral_v || std::is_enum_v) T get(ushort data) const { - if (sizeof(T) * 8 < mNumBits) return T{}; return static_cast((data >> (mEndBit - mNumBits + 1)) & ((1 << mNumBits) - 1)); }