@minecraft/server-gametest 1.0.0-beta.1.21.20-preview.21 → 1.0.0-beta.1.21.3-stable

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 +2 -2
  2. package/package.json +1 -1
  3. package/tests.ts +141 -0
package/index.d.ts CHANGED
@@ -17,7 +17,7 @@
17
17
  * ```json
18
18
  * {
19
19
  * "module_name": "@minecraft/server-gametest",
20
- * "version": "1.0.0-beta"
20
+ * "version": "1.0.0-internal.1.21.3-stable"
21
21
  * }
22
22
  * ```
23
23
  *
@@ -1747,7 +1747,7 @@ export class Test {
1747
1747
  * Location of the fluid container block.
1748
1748
  * @param type
1749
1749
  * Type of fluid to set. See {@link
1750
- * @minecraft/server.FluidType} for a list of values.
1750
+ * @minecraft/server-gametest.FluidType} for a list of values.
1751
1751
  * @throws This function can throw errors.
1752
1752
  *
1753
1753
  * {@link GameTestError}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minecraft/server-gametest",
3
- "version": "1.0.0-beta.1.21.20-preview.21",
3
+ "version": "1.0.0-beta.1.21.3-stable",
4
4
  "description": "",
5
5
  "contributors": [
6
6
  {
package/tests.ts ADDED
@@ -0,0 +1,141 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ import * as mc from '@minecraft/server';
3
+
4
+ export function simpleMobTest(log: (message: string, status?: number) => void, targetLocation: mc.Vector3) {
5
+ gt.register('StarterTests', 'simpleMobTest', (test: gt.Test) => {
6
+ const attackerId = 'fox';
7
+ const victimId = 'chicken';
8
+
9
+ test.spawn(attackerId, { x: 5, y: 2, z: 5 });
10
+ test.spawn(victimId, { x: 2, y: 2, z: 2 });
11
+
12
+ test.assertEntityPresentInArea(victimId, true);
13
+
14
+ test.succeedWhen(() => {
15
+ test.assertEntityPresentInArea(victimId, false);
16
+ });
17
+ })
18
+ .maxTicks(400)
19
+ .structureName('gametests:mediumglass');
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.Vector3) => void>;
27
+ };
28
+
29
+ pendingFuncs: Array<{
30
+ name: string;
31
+ func: (log: (message: string, status?: number) => void, location: mc.Vector3) => void;
32
+ location: mc.Vector3;
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: mc.Vector3 = { x: nearbyBlockLoc.x, y: nearbyBlockLoc.y + 1, z: 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.Vector3) => void>,
90
+ targetLocation: mc.Vector3,
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.Vector3) => 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 gt from '@minecraft/server-gametest'; // keep in for gametest samples
132
+
133
+ const mojangGameTestFuncs: {
134
+ [name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Vector3) => void>;
135
+ } = {
136
+ simpleMobTest: [simpleMobTest],
137
+ };
138
+
139
+ export function register(sampleManager: SampleManager) {
140
+ sampleManager.registerSamples(mojangGameTestFuncs);
141
+ }