Skip to content

Commit

Permalink
fix: fix event bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Dec 24, 2023
1 parent 05e36c2 commit ce54196
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 98 deletions.
4 changes: 2 additions & 2 deletions src/ll/api/event/DynamicListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class DynamicListener : public ListenerBase {
void call(Event& event) override {
CompoundTag data{};
event.serialize(data);
data["eventId"] = reflection::getDynamicRawName(event);
callback(data);
event.deserialize(data);
}

static std::shared_ptr<DynamicListener>
create(callback_fn fn, EventPriority priority = EventPriority::Normal) {
static std::shared_ptr<DynamicListener> create(callback_fn fn, EventPriority priority = EventPriority::Normal) {
return std::make_shared<DynamicListener>(std::move(fn), priority);
}

Expand Down
1 change: 0 additions & 1 deletion src/ll/api/event/player/PlayerClickEvent.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include "ll/api/event/Cancellable.h"
#include "ll/api/event/player/PlayerEvent.h"

namespace ll::event::inline player {
Expand Down
18 changes: 9 additions & 9 deletions src/ll/api/event/player/PlayerDestroyBlockEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "ll/api/memory/Hook.h"

#include "mc/nbt/CompoundTag.h"
#include "mc/world/gamemode/GameMode.h"
#include "mc/world/level/block/Block.h"

namespace ll::event::inline player {

Expand All @@ -17,18 +17,18 @@ BlockPos const& PlayerDestroyBlockEvent::pos() const { return mPos; }
LL_TYPED_INSTANCE_HOOK(
PlayerDestroyBlockEventHook,
HookPriority::Normal,
GameMode,
"?destroyBlock@GameMode@@UEAA_NAEBVBlockPos@@E@Z",
bool,
BlockPos const& blockpos,
uchar a4
Block,
&Block::playerWillDestroy,
Block const*,
Player& player,
BlockPos const& pos
) {
auto event = PlayerDestroyBlockEvent{this->getPlayer(), blockpos};
auto event = PlayerDestroyBlockEvent{player, pos};
EventBus::getInstance().publish(event);
if (event.isCancelled()) {
return false;
return nullptr;
}
return origin(blockpos, a4);
return origin(player, pos);
}

static std::unique_ptr<EmitterBase> emitterFactory(ListenerBase&);
Expand Down
4 changes: 2 additions & 2 deletions src/ll/api/event/player/PlayerDestroyBlockEvent.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#pragma once

#include "ll/api/event/Cancellable.h"
#include "ll/api/event/player/PlayerEvent.h"
#include "ll/api/event/player/PlayerClickEvent.h"

#include "mc/world/actor/player/Player.h"
#include "mc/world/level/BlockPos.h"

namespace ll::event::inline player {

class PlayerDestroyBlockEvent : public Cancellable<PlayerEvent> {
class PlayerDestroyBlockEvent : public Cancellable<PlayerLeftClickEvent> {
BlockPos const& mPos;

public:
Expand Down
28 changes: 19 additions & 9 deletions src/ll/api/event/player/PlayerPlaceBlockEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@ void PlayerPlaceBlockEvent::serialize(CompoundTag& nbt) const {
PlayerEvent::serialize(nbt);
nbt["pos"] = ListTag{pos().x, pos().y, pos().z};
}
void PlayerPlacingBlockEvent::serialize(CompoundTag& nbt) const {
Cancellable::serialize(nbt);
nbt["face"] = face();
}
void PlayerPlacedBlockEvent::serialize(CompoundTag& nbt) const {
PlayerPlaceBlockEvent::serialize(nbt);
nbt["placedBlock"] = (uintptr_t)&placedBlock();
}

BlockPos const& PlayerPlaceBlockEvent::pos() const { return mPos; }
uchar const& PlayerPlacingBlockEvent::face() const { return mFace; }
Block const& PlayerPlacedBlockEvent::placedBlock() const { return mPlacedBlock; }

LL_TYPED_INSTANCE_HOOK(
PlayerPlacingBlockEventHook,
Expand All @@ -23,18 +33,18 @@ LL_TYPED_INSTANCE_HOOK(
bool,
Actor& actor,
BlockPos const& blockpos,
uchar facing,
uchar face,
ItemStackBase const& item,
bool a6
bool genParticle
) {
if (actor.isPlayer()) {
auto event = PlayerPlacingBlockEvent{*(Player*)&actor, blockpos};
if (actor.isType(ActorType::Player)) {
auto event = PlayerPlacingBlockEvent{static_cast<Player&>(actor), blockpos, face};
EventBus::getInstance().publish(event);
if (event.isCancelled()) {
return false;
}
}
return origin(actor, blockpos, facing, item, a6);
return origin(actor, blockpos, face, item, genParticle);
}

LL_TYPED_INSTANCE_HOOK(
Expand All @@ -44,13 +54,13 @@ LL_TYPED_INSTANCE_HOOK(
&BlockEventCoordinator::sendBlockPlacedByPlayer,
void,
Player& player,
Block const& block,
Block const& placedBlock,
BlockPos const& blockpos,
bool a5
bool isUnderwater
) {
auto event = PlayerPlacedBlockEvent{player, blockpos};
auto event = PlayerPlacedBlockEvent{player, blockpos, placedBlock};
EventBus::getInstance().publish(event);
return origin(player, block, blockpos, a5);
return origin(player, placedBlock, blockpos, isUnderwater);
}


Expand Down
30 changes: 21 additions & 9 deletions src/ll/api/event/player/PlayerPlaceBlockEvent.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#pragma once

#include "ll/api/event/Cancellable.h"
#include "ll/api/event/player/PlayerEvent.h"
#include "ll/api/event/player/PlayerClickEvent.h"

#include "mc/world/actor/player/Player.h"
#include "mc/world/level/BlockPos.h"
#include "mc/world/level/block/Block.h"

namespace ll::event::inline player {

class PlayerPlaceBlockEvent : public PlayerEvent {
protected:
class PlayerPlaceBlockEvent : public PlayerRightClickEvent {
BlockPos const& mPos;

constexpr explicit PlayerPlaceBlockEvent(Player& player, BlockPos const& pos) : PlayerEvent(player), mPos(pos) {}
protected:
constexpr explicit PlayerPlaceBlockEvent(Player& player, BlockPos const& pos) : PlayerRightClickEvent(player), mPos(pos) {}

public:
void serialize(CompoundTag&) const override;
Expand All @@ -21,17 +22,28 @@ class PlayerPlaceBlockEvent : public PlayerEvent {
};

class PlayerPlacingBlockEvent : public Cancellable<PlayerPlaceBlockEvent> {
public:
constexpr explicit PlayerPlacingBlockEvent(Player& player, BlockPos const& pos) : Cancellable(player, pos) {}
uchar const& mFace;

public:
constexpr explicit PlayerPlacingBlockEvent(Player& player, BlockPos const& pos, uchar const& face)
: Cancellable(player, pos),
mFace(face) {}

void serialize(CompoundTag&) const override;

LLNDAPI uchar const& face() const;
};

class PlayerPlacedBlockEvent : public PlayerPlaceBlockEvent {
public:
constexpr explicit PlayerPlacedBlockEvent(Player& player, BlockPos const& pos)
: PlayerPlaceBlockEvent(player, pos) {}
Block const& mPlacedBlock;

public:
constexpr explicit PlayerPlacedBlockEvent(Player& player, BlockPos const& pos, Block const& placedBlock)
: PlayerPlaceBlockEvent(player, pos),
mPlacedBlock(placedBlock) {}

void serialize(CompoundTag&) const override;

LLNDAPI Block const& placedBlock() const;
};
} // namespace ll::event::inline player
14 changes: 7 additions & 7 deletions src/ll/api/event/player/PlayerUseItemOnEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@ void PlayerUseItemOnEvent::serialize(CompoundTag& nbt) const {
nbt["blockPos"] = ListTag{blockPos().x, blockPos().y, blockPos().z};
nbt["face"] = face();
nbt["clickPos"] = ListTag{clickPos().x, clickPos().y, clickPos().z};
nbt["block"] = (uintptr_t)&block();
nbt["block"] = (uintptr_t)(block().as_ptr());
}

BlockPos const& PlayerUseItemOnEvent::blockPos() const { return mBlockPos; }
uchar const& PlayerUseItemOnEvent::face() const { return mFace; }
Vec3 const& PlayerUseItemOnEvent::clickPos() const { return mClickPos; }
Block const& PlayerUseItemOnEvent::block() const { return mBlock; }
BlockPos const& PlayerUseItemOnEvent::blockPos() const { return mBlockPos; }
uchar const& PlayerUseItemOnEvent::face() const { return mFace; }
Vec3 const& PlayerUseItemOnEvent::clickPos() const { return mClickPos; }
optional_ref<Block const> PlayerUseItemOnEvent::block() const { return mBlock; }

LL_TYPED_INSTANCE_HOOK(
PlayerUseItemOnEventHook,
HookPriority::Normal,
GameMode,
"?useItemOn@GameMode@@UEAA_NAEAVItemStack@@AEBVBlockPos@@EAEBVVec3@@PEBVBlock@@@Z",
"?useItemOn@GameMode@@UEAA?AVInteractionResult@@AEAVItemStack@@AEBVBlockPos@@EAEBVVec3@@PEBVBlock@@@Z",
InteractionResult,
ItemStack& item,
BlockPos const& blockPos,
uchar face,
Vec3 const& clickPos,
Block const* block
) {
auto ev = PlayerUseItemOnEvent(this->getPlayer(), item, blockPos, face, clickPos, *block);
auto ev = PlayerUseItemOnEvent(this->getPlayer(), item, blockPos, face, clickPos, block);
EventBus::getInstance().publish(ev);
if (ev.isCancelled()) {
return {InteractionResult::Result::Fail};
Expand Down
22 changes: 11 additions & 11 deletions src/ll/api/event/player/PlayerUseItemOnEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
namespace ll::event::inline player {

class PlayerUseItemOnEvent : public PlayerUseItemEvent {
BlockPos const& mBlockPos;
uchar const& mFace;
Vec3 const& mClickPos;
Block const& mBlock;
BlockPos const& mBlockPos;
uchar const& mFace;
Vec3 const& mClickPos;
optional_ref<Block const> mBlock;

public:
constexpr PlayerUseItemOnEvent(
Player& player,
ItemStack& item,
BlockPos const& blockPos,
uchar const& face,
Vec3 const& clickPos,
Block const& block
Player& player,
ItemStack& item,
BlockPos const& blockPos,
uchar const& face,
Vec3 const& clickPos,
optional_ref<Block const> block
)
: PlayerUseItemEvent(player, item),
mBlockPos(blockPos),
Expand All @@ -35,6 +35,6 @@ class PlayerUseItemOnEvent : public PlayerUseItemEvent {
LLNDAPI BlockPos const& blockPos() const;
LLNDAPI uchar const& face() const;
LLNDAPI Vec3 const& clickPos() const;
LLNDAPI Block const& block() const;
LLNDAPI optional_ref<Block const> block() const;
};
} // namespace ll::event::inline player
29 changes: 14 additions & 15 deletions src/ll/api/event/world/BlockChangedEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,35 @@

namespace ll::event::inline world {
void BlockChangedEvent::serialize(CompoundTag& nbt) const {
Cancellable::serialize(nbt);
WorldEvent::serialize(nbt);
nbt["layer"] = layer();
nbt["previousBlock"] = (uintptr_t)&previousBlock();
nbt["newBlock"] = (uintptr_t)&newBlock();
nbt["pos"] = ListTag{blockPos().x, blockPos().y, blockPos().z};
nbt["pos"] = ListTag{pos().x, pos().y, pos().z};
}

uint const& BlockChangedEvent::layer() const { return mLayer; }
Block const& BlockChangedEvent::previousBlock() const { return mPreviousBlock; }
Block const& BlockChangedEvent::newBlock() const { return mNewBlock; }
BlockPos const& BlockChangedEvent::blockPos() const { return mBlockPos; }
BlockPos const& BlockChangedEvent::pos() const { return mPos; }

LL_TYPED_INSTANCE_HOOK(
BlockChangedEventHook,
HookPriority::Normal,
BlockSource,
&BlockSource::_blockChanged,
void,
BlockPos const& blockPos,
uint a3,
Block const& afterBlock,
Block const& beforeBlock,
int a6,
ActorBlockSyncMessage const* a7,
Actor* actor
BlockPos const& pos,
uint layer,
Block const& block,
Block const& previousBlock,
int updateFlags,
ActorBlockSyncMessage const* syncMsg,
Actor* blockChangeSource
) {
auto event = BlockChangedEvent{actor->getDimensionBlockSource(), beforeBlock, afterBlock, blockPos};
auto event = BlockChangedEvent{*this, layer, previousBlock, block, pos};
EventBus::getInstance().publish(event);
if (event.isCancelled()) {
return;
}
return origin(blockPos, a3, afterBlock, beforeBlock, a6, a7, actor);
return origin(pos, layer, block, previousBlock, updateFlags, syncMsg, blockChangeSource);
}

static std::unique_ptr<EmitterBase> spawnedEmitterFactory(ListenerBase&);
Expand Down
17 changes: 10 additions & 7 deletions src/ll/api/event/world/BlockChangedEvent.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
#pragma once

#include "ll/api/event/Cancellable.h"
#include "ll/api/event/world/WorldEvent.h"

#include "mc/world/level/BlockPos.h"
#include "mc/world/level/block/Block.h"

namespace ll::event::inline world {

class BlockChangedEvent : public Cancellable<WorldEvent> {
class BlockChangedEvent : public WorldEvent {
uint const& mLayer;
Block const& mPreviousBlock;
Block const& mNewBlock;
BlockPos const& mBlockPos;
BlockPos const& mPos;

public:
constexpr BlockChangedEvent(
BlockSource& blockSource,
uint const& layer,
Block const& previousBlock,
Block const& newBlock,
BlockPos const& blockPos
BlockPos const& pos
)
: Cancellable(blockSource),
: WorldEvent(blockSource),
mLayer(layer),
mPreviousBlock(previousBlock),
mNewBlock(newBlock),
mBlockPos(blockPos) {}
mPos(pos) {}

void serialize(CompoundTag&) const override;

LLNDAPI uint const& layer() const;
LLNDAPI Block const& previousBlock() const;
LLNDAPI Block const& newBlock() const;
LLNDAPI BlockPos const& blockPos() const;
LLNDAPI BlockPos const& pos() const;
};
} // namespace ll::event::inline world
10 changes: 5 additions & 5 deletions src/ll/api/event/world/FireSpreadEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
namespace ll::event::inline world {
void FireSpreadEvent::serialize(CompoundTag& nbt) const {
Cancellable::serialize(nbt);
nbt["pos"] = ListTag{pos().x, pos().y, pos().z};;
nbt["pos"] = ListTag{pos().x, pos().y, pos().z};
}

BlockPos const& FireSpreadEvent::pos() const { return mPos; }

bool onFireSpread_OnPlace = false;
thread_local bool onFireSpreadWhenOnPlace = false;

LL_TYPED_INSTANCE_HOOK(
FireSpreadEventHook1,
Expand All @@ -25,9 +25,9 @@ LL_TYPED_INSTANCE_HOOK(
BlockSource& blockSource,
BlockPos const& blockPos
) {
onFireSpread_OnPlace = true;
onFireSpreadWhenOnPlace = true;
origin(blockSource, blockPos);
onFireSpread_OnPlace = false;
onFireSpreadWhenOnPlace = false;
}

LL_TYPED_INSTANCE_HOOK(
Expand All @@ -40,7 +40,7 @@ LL_TYPED_INSTANCE_HOOK(
BlockPos const& blockPos
) {
auto res = origin(blockSource, blockPos);
if (!onFireSpread_OnPlace || !res) return res;
if (!onFireSpreadWhenOnPlace || !res) return res;

auto event = FireSpreadEvent{blockSource, blockPos};
EventBus::getInstance().publish(event);
Expand Down
Loading

0 comments on commit ce54196

Please sign in to comment.