Skip to content

Commit 683b809

Browse files
committed
fix: fix onRespawnAnchorExplode and onRespawnAnchorExplode
1 parent 1183a33 commit 683b809

3 files changed

Lines changed: 42 additions & 23 deletions

File tree

src/lse/events/BlockEvents.cpp

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "legacy/api/ItemAPI.h"
77
#include "legacy/api/PlayerAPI.h"
88
#include "ll/api/memory/Hook.h"
9-
#include "ll/api/memory/Memory.h"
109
#include "ll/api/service/Bedrock.h"
1110
#include "lse/api/Thread.h"
1211
#include "mc/legacy/ActorUniqueID.h"
@@ -46,9 +45,10 @@
4645
#include "mc/world/level/block/StructureBlock.h"
4746
#include "mc/world/level/block/TntBlock.h"
4847
#include "mc/world/level/block/TrapDoorBlock.h"
49-
#include "mc/world/level/block/VanillaBlockTypeIds.h"
48+
#include "mc/world/level/block/VanillaStates.h"
5049
#include "mc/world/level/block/actor/BaseCommandBlock.h"
5150
#include "mc/world/level/block/actor/PistonBlockActor.h"
51+
#include "mc/world/level/block/block_events/BlockPlayerInteractEvent.h"
5252
#include "mc/world/level/block/block_events/BlockRedstoneUpdateEvent.h"
5353
#include "mc/world/level/dimension/Dimension.h"
5454
#include "mc/world/level/material/Material.h"
@@ -243,18 +243,35 @@ LL_TYPE_INSTANCE_HOOK(ExplodeHook, HookPriority::Normal, Explosion, &Explosion::
243243
}
244244
}
245245
IF_LISTENED_END(EVENT_TYPES::onBlockExplode);
246+
return origin(random);
247+
}
246248

249+
LL_TYPE_INSTANCE_HOOK(
250+
UseRespawnAnchorHook,
251+
HookPriority::Normal,
252+
RespawnAnchorBlock,
253+
&RespawnAnchorBlock::use,
254+
void,
255+
::BlockEvents::BlockPlayerInteractEvent& eventData
256+
) {
247257
IF_LISTENED(EVENT_TYPES::onRespawnAnchorExplode) {
248-
// TODO: find a function which can get player
249-
if (isServerThread()
250-
&& *mRegion.getBlock(*mPos).getBlockType().mNameInfo->mFullName == VanillaBlockTypeIds::RespawnAnchor()) {
251-
if (!CallEvent(EVENT_TYPES::onRespawnAnchorExplode, IntPos::newPos(*mPos, mRegion.getDimensionId()))) {
252-
return false;
258+
if (isServerThread()) {
259+
auto state = _tryLookupAlteredStateCollection(
260+
VanillaStates::RespawnAnchorCharge().mID,
261+
eventData.getBlock().getData()
262+
);
263+
if (state.has_value() && state.value() > 1) {
264+
if (!CallEvent(
265+
EVENT_TYPES::onRespawnAnchorExplode,
266+
IntPos::newPos(eventData.mPos, eventData.mPlayer.getDimensionId())
267+
)) {
268+
return;
269+
}
253270
}
254271
}
255272
}
256273
IF_LISTENED_END(EVENT_TYPES::onRespawnAnchorExplode);
257-
return origin(random);
274+
origin(eventData);
258275
}
259276

260277
LL_TYPE_STATIC_HOOK(
@@ -606,7 +623,7 @@ void PressurePlateTriggerEvent() { static ll::memory::HookRegistrar<PressurePlat
606623
void FarmDecayEvent() { static ll::memory::HookRegistrar<FarmDecayHook> reg; }
607624
void PistonPushEvent() { static ll::memory::HookRegistrar<PistonPushHook> reg; }
608625
void ExplodeEvent() { static ll::memory::HookRegistrar<ExplodeHook> reg; }
609-
void RespawnAnchorExplodeEvent() { ExplodeEvent(); }
626+
void RespawnAnchorExplodeEvent() { static ll::memory::HookRegistrar<UseRespawnAnchorHook> reg; }
610627
void PortalSpawnEvent() { static ll::memory::HookRegistrar<PortalSpawnHook> reg; }
611628
void BlockExplodedEvent() { static ll::memory::HookRegistrar<BlockExplodedHook> reg; }
612629
void RedstoneUpdateEvent() {

src/lse/events/EntityEvents.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "legacy/api/EventAPI.h"
55
#include "legacy/api/PlayerAPI.h"
66
#include "ll/api/memory/Hook.h"
7-
#include "ll/api/memory/Memory.h"
87
#include "ll/api/service/Bedrock.h"
98
#include "lse/api/Thread.h"
109
#include "mc/common/Globals.h"
@@ -26,7 +25,6 @@
2625
#include "mc/world/actor/item/FireworksRocketActor.h"
2726
#include "mc/world/actor/npc/CommandAction.h"
2827
#include "mc/world/actor/npc/StoredCommand.h"
29-
#include "mc/world/actor/npc/UrlAction.h"
3028
#include "mc/world/actor/player/Player.h"
3129
#include "mc/world/effect/MobEffectInstance.h"
3230
#include "mc/world/events/ActorEventCoordinator.h"

src/lse/events/PlayerEvents.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "legacy/api/ItemAPI.h"
66
#include "legacy/api/PlayerAPI.h"
77
#include "ll/api/memory/Hook.h"
8-
#include "ll/api/memory/Memory.h"
98
#include "ll/api/service/Bedrock.h"
109
#include "lse/api/Thread.h"
1110
#include "mc/deps/ecs/WeakEntityRef.h"
@@ -41,6 +40,7 @@
4140
#include "mc/world/level/block/Block.h"
4241
#include "mc/world/level/block/ItemFrameBlock.h"
4342
#include "mc/world/level/block/RespawnAnchorBlock.h"
43+
#include "mc/world/level/block/VanillaStates.h"
4444
#include "mc/world/level/block/actor/PistonBlockActor.h"
4545
#include "mc/world/level/block/block_events/BlockPlayerInteractEvent.h"
4646
#include "mc/world/level/dimension/Dimension.h"
@@ -369,30 +369,34 @@ LL_TYPE_INSTANCE_HOOK(
369369
return origin(screenContext);
370370
}
371371

372-
LL_TYPE_STATIC_HOOK(
372+
LL_TYPE_INSTANCE_HOOK(
373373
UseRespawnAnchorHook,
374374
HookPriority::Normal,
375375
RespawnAnchorBlock,
376-
&RespawnAnchorBlock::_trySetSpawn,
377-
bool,
378-
Player& player,
379-
BlockPos const& pos,
380-
BlockSource& region,
381-
class Level& level
376+
&RespawnAnchorBlock::use,
377+
void,
378+
::BlockEvents::BlockPlayerInteractEvent& eventData
382379
) {
383380
IF_LISTENED(EVENT_TYPES::onUseRespawnAnchor) {
384381
if (isServerThread()) {
382+
auto state = _tryLookupAlteredStateCollection(
383+
VanillaStates::RespawnAnchorCharge().mID,
384+
eventData.getBlock().getData()
385+
);
386+
if (!state.has_value() && state.value() <= 0) {
387+
return origin(eventData);
388+
}
385389
if (!CallEvent(
386390
EVENT_TYPES::onUseRespawnAnchor,
387-
PlayerClass::newPlayer(&player),
388-
IntPos::newPos(pos, region.getDimensionId())
391+
PlayerClass::newPlayer(&eventData.mPlayer),
392+
IntPos::newPos(eventData.mPos, eventData.mPlayer.getDimensionId())
389393
)) {
390-
return false;
394+
return;
391395
}
392396
}
393397
}
394398
IF_LISTENED_END(EVENT_TYPES::onUseRespawnAnchor);
395-
return origin(player, pos, region, level);
399+
origin(eventData);
396400
}
397401

398402
LL_TYPE_INSTANCE_HOOK(

0 commit comments

Comments
 (0)