Skip to content

Commit

Permalink
fix: fix PlayerDestroyEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
ShrBox committed Jan 23, 2024
1 parent 0b07b7d commit d517ee1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 35 deletions.
9 changes: 4 additions & 5 deletions src/DllMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
#include <ll/api/plugin/NativePlugin.h>


extern void entry();
extern void LSE_Load();
extern void LSE_Enable();

extern "C" {
_declspec(dllexport) bool ll_plugin_load(ll::plugin::NativePlugin& self) {
entry();

LSE_Load();
return true;
}

_declspec(dllexport) bool ll_plugin_enable(ll::plugin::NativePlugin& self) {
RegisterDebugCommand();

LSE_Enable();
return true;
}
}
86 changes: 58 additions & 28 deletions src/api/EventAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../engine/LocalShareData.h"
#include "../engine/TimeTaskSystem.h"
#include "../main/BuiltinCommands.h"
#include "BlockAPI.h"
#include "CommandCompatibleAPI.h"
#include "EntityAPI.h"
#include "api/APIHelp.h"
Expand All @@ -17,17 +18,21 @@
#include "ll/api/event/player/PlayerAttackEvent.h"
#include "ll/api/event/player/PlayerChatEvent.h"
#include "ll/api/event/player/PlayerConnectEvent.h"
#include "ll/api/event/player/PlayerDestroyBlockEvent.h"
#include "ll/api/event/player/PlayerDieEvent.h"
#include "ll/api/event/player/PlayerJoinEvent.h"
#include "ll/api/event/player/PlayerLeaveEvent.h"
#include "ll/api/event/player/PlayerRespawnEvent.h"
#include "ll/api/memory/Hook.h"
#include "ll/api/schedule/Scheduler.h"
#include "ll/api/schedule/Task.h"
#include "ll/api/service/Bedrock.h"
#include "ll/api/utils/StringUtils.h"
#include "main/Global.h"
#include "mc/server/commands/CommandOriginType.h"
#include "mc/world/actor/player/Player.h"
#include "mc/world/level./BlockSource.h"
#include "mc/world/level/BlockEventCoordinator.h"
#include "mc/world/level/dimension/Dimension.h"

#include <exception>
Expand Down Expand Up @@ -253,6 +258,13 @@ string EventTypeToString(EVENT_TYPES e) { return string(magic_enum::enum_name(e)
} \
} \
return true;
#define IF_LISTENED_END_VOID(TYPE) \
catch (...) { \
logger.error("Event Callback Failed!"); \
logger.error("Uncaught Exception Detected!"); \
logger.error("In Event: " + EventTypeToString(TYPE)); \
} \
}

//////////////////// APIs ////////////////////

Expand Down Expand Up @@ -324,6 +336,27 @@ bool LLSECallEventsOnHotUnload(ScriptEngine* engine) {

//////////////////// Events ////////////////////

LL_AUTO_TYPE_INSTANCE_HOOK(
PlayerStartDestroyHook,
HookPriority::Normal,
BlockEventCoordinator,
&BlockEventCoordinator::sendBlockDestructionStarted,
void,
Player& player,
BlockPos const& blockPos,
uchar unk_char
) {
IF_LISTENED(EVENT_TYPES::onStartDestroyBlock) {
CallEventRtnVoid(
EVENT_TYPES::onStartDestroyBlock,
PlayerClass::newPlayer(&player),
BlockClass::newBlock(blockPos, player.getDimensionId())
);
}
IF_LISTENED_END_VOID(EVENT_TYPES::onStartDestroyBlock)
origin(player, blockPos, unk_char);
}

// Todo
void EnableEventListener(int eventId) {
using namespace ll::event;
Expand Down Expand Up @@ -437,29 +470,22 @@ void EnableEventListener(int eventId) {
});
break;

// case EVENT_TYPES::onStartDestroyBlock:
// Event::PlayerStartDestroyBlockEvent::subscribe(
// [](const PlayerStartDestroyBlockEvent &ev) {
// IF_LISTENED(EVENT_TYPES::onStartDestroyBlock) {
// CallEvent(EVENT_TYPES::onStartDestroyBlock,
// PlayerClass::newPlayer(ev.mPlayer),
// BlockClass::newBlock(ev.mBlockInstance));
// }
// IF_LISTENED_END(EVENT_TYPES::onStartDestroyBlock);
// });
// break;
case EVENT_TYPES::onStartDestroyBlock:

// case EVENT_TYPES::onDestroyBlock:
// Event::PlayerDestroyBlockEvent::subscribe(
// [](const PlayerDestroyBlockEvent &ev) {
// IF_LISTENED(EVENT_TYPES::onDestroyBlock) {
// CallEvent(EVENT_TYPES::onDestroyBlock,
// PlayerClass::newPlayer(ev.mPlayer),
// BlockClass::newBlock(ev.mBlockInstance));
// }
// IF_LISTENED_END(EVENT_TYPES::onDestroyBlock);
// });
// break;
break;

case EVENT_TYPES::onDestroyBlock:
bus.emplaceListener<PlayerDestroyBlockEvent>([](PlayerDestroyBlockEvent& ev) {
IF_LISTENED(EVENT_TYPES::onStartDestroyBlock) {
CallEvent(
EVENT_TYPES::onStartDestroyBlock,
PlayerClass::newPlayer(&ev.self()),
BlockClass::newBlock(ev.pos(), ev.self().getDimensionId())
);
}
IF_LISTENED_END(EVENT_TYPES::onStartDestroyBlock);
});
break;

// case EVENT_TYPES::onPlaceBlock:
// Event::PlayerPlaceBlockEvent::subscribe(
Expand Down Expand Up @@ -1502,14 +1528,18 @@ page, totalPages, Boolean::newBoolean(shouldDropBook));
}
*/

bool LLSECallServerStartedEvent() {
void LLSECallServerStartedEvent() {
IF_LISTENED(EVENT_TYPES::onServerStarted) { CallEventDelayed(EVENT_TYPES::onServerStarted); }
IF_LISTENED_END(EVENT_TYPES::onServerStarted);
isCmdRegisterEnabled = true;
catch (...) {
logger.error("Event Callback Failed!");
logger.error("Uncaught Exception Detected!");
logger.error("In Event: " + EventTypeToString(EVENT_TYPES::onServerStarted));
}
}
isCmdRegisterEnabled = true;

// 处理延迟注册
ProcessRegCmdQueue();
return true;
// 处理延迟注册
ProcessRegCmdQueue();
}

bool MoneyBeforeEventCallback(LLMoneyEvent type, xuid_t from, xuid_t to, money_t value) {
Expand Down
2 changes: 1 addition & 1 deletion src/api/EventAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ bool LLSECallEventsOnHotUnload(ScriptEngine* engine);
bool MoneyBeforeEventCallback(LLMoneyEvent type, xuid_t from, xuid_t to, money_t value);
bool MoneyEventCallback(LLMoneyEvent type, xuid_t from, xuid_t to, money_t value);

bool LLSECallServerStartedEvent();
void LLSECallServerStartedEvent();
10 changes: 9 additions & 1 deletion src/main/ScriptEngine.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "../api/EventAPI.h"
#include "BuiltinCommands.h"
#include "api/APIHelp.h"
#include "api/EventAPI.h"
#include "api/MoreGlobal.h"
Expand Down Expand Up @@ -34,7 +36,7 @@ extern void LoadMain();
extern void BindAPIs(ScriptEngine* engine);
extern void LoadDebugEngine();

void entry() {
void LSE_Load() {
// Register myself
// ll::registerPlugin(LLSE_LOADER_NAME, LLSE_LOADER_DESCRIPTION,
// LITELOADER_VERSION,
Expand Down Expand Up @@ -86,3 +88,9 @@ void entry() {

MoreGlobal::Init();
}


void LSE_Enable() {
RegisterDebugCommand();
LLSECallServerStartedEvent();
}

0 comments on commit d517ee1

Please sign in to comment.