Skip to content

Commit e117469

Browse files
committed
API-ver2
1 parent 3a97bc2 commit e117469

6 files changed

Lines changed: 69 additions & 22 deletions

File tree

‎.poggit.yml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--- # Poggit-CI Manifest
2+
build-by-default: true
3+
branches:
4+
- master
5+
projects:
6+
QueryServerCore:
7+
type: library
8+
path: ""
9+
QueryServer:
10+
path: ""
11+
libs:
12+
- src: NhanAZ-Libraries/QueryServer/QueryServerCore
13+
version: ^0.0.3
14+
...

‎README.md‎

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,46 @@
1-
# QueryServer
1+
# QueryServer (virion + plugin)
22

3-
A lightweight PocketMine-MP plugin that queries Bedrock servers using the public `api.mcsrvstat.us` v3 endpoint with a UDP fallback (libpmquery). It includes a small API so other plugins can request queries asynchronously without blocking the main thread.
3+
Two deliverables in one repo:
4+
- **QueryServerCore (virion/library)**: async HTTP (api.mcsrvstat.us v3) + UDP fallback (libpmquery) with a tiny callback-based API for other plugins.
5+
- **QueryServer (plugin)**: thin wrapper exposing `/query` command that uses the core.
46

57
## Features
6-
- `/query <host[:port]> [port]` — fetch server status via HTTP; automatically falls back to UDP.
7-
- Built-in UDP fallback uses an embedded, namespaced copy of **libpmquery**.
8-
- Non-blocking: both HTTP and UDP queries run on async workers.
9-
- Simple API for other plugins to trigger queries programmatically.
8+
- Async HTTP query (no main-thread blocking) with automatic UDP fallback.
9+
- Embedded, namespaced **libpmquery**.
10+
- Callback API for other plugins (no promises/futures needed).
1011

11-
## Installation
12-
1. Drop the plugin phar into your `plugins/` folder.
13-
2. Ensure `php` version matches PocketMine-MP 5.x (PHP 8.1+).
14-
15-
## Command
12+
## Command (plugin)
1613
- `/query <domain/ip[:port]> [port]`
1714
Example: `/query test.pmmp.io 19132`
1815

19-
## API Usage (for other plugins)
16+
## API Usage (virion)
2017
```php
2118
use NhanAZ\QueryServer\Main;
2219

2320
$api = Main::getInstance()->getApi();
2421
$api->query("test.pmmp.io", 19132, function(array $result): void {
2522
if (($result['ok'] ?? false) === true) {
26-
// $result['data'] contains either API status (object) or UDP fields (array)
23+
// $result['source'] === 'api' or 'udp'
24+
// $result['data'] : API status object (api) or UDP array (udp)
2725
} else {
28-
// handle $result['error']
26+
// $result['error']
2927
}
3028
});
3129
```
3230

31+
## Installation
32+
### As plugin
33+
1. Download the `QueryServer` phar.
34+
2. Drop into `plugins/` (PocketMine-MP 5.x / PHP 8.1+).
35+
36+
### As virion
37+
Add dependency in `.poggit.yml`:
38+
```yaml
39+
libs:
40+
- src: NhanAZ-Libraries/QueryServer/QueryServerCore
41+
version: ^0.0.3
42+
```
43+
3344
## License & Attribution
34-
- This plugin is licensed under **LGPL-3.0-or-later**.
35-
- Embedded library **libpmquery** (LGPL-3.0-or-later) by [jasonw4331/libpmquery](https://github.com/jasonw4331/libpmquery).
45+
- LGPL-3.0-or-later.
46+
- Bundled **libpmquery** (LGPL-3.0-or-later) by [jasonw4331/libpmquery](https://github.com/jasonw4331/libpmquery).

‎composer.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "nhanaz/queryserver",
3-
"description": "Query information of other Minecraft servers via HTTP API v3 with UDP fallback.",
2+
"name": "nhanaz-libraries/queryserver",
3+
"description": "QueryServerCore virion + QueryServer plugin: async HTTP (api.mcsrvstat.us v3) with UDP fallback.",
44
"type": "project",
55
"license": "LGPL-3.0-or-later",
66
"require": {

‎src/FallbackQueryTask.php‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ public function __construct(
1717
}
1818

1919
public function onRun(): void {
20-
require_once dirname(__DIR__) . "/src/libpmquery/PMQuery.php";
21-
require_once dirname(__DIR__) . "/src/libpmquery/PmQueryException.php";
22-
2320
$result = [
2421
"ok" => false,
2522
"host" => $this->host,

‎src/Main.php‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public static function handleQueryResult(mixed $result): void {
7777
$console = new ConsoleCommandSender(Server::getInstance(), Server::getInstance()->getLanguage());
7878

7979
$address = is_array($result) ? ($result["address"] ?? null) : null;
80+
$requestId = is_array($result) ? ($result["requestId"] ?? null) : null;
8081
[$host, $port] = self::splitAddress($address);
8182

8283
$apiShown = false;
@@ -89,17 +90,33 @@ public static function handleQueryResult(mixed $result): void {
8990
} else {
9091
$error = is_array($result) ? ($result["error"] ?? "Unknown error") : "No data returned";
9192
$console->sendMessage(self::info("Primary API failed: " . $error));
93+
94+
if ($requestId !== null && self::getInstance()->deliverCallback($requestId, ["ok" => false, "source" => "api", "error" => $error])) {
95+
// still allow fallback to run if available
96+
}
9297
}
9398

9499
if ($host !== null && $port !== null) {
95100
$console->sendMessage(self::info("Using fallback UDP query..."));
96-
Server::getInstance()->getAsyncPool()->submitTask(new FallbackQueryTask($host, $port));
101+
Server::getInstance()->getAsyncPool()->submitTask(new FallbackQueryTask($host, $port, requestId: $result["requestId"] ?? null));
97102
return;
98103
}
99104

100105
if (!$apiShown && !$fallbackShown) {
101106
$console->sendMessage(self::error("The server is offline or has blocked queries!"));
102107
$console->sendMessage(self::info("Try another query method using /query <domain> <port>"));
108+
109+
if ($requestId !== null) {
110+
self::getInstance()->deliverCallback($requestId, ["ok" => false, "source" => "api", "error" => "offline or blocked"]);
111+
}
112+
} elseif ($apiShown && $requestId !== null) {
113+
// deliver API success to caller (even if fallback scheduled elsewhere, which returns separately)
114+
self::getInstance()->deliverCallback($requestId, [
115+
"ok" => true,
116+
"source" => "api",
117+
"address" => $address,
118+
"data" => $result["data"] ?? null
119+
]);
103120
}
104121
}
105122

‎virion.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: QueryServerCore
2+
antigen: NhanAZ\\QueryServer
3+
api: 5.0.0
4+
version: 0.0.3
5+
authors:
6+
- NhanAZ
7+
description: Core library for async server querying (HTTP v3 + UDP fallback)
8+
license: LGPL-3.0-or-later

0 commit comments

Comments
 (0)