@minecraft/server-admin 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.
Files changed (3) hide show
  1. package/index.d.ts +81 -81
  2. package/package.json +1 -1
  3. package/tests.ts +142 -3
package/index.d.ts CHANGED
@@ -1,81 +1,81 @@
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
- * Contains types related to administering a Bedrock Dedicated
12
- * Server. These types allow for the configuration of variables
13
- * and secrets in JSON files in the Bedrock Dedicated Server
14
- * folder. These types cannot be used on Minecraft clients.
15
- *
16
- * Manifest Details
17
- * ```json
18
- * {
19
- * "module_name": "@minecraft/server-admin",
20
- * "version": "1.0.0-beta.00001b50"
21
- * }
22
- * ```
23
- *
24
- */
25
- /**
26
- * This represents a placeholder object that represents a
27
- * secret string. The contents of that string are not available
28
- * to script; this object is just a placeholder.
29
- */
30
- // tslint:disable-next-line:no-unnecessary-class
31
- export class SecretString {
32
- constructor(value: string);
33
- }
34
- /**
35
- * A collection of server secrets defined in dedicated server
36
- * configuration.
37
- */
38
- export class ServerSecrets {
39
- /**
40
- * A list of available, configured server secrets.
41
- */
42
- readonly names: string[];
43
- /**
44
- * @remarks
45
- * Returns a SecretString that is a placeholder for a secret
46
- * configured in a JSON file. In certain objects, like an
47
- * HttpHeader, this Secret is resolved at the time of execution
48
- * but is not made available to the script environment.
49
- * @param name
50
- */
51
- get(name: string): SecretString;
52
- protected constructor();
53
- }
54
- /**
55
- * A collection of server variables defined in dedicated server
56
- * configuration.
57
- */
58
- export class ServerVariables {
59
- /**
60
- * A list of available, configured server variables.
61
- */
62
- readonly names: string[];
63
- /**
64
- * @remarks
65
- * Returns the value of variable that has been configured in a
66
- * dedicated server configuration JSON file.
67
- * @param name
68
- */
69
- get(name: string): any;
70
- protected constructor();
71
- }
72
- /**
73
- * A globally available object that returns a list of
74
- * dedicated-server configured secrets.
75
- */
76
- export const secrets: ServerSecrets;
77
- /**
78
- * A globally available object that returns a list of
79
- * dedicated-server configured variables.
80
- */
81
- export const variables: ServerVariables;
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
+ * Contains types related to administering a Bedrock Dedicated
12
+ * Server. These types allow for the configuration of variables
13
+ * and secrets in JSON files in the Bedrock Dedicated Server
14
+ * folder. These types cannot be used on Minecraft clients.
15
+ *
16
+ * Manifest Details
17
+ * ```json
18
+ * {
19
+ * "module_name": "@minecraft/server-admin",
20
+ * "version": "1.0.0-beta.1.19.60-preview.23"
21
+ * }
22
+ * ```
23
+ *
24
+ */
25
+ /**
26
+ * This represents a placeholder object that represents a
27
+ * secret string. The contents of that string are not available
28
+ * to script; this object is just a placeholder.
29
+ */
30
+ // tslint:disable-next-line:no-unnecessary-class
31
+ export class SecretString {
32
+ constructor(value: string);
33
+ }
34
+ /**
35
+ * A collection of server secrets defined in dedicated server
36
+ * configuration.
37
+ */
38
+ export class ServerSecrets {
39
+ protected constructor();
40
+ /**
41
+ * A list of available, configured server secrets.
42
+ */
43
+ readonly names: string[];
44
+ /**
45
+ * @remarks
46
+ * Returns a SecretString that is a placeholder for a secret
47
+ * configured in a JSON file. In certain objects, like an
48
+ * HttpHeader, this Secret is resolved at the time of execution
49
+ * but is not made available to the script environment.
50
+ * @param name
51
+ */
52
+ get(name: string): SecretString;
53
+ }
54
+ /**
55
+ * A collection of server variables defined in dedicated server
56
+ * configuration.
57
+ */
58
+ export class ServerVariables {
59
+ protected constructor();
60
+ /**
61
+ * A list of available, configured server variables.
62
+ */
63
+ readonly names: string[];
64
+ /**
65
+ * @remarks
66
+ * Returns the value of variable that has been configured in a
67
+ * dedicated server configuration JSON file.
68
+ * @param name
69
+ */
70
+ get(name: string): any;
71
+ }
72
+ /**
73
+ * A globally available object that returns a list of
74
+ * dedicated-server configured secrets.
75
+ */
76
+ export const secrets: ServerSecrets;
77
+ /**
78
+ * A globally available object that returns a list of
79
+ * dedicated-server configured variables.
80
+ */
81
+ export const variables: ServerVariables;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minecraft/server-admin",
3
- "version": "1.0.0-beta.00001b50",
3
+ "version": "1.0.0-beta.1.19.60-preview.23",
4
4
  "description": "",
5
5
  "contributors": [
6
6
  {
package/tests.ts CHANGED
@@ -1,3 +1,142 @@
1
- import * as serverAdmin from '@minecraft/server-admin';
2
-
3
- serverAdmin.secrets.get('authtoken');
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ import * as mc from '@minecraft/server';
3
+
4
+ export async function getPlayerProfile(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
5
+ const serverUrl = mcsa.variables.get('serverEndpoint');
6
+
7
+ const req = new mcnet.HttpRequest(serverUrl + 'getPlayerProfile');
8
+
9
+ req.body = JSON.stringify({
10
+ playerId: 'johndoe',
11
+ });
12
+
13
+ req.method = mcnet.HttpRequestMethod.POST;
14
+ req.headers = [
15
+ new mcnet.HttpHeader('Content-Type', 'application/json'),
16
+ new mcnet.HttpHeader('auth', mcsa.secrets.get('authtoken')),
17
+ ];
18
+
19
+ await mcnet.http.request(req);
20
+ }
21
+
22
+ export default class SampleManager {
23
+ tickCount = 0;
24
+
25
+ _availableFuncs: {
26
+ [name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
27
+ };
28
+
29
+ pendingFuncs: Array<{
30
+ name: string;
31
+ func: (log: (message: string, status?: number) => void, location: mc.Location) => void;
32
+ location: mc.Location;
33
+ }> = [];
34
+
35
+ gameplayLogger(message: string, status?: number) {
36
+ if (status !== undefined && status > 0) {
37
+ message = 'SUCCESS: ' + message;
38
+ } else if (status !== undefined && status < 0) {
39
+ message = 'FAIL: ' + message;
40
+ }
41
+
42
+ this.say(message);
43
+ }
44
+ say(message: string) {
45
+ mc.world.getDimension('overworld').runCommand('say ' + message);
46
+ }
47
+
48
+ newChatMessage(chatEvent: mc.ChatEvent) {
49
+ const message = chatEvent.message.toLowerCase();
50
+
51
+ if (message.startsWith('howto') && chatEvent.sender) {
52
+ const nearbyBlock = chatEvent.sender.getBlockFromViewVector();
53
+ if (!nearbyBlock) {
54
+ this.gameplayLogger('Please look at the block where you want me to run this.');
55
+ return;
56
+ }
57
+
58
+ const nearbyBlockLoc = nearbyBlock.location;
59
+ const nearbyLoc = new mc.Location(nearbyBlockLoc.x, nearbyBlockLoc.y + 1, nearbyBlockLoc.z);
60
+
61
+ const sampleId = message.substring(5).trim();
62
+
63
+ if (sampleId.length < 2) {
64
+ let availableFuncStr = 'Here is my list of available samples:';
65
+
66
+ for (const sampleFuncKey in this._availableFuncs) {
67
+ availableFuncStr += ' ' + sampleFuncKey;
68
+ }
69
+
70
+ this.say(availableFuncStr);
71
+ } else {
72
+ for (const sampleFuncKey in this._availableFuncs) {
73
+ if (sampleFuncKey.toLowerCase() === sampleId) {
74
+ const sampleFunc = this._availableFuncs[sampleFuncKey];
75
+
76
+ this.runSample(sampleFuncKey + this.tickCount, sampleFunc, nearbyLoc);
77
+
78
+ return;
79
+ }
80
+ }
81
+
82
+ this.say(`I couldn't find the sample '${sampleId}"'`);
83
+ }
84
+ }
85
+ }
86
+
87
+ runSample(
88
+ sampleId: string,
89
+ snippetFunctions: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>,
90
+ targetLocation: mc.Location,
91
+ ) {
92
+ for (let i = snippetFunctions.length - 1; i >= 0; i--) {
93
+ this.pendingFuncs.push({ name: sampleId, func: snippetFunctions[i], location: targetLocation });
94
+ }
95
+ }
96
+
97
+ worldTick() {
98
+ if (this.tickCount % 10 === 0) {
99
+ if (this.pendingFuncs.length > 0) {
100
+ const funcSet = this.pendingFuncs.pop();
101
+
102
+ if (funcSet) {
103
+ funcSet.func(this.gameplayLogger, funcSet.location);
104
+ }
105
+ }
106
+ }
107
+
108
+ this.tickCount++;
109
+ }
110
+
111
+ constructor() {
112
+ this._availableFuncs = {};
113
+
114
+ this.gameplayLogger = this.gameplayLogger.bind(this);
115
+
116
+ mc.world.events.tick.subscribe(this.worldTick.bind(this));
117
+ mc.world.events.chat.subscribe(this.newChatMessage.bind(this));
118
+ }
119
+
120
+ registerSamples(sampleSet: {
121
+ [name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
122
+ }) {
123
+ for (const sampleKey in sampleSet) {
124
+ if (sampleKey.length > 1 && sampleSet[sampleKey]) {
125
+ this._availableFuncs[sampleKey] = sampleSet[sampleKey];
126
+ }
127
+ }
128
+ }
129
+ }
130
+
131
+ import * as mcsa from '@minecraft/server-admin';
132
+ import * as mcnet from '@minecraft/server-net'; // keep in for net samples
133
+
134
+ const mojangServerAdminTestFuncs: {
135
+ [name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
136
+ } = {
137
+ getPlayerProfile: [getPlayerProfile],
138
+ };
139
+
140
+ export function register(sampleManager: SampleManager) {
141
+ sampleManager.registerSamples(mojangServerAdminTestFuncs);
142
+ }