Skip to content

Commit

Permalink
feat: adapt es v0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
engsr6982 committed Feb 20, 2025
1 parent e33938b commit e42688c
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/api/PlayerAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include "utils/Using.h"



namespace jse {

ClassDefine<PlayerAPI> PlayerAPI::builder =
Expand Down Expand Up @@ -62,6 +61,7 @@ ClassDefine<PlayerAPI> PlayerAPI::builder =
.instanceFunction("sendPacket", &PlayerAPI::sendPacket)

// PlayerAPI extends MobAPI
.instanceFunction("asMob", &MobAPI::asMob) // MobAPI::asMob override CommandSender::asMob
.instanceFunction("isGliding", &MobAPI::isGliding)

// MobAPI extends ActorAPI
Expand Down
1 change: 1 addition & 0 deletions src/api/actor/ActorAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ ClassDefine<ActorAPI> ActorAPI::builder =
.instanceFunction("asCommandSender", &CommandSenderAPI::asCommandSender)
.instanceFunction("asConsole", &CommandSenderAPI::asConsole)
.instanceFunction("asActor", &CommandSenderAPI::asActor)
.instanceFunction("asMob", &CommandSenderAPI::asMob)
.instanceFunction("asPlayer", &CommandSenderAPI::asPlayer)
.instanceFunction("sendMessage", &CommandSenderAPI::sendMessage)
.instanceFunction("sendErrorMessage", &CommandSenderAPI::sendErrorMessage)
Expand Down
12 changes: 12 additions & 0 deletions src/api/actor/MobAPI.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "api/actor/MobAPI.h"
#include "api/APIHelper.h"
#include "converter/Convert.h"
#include "utils/Using.h"

Expand All @@ -8,6 +9,7 @@ ClassDefine<MobAPI> MobAPI::builder =
defineClass<MobAPI>("Mob")
.constructor(nullptr)
.instanceFunction("toString", &MobAPI::toString)
.instanceFunction("asMob", &MobAPI::asMob) // MobAPI::asMob override CommandSender::asMob
.instanceFunction("isGliding", &MobAPI::isGliding)

// MobAPI extends ActorAPI
Expand Down Expand Up @@ -64,6 +66,16 @@ ClassDefine<MobAPI> MobAPI::builder =

Local<Value> MobAPI::toString(Arguments const& /* args */) { return ConvertToScript("<Mob>"); }

Local<Value> MobAPI::asMob(Arguments const& /* args */) {
try {
if (auto mob = get()->asMob(); mob) {
return MobAPI::newInstance(mob);
}
return {};
}
Catch;
}

Local<Value> MobAPI::isGliding(Arguments const& /* args */) { return ConvertToScript(get()->isGliding()); }

} // namespace jse
1 change: 1 addition & 0 deletions src/api/actor/MobAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MobAPI : public ActorAPI {

public:
METHODS(toString);
METHODS(asMob);
METHODS(isGliding);

public:
Expand Down
11 changes: 11 additions & 0 deletions src/api/command/CommandSenderAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ClassDefine<CommandSenderAPI> CommandSenderAPI::builder =
.instanceFunction("asCommandSender", &CommandSenderAPI::asCommandSender)
.instanceFunction("asConsole", &CommandSenderAPI::asConsole)
.instanceFunction("asActor", &CommandSenderAPI::asActor)
.instanceFunction("asMob", &CommandSenderAPI::asMob)
.instanceFunction("asPlayer", &CommandSenderAPI::asPlayer)
.instanceFunction("sendMessage", &CommandSenderAPI::sendMessage)
.instanceFunction("sendErrorMessage", &CommandSenderAPI::sendErrorMessage)
Expand Down Expand Up @@ -49,6 +50,16 @@ Local<Value> CommandSenderAPI::asActor(Arguments const& /* args */) {
return ActorAPI::newInstance(mSender->asActor());
}

Local<Value> CommandSenderAPI::asMob(Arguments const& /* args */) {
try {
if (auto mob = mSender->asMob(); mob) {
return MobAPI::newInstance(mob);
}
return {};
}
Catch;
}

Local<Value> CommandSenderAPI::asPlayer(Arguments const& /* args */) {
if (mSender->asPlayer() == nullptr) return Local<Value>();
return PlayerAPI::newInstance(mSender->asPlayer());
Expand Down
1 change: 1 addition & 0 deletions src/api/command/CommandSenderAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class CommandSenderAPI : public PermissibleAPI {
METHODS(asCommandSender);
METHODS(asConsole);
METHODS(asActor);
METHODS(asMob);
METHODS(asPlayer);
METHODS(sendMessage);
METHODS(sendErrorMessage);
Expand Down
1 change: 0 additions & 1 deletion src/api/form/ActionFormAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "api/form/FormAPI.h"
#include "api/lang/TranslatableAPI.h"
#include "converter/Convert.h"
#include "endstone/endstone.h"
#include "endstone/message.h"
#include "endstone/player.h"
#include "utils/ResourceSafety.h"
Expand Down
1 change: 0 additions & 1 deletion src/api/form/MessageFormAPI.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "api/PlayerAPI.h"
#include "api/form/FormAPI.h"
#include "converter/Convert.h"
#include "endstone/endstone.h"
#include "endstone/message.h"
#include "utils/ResourceSafety.h"
#include "utils/Using.h"
Expand Down
45 changes: 37 additions & 8 deletions src/api/level/DimensionAPI.cc
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
#include "DimensionAPI.h"
#include "LevelAPI.h"
#include "api/APIHelper.h"
#include "converter/Convert.h"


namespace jse {


ClassDefine<DimensionAPI> DimensionAPI::builder = defineClass<DimensionAPI>("Dimension")
.constructor(nullptr)
.instanceFunction("toString", &DimensionAPI::toString)
.instanceFunction("getName", &DimensionAPI::getName)
.instanceFunction("getType", &DimensionAPI::getType)
.instanceFunction("getLevel", &DimensionAPI::getLevel)
.instanceFunction("getBlockAt", &DimensionAPI::getBlockAt)
.build();
ClassDefine<DimensionAPI> DimensionAPI::builder =
defineClass<DimensionAPI>("Dimension")
.constructor(nullptr)
.instanceFunction("toString", &DimensionAPI::toString)
.instanceFunction("getName", &DimensionAPI::getName)
.instanceFunction("getType", &DimensionAPI::getType)
.instanceFunction("getLevel", &DimensionAPI::getLevel)
.instanceFunction("getBlockAt", &DimensionAPI::getBlockAt)
.instanceFunction("getHighestBlockYAt", &DimensionAPI::getHighestBlockYAt)
.instanceFunction("getHighestBlockAt", &DimensionAPI::getHighestBlockAt)
.instanceFunction("getLoadedChunks", &DimensionAPI::getLoadedChunks)
.build();


Local<Value> DimensionAPI::toString(Arguments const& /* args */) { return ConvertToScript("<Dimension>"); }
Expand All @@ -26,4 +31,28 @@ Local<Value> DimensionAPI::getLevel(Arguments const& /* args */) { return LevelA

Local<Value> DimensionAPI::getBlockAt(Arguments const& /* args */) { return Local<Value>(); } // TODO: BlockAPI

Local<Value> DimensionAPI::getHighestBlockYAt(Arguments const& args) {
try {
CheckArgsCount(args, 2);
CheckArgType(args[0], ValueKind::kNumber);
CheckArgType(args[1], ValueKind::kNumber);
return ConvertToScript(get()->getHighestBlockYAt(ConvertToCpp<int>(args[0]), ConvertToCpp<int>(args[1])));
}
Catch;
}

Local<Value> DimensionAPI::getHighestBlockAt(Arguments const& /* args */) {
try {
return {}; // TODO: BlockAPI
}
Catch;
}

Local<Value> DimensionAPI::getLoadedChunks(Arguments const& /* args */) {
try {
return {}; // TODO: ChunkAPI
}
Catch;
}

} // namespace jse
4 changes: 3 additions & 1 deletion src/api/level/DimensionAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "utils/Using.h"



namespace jse {


Expand All @@ -28,6 +27,9 @@ class DimensionAPI : public ScriptClass {
METHODS(getType);
METHODS(getLevel);
METHODS(getBlockAt); // TODO: BlockAPI
METHODS(getHighestBlockYAt);
METHODS(getHighestBlockAt);
METHODS(getLoadedChunks);

public:
static ClassDefine<DimensionAPI> builder;
Expand Down
10 changes: 6 additions & 4 deletions src/loader/JavaScriptPlugin.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#pragma once
#include <endstone/detail/plugin/plugin_description_builder.h>
#include <cstdint>
#include <endstone/detail/common.h>
#include <endstone/plugin/plugin.h>
#include <endstone/plugin/plugin_description.h>
#include <cstdint>
#include <utility>


namespace jse {

Expand All @@ -15,15 +17,15 @@ class JavaScriptPlugin : public endstone::Plugin {
public:
JavaScriptPlugin(uint64_t engineId, endstone::PluginDescription description)
: engineId_(engineId),
description_(description) {}
description_(std::move(description)) {}
~JavaScriptPlugin() override;

public:
void onLoad() override;
void onEnable() override;
void onDisable() override;

const endstone::PluginDescription& getDescription() const override;
[[nodiscard]] const endstone::PluginDescription& getDescription() const override;

bool
onCommand(endstone::CommandSender& sender, const endstone::Command& command, const std::vector<std::string>& args)
Expand Down
2 changes: 1 addition & 1 deletion src/loader/JavaScriptPluginLoader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ endstone::Plugin* JavaScriptPluginLoader::loadPlugin(std::string file) {

fs::path package = path.parent_path() / "package.json";
if (NodeManager::packageHasDependency(package) && !fs::exists(path.parent_path() / "node_modules")) {
Entry::getInstance()->getLogger().info("Installing dependencies for plugin: {}", path.filename());
Entry::getInstance()->getLogger().info("Installing dependencies for plugin: {}", path.filename().string());
EngineScope enter(wrapper->mEngine);
manager.NpmInstall(path.parent_path().string());
}
Expand Down
5 changes: 5 additions & 0 deletions types/api/actor/Mob.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
declare class Mob extends Actor {
toString(): "<Mob>" | string;

/**
* 将实体转换为Mob类型
*/
asMob(): Mob;

/**
* 检查是否使用滑翔器或任何其他方式飞行的实体。返回真如果是这样的实体。
* @return true如果这个实体在滑翔或使用滑翔器等方法飞行。
Expand Down
5 changes: 5 additions & 0 deletions types/api/command/CommandSender.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ declare class CommandSender extends Permissible {
*/
asActor(): Actor | undefined;

/**
* 该方法获取一个CommandSender作为Mob。
*/
asMob(): Mob | undefined;

/**
* 该方法获取一个CommandSender作为Player。
*
Expand Down
29 changes: 28 additions & 1 deletion types/api/level/Dimension.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,32 @@ declare class Dimension {
* @param location 块的位置
* @return 包含给定坐标的块
*/
// getBlocks(location: Location): Block; // TODO: BlockAPI、LocationAPI
// getBlocks(location: Location): Block; // TODO: BlockAPI

/**
* 获取给定坐标的最高块。
* @param x 方块的 X 坐标
* @param z 方块的 Z 坐标
*/
getHighestBlockYAt(x: number, z: number): number;

/**
* 获取给定位置的最高块。
* @param location 方块的位置
*/
// getHighestBlockAt(location: Location): Block;

/**
* 获取给定坐标的最高块。
* @param x 方块的 X 坐标
* @param z 方块的 Z 坐标
* @return 包含给定坐标的最高块的块
*/
// getHighestBlockAt(x: number, z: number): Block;

/**
* 获取此维度中已加载的块。
* @return 此维度中已加载的块
*/
// getLoadedChunks(): Chunk[];
}
2 changes: 1 addition & 1 deletion xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ add_rules("mode.debug", "mode.release")
add_repositories("iceblcokmc https://github.com/IceBlcokMC/xmake-repo.git")

-- iceblockmc
add_requires("endstone 0.5.7.1")
add_requires("endstone 0.6.0")
add_requires("scriptx jse", { configs = { backend = "V8" } })
add_requires("nodejs 22.12.0")

Expand Down

0 comments on commit e42688c

Please sign in to comment.