Skip to content

Commit

Permalink
feat(api): add OfflinePlayer API
Browse files Browse the repository at this point in the history
  • Loading branch information
engsr6982 committed Feb 20, 2025
1 parent 57c57bc commit d2f212d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/api/OfflinePlayerAPI.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "OfflinePlayerAPI.h"
#include "converter/Convert.h"
#include "utils/Using.h"


namespace jse {

ClassDefine<OfflinePlayerAPI> OfflinePlayerAPI::builder =
defineClass<OfflinePlayerAPI>("OfflinePlayer")
.instanceFunction("getName", &OfflinePlayerAPI::getName)
.instanceFunction("getUniqueId", &OfflinePlayerAPI::getUniqueId)
.build();


Local<Value> OfflinePlayerAPI::toString(Arguments const& /* args */) {
try {
return ConvertToScript("OfflinePlayer");
}
Catch;
}

Local<Value> OfflinePlayerAPI::getName(Arguments const& /* args */) {
try {
return ConvertToScript(get()->getName());
}
Catch;
}

Local<Value> OfflinePlayerAPI::getUniqueId(Arguments const& /* args */) {
try {
return ConvertToScript(get()->getUniqueId());
}
Catch;
}


} // namespace jse
33 changes: 33 additions & 0 deletions src/api/OfflinePlayerAPI.h
Original file line number Diff line number Diff line change
@@ -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<endstone::OfflinePlayer> mData;

public:
explicit OfflinePlayerAPI(endstone::OfflinePlayer* data)
: ScriptClass(ConstructFromCpp<OfflinePlayerAPI>{}),
mData(data) {}

Local<Object> 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<OfflinePlayerAPI> builder;
};


} // namespace jse
2 changes: 2 additions & 0 deletions src/manager/BindAPI.h
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions types/api/OfflinePlayer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference path="../index.d.ts"/>

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;
}
2 changes: 1 addition & 1 deletion types/api/Player.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 玩家类
* @hideconstructor
*/
declare class Player extends Mob {
declare class Player extends Mob implements OfflinePlayer {
toString(): "<Player>" | string;

/**
Expand Down

0 comments on commit d2f212d

Please sign in to comment.