Skip to content

Commit

Permalink
fix: fix FireSpreadEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamguxiang committed Dec 29, 2024
1 parent 5290d87 commit b091818
Showing 1 changed file with 43 additions and 45 deletions.
88 changes: 43 additions & 45 deletions src/ll/api/event/world/FireSpreadEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include "mc/nbt/CompoundTag.h"
#include "mc/world/level/block/FireBlock.h"

#include "mc/world/level/block/block_events/BlockPlaceEvent.h"

namespace ll::event::inline world {
void FireSpreadEvent::serialize(CompoundTag& nbt) const {
Expand All @@ -13,49 +13,47 @@ void FireSpreadEvent::serialize(CompoundTag& nbt) const {
}

BlockPos const& FireSpreadEvent::pos() const { return mPos; }
// TODO:
// thread_local bool onFireSpreadWhenOnPlace = false;

// LL_TYPE_INSTANCE_HOOK(
// FireSpreadEventHook1,
// HookPriority::Normal,
// FireBlock,
// &FireBlock::$onPlace,
// void,
// BlockSource& blockSource,
// BlockPos const& blockPos
// ) {
// onFireSpreadWhenOnPlace = true;
// origin(blockSource, blockPos);
// onFireSpreadWhenOnPlace = false;
// }

// LL_TYPE_INSTANCE_HOOK(
// FireSpreadEventHook2,
// HookPriority::Normal,
// FireBlock,
// &FireBlock::$mayPlace,
// bool,
// BlockSource& blockSource,
// BlockPos const& blockPos
// ) {
// auto res = origin(blockSource, blockPos);
// if (!onFireSpreadWhenOnPlace || !res) return res;

// auto event = FireSpreadEvent{blockSource, blockPos};
// EventBus::getInstance().publish(event);
// if (event.isCancelled()) {
// return false;
// }
// return res;
// }

// static std::unique_ptr<EmitterBase> emitterFactory();
// class FireSpreadEventEmitter : public Emitter<emitterFactory, FireSpreadEvent> {
// memory::HookRegistrar<FireSpreadEventHook1> hook1;
// memory::HookRegistrar<FireSpreadEventHook2> hook2;
// };

// static std::unique_ptr<EmitterBase> emitterFactory() { return std::make_unique<FireSpreadEventEmitter>(); }

thread_local bool onFireSpreadWhenOnPlace = false;

LL_TYPE_INSTANCE_HOOK(
FireSpreadEventHook1,
HookPriority::Normal,
FireBlock,
&FireBlock::onPlace,
void,
BlockEvents::BlockPlaceEvent& eventData
) {
onFireSpreadWhenOnPlace = true;
origin(eventData);
onFireSpreadWhenOnPlace = false;
}

LL_TYPE_INSTANCE_HOOK(
FireSpreadEventHook2,
HookPriority::Normal,
FireBlock,
&FireBlock::$mayPlace,
bool,
BlockSource& blockSource,
BlockPos const& blockPos
) {
auto res = origin(blockSource, blockPos);
if (!onFireSpreadWhenOnPlace || !res) return res;
auto event = FireSpreadEvent{blockSource, blockPos};
EventBus::getInstance().publish(event);
if (event.isCancelled()) {
return false;
}
return res;
}

static std::unique_ptr<EmitterBase> emitterFactory();
class FireSpreadEventEmitter : public Emitter<emitterFactory, FireSpreadEvent> {
memory::HookRegistrar<FireSpreadEventHook1> hook1;
memory::HookRegistrar<FireSpreadEventHook2> hook2;
};

static std::unique_ptr<EmitterBase> emitterFactory() { return std::make_unique<FireSpreadEventEmitter>(); }

} // namespace ll::event::inline world

0 comments on commit b091818

Please sign in to comment.