Skip to content

Commit 5e39cd8

Browse files
committed
feat: add some apis
1 parent 19e1092 commit 5e39cd8

6 files changed

Lines changed: 225 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added packet sending APIs: `Packet::sendTo`, `Packet::sendToClients` and `Packet::sendToServer` @zimuya4153
13+
- Added `BinaryStream` APIs: `getReadPointer`, `setReadPointer`, `setData`, `writeBytes`, `writeUuid` and `writeNormalizedFloat` @zimuya4153
14+
- Added server APIs: `mc.getMotd`, `mc.getOnlinePlayerNum`, `mc.getMaxNumPlayers`, `mc.getDimensionId` and `mc.getDimensionName` @zimuya4153
15+
16+
### Changed
17+
18+
- `BinaryStream::getData([clear])` now supports the optional `clear` parameter and returns `ByteBuffer` instead of `String` @zimuya4153
19+
- `BinaryStream::createPacket(pktid[,raw])` now supports creating raw packets @zimuya4153
20+
- Allowed string arguments for several `BinaryStream::write*` numeric APIs to better support BigInt values @zimuya4153
21+
1022
## [0.18.2] - 2026-04-17
1123

1224
### Fixed

docs/apis/GameAPI/Server.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ The following APIs provide interfaces for customizing some server settings:
2020

2121

2222

23+
### Get Server Motd String
24+
25+
!!! warning
26+
This function is only available in 0.19.0 and later.
27+
28+
`mc.getMotd()`
29+
30+
- Return value: Current server Motd string
31+
- Return value type: `String`
32+
33+
34+
2335
### Set Server Motd String
2436

2537
`mc.setMotd(motd)`
@@ -44,6 +56,33 @@ The following APIs provide interfaces for customizing some server settings:
4456

4557

4658

59+
### Get Online Player Count
60+
61+
!!! warning
62+
This function is only available in 0.19.0 and later.
63+
64+
`mc.getOnlinePlayerNum([ignoreSimulatedPlayer])`
65+
66+
- Parameters:
67+
- ignoreSimulatedPlayer : `Boolean` = `false`
68+
Whether to ignore simulated players
69+
- Return value: Current number of online players
70+
- Return value type: `Number`
71+
72+
73+
74+
### Get Server Maximum Player Count
75+
76+
!!! warning
77+
This function is only available in 0.19.0 and later.
78+
79+
`mc.getMaxNumPlayers()`
80+
81+
- Return value: Current maximum number of players on the server
82+
- Return value type: `Number`
83+
84+
85+
4786
### Get Sever time
4887

4988
`mc.getTime(TimeID)`
@@ -90,3 +129,33 @@ Among them, daytime is the number of game ticks since dawn, gametime is the age
90129
- Return value type: `Boolean`
91130

92131

132+
133+
### Get Dimension ID
134+
135+
!!! warning
136+
This function is only available in 0.19.0 and later.
137+
138+
`mc.getDimensionId(name)`
139+
140+
- Parameters:
141+
- name : `String`
142+
Dimension name
143+
- Return value: Dimension ID corresponding to the dimension name, or `null` if the dimension name is invalid
144+
- Return value type: `Number`
145+
146+
147+
148+
### Get Dimension Name
149+
150+
!!! warning
151+
This function is only available in 0.19.0 and later.
152+
153+
`mc.getDimensionName(dimid)`
154+
155+
- Parameters:
156+
- dimid : `Integer`
157+
Dimension ID
158+
- Return value: Dimension name corresponding to the dimension ID, or `null` if the dimension ID is invalid
159+
- Return value type: `String`
160+
161+

docs/apis/GameAPI/Server.zh.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@
2020

2121

2222

23+
### 获取服务器MOTD字符串
24+
25+
!!! warning
26+
此函数仅在0.19.0及以后版本可用
27+
28+
`mc.getMotd()`
29+
30+
- 返回值:当前服务器MOTD字符串
31+
- 返回值类型:`String`
32+
33+
34+
2335
### 设置服务器MOTD字符串
2436

2537
`mc.setMotd(motd)`
@@ -44,6 +56,33 @@
4456

4557

4658

59+
### 获取在线玩家数量
60+
61+
!!! warning
62+
此函数仅在0.19.0及以后版本可用
63+
64+
`mc.getOnlinePlayerNum([ignoreSimulatedPlayer])`
65+
66+
- 参数:
67+
- ignoreSimulatedPlayer : `Boolean` = `false`
68+
是否忽略模拟玩家
69+
- 返回值:当前在线玩家数量
70+
- 返回值类型:`Number`
71+
72+
73+
74+
### 获取服务器最大玩家数
75+
76+
!!! warning
77+
此函数仅在0.19.0及以后版本可用
78+
79+
`mc.getMaxNumPlayers()`
80+
81+
- 返回值:当前服务器最大玩家数
82+
- 返回值类型:`Number`
83+
84+
85+
4786
### 获取服务器游戏时间
4887

4988
`mc.getTime(TimeID)`
@@ -90,3 +129,33 @@
90129
- 返回值类型:`Boolean`
91130

92131

132+
133+
### 获取维度ID
134+
135+
!!! warning
136+
此函数仅在0.19.0及以后版本可用
137+
138+
`mc.getDimensionId(name)`
139+
140+
- 参数:
141+
- name : `String`
142+
维度名称
143+
- 返回值:对应维度名称的维度ID,若维度名称无效则返回`null`
144+
- 返回值类型:`Number`
145+
146+
147+
148+
### 获取维度名称
149+
150+
!!! warning
151+
此函数仅在0.19.0及以后版本可用
152+
153+
`mc.getDimensionName(dimid)`
154+
155+
- 参数:
156+
- dimid : `Integer`
157+
维度ID
158+
- 返回值:对应维度ID的维度名称,若维度ID无效则返回`null`
159+
- 返回值类型:`String`
160+
161+

src/legacy/api/McAPI.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ ClassDefine<void> McClassBuilder = defineClass("mc")
2626
.function("newSimpleForm", &McClass::newSimpleForm)
2727
.function("newCustomForm", &McClass::newCustomForm)
2828
.function("regConsoleCmd", &McClass::regConsoleCmd)
29+
.function("getMotd", &McClass::getMotd)
2930
.function("setMotd", &McClass::setMotd)
3031
.function("sendCmdOutput", &McClass::sendCmdOutput)
3132
.function("newIntPos", &McClass::newIntPos)
@@ -52,6 +53,10 @@ ClassDefine<void> McClassBuilder = defineClass("mc")
5253
.function("setTime", &McClass::setTime)
5354
.function("getWeather", &McClass::getWeather)
5455
.function("setWeather", &McClass::setWeather)
56+
.function("getDimensionId", &McClass::getDimensionId)
57+
.function("getDimensionName", &McClass::getDimensionName)
58+
.function("getOnlinePlayerNum", &McClass::getOnlinePlayerNum)
59+
.function("getMaxNumPlayers", &McClass::getMaxNumPlayers)
5560
// For Compatibility
5661
.function("getAllScoreObjective", &McClass::getAllScoreObjectives)
5762
.function("getDisplayObjectives", &McClass::getDisplayObjective)

src/legacy/api/McAPI.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ class McClass {
3535
static Local<Value> newCustomForm(Arguments const& args);
3636

3737
static Local<Value> regConsoleCmd(Arguments const& args);
38+
static Local<Value> getMotd(Arguments const& args);
3839
static Local<Value> setMotd(Arguments const& args);
3940
static Local<Value> sendCmdOutput(Arguments const& args);
4041
static Local<Value> crashBDS(Arguments const& args);
4142

43+
static Local<Value> getOnlinePlayerNum(Arguments const& args);
44+
static Local<Value> getMaxNumPlayers(Arguments const& args);
4245
static Local<Value> setMaxNumPlayers(Arguments const& args);
4346

4447
static Local<Value> newIntPos(Arguments const& args);
@@ -69,5 +72,8 @@ class McClass {
6972
static Local<Value> setTime(Arguments const& args);
7073
static Local<Value> getWeather(Arguments const& args);
7174
static Local<Value> setWeather(Arguments const& args);
75+
76+
static Local<Value> getDimensionId(Arguments const& args);
77+
static Local<Value> getDimensionName(Arguments const& args);
7278
};
7379
extern ClassDefine<> McClassBuilder;

src/legacy/api/ServerAPI.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,18 @@
88
#include "mc/common/SharedConstants.h"
99
#include "mc/network/ServerNetworkHandler.h"
1010
#include "mc/network/packet/SetTimePacket.h"
11+
#include "mc/world/level/dimension/VanillaDimensions.h"
1112
#include "mc/world/level/storage/LevelData.h"
1213

1314
#include <ll/api/service/ServerInfo.h>
1415

16+
Local<Value> McClass::getMotd(Arguments const& args) {
17+
try {
18+
return String::newString(ll::service::getServerNetworkHandler().and_then(&ServerNetworkHandler::mServerName));
19+
}
20+
CATCH_AND_THROW
21+
}
22+
1523
Local<Value> McClass::setMotd(Arguments const& args) {
1624
CHECK_ARGS_COUNT(args, 1)
1725
CHECK_ARG_TYPE(args[0], ValueKind::kString)
@@ -24,6 +32,30 @@ Local<Value> McClass::setMotd(Arguments const& args) {
2432

2533
Local<Value> McClass::crashBDS(Arguments const&) { return Boolean::newBoolean(false); }
2634

35+
Local<Value> McClass::getOnlinePlayerNum(Arguments const& args) {
36+
if (args.size() >= 1) CHECK_ARG_TYPE(args[0], ValueKind::kBoolean);
37+
38+
try {
39+
if (args.size() >= 1 && args[0].asBoolean().value()) {
40+
return Number::newNumber(ll::service::getServerNetworkHandler().and_then([](auto& handler) {
41+
return static_cast<int64>(handler.mClients->size());
42+
}));
43+
} else {
44+
return Number::newNumber(ll::service::getLevel().and_then(&Level::getUserCount));
45+
}
46+
}
47+
CATCH_AND_THROW
48+
}
49+
50+
Local<Value> McClass::getMaxNumPlayers(Arguments const& args) {
51+
try {
52+
return Number::newNumber(
53+
ll::service::getServerNetworkHandler().and_then(&ServerNetworkHandler::mMaxNumPlayers)
54+
);
55+
}
56+
CATCH_AND_THROW
57+
}
58+
2759
Local<Value> McClass::setMaxNumPlayers(Arguments const& args) {
2860
CHECK_ARGS_COUNT(args, 1)
2961
CHECK_ARG_TYPE(args[0], ValueKind::kNumber)
@@ -128,3 +160,35 @@ Local<Value> McClass::setWeather(Arguments const& args) {
128160

129161
return Boolean::newBoolean(true);
130162
}
163+
164+
Local<Value> McClass::getDimensionId(Arguments const& args) {
165+
CHECK_ARGS_COUNT(args, 1)
166+
CHECK_ARG_TYPE(args[0], ValueKind::kString)
167+
168+
try {
169+
if (auto dimid = VanillaDimensions::fromString(args[0].asString().toString());
170+
dimid != VanillaDimensions::Undefined()) {
171+
return Number::newNumber(dimid);
172+
}
173+
return {};
174+
}
175+
CATCH_AND_THROW
176+
177+
return Boolean::newBoolean(true);
178+
}
179+
180+
Local<Value> McClass::getDimensionName(Arguments const& args) {
181+
CHECK_ARGS_COUNT(args, 1)
182+
CHECK_ARG_TYPE(args[0], ValueKind::kNumber)
183+
184+
try {
185+
auto& map = VanillaDimensions::DimensionMap().mLeft;
186+
if (auto it = map.find(args[0].asNumber().toInt32()); it != map.end()) {
187+
return String::newString(it->second);
188+
}
189+
return {};
190+
}
191+
CATCH_AND_THROW
192+
193+
return Boolean::newBoolean(true);
194+
}

0 commit comments

Comments
 (0)