@minecraft/server-net 1.0.0-beta.00001b50 → 1.0.0-beta.1.19.60-preview.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +186 -175
- package/package.json +2 -2
- package/tests.ts +139 -127
package/index.d.ts
CHANGED
|
@@ -1,175 +1,186 @@
|
|
|
1
|
-
// Type definitions for Minecraft Bedrock Edition script APIs
|
|
2
|
-
// Project: https://docs.microsoft.com/minecraft/creator/
|
|
3
|
-
// Definitions by: Jake Shirley <https://github.com/JakeShirley>
|
|
4
|
-
// Mike Ammerlaan <https://github.com/mammerla>
|
|
5
|
-
|
|
6
|
-
/* *****************************************************************************
|
|
7
|
-
Copyright (c) Microsoft Corporation.
|
|
8
|
-
***************************************************************************** */
|
|
9
|
-
/**
|
|
10
|
-
* @packageDocumentation
|
|
11
|
-
* The `@minecraft/server-net` module contains types for
|
|
12
|
-
* executing HTTP-based requests. This module can only be used
|
|
13
|
-
* on Bedrock Dedicated Server.
|
|
14
|
-
*
|
|
15
|
-
* Manifest Details
|
|
16
|
-
* ```json
|
|
17
|
-
* {
|
|
18
|
-
* "module_name": "@minecraft/server-net",
|
|
19
|
-
* "version": "1.0.0-beta.
|
|
20
|
-
* }
|
|
21
|
-
* ```
|
|
22
|
-
*
|
|
23
|
-
*/
|
|
24
|
-
import * as minecraftserveradmin from '@minecraft/server-admin';
|
|
25
|
-
export enum HttpRequestMethod {
|
|
26
|
-
/**
|
|
27
|
-
* Represents the method for an HTTP HEAD request. HEAD
|
|
28
|
-
* requests are similar to a GET request, but are commonly used
|
|
29
|
-
* to retrieve just the HTTP response headers from the
|
|
30
|
-
* specified URI, and not the body contents.
|
|
31
|
-
*/
|
|
32
|
-
DELETE = 'DELETE',
|
|
33
|
-
/**
|
|
34
|
-
* Represents the method for an HTTP PUT request. POST requests
|
|
35
|
-
* are commonly used to create a new resource that is a
|
|
36
|
-
* subordinate of the specified URI.
|
|
37
|
-
*/
|
|
38
|
-
GET = 'GET',
|
|
39
|
-
/**
|
|
40
|
-
* Represents the method for an HTTP PUT request. GET requests
|
|
41
|
-
* are commonly used to retrieve information about a resource
|
|
42
|
-
* at the specified URI.
|
|
43
|
-
*/
|
|
44
|
-
HEAD = 'HEAD',
|
|
45
|
-
/**
|
|
46
|
-
* Represents the method for an HTTP PUT request. GET requests
|
|
47
|
-
* are commonly used to retrieve information about a resource
|
|
48
|
-
* at the specified URI.
|
|
49
|
-
*/
|
|
50
|
-
POST = 'POST',
|
|
51
|
-
/**
|
|
52
|
-
* Represents the method for an HTTP PUT request. PUT requests
|
|
53
|
-
* are commonly used to update a single resource that already
|
|
54
|
-
* exists in a resource collection.
|
|
55
|
-
*/
|
|
56
|
-
PUT = 'PUT',
|
|
57
|
-
}
|
|
58
|
-
export class HttpClient {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Represents an HTTP header - a key/value pair of
|
|
91
|
-
* meta-information about a request.
|
|
92
|
-
*/
|
|
93
|
-
export class HttpHeader {
|
|
94
|
-
/**
|
|
95
|
-
* Key of the HTTP header.
|
|
96
|
-
*/
|
|
97
|
-
key: string;
|
|
98
|
-
/**
|
|
99
|
-
* Value of the HTTP header.
|
|
100
|
-
*/
|
|
101
|
-
value: minecraftserveradmin.SecretString | string;
|
|
102
|
-
constructor(key: string, value: minecraftserveradmin.SecretString | string);
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Main object for structuring a request.
|
|
106
|
-
*/
|
|
107
|
-
export class HttpRequest {
|
|
108
|
-
/**
|
|
109
|
-
* Content of the body of the HTTP request.
|
|
110
|
-
*/
|
|
111
|
-
body: string;
|
|
112
|
-
/**
|
|
113
|
-
* A collection of HTTP headers to add to the outbound request.
|
|
114
|
-
*/
|
|
115
|
-
headers: HttpHeader[];
|
|
116
|
-
/**
|
|
117
|
-
* HTTP method (e.g., GET or PUT or PATCH) to use for making
|
|
118
|
-
* the request.
|
|
119
|
-
*/
|
|
120
|
-
method: HttpRequestMethod;
|
|
121
|
-
/**
|
|
122
|
-
* Amount of time, in seconds, before the request times out and
|
|
123
|
-
* is abandoned.
|
|
124
|
-
*/
|
|
125
|
-
timeout: number;
|
|
126
|
-
/**
|
|
127
|
-
* The HTTP resource to access.
|
|
128
|
-
*/
|
|
129
|
-
uri: string;
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
* @param
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
*
|
|
142
|
-
*
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
*
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
*
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
1
|
+
// Type definitions for Minecraft Bedrock Edition script APIs
|
|
2
|
+
// Project: https://docs.microsoft.com/minecraft/creator/
|
|
3
|
+
// Definitions by: Jake Shirley <https://github.com/JakeShirley>
|
|
4
|
+
// Mike Ammerlaan <https://github.com/mammerla>
|
|
5
|
+
|
|
6
|
+
/* *****************************************************************************
|
|
7
|
+
Copyright (c) Microsoft Corporation.
|
|
8
|
+
***************************************************************************** */
|
|
9
|
+
/**
|
|
10
|
+
* @packageDocumentation
|
|
11
|
+
* The `@minecraft/server-net` module contains types for
|
|
12
|
+
* executing HTTP-based requests. This module can only be used
|
|
13
|
+
* on Bedrock Dedicated Server.
|
|
14
|
+
*
|
|
15
|
+
* Manifest Details
|
|
16
|
+
* ```json
|
|
17
|
+
* {
|
|
18
|
+
* "module_name": "@minecraft/server-net",
|
|
19
|
+
* "version": "1.0.0-beta.1.19.60-preview.23"
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
import * as minecraftserveradmin from '@minecraft/server-admin';
|
|
25
|
+
export enum HttpRequestMethod {
|
|
26
|
+
/**
|
|
27
|
+
* Represents the method for an HTTP HEAD request. HEAD
|
|
28
|
+
* requests are similar to a GET request, but are commonly used
|
|
29
|
+
* to retrieve just the HTTP response headers from the
|
|
30
|
+
* specified URI, and not the body contents.
|
|
31
|
+
*/
|
|
32
|
+
DELETE = 'DELETE',
|
|
33
|
+
/**
|
|
34
|
+
* Represents the method for an HTTP PUT request. POST requests
|
|
35
|
+
* are commonly used to create a new resource that is a
|
|
36
|
+
* subordinate of the specified URI.
|
|
37
|
+
*/
|
|
38
|
+
GET = 'GET',
|
|
39
|
+
/**
|
|
40
|
+
* Represents the method for an HTTP PUT request. GET requests
|
|
41
|
+
* are commonly used to retrieve information about a resource
|
|
42
|
+
* at the specified URI.
|
|
43
|
+
*/
|
|
44
|
+
HEAD = 'HEAD',
|
|
45
|
+
/**
|
|
46
|
+
* Represents the method for an HTTP PUT request. GET requests
|
|
47
|
+
* are commonly used to retrieve information about a resource
|
|
48
|
+
* at the specified URI.
|
|
49
|
+
*/
|
|
50
|
+
POST = 'POST',
|
|
51
|
+
/**
|
|
52
|
+
* Represents the method for an HTTP PUT request. PUT requests
|
|
53
|
+
* are commonly used to update a single resource that already
|
|
54
|
+
* exists in a resource collection.
|
|
55
|
+
*/
|
|
56
|
+
PUT = 'PUT',
|
|
57
|
+
}
|
|
58
|
+
export class HttpClient {
|
|
59
|
+
protected constructor();
|
|
60
|
+
/**
|
|
61
|
+
* @remarks
|
|
62
|
+
* Cancels all pending requests.
|
|
63
|
+
* @param reason
|
|
64
|
+
*/
|
|
65
|
+
cancelAll(reason: string): void;
|
|
66
|
+
/**
|
|
67
|
+
* @remarks
|
|
68
|
+
* Performs a simple HTTP get request.
|
|
69
|
+
* @param uri
|
|
70
|
+
* URL to make an HTTP Request to.
|
|
71
|
+
* @returns
|
|
72
|
+
* An awaitable promise that contains the HTTP response.
|
|
73
|
+
*/
|
|
74
|
+
get(uri: string): Promise<HttpResponse>;
|
|
75
|
+
/**
|
|
76
|
+
* @remarks
|
|
77
|
+
* Performs an HTTP request.
|
|
78
|
+
* @param config
|
|
79
|
+
* Contains an HTTP Request object with configuration data on
|
|
80
|
+
* the HTTP request.
|
|
81
|
+
* @returns
|
|
82
|
+
* An awaitable promise that contains the HTTP response.
|
|
83
|
+
*/
|
|
84
|
+
request(config: HttpRequest): Promise<HttpResponse>;
|
|
85
|
+
testOnly_fulfillRequest(requestId: number, headers: HttpHeader[], body: string, status: number): void;
|
|
86
|
+
testOnly_getRequests(): number[];
|
|
87
|
+
testOnly_rejectRequest(requestId: number, reason: string): void;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Represents an HTTP header - a key/value pair of
|
|
91
|
+
* meta-information about a request.
|
|
92
|
+
*/
|
|
93
|
+
export class HttpHeader {
|
|
94
|
+
/**
|
|
95
|
+
* Key of the HTTP header.
|
|
96
|
+
*/
|
|
97
|
+
key: string;
|
|
98
|
+
/**
|
|
99
|
+
* Value of the HTTP header.
|
|
100
|
+
*/
|
|
101
|
+
value: minecraftserveradmin.SecretString | string;
|
|
102
|
+
constructor(key: string, value: minecraftserveradmin.SecretString | string);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Main object for structuring a request.
|
|
106
|
+
*/
|
|
107
|
+
export class HttpRequest {
|
|
108
|
+
/**
|
|
109
|
+
* Content of the body of the HTTP request.
|
|
110
|
+
*/
|
|
111
|
+
body: string;
|
|
112
|
+
/**
|
|
113
|
+
* A collection of HTTP headers to add to the outbound request.
|
|
114
|
+
*/
|
|
115
|
+
headers: HttpHeader[];
|
|
116
|
+
/**
|
|
117
|
+
* HTTP method (e.g., GET or PUT or PATCH) to use for making
|
|
118
|
+
* the request.
|
|
119
|
+
*/
|
|
120
|
+
method: HttpRequestMethod;
|
|
121
|
+
/**
|
|
122
|
+
* Amount of time, in seconds, before the request times out and
|
|
123
|
+
* is abandoned.
|
|
124
|
+
*/
|
|
125
|
+
timeout: number;
|
|
126
|
+
/**
|
|
127
|
+
* The HTTP resource to access.
|
|
128
|
+
*/
|
|
129
|
+
uri: string;
|
|
130
|
+
constructor(uri: string);
|
|
131
|
+
/**
|
|
132
|
+
* @remarks
|
|
133
|
+
* Adds an additional header to the overall list of headers
|
|
134
|
+
* used in the corresponding HTTP request.
|
|
135
|
+
* @param key
|
|
136
|
+
* @param value
|
|
137
|
+
*/
|
|
138
|
+
addHeader(key: string, value: minecraftserveradmin.SecretString | string): HttpRequest;
|
|
139
|
+
/**
|
|
140
|
+
* @remarks
|
|
141
|
+
* Updates the content of the body of the HTTP request.
|
|
142
|
+
* @param body
|
|
143
|
+
*/
|
|
144
|
+
setBody(body: string): HttpRequest;
|
|
145
|
+
/**
|
|
146
|
+
* @remarks
|
|
147
|
+
* Replaces and applies a set of HTTP Headers for the request.
|
|
148
|
+
* @param headers
|
|
149
|
+
*/
|
|
150
|
+
setHeaders(headers: HttpHeader[]): HttpRequest;
|
|
151
|
+
/**
|
|
152
|
+
* @remarks
|
|
153
|
+
* Sets the desired HTTP method (e.g., GET or PUT or PATCH) to
|
|
154
|
+
* use for making the request.
|
|
155
|
+
* @param method
|
|
156
|
+
*/
|
|
157
|
+
setMethod(method: HttpRequestMethod): HttpRequest;
|
|
158
|
+
setTimeout(timeout: number): HttpRequest;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Main object that contains result information from a request.
|
|
162
|
+
*/
|
|
163
|
+
export class HttpResponse {
|
|
164
|
+
protected constructor();
|
|
165
|
+
/**
|
|
166
|
+
* Body content of the HTTP response.
|
|
167
|
+
*/
|
|
168
|
+
readonly body: string;
|
|
169
|
+
/**
|
|
170
|
+
* A collection of HTTP response headers returned from the
|
|
171
|
+
* request.
|
|
172
|
+
*/
|
|
173
|
+
readonly headers: HttpHeader[];
|
|
174
|
+
/**
|
|
175
|
+
* Information that was used to formulate the HTTP response
|
|
176
|
+
* that this object represents.
|
|
177
|
+
*/
|
|
178
|
+
readonly request: HttpRequest;
|
|
179
|
+
/**
|
|
180
|
+
* HTTP response code for the request. For example, 404
|
|
181
|
+
* represents resource not found, and 500 represents an
|
|
182
|
+
* internal server error.
|
|
183
|
+
*/
|
|
184
|
+
readonly status: number;
|
|
185
|
+
}
|
|
186
|
+
export const http: HttpClient;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@minecraft/server-net",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.1.19.60-preview.23",
|
|
4
4
|
"description": "",
|
|
5
5
|
"contributors": [
|
|
6
6
|
{
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
}
|
|
14
14
|
],
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@minecraft/server-admin": "1.0.0-beta.
|
|
16
|
+
"@minecraft/server-admin": "1.0.0-beta.1.19.60-preview.23"
|
|
17
17
|
},
|
|
18
18
|
"license": "MIT"
|
|
19
19
|
}
|
package/tests.ts
CHANGED
|
@@ -1,127 +1,139 @@
|
|
|
1
|
-
|
|
2
|
-
import * as
|
|
3
|
-
|
|
4
|
-
export async function updateScore() {
|
|
5
|
-
const req = new mcnet.HttpRequest('http://localhost:3000/updateScore');
|
|
6
|
-
|
|
7
|
-
req.body = JSON.stringify({
|
|
8
|
-
score: 22,
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
req.method = mcnet.HttpRequestMethod.POST;
|
|
12
|
-
req.headers = [
|
|
13
|
-
new mcnet.HttpHeader('Content-Type', 'application/json'),
|
|
14
|
-
new mcnet.HttpHeader('auth', 'my-auth-token'),
|
|
15
|
-
];
|
|
16
|
-
|
|
17
|
-
await mcnet.http.request(req);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export default class SampleManager {
|
|
21
|
-
tickCount = 0;
|
|
22
|
-
|
|
23
|
-
_availableFuncs: {
|
|
24
|
-
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
pendingFuncs: Array<{
|
|
28
|
-
name: string;
|
|
29
|
-
func: (log: (message: string, status?: number) => void, location: mc.Location) => void;
|
|
30
|
-
location: mc.Location;
|
|
31
|
-
}> = [];
|
|
32
|
-
|
|
33
|
-
gameplayLogger(message: string, status?: number) {
|
|
34
|
-
if (status !== undefined && status > 0) {
|
|
35
|
-
message = 'SUCCESS: ' + message;
|
|
36
|
-
} else if (status !== undefined && status < 0) {
|
|
37
|
-
message = 'FAIL: ' + message;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
this.say(message);
|
|
41
|
-
}
|
|
42
|
-
say(message: string) {
|
|
43
|
-
mc.world.getDimension('overworld').runCommand('say ' + message);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
newChatMessage(chatEvent: mc.ChatEvent) {
|
|
47
|
-
const message = chatEvent.message.toLowerCase();
|
|
48
|
-
|
|
49
|
-
if (message.startsWith('howto') && chatEvent.sender) {
|
|
50
|
-
const nearbyBlock = chatEvent.sender.getBlockFromViewVector();
|
|
51
|
-
if (!nearbyBlock) {
|
|
52
|
-
this.gameplayLogger('Please look at the block where you want me to run this.');
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const nearbyBlockLoc = nearbyBlock.location;
|
|
57
|
-
const nearbyLoc = new mc.Location(nearbyBlockLoc.x, nearbyBlockLoc.y + 1, nearbyBlockLoc.z);
|
|
58
|
-
|
|
59
|
-
const sampleId = message.substring(5).trim();
|
|
60
|
-
|
|
61
|
-
if (sampleId.length < 2) {
|
|
62
|
-
let availableFuncStr = 'Here is my list of available samples:';
|
|
63
|
-
|
|
64
|
-
for (const sampleFuncKey in this._availableFuncs) {
|
|
65
|
-
availableFuncStr += ' ' + sampleFuncKey;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
this.say(availableFuncStr);
|
|
69
|
-
} else {
|
|
70
|
-
for (const sampleFuncKey in this._availableFuncs) {
|
|
71
|
-
if (sampleFuncKey.toLowerCase() === sampleId) {
|
|
72
|
-
const sampleFunc = this._availableFuncs[sampleFuncKey];
|
|
73
|
-
|
|
74
|
-
this.runSample(sampleFuncKey + this.tickCount, sampleFunc, nearbyLoc);
|
|
75
|
-
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
this.say(`I couldn't find the sample '${sampleId}"'`);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
runSample(
|
|
86
|
-
sampleId: string,
|
|
87
|
-
snippetFunctions: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>,
|
|
88
|
-
targetLocation: mc.Location,
|
|
89
|
-
) {
|
|
90
|
-
for (let i = snippetFunctions.length - 1; i >= 0; i--) {
|
|
91
|
-
this.pendingFuncs.push({ name: sampleId, func: snippetFunctions[i], location: targetLocation });
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
worldTick() {
|
|
96
|
-
if (this.tickCount % 10 === 0) {
|
|
97
|
-
if (this.pendingFuncs.length > 0) {
|
|
98
|
-
const funcSet = this.pendingFuncs.pop();
|
|
99
|
-
|
|
100
|
-
if (funcSet) {
|
|
101
|
-
funcSet.func(this.gameplayLogger, funcSet.location);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
this.tickCount++;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
constructor() {
|
|
110
|
-
this._availableFuncs = {};
|
|
111
|
-
|
|
112
|
-
this.gameplayLogger = this.gameplayLogger.bind(this);
|
|
113
|
-
|
|
114
|
-
mc.world.events.tick.subscribe(this.worldTick.bind(this));
|
|
115
|
-
mc.world.events.chat.subscribe(this.newChatMessage.bind(this));
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
registerSamples(sampleSet: {
|
|
119
|
-
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
120
|
-
}) {
|
|
121
|
-
for (const sampleKey in sampleSet) {
|
|
122
|
-
if (sampleKey.length > 1 && sampleSet[sampleKey]) {
|
|
123
|
-
this._availableFuncs[sampleKey] = sampleSet[sampleKey];
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import * as mc from '@minecraft/server';
|
|
3
|
+
|
|
4
|
+
export async function updateScore(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
5
|
+
const req = new mcnet.HttpRequest('http://localhost:3000/updateScore');
|
|
6
|
+
|
|
7
|
+
req.body = JSON.stringify({
|
|
8
|
+
score: 22,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
req.method = mcnet.HttpRequestMethod.POST;
|
|
12
|
+
req.headers = [
|
|
13
|
+
new mcnet.HttpHeader('Content-Type', 'application/json'),
|
|
14
|
+
new mcnet.HttpHeader('auth', 'my-auth-token'),
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
await mcnet.http.request(req);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default class SampleManager {
|
|
21
|
+
tickCount = 0;
|
|
22
|
+
|
|
23
|
+
_availableFuncs: {
|
|
24
|
+
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
pendingFuncs: Array<{
|
|
28
|
+
name: string;
|
|
29
|
+
func: (log: (message: string, status?: number) => void, location: mc.Location) => void;
|
|
30
|
+
location: mc.Location;
|
|
31
|
+
}> = [];
|
|
32
|
+
|
|
33
|
+
gameplayLogger(message: string, status?: number) {
|
|
34
|
+
if (status !== undefined && status > 0) {
|
|
35
|
+
message = 'SUCCESS: ' + message;
|
|
36
|
+
} else if (status !== undefined && status < 0) {
|
|
37
|
+
message = 'FAIL: ' + message;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.say(message);
|
|
41
|
+
}
|
|
42
|
+
say(message: string) {
|
|
43
|
+
mc.world.getDimension('overworld').runCommand('say ' + message);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
newChatMessage(chatEvent: mc.ChatEvent) {
|
|
47
|
+
const message = chatEvent.message.toLowerCase();
|
|
48
|
+
|
|
49
|
+
if (message.startsWith('howto') && chatEvent.sender) {
|
|
50
|
+
const nearbyBlock = chatEvent.sender.getBlockFromViewVector();
|
|
51
|
+
if (!nearbyBlock) {
|
|
52
|
+
this.gameplayLogger('Please look at the block where you want me to run this.');
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const nearbyBlockLoc = nearbyBlock.location;
|
|
57
|
+
const nearbyLoc = new mc.Location(nearbyBlockLoc.x, nearbyBlockLoc.y + 1, nearbyBlockLoc.z);
|
|
58
|
+
|
|
59
|
+
const sampleId = message.substring(5).trim();
|
|
60
|
+
|
|
61
|
+
if (sampleId.length < 2) {
|
|
62
|
+
let availableFuncStr = 'Here is my list of available samples:';
|
|
63
|
+
|
|
64
|
+
for (const sampleFuncKey in this._availableFuncs) {
|
|
65
|
+
availableFuncStr += ' ' + sampleFuncKey;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
this.say(availableFuncStr);
|
|
69
|
+
} else {
|
|
70
|
+
for (const sampleFuncKey in this._availableFuncs) {
|
|
71
|
+
if (sampleFuncKey.toLowerCase() === sampleId) {
|
|
72
|
+
const sampleFunc = this._availableFuncs[sampleFuncKey];
|
|
73
|
+
|
|
74
|
+
this.runSample(sampleFuncKey + this.tickCount, sampleFunc, nearbyLoc);
|
|
75
|
+
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
this.say(`I couldn't find the sample '${sampleId}"'`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
runSample(
|
|
86
|
+
sampleId: string,
|
|
87
|
+
snippetFunctions: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>,
|
|
88
|
+
targetLocation: mc.Location,
|
|
89
|
+
) {
|
|
90
|
+
for (let i = snippetFunctions.length - 1; i >= 0; i--) {
|
|
91
|
+
this.pendingFuncs.push({ name: sampleId, func: snippetFunctions[i], location: targetLocation });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
worldTick() {
|
|
96
|
+
if (this.tickCount % 10 === 0) {
|
|
97
|
+
if (this.pendingFuncs.length > 0) {
|
|
98
|
+
const funcSet = this.pendingFuncs.pop();
|
|
99
|
+
|
|
100
|
+
if (funcSet) {
|
|
101
|
+
funcSet.func(this.gameplayLogger, funcSet.location);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this.tickCount++;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
constructor() {
|
|
110
|
+
this._availableFuncs = {};
|
|
111
|
+
|
|
112
|
+
this.gameplayLogger = this.gameplayLogger.bind(this);
|
|
113
|
+
|
|
114
|
+
mc.world.events.tick.subscribe(this.worldTick.bind(this));
|
|
115
|
+
mc.world.events.chat.subscribe(this.newChatMessage.bind(this));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
registerSamples(sampleSet: {
|
|
119
|
+
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
120
|
+
}) {
|
|
121
|
+
for (const sampleKey in sampleSet) {
|
|
122
|
+
if (sampleKey.length > 1 && sampleSet[sampleKey]) {
|
|
123
|
+
this._availableFuncs[sampleKey] = sampleSet[sampleKey];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
import * as mcnet from '@minecraft/server-net'; // keep in for net samples
|
|
130
|
+
|
|
131
|
+
const mojangNetAdminTestFuncs: {
|
|
132
|
+
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
133
|
+
} = {
|
|
134
|
+
updateScore: [updateScore],
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export function register(sampleManager: SampleManager) {
|
|
138
|
+
sampleManager.registerSamples(mojangNetAdminTestFuncs);
|
|
139
|
+
}
|