@minecraft/server-net 0.0.1-beta

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/.eslintrc.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "env": {
3
+ "browser": true,
4
+ "es2021": true
5
+ },
6
+ "extends": [
7
+ "eslint:recommended",
8
+ "plugin:@typescript-eslint/recommended"
9
+ ],
10
+ "overrides": [
11
+ ],
12
+ "parser": "@typescript-eslint/parser",
13
+ "parserOptions": {
14
+ "ecmaVersion": "latest",
15
+ "sourceType": "module"
16
+ },
17
+ "plugins": [
18
+ "@typescript-eslint"
19
+ ],
20
+ "rules": {
21
+ "@typescript-eslint/no-explicit-any": "off"
22
+ }
23
+ }
package/index.d.ts ADDED
@@ -0,0 +1,63 @@
1
+ // Type definitions for Minecraft Bedrock Edition script APIs (experimental) 0.1
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
+ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6
+
7
+ /* *****************************************************************************
8
+ Copyright (c) Microsoft Corporation.
9
+ ***************************************************************************** */
10
+ /**
11
+ *
12
+ * Manifest Details
13
+ * ```json
14
+ * {
15
+ * "module_name": "@minecraft/server-net",
16
+ * "version": "1.0.0-beta"
17
+ * }
18
+ * ```
19
+ *
20
+ */
21
+ import * as minecraftserveradmin from '@minecraft/server-admin';
22
+ export enum HttpRequestMethod {
23
+ DELETE = 'DELETE',
24
+ GET = 'GET',
25
+ HEAD = 'HEAD',
26
+ POST = 'POST',
27
+ PUT = 'PUT',
28
+ }
29
+ export class HttpClient {
30
+ cancelAll(reason: string): void;
31
+ get(uri: string): Promise<HttpResponse>;
32
+ request(config: HttpRequest): Promise<HttpResponse>;
33
+ testOnly_fulfillRequest(requestId: number, headers: HttpHeader[], body: string, status: number): void;
34
+ testOnly_getRequests(): number[];
35
+ testOnly_rejectRequest(requestId: number, reason: string): void;
36
+ protected constructor();
37
+ }
38
+ export class HttpHeader {
39
+ key: string;
40
+ value: minecraftserveradmin.SecretString | string;
41
+ constructor(key: string, value: minecraftserveradmin.SecretString | string);
42
+ }
43
+ export class HttpRequest {
44
+ body: string;
45
+ headers: HttpHeader[];
46
+ method: HttpRequestMethod;
47
+ timeout: number;
48
+ uri: string;
49
+ addHeader(key: string, value: minecraftserveradmin.SecretString | string): HttpRequest;
50
+ constructor(uri: string);
51
+ setBody(body: string): HttpRequest;
52
+ setHeaders(headers: HttpHeader[]): HttpRequest;
53
+ setMethod(method: HttpRequestMethod): HttpRequest;
54
+ setTimeout(timeout: number): HttpRequest;
55
+ }
56
+ export class HttpResponse {
57
+ readonly body: string;
58
+ readonly headers: HttpHeader[];
59
+ readonly request: HttpRequest;
60
+ readonly status: number;
61
+ protected constructor();
62
+ }
63
+ export const http: HttpClient;
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@minecraft/server-net",
3
+ "version": "0.0.1-beta",
4
+ "description": "",
5
+ "contributors": [
6
+ {
7
+ "name": "Jake Shirley",
8
+ "email": "jake@xbox.com"
9
+ },
10
+ {
11
+ "name": "Mike Ammerlaan",
12
+ "email": "mikeam@microsoft.com"
13
+ }
14
+ ],
15
+ "license": "MIT",
16
+ "dependencies": {
17
+ "@minecraft/server-admin": "0.0.1-beta"
18
+ }
19
+ }
package/tests.ts ADDED
@@ -0,0 +1,127 @@
1
+ import * as mc from '@minecraft/server';
2
+ import * as mcnet from '@minecraft/server-net';
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
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "lib": ["es6"],
5
+ "target": "es6",
6
+ "forceConsistentCasingInFileNames": true,
7
+ "noEmit": true,
8
+ "noImplicitAny": true,
9
+ "noImplicitThis": true,
10
+ "strictFunctionTypes": true,
11
+ "strictNullChecks": true,
12
+ "baseUrl": "../",
13
+ "typeRoots": ["../"],
14
+ "types": []
15
+ },
16
+ "files": [
17
+ "index.d.ts",
18
+ "tests.ts"
19
+ ]
20
+ }