From d2f212d759c6c0d275df6e17793fc6a69327fe08 Mon Sep 17 00:00:00 2001 From: engsr6982 Date: Thu, 20 Feb 2025 10:48:16 +0800 Subject: [PATCH] feat(api): add OfflinePlayer API --- src/api/OfflinePlayerAPI.cc | 37 ++++++++++++++++++++++++++++++++++++ src/api/OfflinePlayerAPI.h | 33 ++++++++++++++++++++++++++++++++ src/manager/BindAPI.h | 2 ++ types/api/OfflinePlayer.d.ts | 18 ++++++++++++++++++ types/api/Player.d.ts | 2 +- 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/api/OfflinePlayerAPI.cc create mode 100644 src/api/OfflinePlayerAPI.h create mode 100644 types/api/OfflinePlayer.d.ts diff --git a/src/api/OfflinePlayerAPI.cc b/src/api/OfflinePlayerAPI.cc new file mode 100644 index 00000000..372d63dc --- /dev/null +++ b/src/api/OfflinePlayerAPI.cc @@ -0,0 +1,37 @@ +#include "OfflinePlayerAPI.h" +#include "converter/Convert.h" +#include "utils/Using.h" + + +namespace jse { + +ClassDefine OfflinePlayerAPI::builder = + defineClass("OfflinePlayer") + .instanceFunction("getName", &OfflinePlayerAPI::getName) + .instanceFunction("getUniqueId", &OfflinePlayerAPI::getUniqueId) + .build(); + + +Local OfflinePlayerAPI::toString(Arguments const& /* args */) { + try { + return ConvertToScript("OfflinePlayer"); + } + Catch; +} + +Local OfflinePlayerAPI::getName(Arguments const& /* args */) { + try { + return ConvertToScript(get()->getName()); + } + Catch; +} + +Local OfflinePlayerAPI::getUniqueId(Arguments const& /* args */) { + try { + return ConvertToScript(get()->getUniqueId()); + } + Catch; +} + + +} // namespace jse \ No newline at end of file diff --git a/src/api/OfflinePlayerAPI.h b/src/api/OfflinePlayerAPI.h new file mode 100644 index 00000000..042bfad8 --- /dev/null +++ b/src/api/OfflinePlayerAPI.h @@ -0,0 +1,33 @@ +#pragma once + +#include "api/APIHelper.h" +#include "endstone/offline_player.h" +#include "endstone/player.h" +#include "utils/ResourceSafety.h" +#include "utils/Using.h" +namespace jse { + + +class OfflinePlayerAPI : public ScriptClass { + SafePointerHolder mData; + +public: + explicit OfflinePlayerAPI(endstone::OfflinePlayer* data) + : ScriptClass(ConstructFromCpp{}), + mData(data) {} + + Local newInstance(endstone::OfflinePlayer* data) { return (new OfflinePlayerAPI(data))->getScriptObject(); } + + endstone::OfflinePlayer* get() { return mData.get(); } + +public: + METHODS(toString); + METHODS(getName); + METHODS(getUniqueId); + +public: + static ClassDefine builder; +}; + + +} // namespace jse \ No newline at end of file diff --git a/src/manager/BindAPI.h b/src/manager/BindAPI.h index 63c6d242..4cc35b08 100644 --- a/src/manager/BindAPI.h +++ b/src/manager/BindAPI.h @@ -1,6 +1,7 @@ #pragma once #include "api/ColorFormatAPI.h" #include "api/LoggerAPI.h" +#include "api/OfflinePlayerAPI.h" #include "api/PlayerAPI.h" #include "api/ServerAPI.h" #include "api/SkinAPI.h" @@ -82,6 +83,7 @@ inline void BindAPI(ScriptEngine* engine) { INSTANCE_CLASS(LoggerAPI); INSTANCE_CLASS(PlayerAPI); INSTANCE_CLASS(ServerAPI); + INSTANCE_CLASS(OfflinePlayerAPI); INSTANCE_CLASS(SkinAPI); INSTANCE_CLASS(SkinImageDataAPI); STATIC_CLASS(ColorFormatAPI); diff --git a/types/api/OfflinePlayer.d.ts b/types/api/OfflinePlayer.d.ts new file mode 100644 index 00000000..3b3acb7b --- /dev/null +++ b/types/api/OfflinePlayer.d.ts @@ -0,0 +1,18 @@ +/// + +declare interface OfflinePlayer { + /** + * Returns the name of this player + * 返回玩家的名字 + * + * @returns Player name or null if we have not seen a name for this player yet + * @returns 玩家的名字,如果尚未看到该玩家的名字,则返回null + */ + getName(): string; + + /** + * Returns the UUID of this player + * 返回玩家的UUID + */ + getUniqueId(): UUID; +} diff --git a/types/api/Player.d.ts b/types/api/Player.d.ts index 9f79a80d..37c2bc3f 100644 --- a/types/api/Player.d.ts +++ b/types/api/Player.d.ts @@ -4,7 +4,7 @@ * 玩家类 * @hideconstructor */ -declare class Player extends Mob { +declare class Player extends Mob implements OfflinePlayer { toString(): "" | string; /**