@minecraft/server-admin 1.0.0-beta.00001b50 → 1.0.0-beta.11940b24

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 +1 -1
  2. package/package.json +1 -1
  3. package/tests.ts +141 -2
package/index.d.ts CHANGED
@@ -17,7 +17,7 @@
17
17
  * ```json
18
18
  * {
19
19
  * "module_name": "@minecraft/server-admin",
20
- * "version": "1.0.0-beta.00001b50"
20
+ * "version": "1.0.0-beta.11940b24"
21
21
  * }
22
22
  * ```
23
23
  *
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.11940b24",
4
4
  "description": "",
5
5
  "contributors": [
6
6
  {
package/tests.ts CHANGED
@@ -1,3 +1,142 @@
1
- import * as serverAdmin from '@minecraft/server-admin';
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ import * as mc from '@minecraft/server';
2
3
 
3
- serverAdmin.secrets.get('authtoken');
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
+ }