@minecraft/server 0.0.1-beta.1234
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 +23 -0
- package/README.md +9 -0
- package/index.d.ts +13929 -0
- package/package.json +17 -0
- package/tests.ts +270 -0
- package/tsconfig.json +20 -0
package/package.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@minecraft/server",
|
|
3
|
+
"version": "0.0.1-beta.1234",
|
|
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
|
+
}
|
package/tests.ts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import * as mc from '@minecraft/server';
|
|
2
|
+
|
|
3
|
+
const overworld = mc.world.getDimension('overworld');
|
|
4
|
+
|
|
5
|
+
export function createExplosion(targetLocation: mc.Location) {
|
|
6
|
+
overworld.createExplosion(targetLocation, 10, new mc.ExplosionOptions());
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function createNoBlockExplosion(targetLocation: mc.Location) {
|
|
10
|
+
const explosionOptions = new mc.ExplosionOptions();
|
|
11
|
+
|
|
12
|
+
// Start by exploding without breaking blocks
|
|
13
|
+
explosionOptions.breaksBlocks = false;
|
|
14
|
+
|
|
15
|
+
const explodeNoBlocksLoc = new mc.Location(
|
|
16
|
+
Math.floor(targetLocation.x + 1),
|
|
17
|
+
Math.floor(targetLocation.y + 2),
|
|
18
|
+
Math.floor(targetLocation.z + 1),
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
overworld.createExplosion(explodeNoBlocksLoc, 15, explosionOptions);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createFireAndWaterExplosions(targetLocation: mc.Location) {
|
|
25
|
+
const explosionLoc = new mc.Location(targetLocation.x + 0.5, targetLocation.y + 0.5, targetLocation.z + 0.5);
|
|
26
|
+
|
|
27
|
+
const fireExplosionOptions = new mc.ExplosionOptions();
|
|
28
|
+
|
|
29
|
+
// Explode with fire
|
|
30
|
+
fireExplosionOptions.causesFire = true;
|
|
31
|
+
|
|
32
|
+
overworld.createExplosion(explosionLoc, 15, fireExplosionOptions);
|
|
33
|
+
const waterExplosionOptions = new mc.ExplosionOptions();
|
|
34
|
+
|
|
35
|
+
// Explode in water
|
|
36
|
+
waterExplosionOptions.allowUnderwater = true;
|
|
37
|
+
|
|
38
|
+
const belowWaterLoc = new mc.Location(targetLocation.x + 3, targetLocation.y + 1, targetLocation.z + 3);
|
|
39
|
+
|
|
40
|
+
overworld.createExplosion(belowWaterLoc, 10, waterExplosionOptions);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function itemStacks() {
|
|
44
|
+
const oneItemLoc = new mc.BlockLocation(3, 2, 1);
|
|
45
|
+
const fiveItemsLoc = new mc.BlockLocation(1, 2, 1);
|
|
46
|
+
const diamondPickaxeLoc = new mc.BlockLocation(2, 2, 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
|
+
overworld.spawnItem(oneEmerald, oneItemLoc);
|
|
53
|
+
overworld.spawnItem(fiveEmeralds, fiveItemsLoc);
|
|
54
|
+
overworld.spawnItem(onePickaxe, diamondPickaxeLoc);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function quickFoxLazyDog(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
58
|
+
const fox = overworld.spawnEntity(
|
|
59
|
+
'minecraft:fox',
|
|
60
|
+
new mc.BlockLocation(targetLocation.x + 1, targetLocation.y + 2, targetLocation.z + 3),
|
|
61
|
+
);
|
|
62
|
+
fox.addEffect(mc.MinecraftEffectTypes.speed, 10, 20);
|
|
63
|
+
log('Created a fox.');
|
|
64
|
+
|
|
65
|
+
const wolf = overworld.spawnEntity(
|
|
66
|
+
'minecraft:wolf',
|
|
67
|
+
new mc.BlockLocation(targetLocation.x + 4, targetLocation.y + 2, targetLocation.z + 3),
|
|
68
|
+
);
|
|
69
|
+
wolf.addEffect(mc.MinecraftEffectTypes.slowness, 10, 20);
|
|
70
|
+
wolf.isSneaking = true;
|
|
71
|
+
log('Created a sneaking wolf.', 1);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function runEntityCreatedEvent(log: (message: string, status?: number) => void) {
|
|
75
|
+
// register a new function that is called when a new entity is created.
|
|
76
|
+
const entityCreatedCallback = mc.world.events.entityCreate.subscribe((entityEvent: mc.EntityCreateEvent) => {
|
|
77
|
+
if (entityEvent && entityEvent.entity) {
|
|
78
|
+
log(`New entity of type '${entityEvent.entity}' created!`, 1);
|
|
79
|
+
} else {
|
|
80
|
+
log(`The entity event didn't work as expected.`, -1);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
mc.world.events.entityCreate.unsubscribe(entityCreatedCallback);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function createOldHorse(targetLocation: mc.Location) {
|
|
87
|
+
// create a horse and trigger the 'ageable_grow_up' event, ensuring the horse is created as an adult
|
|
88
|
+
overworld.spawnEntity('minecraft:horse<minecraft:ageable_grow_up>', targetLocation);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function pistonEvent(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
92
|
+
const pistonLoc = new mc.BlockLocation(
|
|
93
|
+
Math.floor(targetLocation.x) + 1,
|
|
94
|
+
Math.floor(targetLocation.y) + 2,
|
|
95
|
+
Math.floor(targetLocation.z) + 1,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const pistonCallback = mc.world.events.beforePistonActivate.subscribe(
|
|
99
|
+
(pistonEvent: mc.BeforePistonActivateEvent) => {
|
|
100
|
+
if (pistonEvent.piston.location.equals(pistonLoc)) {
|
|
101
|
+
log('Cancelling piston event');
|
|
102
|
+
pistonEvent.cancel = true;
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
mc.world.events.beforePistonActivate.unsubscribe(pistonCallback);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function spawnItem(log: (message: string, status?: number) => void, targetLocation: mc.Location) {
|
|
111
|
+
const featherItem = new mc.ItemStack(mc.MinecraftItemTypes.feather, 1, 0);
|
|
112
|
+
|
|
113
|
+
overworld.spawnItem(featherItem, targetLocation);
|
|
114
|
+
log('New feather created!');
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function testThatEntityIsFeatherItem(log: (message: string, status?: number) => void) {
|
|
118
|
+
const items = overworld.getEntities();
|
|
119
|
+
|
|
120
|
+
for (const item of items) {
|
|
121
|
+
const itemComp = item.getComponent('item') as any;
|
|
122
|
+
|
|
123
|
+
if (itemComp) {
|
|
124
|
+
if (itemComp.itemStack.id.endsWith('feather')) {
|
|
125
|
+
log('Success! Found a feather', 1);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function trapTick() {
|
|
132
|
+
let ticks = 0;
|
|
133
|
+
|
|
134
|
+
mc.world.events.tick.subscribe(() => {
|
|
135
|
+
ticks++;
|
|
136
|
+
|
|
137
|
+
// Minecraft runs at 20 ticks per second
|
|
138
|
+
if (ticks % 1200 === 0) {
|
|
139
|
+
overworld.runCommand('say Another minute passes...');
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export default class SampleManager {
|
|
145
|
+
tickCount = 0;
|
|
146
|
+
|
|
147
|
+
_availableFuncs: {
|
|
148
|
+
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
pendingFuncs: Array<{
|
|
152
|
+
name: string;
|
|
153
|
+
func: (log: (message: string, status?: number) => void, location: mc.Location) => void;
|
|
154
|
+
location: mc.Location;
|
|
155
|
+
}> = [];
|
|
156
|
+
|
|
157
|
+
gameplayLogger(message: string, status?: number) {
|
|
158
|
+
if (status !== undefined && status > 0) {
|
|
159
|
+
message = 'SUCCESS: ' + message;
|
|
160
|
+
} else if (status !== undefined && status < 0) {
|
|
161
|
+
message = 'FAIL: ' + message;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
this.say(message);
|
|
165
|
+
}
|
|
166
|
+
say(message: string) {
|
|
167
|
+
mc.world.getDimension('overworld').runCommand('say ' + message);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
newChatMessage(chatEvent: mc.ChatEvent) {
|
|
171
|
+
const message = chatEvent.message.toLowerCase();
|
|
172
|
+
|
|
173
|
+
if (message.startsWith('howto') && chatEvent.sender) {
|
|
174
|
+
const nearbyBlock = chatEvent.sender.getBlockFromViewVector();
|
|
175
|
+
if (!nearbyBlock) {
|
|
176
|
+
this.gameplayLogger('Please look at the block where you want me to run this.');
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const nearbyBlockLoc = nearbyBlock.location;
|
|
181
|
+
const nearbyLoc = new mc.Location(nearbyBlockLoc.x, nearbyBlockLoc.y + 1, nearbyBlockLoc.z);
|
|
182
|
+
|
|
183
|
+
const sampleId = message.substring(5).trim();
|
|
184
|
+
|
|
185
|
+
if (sampleId.length < 2) {
|
|
186
|
+
let availableFuncStr = 'Here is my list of available samples:';
|
|
187
|
+
|
|
188
|
+
for (const sampleFuncKey in this._availableFuncs) {
|
|
189
|
+
availableFuncStr += ' ' + sampleFuncKey;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
this.say(availableFuncStr);
|
|
193
|
+
} else {
|
|
194
|
+
for (const sampleFuncKey in this._availableFuncs) {
|
|
195
|
+
if (sampleFuncKey.toLowerCase() === sampleId) {
|
|
196
|
+
const sampleFunc = this._availableFuncs[sampleFuncKey];
|
|
197
|
+
|
|
198
|
+
this.runSample(sampleFuncKey + this.tickCount, sampleFunc, nearbyLoc);
|
|
199
|
+
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
this.say(`I couldn't find the sample '${sampleId}"'`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
runSample(
|
|
210
|
+
sampleId: string,
|
|
211
|
+
snippetFunctions: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>,
|
|
212
|
+
targetLocation: mc.Location,
|
|
213
|
+
) {
|
|
214
|
+
for (let i = snippetFunctions.length - 1; i >= 0; i--) {
|
|
215
|
+
this.pendingFuncs.push({ name: sampleId, func: snippetFunctions[i], location: targetLocation });
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
worldTick() {
|
|
220
|
+
if (this.tickCount % 10 === 0) {
|
|
221
|
+
if (this.pendingFuncs.length > 0) {
|
|
222
|
+
const funcSet = this.pendingFuncs.pop();
|
|
223
|
+
|
|
224
|
+
if (funcSet) {
|
|
225
|
+
funcSet.func(this.gameplayLogger, funcSet.location);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
this.tickCount++;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
constructor() {
|
|
234
|
+
this._availableFuncs = {};
|
|
235
|
+
|
|
236
|
+
this.gameplayLogger = this.gameplayLogger.bind(this);
|
|
237
|
+
|
|
238
|
+
mc.world.events.tick.subscribe(this.worldTick.bind(this));
|
|
239
|
+
mc.world.events.chat.subscribe(this.newChatMessage.bind(this));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
registerSamples(sampleSet: {
|
|
243
|
+
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
244
|
+
}) {
|
|
245
|
+
for (const sampleKey in sampleSet) {
|
|
246
|
+
if (sampleKey.length > 1 && sampleSet[sampleKey]) {
|
|
247
|
+
this._availableFuncs[sampleKey] = sampleSet[sampleKey];
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const mojangMinecraftFuncs: {
|
|
254
|
+
[name: string]: Array<(log: (message: string, status?: number) => void, location: mc.Location) => void>;
|
|
255
|
+
} = {
|
|
256
|
+
runEntityCreatedEvent: [runEntityCreatedEvent, createOldHorse],
|
|
257
|
+
createOldHorse: [createOldHorse],
|
|
258
|
+
spawnItem: [spawnItem, testThatEntityIsFeatherItem],
|
|
259
|
+
createNoBlockExplosion: [createExplosion],
|
|
260
|
+
createFireAndWaterExplosions: [createFireAndWaterExplosions],
|
|
261
|
+
createExplosion: [createExplosion],
|
|
262
|
+
itemStacks: [itemStacks],
|
|
263
|
+
quickFoxLazyDog: [quickFoxLazyDog],
|
|
264
|
+
pistonEvent: [pistonEvent],
|
|
265
|
+
trapTick: [trapTick],
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
export function register(sampleManager: SampleManager) {
|
|
269
|
+
sampleManager.registerSamples(mojangMinecraftFuncs);
|
|
270
|
+
}
|
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
|
+
}
|