新增2接口2事件

- 新增getMaxPlayers和dropPlayerItem接口
- 新增handleRequestTryAction和handleRequestAction事件
This commit is contained in:
子沐呀
2024-06-26 20:28:16 +08:00
Unverified
parent ea9934f315
commit 5afb0f8433
8 changed files with 201 additions and 5 deletions
+5
View File
@@ -743,4 +743,9 @@ void Export_Compatibility_API() {
RemoteCall::exportAs("GMLIB_API", "getEnchantNameAndLevel", [](int id, int level) -> std::string {
return EnchantUtils::getEnchantNameAndLevel((Enchant::Type)id, level);
});
RemoteCall::exportAs(
"GMLIB_API",
"dropPlayerItem",
[](Player* player, ItemStack const* item, bool randomly) -> bool { return player->drop(*item, randomly); }
);
}
@@ -0,0 +1,41 @@
#include "HandleRequestAction.h"
#include "Global.h"
HandleRequestActionBeforeEvent::HandleRequestActionBeforeEvent(
Player* player,
ItemStackRequestAction& requestAction
)
: mPlayer(player),
mRequestAction(requestAction) {}
Player* HandleRequestActionBeforeEvent::self() const { return mPlayer; }
ItemStackRequestAction& HandleRequestActionBeforeEvent::getRequestAction() const { return mRequestAction; }
HandleRequestActionAfterEvent::HandleRequestActionAfterEvent(
Player* player,
ItemStackRequestAction& requestAction
)
: mPlayer(player),
mRequestAction(requestAction) {}
Player* HandleRequestActionAfterEvent::self() const { return mPlayer; }
ItemStackRequestAction& HandleRequestActionAfterEvent::getRequestAction() const { return mRequestAction; }
LL_AUTO_TYPE_INSTANCE_HOOK(
HandleRequestActionHook,
HookPriority::Normal,
ItemStackRequestActionHandler,
"?handleRequestAction@ItemStackRequestActionHandler@@QEAA?AW4ItemStackNetResult@@AEBVItemStackRequestAction@@@Z",
void,
ItemStackRequestAction& requestAction
) {
auto beforeEvent = HandleRequestActionBeforeEvent(((Player*)(*(__int64*)this)), requestAction);
ll::event::EventBus::getInstance().publish(beforeEvent);
if (beforeEvent.isCancelled()) return;
ll::event::EventBus::getInstance().publish(HandleRequestActionAfterEvent(((Player*)(*(__int64*)this)), requestAction));
return origin(requestAction);
}
@@ -0,0 +1,28 @@
#include "Global.h"
#include "ll/api/event/Cancellable.h"
class HandleRequestActionBeforeEvent final : public ll::event::Cancellable<ll::event::Event> {
private:
Player* mPlayer{};
ItemStackRequestAction& mRequestAction;
public:
HandleRequestActionBeforeEvent(Player* player, ItemStackRequestAction& requestAction);
Player* self() const;
ItemStackRequestAction& getRequestAction() const;
};
class HandleRequestActionAfterEvent final : public ll::event::Event {
private:
Player* mPlayer;
ItemStackRequestAction& mRequestAction;
public:
HandleRequestActionAfterEvent(Player* player, ItemStackRequestAction& requestAction);
Player* self() const;
ItemStackRequestAction& getRequestAction() const;
};
+42 -1
View File
@@ -1,4 +1,5 @@
#include "Global.h"
#include "Event/HandleRequestAction/HandleRequestAction.h"
using namespace ll::hash_utils;
void Export_Event_API() {
@@ -234,7 +235,7 @@ void Export_Event_API() {
}
}
);
return true;
return true;
}
case doHash("ProjectileCreate"): {
auto Call = RemoteCall::importAs<bool(Actor * mob)>(eventName, eventId);
@@ -247,6 +248,46 @@ void Export_Event_API() {
);
return true;
}
case doHash("handleRequestTryAction"): {
auto Call =
RemoteCall::importAs<bool(Player * player, int requestAction, int slot, int containerNetId)>(
eventName,
eventId
);
eventBus->emplaceListener<HandleRequestActionBeforeEvent>([Call](HandleRequestActionBeforeEvent& ev) {
bool result = true;
try {
result = Call(
ev.self(),
(int)ev.getRequestAction().getActionType(),
(int)ev.getRequestAction().getSrc().mSlot,
(int)ev.getRequestAction().getSrc().mOpenContainerNetId
);
} catch (...) {}
if (!result) {
ev.cancel();
}
});
return true;
}
case doHash("handleRequestAction"): {
auto Call =
RemoteCall::importAs<bool(Player * player, int requestAction, int slot, int containerNetId)>(
eventName,
eventId
);
eventBus->emplaceListener<HandleRequestActionAfterEvent>([Call](HandleRequestActionAfterEvent& ev) {
try {
Call(
ev.self(),
(int)ev.getRequestAction().getActionType(),
(int)ev.getRequestAction().getSrc().mSlot,
(int)ev.getRequestAction().getSrc().mOpenContainerNetId
);
} catch (...) {}
});
return true;
}
default:
return false;
}
+5
View File
@@ -1,6 +1,8 @@
#include "GMLIB/Server/FakeListAPI.h"
#include "GMLIB/Server/LevelAPI.h"
#include "Global.h"
#include "mc/world/ActorUniqueID.h"
#include <variant>
void Export_Legacy_GMLib_ServerAPI() {
RemoteCall::exportAs("GMLib_ServerAPI", "setEducationFeatureEnabled", []() -> void {
@@ -73,4 +75,7 @@ void Export_Legacy_GMLib_ServerAPI() {
RemoteCall::exportAs("GMLib_ServerAPI", "removeAllFakeList", []() -> void {
return FakeList::removeAllFakeLists();
});
RemoteCall::exportAs("GMLib_ServerAPI", "getMaxPlayers", []() -> int {
return ll::memory::dAccess<int>(ll::service::getServerNetworkHandler().as_ptr(), 200);
});
}