From 27518fa2fce9bae72ca9ebeb7558c93f25ce9f9e 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: Mon, 8 Jul 2024 15:55:47 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E2=E4=BA=8B=E4=BB=B6=EF=BC=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B92=E4=BA=8B=E4=BB=B6=EF=BC=8C2=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增事件: - SpawnWanderingTrader - HandleRequestAction 修改事件: - ProjectileTryCreate - ProjectileCreate 修改接口: - getPlayerFromUniqueId - getEntityFromUniqueId --- lib/EventAPI-JS.d.ts | 36 +++++++++++++++++++++++++++-- lib/GMLIB_API-JS.d.ts | 4 ++-- lib/GMLIB_API-JS.js | 8 +++---- src/EventAPI.cpp | 53 +++++++++++++++++++++++++++++++++++++++---- 4 files changed, 88 insertions(+), 13 deletions(-) diff --git a/lib/EventAPI-JS.d.ts b/lib/EventAPI-JS.d.ts index 7cbaa7f..897d826 100644 --- a/lib/EventAPI-JS.d.ts +++ b/lib/EventAPI-JS.d.ts @@ -177,7 +177,9 @@ declare class Event { /** 回调函数 */ listener: ( /** 弹射物实体对象 */ - entity: Entity + entity: Entity, + /** 创建弹射的实体的uniqueID */ + uniqueId: number ) => boolean | void ): boolean; @@ -188,7 +190,37 @@ declare class Event { /** 回调函数 */ listener: ( /** 弹射物实体对象 */ - entity: Entity + entity: Entity, + /** 创建弹射的实体的uniqueID */ + uniqueId: number ) => void ): boolean; + + /** 生成流浪商人 */ + static listen( + /** 事件名 */ + event: "SpawnWanderingTrader", + /** 回调函数 */ + listener: ( + /** 生成的坐标 */ + pos: IntPos + ) => boolean | void + ): boolean; + + /** 处理物品请求 */ + static listen( + /** 事件名 */ + event: "HandleRequestAction", + /** 回调函数 */ + listener: ( + /** 请求玩家 */ + player: Player, + /** 请求类型 */ + actionType: string, + /** 容器ID */ + ContainerNetId: string, + /** 请求的槽位 */ + slot: number + ) => boolean | void + ): boolean; } diff --git a/lib/GMLIB_API-JS.d.ts b/lib/GMLIB_API-JS.d.ts index 9c9d0d8..f4d6aca 100644 --- a/lib/GMLIB_API-JS.d.ts +++ b/lib/GMLIB_API-JS.d.ts @@ -276,12 +276,12 @@ declare class Minecraft { /** 根据UniqueId获取玩家对象 */ static getPlayerFromUniqueId( - uniqueId: string + uniqueId: string | number ): Player; /** 根据UniqueId获取实体对象 */ static getEntityFromUniqueId( - uniqueId: string + uniqueId: string | number ): Entity; /** 获取方块runtimeId */ diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index e71f69f..5f66202 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -805,20 +805,20 @@ class Minecraft { /** * 根据UniqueId获取玩家对象 - * @param {string} uniqueId 玩家的UniqueId + * @param {string|number} uniqueId 玩家的UniqueId * @returns {Player} 玩家对象 */ static getPlayerFromUniqueId(uniqueId) { - return GMLIB_API.getPlayerFromUniqueId(uniqueId); + return GMLIB_API.getPlayerFromUniqueId(uniqueId.toString()); } /** * 根据UniqueId获取实体对象 - * @param {string} uniqueId 实体的UniqueId + * @param {string|number} uniqueId 实体的UniqueId * @returns {Entity} 实体对象 */ static getEntityFromUniqueId(uniqueId) { - return GMLIB_API.getEntityFromUniqueId(uniqueId); + return GMLIB_API.getEntityFromUniqueId(uniqueId.toString()); } /** diff --git a/src/EventAPI.cpp b/src/EventAPI.cpp index 2b37197..1ae5c61 100644 --- a/src/EventAPI.cpp +++ b/src/EventAPI.cpp @@ -1,4 +1,5 @@ #include "Global.h" +#include "mc/world/ActorUniqueID.h" using namespace ll::hash_utils; void Export_Event_API() { @@ -222,31 +223,73 @@ void Export_Event_API() { return true; } case doHash("ProjectileTryCreate"): { - auto Call = RemoteCall::importAs(eventName, eventId); + auto Call = RemoteCall::importAs(eventName, eventId); eventBus->emplaceListener( [Call](Event::EntityEvent::ProjectileCreateBeforeEvent& ev) { bool result = true; try { - result = Call(&ev.self()); + auto uid = ev.getShooter() ? ev.getShooter()->getOrCreateUniqueID().id : -1; + result = Call(&ev.self(), uid); } catch (...) {} if (!result) { ev.cancel(); } } ); - return true; + return true; } case doHash("ProjectileCreate"): { - auto Call = RemoteCall::importAs(eventName, eventId); + auto Call = RemoteCall::importAs(eventName, eventId); eventBus->emplaceListener( [Call](Event::EntityEvent::ProjectileCreateAfterEvent& ev) { try { - Call(&ev.self()); + auto uid = ev.getShooter() ? ev.getShooter()->getOrCreateUniqueID().id : -1; + Call(&ev.self(), uid); } catch (...) {} } ); return true; } + case doHash("SpawnWanderingTrader"): { + auto Call = RemoteCall::importAs pos)>(eventName, eventId); + eventBus->emplaceListener( + [Call](Event::EntityEvent::SpawnWanderingTraderBeforeEvent& ev) { + bool result = true; + try { + result = Call({ev.getPos(), 0}); + } catch (...) {} + if (!result) { + ev.cancel(); + } + } + ); + return true; + } + case doHash("HandleRequestAction"): { + auto Call = RemoteCall::importAs< + bool(Player * player, std::string const& actionType, std::string const& ContainerNetId, int slot)>( + eventName, + eventId + ); + eventBus->emplaceListener( + [Call](Event::PlayerEvent::HandleRequestActionBeforeEvent& ev) { + bool result = true; + try { + auto requestAction = (ItemStackRequestActionTransferBase*)&ev.getRequestAction(); + result = Call( + &ev.self(), + magic_enum::enum_name(requestAction->mActionType).data(), + magic_enum::enum_name(requestAction->getSrc().mOpenContainerNetId).data(), + requestAction->getSrc().mSlot + ); + } catch (...) {} + if (!result) { + ev.cancel(); + } + } + ); + return true; + } default: return false; }