Skip to content

Commit

Permalink
refactor: refactor block state getter and setter
Browse files Browse the repository at this point in the history
  • Loading branch information
Dofes committed Jan 6, 2025
1 parent 6c88e84 commit 3210714
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/mc/world/level/block/Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ class Block {

public:
template <typename T>
T getState(uint64 id) const {
std::optional<T> getState(uint64 id) const {
return mLegacyBlock->get()->getState<T>(id, mData);
}

template <typename T>
T getState(BlockState const& state) const {
std::optional<T> getState(BlockState const& state) const {
return mLegacyBlock->get()->getState<T>(state, mData);
}

Expand Down
14 changes: 7 additions & 7 deletions src/mc/world/level/block/BlockLegacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,28 +334,29 @@ class BlockLegacy {
LLNDAPI static optional_ref<BlockLegacy const> tryGetFromRegistry(uint legacyBlockID);

template <typename T>
T getState(uint64 id, ushort data) const {
std::optional<T> getState(uint64 id, ushort data) const {
auto it = mStates->lower_bound(id);

if (it == mStates->end() || it->first != id) {
std::optional<int> result = _tryLookupAlteredStateCollection(id, data);
if (result.has_value()) {
return static_cast<T>(result.value());
} else {
return T{};
return std::nullopt;
}
}

return it->second.get<T>(data);
}

template <typename T>
T getState(BlockState const& stateType, ushort data) const {
std::optional<T> getState(BlockState const& stateType, ushort data) const {
return getState<T>(stateType.mID, data);
}

template <typename T>
Block const* trySetState(uint64 id, T val, ushort data) {
requires(std::is_integral_v<T> || std::is_enum_v<T>)
optional_ref<Block const> trySetState(uint64 id, T val, ushort data) {
auto it = mStates->lower_bound(id);

if (it != mStates->end() && it->first == id) {
Expand All @@ -372,8 +373,7 @@ class BlockLegacy {
}
}

Block const* alteredStateBlock = _trySetStateFromAlteredStateCollection(id, static_cast<int>(val), data);
if (alteredStateBlock) {
if (auto alteredStateBlock = _trySetStateFromAlteredStateCollection(id, static_cast<int>(val), data)) {
return alteredStateBlock;
}

Expand All @@ -385,7 +385,7 @@ class BlockLegacy {
}

template <typename T>
Block const* trySetState(BlockState const& stateType, T val, ushort data) {
optional_ref<Block const> trySetState(BlockState const& stateType, T val, ushort data) {
return trySetState(stateType.mID, val, data);
}

Expand Down
2 changes: 1 addition & 1 deletion src/mc/world/level/block/states/BlockStateInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class BlockStateInstance {

public:
template <typename T>
requires(std::is_integral_v<T> || std::is_enum_v<T>)
T get(ushort data) const {
if (sizeof(T) * 8 < mNumBits) return T{};
return static_cast<T>((data >> (mEndBit - mNumBits + 1)) & ((1 << mNumBits) - 1));
}

Expand Down

0 comments on commit 3210714

Please sign in to comment.