@minecraft/server 1.0.0-preview.1.19.60.22 → 1.0.0-rc.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 +220 -213
- package/package.json +1 -1
- package/tests.ts +286 -286
package/index.d.ts
CHANGED
|
@@ -1,213 +1,220 @@
|
|
|
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 many types related to manipulating a Minecraft
|
|
12
|
-
* world, including entities, blocks, dimensions, and more.
|
|
13
|
-
*
|
|
14
|
-
* Manifest Details
|
|
15
|
-
* ```json
|
|
16
|
-
* {
|
|
17
|
-
* "module_name": "@minecraft/server",
|
|
18
|
-
* "version": "1.1.0-internal.
|
|
19
|
-
* }
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
*/
|
|
23
|
-
/**
|
|
24
|
-
* Contains return data on the result of a command execution.
|
|
25
|
-
*/
|
|
26
|
-
export class CommandResult {
|
|
27
|
-
protected constructor();
|
|
28
|
-
/**
|
|
29
|
-
* If the command operates against a number of entities,
|
|
30
|
-
* blocks, or items, this returns the number of successful
|
|
31
|
-
* applications of this command.
|
|
32
|
-
*/
|
|
33
|
-
readonly successCount: number;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* A class that represents a particular dimension (e.g., The
|
|
37
|
-
* End) within a world.
|
|
38
|
-
*/
|
|
39
|
-
export class Dimension {
|
|
40
|
-
protected constructor();
|
|
41
|
-
/**
|
|
42
|
-
* Identifier of the dimension.
|
|
43
|
-
* @throws This property can throw when used.
|
|
44
|
-
*/
|
|
45
|
-
readonly id: string;
|
|
46
|
-
/**
|
|
47
|
-
* @remarks
|
|
48
|
-
* Runs a particular command asynchronously from the context of
|
|
49
|
-
* the broader dimension. Note that there is a maximum queue
|
|
50
|
-
* of 128 asynchronous commands that can be run in a given
|
|
51
|
-
* tick.
|
|
52
|
-
* @param commandString
|
|
53
|
-
* Command to run. Note that command strings should not start
|
|
54
|
-
* with slash.
|
|
55
|
-
* @returns
|
|
56
|
-
* For commands that return data, returns a CommandResult with
|
|
57
|
-
* an indicator of command results.
|
|
58
|
-
* @throws This function can throw errors.
|
|
59
|
-
*/
|
|
60
|
-
runCommandAsync(commandString: string): Promise<CommandResult>;
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Represents the state of an entity (a mob, the player, or
|
|
64
|
-
* other moving objects like minecarts) in the world.
|
|
65
|
-
*/
|
|
66
|
-
export class Entity {
|
|
67
|
-
protected constructor();
|
|
68
|
-
/**
|
|
69
|
-
* Unique identifier of the entity. This identifier is intended
|
|
70
|
-
* to be consistent across loads of a world instance. No
|
|
71
|
-
* meaning should be inferred from the value and structure of
|
|
72
|
-
* this unique identifier - do not parse or interpret it.
|
|
73
|
-
* @throws This property can throw when used.
|
|
74
|
-
*/
|
|
75
|
-
readonly id: string;
|
|
76
|
-
/**
|
|
77
|
-
* Unique identifier of the type of the entity - for example,
|
|
78
|
-
* 'minecraft:skeleton'.
|
|
79
|
-
* @throws This property can throw when used.
|
|
80
|
-
*/
|
|
81
|
-
readonly typeId: string;
|
|
82
|
-
/**
|
|
83
|
-
* @remarks
|
|
84
|
-
* Runs a particular command asynchronously from the context of
|
|
85
|
-
* this entity. Note that there is a maximum queue of 128
|
|
86
|
-
* asynchronous commands that can be run in a given tick.
|
|
87
|
-
* @param commandString
|
|
88
|
-
* Command to run. Note that command strings should not start
|
|
89
|
-
* with slash.
|
|
90
|
-
* @returns
|
|
91
|
-
* For commands that return data, returns a JSON structure with
|
|
92
|
-
* command response values.
|
|
93
|
-
* @throws This function can throw errors.
|
|
94
|
-
*/
|
|
95
|
-
runCommandAsync(commandString: string): Promise<CommandResult>;
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* A collection of default Minecraft dimension types.
|
|
99
|
-
*/
|
|
100
|
-
// tslint:disable-next-line:no-unnecessary-class
|
|
101
|
-
export class MinecraftDimensionTypes {
|
|
102
|
-
protected constructor();
|
|
103
|
-
/**
|
|
104
|
-
* The Nether is a collection of biomes separate from the
|
|
105
|
-
* Overworld, including Soul Sand Valleys and Crimson forests.
|
|
106
|
-
* Nether fortresses contain exclusive resources. Mobs such as
|
|
107
|
-
* Blaze, Hoglins, Piglins, and Ghasts congregate here.
|
|
108
|
-
*/
|
|
109
|
-
static readonly nether = 'minecraft:nether';
|
|
110
|
-
/**
|
|
111
|
-
* The overworld is a collection of biomes, including forests,
|
|
112
|
-
* plains, jungles, mountains, deserts, taiga, and more. This
|
|
113
|
-
* is the default starter dimension for Minecraft. Mobs such as
|
|
114
|
-
* Axolotl, Cows, Creepers, and Zombies congregate here.
|
|
115
|
-
*/
|
|
116
|
-
static readonly overworld = 'minecraft:overworld';
|
|
117
|
-
/**
|
|
118
|
-
* The End is separate from the Overworld and the Nether and is
|
|
119
|
-
* generated whenever you create an End portal. Here, a giant
|
|
120
|
-
* center island is surrounded by several smaller areas and
|
|
121
|
-
* islands. You can find Endermen here. End midlands are larger
|
|
122
|
-
* areas that transition you from the center to the outer edges
|
|
123
|
-
* of the End. They contain Shulkers, Endermen, End gateway
|
|
124
|
-
* portals, and End cities. End gateway portals are commonly
|
|
125
|
-
* found at the outermost edge of the void. You usually find
|
|
126
|
-
* End barrens toward the edges of the main areas or land in
|
|
127
|
-
* the End.
|
|
128
|
-
*/
|
|
129
|
-
static readonly theEnd = 'minecraft:the_end';
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Represents a player within the world.
|
|
133
|
-
*/
|
|
134
|
-
export class Player extends Entity {
|
|
135
|
-
protected constructor();
|
|
136
|
-
/**
|
|
137
|
-
* Unique identifier of the player. This identifier is intended
|
|
138
|
-
* to be consistent across loads of a world instance. No
|
|
139
|
-
* meaning should be inferred from the value and structure of
|
|
140
|
-
* this unique identifier - do not parse or interpret it.
|
|
141
|
-
* @throws This property can throw when used.
|
|
142
|
-
*/
|
|
143
|
-
readonly id: string;
|
|
144
|
-
/**
|
|
145
|
-
* Name of the player.
|
|
146
|
-
* @throws This property can throw when used.
|
|
147
|
-
*/
|
|
148
|
-
readonly name: string;
|
|
149
|
-
/**
|
|
150
|
-
* Unique identifier of the type of the entity - for example,
|
|
151
|
-
* 'minecraft:player'.
|
|
152
|
-
* @throws This property can throw when used.
|
|
153
|
-
*/
|
|
154
|
-
readonly typeId: string;
|
|
155
|
-
/**
|
|
156
|
-
* @remarks
|
|
157
|
-
* Runs a particular command asynchronously from the context of
|
|
158
|
-
* this entity. Note that there is a maximum queue of 128
|
|
159
|
-
* asynchronous commands that can be run in a given tick.
|
|
160
|
-
* @param commandString
|
|
161
|
-
* Command to run. Note that command strings should not start
|
|
162
|
-
* with slash.
|
|
163
|
-
* @returns
|
|
164
|
-
* For commands that return data, returns a JSON structure with
|
|
165
|
-
* command response values.
|
|
166
|
-
* @throws This function can throw errors.
|
|
167
|
-
*/
|
|
168
|
-
runCommandAsync(commandString: string): Promise<CommandResult>;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* A class that provides system-level events and functions.
|
|
172
|
-
*/
|
|
173
|
-
export class System {
|
|
174
|
-
protected constructor();
|
|
175
|
-
/**
|
|
176
|
-
* @remarks
|
|
177
|
-
* Runs a specified function at a future time. This is
|
|
178
|
-
* frequently used to implement delayed behaviors and game
|
|
179
|
-
* loops.
|
|
180
|
-
* @param callback
|
|
181
|
-
* Function callback to run when the tickDelay time criteria is
|
|
182
|
-
* met.
|
|
183
|
-
* @returns
|
|
184
|
-
* An opaque identifier that can be used with the `clearRun`
|
|
185
|
-
* function to cancel the execution of this run.
|
|
186
|
-
*/
|
|
187
|
-
run(callback: () => void): number;
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* A class that wraps the state of a world - a set of
|
|
191
|
-
* dimensions and the environment of Minecraft.
|
|
192
|
-
*/
|
|
193
|
-
export class World {
|
|
194
|
-
protected constructor();
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
*
|
|
198
|
-
* @
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
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 many types related to manipulating a Minecraft
|
|
12
|
+
* world, including entities, blocks, dimensions, and more.
|
|
13
|
+
*
|
|
14
|
+
* Manifest Details
|
|
15
|
+
* ```json
|
|
16
|
+
* {
|
|
17
|
+
* "module_name": "@minecraft/server",
|
|
18
|
+
* "version": "1.1.0-internal.1.19.60-preview.23"
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Contains return data on the result of a command execution.
|
|
25
|
+
*/
|
|
26
|
+
export class CommandResult {
|
|
27
|
+
protected constructor();
|
|
28
|
+
/**
|
|
29
|
+
* If the command operates against a number of entities,
|
|
30
|
+
* blocks, or items, this returns the number of successful
|
|
31
|
+
* applications of this command.
|
|
32
|
+
*/
|
|
33
|
+
readonly successCount: number;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A class that represents a particular dimension (e.g., The
|
|
37
|
+
* End) within a world.
|
|
38
|
+
*/
|
|
39
|
+
export class Dimension {
|
|
40
|
+
protected constructor();
|
|
41
|
+
/**
|
|
42
|
+
* Identifier of the dimension.
|
|
43
|
+
* @throws This property can throw when used.
|
|
44
|
+
*/
|
|
45
|
+
readonly id: string;
|
|
46
|
+
/**
|
|
47
|
+
* @remarks
|
|
48
|
+
* Runs a particular command asynchronously from the context of
|
|
49
|
+
* the broader dimension. Note that there is a maximum queue
|
|
50
|
+
* of 128 asynchronous commands that can be run in a given
|
|
51
|
+
* tick.
|
|
52
|
+
* @param commandString
|
|
53
|
+
* Command to run. Note that command strings should not start
|
|
54
|
+
* with slash.
|
|
55
|
+
* @returns
|
|
56
|
+
* For commands that return data, returns a CommandResult with
|
|
57
|
+
* an indicator of command results.
|
|
58
|
+
* @throws This function can throw errors.
|
|
59
|
+
*/
|
|
60
|
+
runCommandAsync(commandString: string): Promise<CommandResult>;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Represents the state of an entity (a mob, the player, or
|
|
64
|
+
* other moving objects like minecarts) in the world.
|
|
65
|
+
*/
|
|
66
|
+
export class Entity {
|
|
67
|
+
protected constructor();
|
|
68
|
+
/**
|
|
69
|
+
* Unique identifier of the entity. This identifier is intended
|
|
70
|
+
* to be consistent across loads of a world instance. No
|
|
71
|
+
* meaning should be inferred from the value and structure of
|
|
72
|
+
* this unique identifier - do not parse or interpret it.
|
|
73
|
+
* @throws This property can throw when used.
|
|
74
|
+
*/
|
|
75
|
+
readonly id: string;
|
|
76
|
+
/**
|
|
77
|
+
* Unique identifier of the type of the entity - for example,
|
|
78
|
+
* 'minecraft:skeleton'.
|
|
79
|
+
* @throws This property can throw when used.
|
|
80
|
+
*/
|
|
81
|
+
readonly typeId: string;
|
|
82
|
+
/**
|
|
83
|
+
* @remarks
|
|
84
|
+
* Runs a particular command asynchronously from the context of
|
|
85
|
+
* this entity. Note that there is a maximum queue of 128
|
|
86
|
+
* asynchronous commands that can be run in a given tick.
|
|
87
|
+
* @param commandString
|
|
88
|
+
* Command to run. Note that command strings should not start
|
|
89
|
+
* with slash.
|
|
90
|
+
* @returns
|
|
91
|
+
* For commands that return data, returns a JSON structure with
|
|
92
|
+
* command response values.
|
|
93
|
+
* @throws This function can throw errors.
|
|
94
|
+
*/
|
|
95
|
+
runCommandAsync(commandString: string): Promise<CommandResult>;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* A collection of default Minecraft dimension types.
|
|
99
|
+
*/
|
|
100
|
+
// tslint:disable-next-line:no-unnecessary-class
|
|
101
|
+
export class MinecraftDimensionTypes {
|
|
102
|
+
protected constructor();
|
|
103
|
+
/**
|
|
104
|
+
* The Nether is a collection of biomes separate from the
|
|
105
|
+
* Overworld, including Soul Sand Valleys and Crimson forests.
|
|
106
|
+
* Nether fortresses contain exclusive resources. Mobs such as
|
|
107
|
+
* Blaze, Hoglins, Piglins, and Ghasts congregate here.
|
|
108
|
+
*/
|
|
109
|
+
static readonly nether = 'minecraft:nether';
|
|
110
|
+
/**
|
|
111
|
+
* The overworld is a collection of biomes, including forests,
|
|
112
|
+
* plains, jungles, mountains, deserts, taiga, and more. This
|
|
113
|
+
* is the default starter dimension for Minecraft. Mobs such as
|
|
114
|
+
* Axolotl, Cows, Creepers, and Zombies congregate here.
|
|
115
|
+
*/
|
|
116
|
+
static readonly overworld = 'minecraft:overworld';
|
|
117
|
+
/**
|
|
118
|
+
* The End is separate from the Overworld and the Nether and is
|
|
119
|
+
* generated whenever you create an End portal. Here, a giant
|
|
120
|
+
* center island is surrounded by several smaller areas and
|
|
121
|
+
* islands. You can find Endermen here. End midlands are larger
|
|
122
|
+
* areas that transition you from the center to the outer edges
|
|
123
|
+
* of the End. They contain Shulkers, Endermen, End gateway
|
|
124
|
+
* portals, and End cities. End gateway portals are commonly
|
|
125
|
+
* found at the outermost edge of the void. You usually find
|
|
126
|
+
* End barrens toward the edges of the main areas or land in
|
|
127
|
+
* the End.
|
|
128
|
+
*/
|
|
129
|
+
static readonly theEnd = 'minecraft:the_end';
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Represents a player within the world.
|
|
133
|
+
*/
|
|
134
|
+
export class Player extends Entity {
|
|
135
|
+
protected constructor();
|
|
136
|
+
/**
|
|
137
|
+
* Unique identifier of the player. This identifier is intended
|
|
138
|
+
* to be consistent across loads of a world instance. No
|
|
139
|
+
* meaning should be inferred from the value and structure of
|
|
140
|
+
* this unique identifier - do not parse or interpret it.
|
|
141
|
+
* @throws This property can throw when used.
|
|
142
|
+
*/
|
|
143
|
+
readonly id: string;
|
|
144
|
+
/**
|
|
145
|
+
* Name of the player.
|
|
146
|
+
* @throws This property can throw when used.
|
|
147
|
+
*/
|
|
148
|
+
readonly name: string;
|
|
149
|
+
/**
|
|
150
|
+
* Unique identifier of the type of the entity - for example,
|
|
151
|
+
* 'minecraft:player'.
|
|
152
|
+
* @throws This property can throw when used.
|
|
153
|
+
*/
|
|
154
|
+
readonly typeId: string;
|
|
155
|
+
/**
|
|
156
|
+
* @remarks
|
|
157
|
+
* Runs a particular command asynchronously from the context of
|
|
158
|
+
* this entity. Note that there is a maximum queue of 128
|
|
159
|
+
* asynchronous commands that can be run in a given tick.
|
|
160
|
+
* @param commandString
|
|
161
|
+
* Command to run. Note that command strings should not start
|
|
162
|
+
* with slash.
|
|
163
|
+
* @returns
|
|
164
|
+
* For commands that return data, returns a JSON structure with
|
|
165
|
+
* command response values.
|
|
166
|
+
* @throws This function can throw errors.
|
|
167
|
+
*/
|
|
168
|
+
runCommandAsync(commandString: string): Promise<CommandResult>;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* A class that provides system-level events and functions.
|
|
172
|
+
*/
|
|
173
|
+
export class System {
|
|
174
|
+
protected constructor();
|
|
175
|
+
/**
|
|
176
|
+
* @remarks
|
|
177
|
+
* Runs a specified function at a future time. This is
|
|
178
|
+
* frequently used to implement delayed behaviors and game
|
|
179
|
+
* loops.
|
|
180
|
+
* @param callback
|
|
181
|
+
* Function callback to run when the tickDelay time criteria is
|
|
182
|
+
* met.
|
|
183
|
+
* @returns
|
|
184
|
+
* An opaque identifier that can be used with the `clearRun`
|
|
185
|
+
* function to cancel the execution of this run.
|
|
186
|
+
*/
|
|
187
|
+
run(callback: () => void): number;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* A class that wraps the state of a world - a set of
|
|
191
|
+
* dimensions and the environment of Minecraft.
|
|
192
|
+
*/
|
|
193
|
+
export class World {
|
|
194
|
+
protected constructor();
|
|
195
|
+
/**
|
|
196
|
+
* @remarks
|
|
197
|
+
* Returns an array of all active players within the world.
|
|
198
|
+
* @throws This function can throw errors.
|
|
199
|
+
*/
|
|
200
|
+
getAllPlayers(): Player[];
|
|
201
|
+
/**
|
|
202
|
+
* @remarks
|
|
203
|
+
* Returns a dimension object.
|
|
204
|
+
* @param dimensionId
|
|
205
|
+
* @returns
|
|
206
|
+
* The requested dimension
|
|
207
|
+
* @throws
|
|
208
|
+
* Throws if the given dimension name is invalid
|
|
209
|
+
*/
|
|
210
|
+
getDimension(dimensionId: string): Dimension;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* A class that provides system-level events and functions.
|
|
214
|
+
*/
|
|
215
|
+
export const system: System;
|
|
216
|
+
/**
|
|
217
|
+
* A class that wraps the state of a world - a set of
|
|
218
|
+
* dimensions and the environment of Minecraft.
|
|
219
|
+
*/
|
|
220
|
+
export const world: World;
|
package/package.json
CHANGED
package/tests.ts
CHANGED
|
@@ -1,286 +1,286 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
import * as mc from '@minecraft/server';
|
|
3
|
-
|
|
4
|
-
export function createExplosion(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
5
|
-
const overworld = mc.world.getDimension('overworld');
|
|
6
|
-
|
|
7
|
-
log('Creating an explosion of radius 10.');
|
|
8
|
-
overworld.createExplosion(targetLocation, 10);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function createNoBlockExplosion(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
12
|
-
const overworld = mc.world.getDimension('overworld');
|
|
13
|
-
|
|
14
|
-
const explodeNoBlocksLoc = new mc.Location(
|
|
15
|
-
Math.floor(targetLocation.x + 1),
|
|
16
|
-
Math.floor(targetLocation.y + 2),
|
|
17
|
-
Math.floor(targetLocation.z + 1),
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
log('Creating an explosion of radius 15 that does not break blocks.');
|
|
21
|
-
overworld.createExplosion(explodeNoBlocksLoc, 15, { breaksBlocks: false });
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function createFireAndWaterExplosions(
|
|
25
|
-
log: (message: string, status?: number) => void,
|
|
26
|
-
targetLocation: mc.Location,
|
|
27
|
-
) {
|
|
28
|
-
const overworld = mc.world.getDimension('overworld');
|
|
29
|
-
|
|
30
|
-
const explosionLoc = new mc.Location(targetLocation.x + 0.5, targetLocation.y + 0.5, targetLocation.z + 0.5);
|
|
31
|
-
|
|
32
|
-
log('Creating an explosion of radius 15 that causes fire.');
|
|
33
|
-
overworld.createExplosion(explosionLoc, 15, { causesFire: true });
|
|
34
|
-
|
|
35
|
-
const belowWaterLoc = new mc.Location(targetLocation.x + 3, targetLocation.y + 1, targetLocation.z + 3);
|
|
36
|
-
|
|
37
|
-
log('Creating an explosion of radius 10 that can go underwater.');
|
|
38
|
-
overworld.createExplosion(belowWaterLoc, 10, { allowUnderwater: true });
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function itemStacks(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
42
|
-
const overworld = mc.world.getDimension('overworld');
|
|
43
|
-
|
|
44
|
-
const oneItemLoc = new mc.BlockLocation(targetLocation.x + targetLocation.y + 3, 2, targetLocation.z + 1);
|
|
45
|
-
const fiveItemsLoc = new mc.BlockLocation(targetLocation.x + 1, targetLocation.y + 2, targetLocation.z + 1);
|
|
46
|
-
const diamondPickaxeLoc = new mc.BlockLocation(targetLocation.x + 2, targetLocation.y + 2, targetLocation.z + 4);
|
|
47
|
-
|
|
48
|
-
const oneEmerald = new mc.ItemStack(mc.MinecraftItemTypes.emerald, 1, 0);
|
|
49
|
-
const onePickaxe = new mc.ItemStack(mc.MinecraftItemTypes.diamondPickaxe, 1, 0);
|
|
50
|
-
const fiveEmeralds = new mc.ItemStack(mc.MinecraftItemTypes.emerald, 5, 0);
|
|
51
|
-
|
|
52
|
-
log(`Spawning an emerald at (${oneItemLoc.x}, ${oneItemLoc.y}, ${oneItemLoc.z})`);
|
|
53
|
-
overworld.spawnItem(oneEmerald, oneItemLoc);
|
|
54
|
-
|
|
55
|
-
log(`Spawning five emeralds at (${fiveItemsLoc.x}, ${fiveItemsLoc.y}, ${fiveItemsLoc.z})`);
|
|
56
|
-
overworld.spawnItem(fiveEmeralds, fiveItemsLoc);
|
|
57
|
-
|
|
58
|
-
log(`Spawning a diamond pickaxe at (${diamondPickaxeLoc.x}, ${diamondPickaxeLoc.y}, ${diamondPickaxeLoc.z})`);
|
|
59
|
-
overworld.spawnItem(onePickaxe, diamondPickaxeLoc);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function quickFoxLazyDog(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
63
|
-
const overworld = mc.world.getDimension('overworld');
|
|
64
|
-
|
|
65
|
-
const fox = overworld.spawnEntity(
|
|
66
|
-
'minecraft:fox',
|
|
67
|
-
new mc.BlockLocation(targetLocation.x + 1, targetLocation.y + 2, targetLocation.z + 3),
|
|
68
|
-
);
|
|
69
|
-
fox.addEffect(mc.MinecraftEffectTypes.speed, 10, 20);
|
|
70
|
-
log('Created a fox.');
|
|
71
|
-
|
|
72
|
-
const wolf = overworld.spawnEntity(
|
|
73
|
-
'minecraft:wolf',
|
|
74
|
-
new mc.BlockLocation(targetLocation.x + 4, targetLocation.y + 2, targetLocation.z + 3),
|
|
75
|
-
);
|
|
76
|
-
wolf.addEffect(mc.MinecraftEffectTypes.slowness, 10, 20);
|
|
77
|
-
wolf.isSneaking = true;
|
|
78
|
-
log('Created a sneaking wolf.', 1);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function runEntityCreatedEvent(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
82
|
-
// register a new function that is called when a new entity is created.
|
|
83
|
-
mc.world.events.entityCreate.subscribe((entityEvent: mc.EntityCreateEvent) => {
|
|
84
|
-
if (entityEvent && entityEvent.entity) {
|
|
85
|
-
log(`New entity of type '${entityEvent.entity}' created!`, 1);
|
|
86
|
-
} else {
|
|
87
|
-
log(`The entity event didn't work as expected.`, -1);
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export function createOldHorse(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
93
|
-
const overworld = mc.world.getDimension('overworld');
|
|
94
|
-
|
|
95
|
-
log("Create a horse and triggering the 'ageable_grow_up' event, ensuring the horse is created as an adult");
|
|
96
|
-
overworld.spawnEntity('minecraft:horse<minecraft:ageable_grow_up>', targetLocation);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export function pistonEvent(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
100
|
-
const pistonLoc = new mc.BlockLocation(
|
|
101
|
-
Math.floor(targetLocation.x) + 1,
|
|
102
|
-
Math.floor(targetLocation.y) + 2,
|
|
103
|
-
Math.floor(targetLocation.z) + 1,
|
|
104
|
-
);
|
|
105
|
-
|
|
106
|
-
mc.world.events.beforePistonActivate.subscribe((pistonEvent: mc.BeforePistonActivateEvent) => {
|
|
107
|
-
if (pistonEvent.piston.location.equals(pistonLoc)) {
|
|
108
|
-
log('Cancelling piston event');
|
|
109
|
-
pistonEvent.cancel = true;
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export function spawnItem(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
115
|
-
const featherItem = new mc.ItemStack(mc.MinecraftItemTypes.feather, 1, 0);
|
|
116
|
-
|
|
117
|
-
overworld.spawnItem(featherItem, targetLocation);
|
|
118
|
-
log(`New feather created at ${targetLocation.x}, ${targetLocation.y}, ${targetLocation.z}!`);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
export function testThatEntityIsFeatherItem(
|
|
122
|
-
log: (message: string, status?: number) => void,
|
|
123
|
-
targetLocation: mc.Location,
|
|
124
|
-
) {
|
|
125
|
-
const overworld = mc.world.getDimension('overworld');
|
|
126
|
-
|
|
127
|
-
const items = overworld.getEntities({
|
|
128
|
-
location: targetLocation,
|
|
129
|
-
maxDistance: 20,
|
|
130
|
-
});
|
|
131
|
-
|
|
132
|
-
for (const item of items) {
|
|
133
|
-
const itemComp = item.getComponent('item') as any;
|
|
134
|
-
|
|
135
|
-
if (itemComp) {
|
|
136
|
-
if (itemComp.itemStack.id.endsWith('feather')) {
|
|
137
|
-
log('Success! Found a feather', 1);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export function trapTick() {
|
|
144
|
-
const overworld = mc.world.getDimension('overworld');
|
|
145
|
-
|
|
146
|
-
try {
|
|
147
|
-
// Minecraft runs at 20 ticks per second
|
|
148
|
-
if (ticks % 1200 === 0) {
|
|
149
|
-
overworld.runCommandAsync('say Another minute passes...');
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
ticks++;
|
|
153
|
-
} catch (e) {
|
|
154
|
-
console.warn('Error: ' + e);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
mc.system.run(trapTick);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
export default class SampleManager {
|
|
161
|
-
tickCount = 0;
|
|
162
|
-
|
|
163
|
-
_availableFuncs: {
|
|
164
|
-
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
pendingFuncs: Array<{
|
|
168
|
-
name: string;
|
|
169
|
-
func: (log: (message: string, status?: number) => void, location: mc.Location) => void;
|
|
170
|
-
location: mc.Location;
|
|
171
|
-
}> = [];
|
|
172
|
-
|
|
173
|
-
gameplayLogger(message: string, status?: number) {
|
|
174
|
-
if (status !== undefined && status > 0) {
|
|
175
|
-
message = 'SUCCESS: ' + message;
|
|
176
|
-
} else if (status !== undefined && status < 0) {
|
|
177
|
-
message = 'FAIL: ' + message;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
this.say(message);
|
|
181
|
-
}
|
|
182
|
-
say(message: string) {
|
|
183
|
-
mc.world.getDimension('overworld').runCommand('say ' + message);
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
newChatMessage(chatEvent: mc.ChatEvent) {
|
|
187
|
-
const message = chatEvent.message.toLowerCase();
|
|
188
|
-
|
|
189
|
-
if (message.startsWith('howto') && chatEvent.sender) {
|
|
190
|
-
const nearbyBlock = chatEvent.sender.getBlockFromViewVector();
|
|
191
|
-
if (!nearbyBlock) {
|
|
192
|
-
this.gameplayLogger('Please look at the block where you want me to run this.');
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
const nearbyBlockLoc = nearbyBlock.location;
|
|
197
|
-
const nearbyLoc = new mc.Location(nearbyBlockLoc.x, nearbyBlockLoc.y + 1, nearbyBlockLoc.z);
|
|
198
|
-
|
|
199
|
-
const sampleId = message.substring(5).trim();
|
|
200
|
-
|
|
201
|
-
if (sampleId.length < 2) {
|
|
202
|
-
let availableFuncStr = 'Here is my list of available samples:';
|
|
203
|
-
|
|
204
|
-
for (const sampleFuncKey in this._availableFuncs) {
|
|
205
|
-
availableFuncStr += ' ' + sampleFuncKey;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
this.say(availableFuncStr);
|
|
209
|
-
} else {
|
|
210
|
-
for (const sampleFuncKey in this._availableFuncs) {
|
|
211
|
-
if (sampleFuncKey.toLowerCase() === sampleId) {
|
|
212
|
-
const sampleFunc = this._availableFuncs[sampleFuncKey];
|
|
213
|
-
|
|
214
|
-
this.runSample(sampleFuncKey + this.tickCount, sampleFunc, nearbyLoc);
|
|
215
|
-
|
|
216
|
-
return;
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
this.say(`I couldn't find the sample '${sampleId}"'`);
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
runSample(
|
|
226
|
-
sampleId: string,
|
|
227
|
-
snippetFunctions: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>,
|
|
228
|
-
targetLocation: mc.Location,
|
|
229
|
-
) {
|
|
230
|
-
for (let i = snippetFunctions.length - 1; i >= 0; i--) {
|
|
231
|
-
this.pendingFuncs.push({ name: sampleId, func: snippetFunctions[i], location: targetLocation });
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
worldTick() {
|
|
236
|
-
if (this.tickCount % 10 === 0) {
|
|
237
|
-
if (this.pendingFuncs.length > 0) {
|
|
238
|
-
const funcSet = this.pendingFuncs.pop();
|
|
239
|
-
|
|
240
|
-
if (funcSet) {
|
|
241
|
-
funcSet.func(this.gameplayLogger, funcSet.location);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
this.tickCount++;
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
constructor() {
|
|
250
|
-
this._availableFuncs = {};
|
|
251
|
-
|
|
252
|
-
this.gameplayLogger = this.gameplayLogger.bind(this);
|
|
253
|
-
|
|
254
|
-
mc.world.events.tick.subscribe(this.worldTick.bind(this));
|
|
255
|
-
mc.world.events.chat.subscribe(this.newChatMessage.bind(this));
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
registerSamples(sampleSet: {
|
|
259
|
-
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
260
|
-
}) {
|
|
261
|
-
for (const sampleKey in sampleSet) {
|
|
262
|
-
if (sampleKey.length > 1 && sampleSet[sampleKey]) {
|
|
263
|
-
this._availableFuncs[sampleKey] = sampleSet[sampleKey];
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
const mojangMinecraftFuncs: {
|
|
270
|
-
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
271
|
-
} = {
|
|
272
|
-
runEntityCreatedEvent: [runEntityCreatedEvent, createOldHorse],
|
|
273
|
-
createOldHorse: [createOldHorse],
|
|
274
|
-
spawnItem: [spawnItem, testThatEntityIsFeatherItem],
|
|
275
|
-
createNoBlockExplosion: [createExplosion],
|
|
276
|
-
createFireAndWaterExplosions: [createFireAndWaterExplosions],
|
|
277
|
-
createExplosion: [createExplosion],
|
|
278
|
-
itemStacks: [itemStacks],
|
|
279
|
-
quickFoxLazyDog: [quickFoxLazyDog],
|
|
280
|
-
pistonEvent: [pistonEvent],
|
|
281
|
-
trapTick: [trapTick],
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
export function register(sampleManager: SampleManager) {
|
|
285
|
-
sampleManager.registerSamples(mojangMinecraftFuncs);
|
|
286
|
-
}
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
+
import * as mc from '@minecraft/server';
|
|
3
|
+
|
|
4
|
+
export function createExplosion(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
5
|
+
const overworld = mc.world.getDimension('overworld');
|
|
6
|
+
|
|
7
|
+
log('Creating an explosion of radius 10.');
|
|
8
|
+
overworld.createExplosion(targetLocation, 10);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function createNoBlockExplosion(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
12
|
+
const overworld = mc.world.getDimension('overworld');
|
|
13
|
+
|
|
14
|
+
const explodeNoBlocksLoc = new mc.Location(
|
|
15
|
+
Math.floor(targetLocation.x + 1),
|
|
16
|
+
Math.floor(targetLocation.y + 2),
|
|
17
|
+
Math.floor(targetLocation.z + 1),
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
log('Creating an explosion of radius 15 that does not break blocks.');
|
|
21
|
+
overworld.createExplosion(explodeNoBlocksLoc, 15, { breaksBlocks: false });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createFireAndWaterExplosions(
|
|
25
|
+
log: (message: string, status?: number) => void,
|
|
26
|
+
targetLocation: mc.Location,
|
|
27
|
+
) {
|
|
28
|
+
const overworld = mc.world.getDimension('overworld');
|
|
29
|
+
|
|
30
|
+
const explosionLoc = new mc.Location(targetLocation.x + 0.5, targetLocation.y + 0.5, targetLocation.z + 0.5);
|
|
31
|
+
|
|
32
|
+
log('Creating an explosion of radius 15 that causes fire.');
|
|
33
|
+
overworld.createExplosion(explosionLoc, 15, { causesFire: true });
|
|
34
|
+
|
|
35
|
+
const belowWaterLoc = new mc.Location(targetLocation.x + 3, targetLocation.y + 1, targetLocation.z + 3);
|
|
36
|
+
|
|
37
|
+
log('Creating an explosion of radius 10 that can go underwater.');
|
|
38
|
+
overworld.createExplosion(belowWaterLoc, 10, { allowUnderwater: true });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function itemStacks(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
42
|
+
const overworld = mc.world.getDimension('overworld');
|
|
43
|
+
|
|
44
|
+
const oneItemLoc = new mc.BlockLocation(targetLocation.x + targetLocation.y + 3, 2, targetLocation.z + 1);
|
|
45
|
+
const fiveItemsLoc = new mc.BlockLocation(targetLocation.x + 1, targetLocation.y + 2, targetLocation.z + 1);
|
|
46
|
+
const diamondPickaxeLoc = new mc.BlockLocation(targetLocation.x + 2, targetLocation.y + 2, targetLocation.z + 4);
|
|
47
|
+
|
|
48
|
+
const oneEmerald = new mc.ItemStack(mc.MinecraftItemTypes.emerald, 1, 0);
|
|
49
|
+
const onePickaxe = new mc.ItemStack(mc.MinecraftItemTypes.diamondPickaxe, 1, 0);
|
|
50
|
+
const fiveEmeralds = new mc.ItemStack(mc.MinecraftItemTypes.emerald, 5, 0);
|
|
51
|
+
|
|
52
|
+
log(`Spawning an emerald at (${oneItemLoc.x}, ${oneItemLoc.y}, ${oneItemLoc.z})`);
|
|
53
|
+
overworld.spawnItem(oneEmerald, oneItemLoc);
|
|
54
|
+
|
|
55
|
+
log(`Spawning five emeralds at (${fiveItemsLoc.x}, ${fiveItemsLoc.y}, ${fiveItemsLoc.z})`);
|
|
56
|
+
overworld.spawnItem(fiveEmeralds, fiveItemsLoc);
|
|
57
|
+
|
|
58
|
+
log(`Spawning a diamond pickaxe at (${diamondPickaxeLoc.x}, ${diamondPickaxeLoc.y}, ${diamondPickaxeLoc.z})`);
|
|
59
|
+
overworld.spawnItem(onePickaxe, diamondPickaxeLoc);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function quickFoxLazyDog(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
63
|
+
const overworld = mc.world.getDimension('overworld');
|
|
64
|
+
|
|
65
|
+
const fox = overworld.spawnEntity(
|
|
66
|
+
'minecraft:fox',
|
|
67
|
+
new mc.BlockLocation(targetLocation.x + 1, targetLocation.y + 2, targetLocation.z + 3),
|
|
68
|
+
);
|
|
69
|
+
fox.addEffect(mc.MinecraftEffectTypes.speed, 10, 20);
|
|
70
|
+
log('Created a fox.');
|
|
71
|
+
|
|
72
|
+
const wolf = overworld.spawnEntity(
|
|
73
|
+
'minecraft:wolf',
|
|
74
|
+
new mc.BlockLocation(targetLocation.x + 4, targetLocation.y + 2, targetLocation.z + 3),
|
|
75
|
+
);
|
|
76
|
+
wolf.addEffect(mc.MinecraftEffectTypes.slowness, 10, 20);
|
|
77
|
+
wolf.isSneaking = true;
|
|
78
|
+
log('Created a sneaking wolf.', 1);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function runEntityCreatedEvent(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
82
|
+
// register a new function that is called when a new entity is created.
|
|
83
|
+
mc.world.events.entityCreate.subscribe((entityEvent: mc.EntityCreateEvent) => {
|
|
84
|
+
if (entityEvent && entityEvent.entity) {
|
|
85
|
+
log(`New entity of type '${entityEvent.entity}' created!`, 1);
|
|
86
|
+
} else {
|
|
87
|
+
log(`The entity event didn't work as expected.`, -1);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function createOldHorse(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
93
|
+
const overworld = mc.world.getDimension('overworld');
|
|
94
|
+
|
|
95
|
+
log("Create a horse and triggering the 'ageable_grow_up' event, ensuring the horse is created as an adult");
|
|
96
|
+
overworld.spawnEntity('minecraft:horse<minecraft:ageable_grow_up>', targetLocation);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function pistonEvent(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
100
|
+
const pistonLoc = new mc.BlockLocation(
|
|
101
|
+
Math.floor(targetLocation.x) + 1,
|
|
102
|
+
Math.floor(targetLocation.y) + 2,
|
|
103
|
+
Math.floor(targetLocation.z) + 1,
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
mc.world.events.beforePistonActivate.subscribe((pistonEvent: mc.BeforePistonActivateEvent) => {
|
|
107
|
+
if (pistonEvent.piston.location.equals(pistonLoc)) {
|
|
108
|
+
log('Cancelling piston event');
|
|
109
|
+
pistonEvent.cancel = true;
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function spawnItem(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
115
|
+
const featherItem = new mc.ItemStack(mc.MinecraftItemTypes.feather, 1, 0);
|
|
116
|
+
|
|
117
|
+
overworld.spawnItem(featherItem, targetLocation);
|
|
118
|
+
log(`New feather created at ${targetLocation.x}, ${targetLocation.y}, ${targetLocation.z}!`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function testThatEntityIsFeatherItem(
|
|
122
|
+
log: (message: string, status?: number) => void,
|
|
123
|
+
targetLocation: mc.Location,
|
|
124
|
+
) {
|
|
125
|
+
const overworld = mc.world.getDimension('overworld');
|
|
126
|
+
|
|
127
|
+
const items = overworld.getEntities({
|
|
128
|
+
location: targetLocation,
|
|
129
|
+
maxDistance: 20,
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
for (const item of items) {
|
|
133
|
+
const itemComp = item.getComponent('item') as any;
|
|
134
|
+
|
|
135
|
+
if (itemComp) {
|
|
136
|
+
if (itemComp.itemStack.id.endsWith('feather')) {
|
|
137
|
+
log('Success! Found a feather', 1);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function trapTick() {
|
|
144
|
+
const overworld = mc.world.getDimension('overworld');
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
// Minecraft runs at 20 ticks per second
|
|
148
|
+
if (ticks % 1200 === 0) {
|
|
149
|
+
overworld.runCommandAsync('say Another minute passes...');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
ticks++;
|
|
153
|
+
} catch (e) {
|
|
154
|
+
console.warn('Error: ' + e);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
mc.system.run(trapTick);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export default class SampleManager {
|
|
161
|
+
tickCount = 0;
|
|
162
|
+
|
|
163
|
+
_availableFuncs: {
|
|
164
|
+
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
pendingFuncs: Array<{
|
|
168
|
+
name: string;
|
|
169
|
+
func: (log: (message: string, status?: number) => void, location: mc.Location) => void;
|
|
170
|
+
location: mc.Location;
|
|
171
|
+
}> = [];
|
|
172
|
+
|
|
173
|
+
gameplayLogger(message: string, status?: number) {
|
|
174
|
+
if (status !== undefined && status > 0) {
|
|
175
|
+
message = 'SUCCESS: ' + message;
|
|
176
|
+
} else if (status !== undefined && status < 0) {
|
|
177
|
+
message = 'FAIL: ' + message;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
this.say(message);
|
|
181
|
+
}
|
|
182
|
+
say(message: string) {
|
|
183
|
+
mc.world.getDimension('overworld').runCommand('say ' + message);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
newChatMessage(chatEvent: mc.ChatEvent) {
|
|
187
|
+
const message = chatEvent.message.toLowerCase();
|
|
188
|
+
|
|
189
|
+
if (message.startsWith('howto') && chatEvent.sender) {
|
|
190
|
+
const nearbyBlock = chatEvent.sender.getBlockFromViewVector();
|
|
191
|
+
if (!nearbyBlock) {
|
|
192
|
+
this.gameplayLogger('Please look at the block where you want me to run this.');
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const nearbyBlockLoc = nearbyBlock.location;
|
|
197
|
+
const nearbyLoc = new mc.Location(nearbyBlockLoc.x, nearbyBlockLoc.y + 1, nearbyBlockLoc.z);
|
|
198
|
+
|
|
199
|
+
const sampleId = message.substring(5).trim();
|
|
200
|
+
|
|
201
|
+
if (sampleId.length < 2) {
|
|
202
|
+
let availableFuncStr = 'Here is my list of available samples:';
|
|
203
|
+
|
|
204
|
+
for (const sampleFuncKey in this._availableFuncs) {
|
|
205
|
+
availableFuncStr += ' ' + sampleFuncKey;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
this.say(availableFuncStr);
|
|
209
|
+
} else {
|
|
210
|
+
for (const sampleFuncKey in this._availableFuncs) {
|
|
211
|
+
if (sampleFuncKey.toLowerCase() === sampleId) {
|
|
212
|
+
const sampleFunc = this._availableFuncs[sampleFuncKey];
|
|
213
|
+
|
|
214
|
+
this.runSample(sampleFuncKey + this.tickCount, sampleFunc, nearbyLoc);
|
|
215
|
+
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
this.say(`I couldn't find the sample '${sampleId}"'`);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
runSample(
|
|
226
|
+
sampleId: string,
|
|
227
|
+
snippetFunctions: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>,
|
|
228
|
+
targetLocation: mc.Location,
|
|
229
|
+
) {
|
|
230
|
+
for (let i = snippetFunctions.length - 1; i >= 0; i--) {
|
|
231
|
+
this.pendingFuncs.push({ name: sampleId, func: snippetFunctions[i], location: targetLocation });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
worldTick() {
|
|
236
|
+
if (this.tickCount % 10 === 0) {
|
|
237
|
+
if (this.pendingFuncs.length > 0) {
|
|
238
|
+
const funcSet = this.pendingFuncs.pop();
|
|
239
|
+
|
|
240
|
+
if (funcSet) {
|
|
241
|
+
funcSet.func(this.gameplayLogger, funcSet.location);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
this.tickCount++;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
constructor() {
|
|
250
|
+
this._availableFuncs = {};
|
|
251
|
+
|
|
252
|
+
this.gameplayLogger = this.gameplayLogger.bind(this);
|
|
253
|
+
|
|
254
|
+
mc.world.events.tick.subscribe(this.worldTick.bind(this));
|
|
255
|
+
mc.world.events.chat.subscribe(this.newChatMessage.bind(this));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
registerSamples(sampleSet: {
|
|
259
|
+
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
260
|
+
}) {
|
|
261
|
+
for (const sampleKey in sampleSet) {
|
|
262
|
+
if (sampleKey.length > 1 && sampleSet[sampleKey]) {
|
|
263
|
+
this._availableFuncs[sampleKey] = sampleSet[sampleKey];
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
const mojangMinecraftFuncs: {
|
|
270
|
+
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
271
|
+
} = {
|
|
272
|
+
runEntityCreatedEvent: [runEntityCreatedEvent, createOldHorse],
|
|
273
|
+
createOldHorse: [createOldHorse],
|
|
274
|
+
spawnItem: [spawnItem, testThatEntityIsFeatherItem],
|
|
275
|
+
createNoBlockExplosion: [createExplosion],
|
|
276
|
+
createFireAndWaterExplosions: [createFireAndWaterExplosions],
|
|
277
|
+
createExplosion: [createExplosion],
|
|
278
|
+
itemStacks: [itemStacks],
|
|
279
|
+
quickFoxLazyDog: [quickFoxLazyDog],
|
|
280
|
+
pistonEvent: [pistonEvent],
|
|
281
|
+
trapTick: [trapTick],
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
export function register(sampleManager: SampleManager) {
|
|
285
|
+
sampleManager.registerSamples(mojangMinecraftFuncs);
|
|
286
|
+
}
|