diff --git a/lib/EventAPI-JS.d.ts b/lib/EventAPI-JS.d.ts index 1234a35..7cbaa7f 100644 --- a/lib/EventAPI-JS.d.ts +++ b/lib/EventAPI-JS.d.ts @@ -191,38 +191,4 @@ declare class Event { 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 08de70f..9c9d0d8 100644 --- a/lib/GMLIB_API-JS.d.ts +++ b/lib/GMLIB_API-JS.d.ts @@ -352,12 +352,18 @@ declare class Minecraft { /** 获取附魔名字和等级 */ static getEnchantNameAndLevel( - /** 附魔ID */ - id: number, + /** 附魔命名空间 */ + typeName: string, /** 附魔的等级 */ level: number ): string; + /** 通过附魔ID获取附魔命名空间 */ + static getMaxPlayers( + /** 附魔ID */ + id: number + ): string | null; + /** 获取最大玩家数 */ static getMaxPlayers(): number; } @@ -1130,12 +1136,12 @@ interface Item { ): boolean; /** 获取物品可以拥有的附魔 */ - getLegalEnchants(): number[] + getLegalEnchants(): string[] /** 添加附魔 */ applyEnchant( - /** 附魔ID */ - id: number, + /** 附魔的命名空间 */ + typeName: string, /** 附魔等级 */ level: number, /** 允许非原版附魔 */ @@ -1147,13 +1153,13 @@ interface Item { /** 判断是否拥有附魔 */ hasEnchant( - /** 附魔ID */ - id: number + /** 附魔的命名空间 */ + typeName: string ): boolean; /** 获取附魔等级 */ getEnchantLevel( - /** 附魔ID */ - id: number + /** 附魔的命名空间 */ + typeName: string ): number; } \ No newline at end of file diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index 476b305..e71f69f 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -264,22 +264,24 @@ const GMLIB_API = { getBlockLightEmission: ll.import("GMLIB_API", "getBlockLightEmission"), /** 获取游戏规则列表 @type {function():Array.<{Name:string,Value:string,Type:"Bool"|"Float"|"Int"}>} */ getGameRules: ll.import("GMLIB_API", "getGameRules"), - /** 获取物品可以拥有的附魔 @type {function(Item):Array.} */ + /** 获取物品可以拥有的附魔 @type {function(Item):Array.} */ getLegalEnchants: ll.import("GMLIB_API", "getLegalEnchants"), - /** 给物品添加附魔 @type {function(Item,number,number,boolean):boolean} */ + /** 给物品添加附魔 @type {function(Item,string,number,boolean):boolean} */ applyEnchant: ll.import("GMLIB_API", "applyEnchant"), /** 删除物品所有附魔 @type {function(Item):void} */ removeEnchants: ll.import("GMLIB_API", "removeEnchants"), - /** 判断物品是否拥有附魔 @type {function(Item,number):boolean} */ + /** 判断物品是否拥有附魔 @type {function(Item,string):boolean} */ hasEnchant: ll.import("GMLIB_API", "hasEnchant"), - /** 获取附魔等级 @type {function(Item,number):number} */ + /** 获取附魔等级 @type {function(Item,string):number} */ getEnchantLevel: ll.import("GMLIB_API", "getEnchantLevel"), - /** 获取附魔名字 @type {function(number,number):number} */ + /** 获取附魔名字 @type {function(string,number):number} */ getEnchantNameAndLevel: ll.import("GMLIB_API", "getEnchantNameAndLevel"), + /** 通过ID获取附魔命名空间 @type {function(number):string} */ + getEnchantTypeNameFromId: ll.import("GMLIB_API", "getEnchantTypeNameFromId"), /** 获取最大玩家数 @type {function():number} */ - getMaxPlayers:ll.import("GMLIB_ServerAPI","getMaxPlayers"), + getMaxPlayers: ll.import("GMLib_ServerAPI", "getMaxPlayers"), /** 丢出玩家背包内物品 @type {function(Player,Item,boolean):boolean} */ - dropPlayerItem:ll.import("GMLIB_API","dropPlayerItem") + dropPlayerItem: ll.import("GMLIB_API", "dropPlayerItem") } /** 静态悬浮字类列表 @type {Map} */ @@ -935,19 +937,29 @@ class Minecraft { /** * 获取附魔名字和等级 - * @param {number} id 附魔ID + * @param {string} typeName 附魔的命名空间 * @param {number} level 附魔等级 * @returns {string} 文本 */ - static getEnchantNameAndLevel(id, level) { - return GMLIB_API.getEnchantNameAndLevel(id, level); + static getEnchantNameAndLevel(typeName, level) { + return GMLIB_API.getEnchantNameAndLevel(typeName, level); + } + + /** + * 通过附魔ID获取附魔命名空间 + * @param {number} id 附魔ID + * @returns {string?} 附魔的命名空间 + */ + static getEnchantTypeNameFromId(id) { + const result = GMLIB_API.getEnchantTypeNameFromId(id); + return result === "" ? null : result; } /** * 获取最大玩家数量 * @returns {number} 最大玩家数量 */ - static getMaxPlayers(){ + static getMaxPlayers() { return GMLIB_API.getMaxPlayers(); } } @@ -2111,7 +2123,7 @@ LLSE_Player.prototype.pullInEntity = LLSE_Item.prototype.getLegalEnchants = /** * 获取物品可以拥有的合法附魔 - * @returns {Array.} 附魔ID列表 + * @returns {Array.} 附魔ID列表 */ function () { return GMLIB_API.getLegalEnchants(this); @@ -2120,13 +2132,13 @@ LLSE_Item.prototype.getLegalEnchants = LLSE_Item.prototype.applyEnchant = /** * 添加附魔 - * @param {number} id 附魔ID + * @param {string} typeName 附魔的命名空间 * @param {number} level 等级 * @param {boolean} allowNonVanilla 允许非原版附魔 * @returns {boolean} 是否附魔成功 */ - function (id, level, allowNonVanilla = true) { - return GMLIB_API.applyEnchant(this, id, level, allowNonVanilla); + function (typeName, level, allowNonVanilla = true) { + return GMLIB_API.applyEnchant(this, typeName, level, allowNonVanilla); } LLSE_Item.prototype.removeEnchants = @@ -2140,21 +2152,21 @@ LLSE_Item.prototype.removeEnchants = LLSE_Item.prototype.hasEnchant = /** * 判断是否拥有附魔 - * @param {number} id 附魔ID + * @param {number} typeName 附魔的命名空间 * @returns {boolean} 是否拥有附魔 */ - function (id) { - return GMLIB_API.hasEnchant(this, id); + function (typeName) { + return GMLIB_API.hasEnchant(this, typeName); } LLSE_Item.prototype.getEnchantLevel = /** * 获取附魔等级 - * @param {number} id 附魔ID + * @param {number} typeName 附魔ID * @returns {number} 附魔等级 */ - function (id) { - return GMLIB_API.getEnchantLevel(this, id); + function (typeName) { + return GMLIB_API.getEnchantLevel(this, typeName); } LLSE_Player.prototype.dropItem = diff --git a/src/CompatibilityApi.cpp b/src/CompatibilityApi.cpp index 65241e8..df6e229 100644 --- a/src/CompatibilityApi.cpp +++ b/src/CompatibilityApi.cpp @@ -1,4 +1,5 @@ #include "Global.h" +#include "mc/deps/core/string/HashedString.h" #include bool isInteger(const std::string& str) { @@ -721,28 +722,51 @@ void Export_Compatibility_API() { return result; } ); - RemoteCall::exportAs("GMLIB_API", "getLegalEnchants", [](ItemStack const* item) -> std::vector { - return EnchantUtils::getLegalEnchants(item->getItem()); + RemoteCall::exportAs("GMLIB_API", "getLegalEnchants", [](ItemStack const* item) -> std::vector { + std::vector enchants = EnchantUtils::getLegalEnchants(item->getItem()); + std::vector result; + for (auto& enchant : enchants) { + result.push_back(Enchant::getEnchant((Enchant::Type)enchant)->getStringId()); + } + return result; + }); + RemoteCall::exportAs("GMLIB_API", "getEnchantTypeNameFromId", [](int id) -> std::string { + if (auto enchant = Enchant::getEnchant((Enchant::Type)id)) { + return enchant->getStringId(); + } + return ""; }); RemoteCall::exportAs( "GMLIB_API", "applyEnchant", - [](ItemStack const* item, int id, int level, bool allowNonVanilla) -> bool { - return EnchantUtils::applyEnchant((ItemStackBase&)*item, (Enchant::Type)id, level, allowNonVanilla); + [](ItemStack const* item, std::string const& typeName, int level, bool allowNonVanilla) -> bool { + return EnchantUtils::applyEnchant( + (ItemStackBase&)*item, + Enchant::getEnchantTypeFromName(HashedString(typeName)), + level, + allowNonVanilla + ); } ); RemoteCall::exportAs("GMLIB_API", "removeEnchants", [](ItemStack const* item) -> void { EnchantUtils::removeEnchants((ItemStack&)*item); }); - RemoteCall::exportAs("GMLIB_API", "hasEnchant", [](ItemStack const* item, int id) -> bool { - return EnchantUtils::hasEnchant((Enchant::Type)id, (ItemStackBase&)*item); + RemoteCall::exportAs("GMLIB_API", "hasEnchant", [](ItemStack const* item, std::string const& typeName) -> bool { + return EnchantUtils::hasEnchant(Enchant::getEnchantTypeFromName(HashedString(typeName)), (ItemStackBase&)*item); }); - RemoteCall::exportAs("GMLIB_API", "getEnchantLevel", [](ItemStack const* item, int id) -> int { - return EnchantUtils::getEnchantLevel((Enchant::Type)id, (ItemStackBase&)*item); - }); - RemoteCall::exportAs("GMLIB_API", "getEnchantNameAndLevel", [](int id, int level) -> std::string { - return EnchantUtils::getEnchantNameAndLevel((Enchant::Type)id, level); + RemoteCall::exportAs("GMLIB_API", "getEnchantLevel", [](ItemStack const* item, std::string const& typeName) -> int { + return EnchantUtils::getEnchantLevel( + Enchant::getEnchantTypeFromName(HashedString(typeName)), + (ItemStackBase&)*item + ); }); + RemoteCall::exportAs( + "GMLIB_API", + "getEnchantNameAndLevel", + [](std::string const& typeName, int level) -> std::string { + return EnchantUtils::getEnchantNameAndLevel(Enchant::getEnchantTypeFromName(HashedString(typeName)), level); + } + ); RemoteCall::exportAs( "GMLIB_API", "dropPlayerItem", diff --git a/src/Event/HandleRequestAction/HandleRequestAction.cpp b/src/Event/HandleRequestAction/HandleRequestAction.cpp deleted file mode 100644 index 8073cbd..0000000 --- a/src/Event/HandleRequestAction/HandleRequestAction.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#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 deleted file mode 100644 index 89922c8..0000000 --- a/src/Event/HandleRequestAction/HandleRequestAction.h +++ /dev/null @@ -1,28 +0,0 @@ -#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 685021f..2b37197 100644 --- a/src/EventAPI.cpp +++ b/src/EventAPI.cpp @@ -1,5 +1,4 @@ #include "Global.h" -#include "Event/HandleRequestAction/HandleRequestAction.h" using namespace ll::hash_utils; void Export_Event_API() { @@ -248,46 +247,6 @@ 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 6a2dd50..ccb9313 100644 --- a/src/LegacyServerApi.cpp +++ b/src/LegacyServerApi.cpp @@ -1,8 +1,4 @@ -#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 { @@ -76,6 +72,9 @@ void Export_Legacy_GMLib_ServerAPI() { return FakeList::removeAllFakeLists(); }); RemoteCall::exportAs("GMLib_ServerAPI", "getMaxPlayers", []() -> int { - return ll::memory::dAccess(ll::service::getServerNetworkHandler().as_ptr(), 200); + if (auto level = GMLIB_Level::getInstance()) { + return level->getMaxPlayerCount(); + } + return {}; }); }