Skip to content

Commit 3210714

Browse files
committed
refactor: refactor block state getter and setter
1 parent 6c88e84 commit 3210714

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/mc/world/level/block/Block.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ class Block {
9292

9393
public:
9494
template <typename T>
95-
T getState(uint64 id) const {
95+
std::optional<T> getState(uint64 id) const {
9696
return mLegacyBlock->get()->getState<T>(id, mData);
9797
}
9898

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

src/mc/world/level/block/BlockLegacy.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -334,28 +334,29 @@ class BlockLegacy {
334334
LLNDAPI static optional_ref<BlockLegacy const> tryGetFromRegistry(uint legacyBlockID);
335335

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

340340
if (it == mStates->end() || it->first != id) {
341341
std::optional<int> result = _tryLookupAlteredStateCollection(id, data);
342342
if (result.has_value()) {
343343
return static_cast<T>(result.value());
344344
} else {
345-
return T{};
345+
return std::nullopt;
346346
}
347347
}
348348

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

352352
template <typename T>
353-
T getState(BlockState const& stateType, ushort data) const {
353+
std::optional<T> getState(BlockState const& stateType, ushort data) const {
354354
return getState<T>(stateType.mID, data);
355355
}
356356

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

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

375-
Block const* alteredStateBlock = _trySetStateFromAlteredStateCollection(id, static_cast<int>(val), data);
376-
if (alteredStateBlock) {
376+
if (auto alteredStateBlock = _trySetStateFromAlteredStateCollection(id, static_cast<int>(val), data)) {
377377
return alteredStateBlock;
378378
}
379379

@@ -385,7 +385,7 @@ class BlockLegacy {
385385
}
386386

387387
template <typename T>
388-
Block const* trySetState(BlockState const& stateType, T val, ushort data) {
388+
optional_ref<Block const> trySetState(BlockState const& stateType, T val, ushort data) {
389389
return trySetState(stateType.mID, val, data);
390390
}
391391

src/mc/world/level/block/states/BlockStateInstance.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class BlockStateInstance {
2121

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

0 commit comments

Comments
 (0)