From 5afb0f843389dca6a7806624870e8e6ee4bf5cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=90=E6=B2=90=E5=91=80?= <163636894+zimuya4153@users.noreply.github.com> Date: Wed, 26 Jun 2024 20:28:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E2=E6=8E=A5=E5=8F=A32=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增getMaxPlayers和dropPlayerItem接口 - 新增handleRequestTryAction和handleRequestAction事件 --- lib/EventAPI-JS.d.ts | 34 +++++++++++++++ lib/GMLIB_API-JS.d.ts | 23 +++++++++- lib/GMLIB_API-JS.js | 27 +++++++++++- src/CompatibilityApi.cpp | 5 +++ .../HandleRequestAction.cpp | 41 ++++++++++++++++++ .../HandleRequestAction/HandleRequestAction.h | 28 ++++++++++++ src/EventAPI.cpp | 43 ++++++++++++++++++- src/LegacyServerApi.cpp | 5 +++ 8 files changed, 201 insertions(+), 5 deletions(-) create mode 100644 src/Event/HandleRequestAction/HandleRequestAction.cpp create mode 100644 src/Event/HandleRequestAction/HandleRequestAction.h diff --git a/lib/EventAPI-JS.d.ts b/lib/EventAPI-JS.d.ts index 54af930..615af95 100644 --- a/lib/EventAPI-JS.d.ts +++ b/lib/EventAPI-JS.d.ts @@ -191,4 +191,38 @@ declare class Event2 { entity: Entity ) => void ): boolean; + + /** 尝试物品物品请求 */ + static listen( + /** 事件名 */ + event: "handleRequestTryAction", + /** 回调函数 */ + listener: ( + /** 请求的玩家对象 */ + Player: Player, + /** 请求的操作 */ + actionType: number, + /** 请求的槽位 */ + slot: number, + /** 容器的ID */ + containerNetId: number + ) => boolean | void + ): boolean; + + /** 处理物品请求后(不可拦截) */ + static listen( + /** 事件名 */ + event: "handleRequestAction", + /** 回调函数 */ + listener: ( + /** 请求的玩家对象 */ + Player: Player, + /** 请求的操作 */ + actionType: number, + /** 请求的槽位 */ + slot: number, + /** 容器的ID */ + containerNetId: number + ) => void + ): boolean; } diff --git a/lib/GMLIB_API-JS.d.ts b/lib/GMLIB_API-JS.d.ts index 20963e8..08de70f 100644 --- a/lib/GMLIB_API-JS.d.ts +++ b/lib/GMLIB_API-JS.d.ts @@ -348,7 +348,18 @@ declare class Minecraft { ): number; /** 获取游戏规则列表 */ - static getGameRules(): { Name: string; Value: boolean | number; }[] + static getGameRules(): { Name: string; Value: boolean | number; }[]; + + /** 获取附魔名字和等级 */ + static getEnchantNameAndLevel( + /** 附魔ID */ + id: number, + /** 附魔的等级 */ + level: number + ): string; + + /** 获取最大玩家数 */ + static getMaxPlayers(): number; } /** 合成表类 */ @@ -1002,6 +1013,14 @@ interface Player { /** (GMLIB)清除玩家重生点 */ clearSpawnPoint(): void; + + /** 丢出玩家物品 */ + dropItem( + /** 物品对象 */ + item: Item, + /** 随机丢出位置 */ + randomly: boolean | undefined + ): boolean; } interface Entity { @@ -1072,7 +1091,7 @@ interface Block { /** (GMLIB)方块是否不需要工具采集 */ isAlwaysDestroyable(): boolean; - /** (GMLIB) */ + /** (GMLIB)检测方块是否能被玩家挖掘(比如插件拦截) */ playerWillDestroy( /** 挖掘的玩家对象 */ player: Player diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index 2d8b90a..476b305 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -275,7 +275,11 @@ const GMLIB_API = { /** 获取附魔等级 @type {function(Item,number):number} */ getEnchantLevel: ll.import("GMLIB_API", "getEnchantLevel"), /** 获取附魔名字 @type {function(number,number):number} */ - getEnchantNameAndLevel: ll.import("GMLIB_API", "getEnchantNameAndLevel") + getEnchantNameAndLevel: ll.import("GMLIB_API", "getEnchantNameAndLevel"), + /** 获取最大玩家数 @type {function():number} */ + getMaxPlayers:ll.import("GMLIB_ServerAPI","getMaxPlayers"), + /** 丢出玩家背包内物品 @type {function(Player,Item,boolean):boolean} */ + dropPlayerItem:ll.import("GMLIB_API","dropPlayerItem") } /** 静态悬浮字类列表 @type {Map} */ @@ -938,6 +942,14 @@ class Minecraft { static getEnchantNameAndLevel(id, level) { return GMLIB_API.getEnchantNameAndLevel(id, level); } + + /** + * 获取最大玩家数量 + * @returns {number} 最大玩家数量 + */ + static getMaxPlayers(){ + return GMLIB_API.getMaxPlayers(); + } } /** 合成表类 */ @@ -2068,7 +2080,7 @@ LLSE_Block.prototype.isAlwaysDestroyable = LLSE_Block.prototype.playerWillDestroy = /** - * + * 检测方块是否能被玩家挖掘(比如插件拦截) * @param {Player} player 玩家对象 * @returns {boolean} */ @@ -2145,6 +2157,17 @@ LLSE_Item.prototype.getEnchantLevel = return GMLIB_API.getEnchantLevel(this, id); } +LLSE_Player.prototype.dropItem = + /** + * 使玩家丢出物品 + * @param {Item} item 物品对象 + * @param {boolean} [randomly=false] 随机丢出位置 + * @returns {boolean} 是否丢出成功 + */ + function (item, randomly = false) { + return GMLIB_API.dropPlayerItem(this, item, randomly); + } + module.exports = { StaticFloatingText, DynamicFloatingText, diff --git a/src/CompatibilityApi.cpp b/src/CompatibilityApi.cpp index b02f3c0..65241e8 100644 --- a/src/CompatibilityApi.cpp +++ b/src/CompatibilityApi.cpp @@ -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); } + ); } \ No newline at end of file diff --git a/src/Event/HandleRequestAction/HandleRequestAction.cpp b/src/Event/HandleRequestAction/HandleRequestAction.cpp new file mode 100644 index 0000000..8073cbd --- /dev/null +++ b/src/Event/HandleRequestAction/HandleRequestAction.cpp @@ -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); +} \ No newline at end of file diff --git a/src/Event/HandleRequestAction/HandleRequestAction.h b/src/Event/HandleRequestAction/HandleRequestAction.h new file mode 100644 index 0000000..89922c8 --- /dev/null +++ b/src/Event/HandleRequestAction/HandleRequestAction.h @@ -0,0 +1,28 @@ +#include "Global.h" +#include "ll/api/event/Cancellable.h" + +class HandleRequestActionBeforeEvent final : public ll::event::Cancellable { +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; +}; \ No newline at end of file diff --git a/src/EventAPI.cpp b/src/EventAPI.cpp index bfa03d2..685021f 100644 --- a/src/EventAPI.cpp +++ b/src/EventAPI.cpp @@ -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(eventName, eventId); @@ -247,6 +248,46 @@ void Export_Event_API() { ); return true; } + case doHash("handleRequestTryAction"): { + auto Call = + RemoteCall::importAs( + eventName, + eventId + ); + eventBus->emplaceListener([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( + eventName, + eventId + ); + eventBus->emplaceListener([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; } diff --git a/src/LegacyServerApi.cpp b/src/LegacyServerApi.cpp index d3fb494..6a2dd50 100644 --- a/src/LegacyServerApi.cpp +++ b/src/LegacyServerApi.cpp @@ -1,6 +1,8 @@ #include "GMLIB/Server/FakeListAPI.h" +#include "GMLIB/Server/LevelAPI.h" #include "Global.h" #include "mc/world/ActorUniqueID.h" +#include 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(ll::service::getServerNetworkHandler().as_ptr(), 200); + }); }