Skip to content

Commit a9a65aa

Browse files
committed
fix: fix the container close event [#294]
1 parent 2c59848 commit a9a65aa

4 files changed

Lines changed: 20 additions & 46 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Fixed the container close event [#294] @zimuya4153
13+
1014
## [0.17.7] - 2026-03-08
1115

1216
### Changed
@@ -1167,6 +1171,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
11671171
[#278]: https://github.com/LiteLDev/LegacyScriptEngine/issues/278
11681172
[#285]: https://github.com/LiteLDev/LegacyScriptEngine/issues/285
11691173
[#291]: https://github.com/LiteLDev/LegacyScriptEngine/issues/291
1174+
[#294]: https://github.com/LiteLDev/LegacyScriptEngine/issues/294
11701175
[#299]: https://github.com/LiteLDev/LegacyScriptEngine/issues/299
11711176
[#300]: https://github.com/LiteLDev/LegacyScriptEngine/issues/300
11721177
[#303]: https://github.com/LiteLDev/LegacyScriptEngine/issues/303

docs/apis/EventAPI/PlayerEvents.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,7 @@ The **container** here is a broad concept of container, including boxes, buckets
449449
The player that closes the container.
450450
- block : `Block`
451451
The container that was closed.
452-
- Intercept events: function returns `false`
453-
454-
Due to the limitation of the monitoring function, the containers that currently support monitoring and closing are: chests (`minecraft:chest`), and wooden barrels (`minecraft:barrel`).
452+
- Intercept event: cannot be intercepted.
455453

456454

457455

docs/apis/EventAPI/PlayerEvents.zh.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,7 @@
465465

466466
- block : `Block`
467467
被关闭的容器方块对象
468-
- 拦截事件:函数返回`false`
469-
470-
由于监听函数下限制,目前支持监听关闭的容器有:箱子(`minecraft:chest`)、木桶(`minecraft:barrel`
468+
- 拦截事件:不可以拦截
471469

472470

473471

src/lse/events/PlayerEvents.cpp

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "mc/world/actor/player/Inventory.h"
2222
#include "mc/world/actor/player/Player.h"
2323
#include "mc/world/actor/player/PlayerInventory.h"
24+
#include "mc/world/containers/managers/models/ContainerManagerModel.h"
2425
#include "mc/world/effect/MobEffectInstance.h"
2526
#include "mc/world/events/BlockEventCoordinator.h"
2627
#include "mc/world/events/EventResult.h"
@@ -140,51 +141,26 @@ LL_TYPE_INSTANCE_HOOK(
140141
}
141142

142143
LL_TYPE_INSTANCE_HOOK(
143-
CloseContainerHook1,
144+
CloseContainerHook,
144145
HookPriority::Normal,
145-
ChestBlockActor,
146-
&ChestBlockActor::$stopOpen,
146+
ServerPlayer,
147+
&ServerPlayer::doDeleteContainerManager,
147148
void,
148-
Actor& actor
149+
bool forceDisconnect
149150
) {
150151
IF_LISTENED(EVENT_TYPES::onCloseContainer) {
151-
if (checkClientIsServerThread() && actor.isPlayer()) {
152-
Player& player = static_cast<Player&>(actor);
153-
if (!CallEvent(
154-
EVENT_TYPES::onCloseContainer,
155-
PlayerClass::newPlayer(&player),
156-
BlockClass::newBlock(mPosition, player.getDimensionId().id)
157-
)) {
158-
return;
159-
}
160-
}
161-
}
162-
IF_LISTENED_END(EVENT_TYPES::onCloseContainer);
163-
origin(actor);
164-
}
165-
166-
LL_TYPE_INSTANCE_HOOK(
167-
CloseContainerHook2,
168-
HookPriority::Normal,
169-
BarrelBlockActor,
170-
&BarrelBlockActor::$stopOpen,
171-
void,
172-
Actor& actor
173-
) {
174-
IF_LISTENED(EVENT_TYPES::onCloseContainer) {
175-
if (checkClientIsServerThread() && actor.isPlayer()) {
176-
Player& player = static_cast<Player&>(actor);
177-
if (!CallEvent(
152+
if (mContainerManager) {
153+
if (auto* pos = std::get_if<BlockPos>(&*mContainerManager->mScreenContext->mOwner); pos) {
154+
CallEvent(
178155
EVENT_TYPES::onCloseContainer,
179-
PlayerClass::newPlayer(&player),
180-
BlockClass::newBlock(mPosition, player.getDimensionId().id)
181-
)) {
182-
return;
156+
PlayerClass::newPlayer(this),
157+
BlockClass::newBlock(*pos, getDimensionId().id)
158+
);
183159
}
184160
}
185161
}
186162
IF_LISTENED_END(EVENT_TYPES::onCloseContainer);
187-
origin(actor);
163+
origin(forceDisconnect);
188164
}
189165

190166
LL_TYPE_INSTANCE_HOOK(
@@ -659,10 +635,7 @@ void DropItem() {
659635
DropItemHook2::hook();
660636
}
661637
void OpenContainerEvent() { OpenContainerHook::hook(); }
662-
void CloseContainerEvent() {
663-
CloseContainerHook1::hook();
664-
CloseContainerHook2::hook();
665-
}
638+
void CloseContainerEvent() { CloseContainerHook::hook(); }
666639
void ChangeSlotEvent() { ChangeSlotHook::hook(); }
667640
void AttackBlockEvent() { StartDestroyBlockHook::hook(); }
668641
void UseFrameEvent() {

0 commit comments

Comments
 (0)