Skip to content

Commit 7c668e7

Browse files
committed
fix: fix onUseRespawnAnchor and onRespawnAnchorExplode
1 parent dce2b08 commit 7c668e7

2 files changed

Lines changed: 33 additions & 20 deletions

File tree

src/lse/events/BlockEvents.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "mc/world/actor/Hopper.h"
1717
#include "mc/world/actor/player/Player.h"
1818
#include "mc/world/containers/models/LevelContainerModel.h"
19+
#include "mc/world/item/Item.h"
1920
#include "mc/world/item/ItemStack.h"
2021
#include "mc/world/level/BlockSource.h"
2122
#include "mc/world/level/Explosion.h"
@@ -45,14 +46,14 @@
4546
#include "mc/world/level/block/StructureBlock.h"
4647
#include "mc/world/level/block/TntBlock.h"
4748
#include "mc/world/level/block/TrapDoorBlock.h"
49+
#include "mc/world/level/block/VanillaBlockTypeIds.h"
4850
#include "mc/world/level/block/VanillaStates.h"
4951
#include "mc/world/level/block/actor/BaseCommandBlock.h"
5052
#include "mc/world/level/block/actor/PistonBlockActor.h"
5153
#include "mc/world/level/block/block_events/BlockPlayerInteractEvent.h"
5254
#include "mc/world/level/block/block_events/BlockRedstoneUpdateEvent.h"
5355
#include "mc/world/level/dimension/Dimension.h"
5456
#include "mc/world/level/material/Material.h"
55-
5657
namespace lse::events::block {
5758

5859
using api::thread::isServerThread;
@@ -256,14 +257,19 @@ LL_TYPE_INSTANCE_HOOK(
256257
) {
257258
IF_LISTENED(EVENT_TYPES::onRespawnAnchorExplode) {
258259
if (isServerThread()) {
259-
auto state = _tryLookupAlteredStateCollection(
260-
VanillaStates::RespawnAnchorCharge().mID,
261-
eventData.getBlock().getData()
262-
);
263-
if (state.has_value() && state.value() > 1) {
260+
// 原版逻辑:手持萤石且未满时 _tryCharge 会优先充能并短路,不会爆炸;
261+
// 充能大于 0 且不在下界(维度 1)时 _trySetSpawn 才会调用 _explode 引爆
262+
auto& block = eventData.mPlayer.getDimensionBlockSource().getBlock(eventData.mPos);
263+
int charge = block.getState<int>(VanillaStates::RespawnAnchorCharge().mID).value_or(0);
264+
auto& item = eventData.mPlayer.getSelectedItem();
265+
auto* itemBlockType = item.mItem ? item.mItem->mBlockType.get().get() : nullptr;
266+
bool isCharging = itemBlockType && *itemBlockType->mNameInfo->mFullName == VanillaBlockTypeIds::Glowstone()
267+
&& charge < static_cast<int>(VanillaStates::RespawnAnchorCharge().mVariationCount) - 1;
268+
if (charge > 0 && !isCharging && eventData.mPlayer.getDimensionId() != 1) {
264269
if (!CallEvent(
265270
EVENT_TYPES::onRespawnAnchorExplode,
266-
IntPos::newPos(eventData.mPos, eventData.mPlayer.getDimensionId())
271+
IntPos::newPos(eventData.mPos, eventData.mPlayer.getDimensionId()),
272+
PlayerClass::newPlayer(&eventData.mPlayer)
267273
)) {
268274
return;
269275
}

src/lse/events/PlayerEvents.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -381,19 +381,26 @@ LL_TYPE_INSTANCE_HOOK(
381381
) {
382382
IF_LISTENED(EVENT_TYPES::onUseRespawnAnchor) {
383383
if (isServerThread()) {
384-
auto state = _tryLookupAlteredStateCollection(
385-
VanillaStates::RespawnAnchorCharge().mID,
386-
eventData.getBlock().getData()
387-
);
388-
if (!state.has_value() && state.value() <= 0) {
389-
return origin(eventData);
390-
}
391-
if (!CallEvent(
392-
EVENT_TYPES::onUseRespawnAnchor,
393-
PlayerClass::newPlayer(&eventData.mPlayer),
394-
IntPos::newPos(eventData.mPos, eventData.mPlayer.getDimensionId())
395-
)) {
396-
return;
384+
// 原版逻辑:手持萤石且未满时 _tryCharge 会优先充能并短路;
385+
// 充能大于 0 且位于下界(维度 1)时 _trySetSpawn 才会设置重生点,重生点已是此锚时无事发生
386+
auto& block = eventData.mPlayer.getDimensionBlockSource().getBlock(eventData.mPos);
387+
int charge = block.getState<int>(VanillaStates::RespawnAnchorCharge().mID).value_or(0);
388+
auto& item = eventData.mPlayer.getSelectedItem();
389+
auto* itemBlockType = item.mItem ? item.mItem->mBlockType.get().get() : nullptr;
390+
bool isCharging = itemBlockType
391+
&& *itemBlockType->mNameInfo->mFullName == VanillaBlockTypeIds::Glowstone()
392+
&& charge < static_cast<int>(VanillaStates::RespawnAnchorCharge().mVariationCount) - 1;
393+
auto& spawnPoint = eventData.mPlayer.mPlayerRespawnPoint;
394+
if (charge > 0 && !isCharging && eventData.mPlayer.getDimensionId() == 1
395+
&& !(spawnPoint->mDimension->mValue == 1
396+
&& *spawnPoint->mSpawnBlockPos == *eventData.mPos)) {
397+
if (!CallEvent(
398+
EVENT_TYPES::onUseRespawnAnchor,
399+
PlayerClass::newPlayer(&eventData.mPlayer),
400+
IntPos::newPos(eventData.mPos, eventData.mPlayer.getDimensionId())
401+
)) {
402+
return;
403+
}
397404
}
398405
}
399406
}

0 commit comments

Comments
 (0)