mineflayer 3.17.0 → 4.1.0

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.
@@ -17,7 +17,7 @@ Crea bots para Minecraft con una API de JavaScript potente, estable y de alto ni
17
17
 
18
18
  ## Características
19
19
 
20
- * Soporta Minecraft 1.8, 1.9, 1.10, 1.11, 1.12, 1.13, 1.14, 1.15 y 1.16.
20
+ * Soporta Minecraft 1.8, 1.9, 1.10, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16, 1.17 y 1.18.
21
21
  * Rastreo e información de entidades.
22
22
  * Información sobre bloques. Puedes solicitar información de todo lo que te rodea. Encuentra bloques en milisegundos
23
23
  * Físicas y movimientos básicos - maneja todos los cuadros de colisión
@@ -34,6 +34,7 @@ Crea bots para Minecraft con una API de JavaScript potente, estable y de alto ni
34
34
  Echa un vistazo a nuestros [proyectos actuales](https://github.com/PrismarineJS/mineflayer/wiki/Big-Prismarine-projects)
35
35
 
36
36
  ## Instalación
37
+ Primero instala Node.js >= 14 desde [nodejs.org](https://nodejs.org/)
37
38
 
38
39
  `npm install mineflayer`
39
40
 
@@ -66,10 +67,9 @@ Si quieres aprender más, puedes mirar [aquí, ](https://www.youtube.com/playlis
66
67
 
67
68
  **Empezando**
68
69
 
69
- Sin especificar una versión, la versión del servidor se detectará automáticamente, puedes configurar una versión específica utilizando la opción de versión.
70
- Por ejemplo `version:" 1.8 "`.
70
+ Si no se especifica una versión, la versión del servidor se detectará automáticamente. Si no se especifica ningún tipo de autenticación, se utilizará el login de Mojang automáticamente.
71
71
 
72
- ### Ejemplo
72
+ ### Ejemplo: echo
73
73
  ```js
74
74
  const mineflayer = require('mineflayer')
75
75
 
@@ -92,7 +92,7 @@ bot.on('kicked', (reason, loggedIn) => console.log(reason, loggedIn))
92
92
  bot.on('error', err => console.log(err))
93
93
  ```
94
94
 
95
- ### Mira lo que tu bot hace
95
+ ### Mira lo que hace tu bot
96
96
 
97
97
  Gracias al proyecto [prismarine-viewer](https://github.com/PrismarineJS/prismarine-viewer), puedes ver en una pestaña del navegador qué está haciendo tu bot.
98
98
  Solo tienes que ejecutar `npm install prismarine-viewer` y añadir lo siguiente a tu bot:
@@ -124,7 +124,7 @@ Más ejemplos en la carpeta de [ejemplos](https://github.com/PrismarineJS/minefl
124
124
 
125
125
  ### Módulos
126
126
 
127
- La mayoría del desarrollo se esta produciendo dentro de pequeños módulos npm que son usados por mineflayer
127
+ La mayoría del desarrollo se está produciendo dentro de pequeños módulos npm que son usados por mineflayer
128
128
 
129
129
  #### The Node Way™
130
130
 
package/docs/history.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## 4.1.0
2
+
3
+ * 1.18.2 support
4
+ * Add nbt option to withdraw and deposit
5
+
6
+ ## 4.0.0
7
+
8
+ * useEntity maintains sneak state
9
+ * BREAKING: remove all callbacks in favor of promises
10
+
11
+ ## 3.18.0
12
+
13
+ * remove callbacks from types and docs
14
+
1
15
  ## 3.17.0
2
16
 
3
17
  * callback are now depreciated with mineflayer. Any use of them will print a warning
package/docs/tutorial.md CHANGED
@@ -17,7 +17,7 @@
17
17
  - [Logging in](#logging-in)
18
18
  - [Passing along functions](#passing-along-functions)
19
19
  - [Listening for an event](#listening-for-an-event)
20
- - [Callbacks](#callbacks)
20
+ - [Promises](#promises)
21
21
  - [Correct and incorrect approach](#correct-and-incorrect-approach)
22
22
  - [Advanced](#advanced)
23
23
  - [Asynchronousy](#asynchronousy)
@@ -430,18 +430,18 @@ To remove specific listener you can use `bot.removeListener()` method.
430
430
 
431
431
  Not only bot object, [`Chest`](http://mineflayer.prismarine.js.org/#/api?id=mineflayerchest), [`Furnace`](http://mineflayer.prismarine.js.org/#/api?id=mineflayerfurnace), [`Dispenser`](http://mineflayer.prismarine.js.org/#/api?id=mineflayerdispenser), [`EnchantmentTable`](http://mineflayer.prismarine.js.org/#/api?id=mineflayerenchantmenttable), [`Villager`](http://mineflayer.prismarine.js.org/#/api?id=mineflayervillager) object also have their own events!
432
432
 
433
- ### Callbacks
434
- A [callback](https://en.wikipedia.org/wiki/Callback_(computer_programming)) is a function that you can give to another function, that is expected to be *called back*, generally when that function ends.
435
- In Mineflayer, callbacks are often used to handle errors.
433
+ ### Promises
434
+ A [promise](https://nodejs.dev/learn/understanding-javascript-promises) is a function that you can use the `await` variable to wait on until it's job is complete. (you can omit the await to not wait for results)
436
435
 
437
436
  ```js
438
- bot.consume((error) => {
439
- if (error) { // This checks if an error occurred.
440
- console.log(error)
441
- } else {
437
+ async function consume (bot) {
438
+ try {
439
+ await bot.consume()
442
440
  console.log('Finished consuming')
441
+ } catch (err) {
442
+ console.log(error)
443
443
  }
444
- })
444
+ }
445
445
  ```
446
446
 
447
447
  The above code will try to consume what the bot is currently holding.
@@ -456,91 +456,40 @@ Below is an example of a bot that will craft oak logs into oak planks and then i
456
456
  Incorect approach ❌:
457
457
 
458
458
  ```js
459
- const plankRecipe = bot.recipesFor(5)[0] // Get the first recipe for item id 5, which is oak planks.
460
- bot.craft(plankRecipe, 1) // ❌ start crafting oak planks.
459
+ function craft (bot) {
460
+ const mcData = require('minecraft-data')(bot.version)
461
+ const plankRecipe = bot.recipesFor(mcData.itemsByName.oak_planks.id ?? mcData.itemsByName.planks.id)[0] // Get the first recipe for oak planks
462
+ bot.craft(plankRecipe, 1) // ❌ start crafting oak planks.
461
463
 
462
- const stickRecipe = bot.recipesFor(280)[0] // Get the first recipe for item id 5, which is sticks.
463
- bot.craft(stickRecipe, 1) // ❌ start crafting sticks.
464
+ const stickRecipe = bot.recipesFor(mcData.itemsByName.sticks.id)[0] // Get the first recipe for sticks
465
+ bot.craft(stickRecipe, 1) // ❌ start crafting sticks.
466
+ }
464
467
  ```
465
468
 
466
- Correct approach with callbacks ✔️:
469
+ Correct approach with promises ✔️:
467
470
 
468
471
  ```js
469
- const plankRecipe = bot.recipesFor(5)[0]
470
-
471
- bot.craft(plankRecipe, 1, null, (error) => {
472
- // After bot.craft(plankRecipe, ...) is finished, this callback is called and we continue. ✔️
473
- if (error) { // Check if an error happened.
474
- console.log(error)
475
- } else {
476
- const stickRecipe = bot.recipesFor(280)[0]
477
-
478
- bot.craft(stickRecipe, 1, null, (error) => {
479
- // After bot.craft(stickRecipe, ...) is finished, this callback is called and we continue. ✔️
480
- if (error) { // Check if an error happened.
481
- console.log(error)
482
- } else {
483
- bot.chat('Crafting Sticks finished')
484
- }
485
- })
486
- }
487
- })
472
+ async function craft (bot) {
473
+ const mcData = require('minecraft-data')(bot.version)
474
+ const plankRecipe = bot.recipesFor(mcData.itemsByName.oak_planks.id ?? mcData.itemsByName.planks.id)[0]
475
+ await bot.craft(plankRecipe, 1, null)
476
+ const stickRecipe = bot.recipesFor(mcData.itemsByName.sticks.id)[0]
477
+ await bot.craft(stickRecipe, 1, null)
478
+ bot.chat('Crafting Sticks finished')
479
+ }
488
480
  ```
489
481
 
490
482
  The reason the incorrect approach is wrong is because when `bot.craft()` is called, the code will continue below while the bot is crafting.
491
483
  By the time the code reaches the second `bot.craft()`, the first probably hasn't finished yet, which means the wanted resource is not available yet.
492
- Using callbacks can fix this because they will only be called after the `bot.craft()` is finished.
484
+ Using promises can fix this because they will only be called after the `bot.craft()` is finished.
493
485
 
494
- More on the [bot.craft()](https://mineflayer.prismarine.js.org/#/api?id=botcraftrecipe-count-craftingtable-callback) method.
486
+ More on the [bot.craft()](https://github.com/PrismarineJS/mineflayer/blob/master/docs/api.md#botcraftrecipe-count-craftingtable) method.
495
487
 
496
488
  ## Advanced
497
489
 
498
490
  The following concepts aren't necessary to create a Mineflayer bot, but they can be useful to understand and create more advanced bots.
499
491
  We assume you have understood the [Basics](#basics) tutorial.
500
492
 
501
- ### Asynchronousy
502
- In Javascript, asynchronousy is an important concept.
503
- By default, Javascript will run everything line by line, and only go to the next line if the current line is done. This is called blocking.
504
- However, sometimes you have to do something that takes a relatively long time, and you don't want your whole program to block and wait for it to finish.
505
-
506
- Interacting with the filesystem is often done using asynchronousy, because reading and writing large files can take a long time.
507
-
508
- ```js
509
- const myPromise = new Promise((resolve, reject) => {
510
- setTimeout(() => {
511
- resolve('Success!') // Yay! Everything went well!
512
- }, 1000)
513
- })
514
-
515
- myPromise.then((successMessage) => {
516
- console.log(successMessage)
517
- })
518
-
519
- myPromise.catch((error) => {
520
- console.log(error)
521
- })
522
- ```
523
-
524
- The above codes uses what is called a Promise. A promise promises it will eventually complete.
525
- The function given you a promise always has 2 parameters, a `resolve` function and a `reject` function.
526
- If the promise is successful, it will call the `resolve` function, otherwise it will call the `reject` function.
527
- The above code uses a `setTimeout`, which calls the given function after the set amount of milliseconds, 1000 in this case.
528
- You can then tell the promise what it should do when it succeeds with `.then(function)` or when it fails with `.catch(function)`
529
-
530
- The `.then` and `.catch` function can also be chained together with the promise to simplify the code.
531
-
532
- ```js
533
- const myPromise = new Promise((resolve, reject) => {
534
- setTimeout(() => {
535
- resolve('Success!') // Yay! Everything went well!
536
- }, 1000)
537
- }).then((successMessage) => {
538
- console.log(successMessage)
539
- }).catch((error) => {
540
- console.log(error)
541
- })
542
- ```
543
-
544
493
  ### Loop over an object
545
494
 
546
495
  The `for of` loop described in the [loops](#loops) chapter can also be used to loop over an object.
package/index.d.ts CHANGED
@@ -229,7 +229,6 @@ export interface Bot extends TypedEmitter<BotEvents> {
229
229
 
230
230
  tabComplete: (
231
231
  str: string,
232
- cb: (matches: string[]) => void,
233
232
  assumeCommand?: boolean,
234
233
  sendBlockInSight?: boolean
235
234
  ) => Promise<string[]>
@@ -248,11 +247,11 @@ export interface Bot extends TypedEmitter<BotEvents> {
248
247
 
249
248
  hasPlugin: (plugin: Plugin) => boolean
250
249
 
251
- sleep: (bedBlock: Block, cb?: (err?: Error) => void) => Promise<void>
250
+ sleep: (bedBlock: Block) => Promise<void>
252
251
 
253
252
  isABed: (bedBlock: Block) => void
254
253
 
255
- wake: (cb?: (err?: Error) => void) => Promise<void>
254
+ wake: () => Promise<void>
256
255
 
257
256
  setControlState: (control: ControlState, state: boolean) => void
258
257
 
@@ -262,38 +261,34 @@ export interface Bot extends TypedEmitter<BotEvents> {
262
261
 
263
262
  getExplosionDamages: (targetEntity: Entity, position: Vec3, radius: number, rawDamages?: boolean) => number | null
264
263
 
265
- lookAt: (point: Vec3, force?: boolean, callback?: () => void) => Promise<void>
264
+ lookAt: (point: Vec3, force?: boolean) => Promise<void>
266
265
 
267
266
  look: (
268
267
  yaw: number,
269
268
  pitch: number,
270
- force?: boolean,
271
- callback?: () => void
269
+ force?: boolean
272
270
  ) => Promise<void>
273
271
 
274
272
  updateSign: (block: Block, text: string) => void
275
273
 
276
274
  equip: (
277
275
  item: Item | number,
278
- destination: EquipmentDestination | null,
279
- callback?: (error?: Error) => void
276
+ destination: EquipmentDestination | null
280
277
  ) => Promise<void>
281
278
 
282
279
  unequip: (
283
- destination: EquipmentDestination | null,
284
- callback?: () => void
280
+ destination: EquipmentDestination | null
285
281
  ) => Promise<void>
286
282
 
287
- tossStack: (item: Item, callback?: (error?: Error) => void) => Promise<void>
283
+ tossStack: (item: Item) => Promise<void>
288
284
 
289
285
  toss: (
290
286
  itemType: number,
291
287
  metadata: number | null,
292
- count: number | null,
293
- callback?: (err?: Error) => void
288
+ count: number | null
294
289
  ) => Promise<void>
295
290
 
296
- dig: ((block: Block, forceLook?: boolean | 'ignore', callback?: (err?: Error) => void) => Promise<void>) & ((block: Block, forceLook: boolean | 'ignore', digFace: 'auto' | Vec3 | 'raycast', callback?: (err?: Error) => void) => Promise<void>)
291
+ dig: ((block: Block, forceLook?: boolean | 'ignore') => Promise<void>) & ((block: Block, forceLook: boolean | 'ignore', digFace: 'auto' | Vec3 | 'raycast') => Promise<void>)
297
292
 
298
293
  stopDigging: () => void
299
294
 
@@ -303,15 +298,15 @@ export interface Bot extends TypedEmitter<BotEvents> {
303
298
 
304
299
  placeEntity: (referenceBlock: Block, faceVector: Vec3) => Promise<Entity>
305
300
 
306
- activateBlock: (block: Block, callback?: (err?: Error) => void) => Promise<void>
301
+ activateBlock: (block: Block) => Promise<void>
307
302
 
308
- activateEntity: (block: Entity, callback?: (err?: Error) => void) => Promise<void>
303
+ activateEntity: (block: Entity) => Promise<void>
309
304
 
310
- activateEntityAt: (block: Entity, position: Vec3, callback?: (err?: Error) => void) => Promise<void>
305
+ activateEntityAt: (block: Entity, position: Vec3) => Promise<void>
311
306
 
312
- consume: (callback?: (err?: Error) => void) => Promise<void>
307
+ consume: () => Promise<void>
313
308
 
314
- fish: (callback?: (err?: Error) => void) => Promise<void>
309
+ fish: () => Promise<void>
315
310
 
316
311
  activateItem: (offhand?: boolean) => void
317
312
 
@@ -333,15 +328,13 @@ export interface Bot extends TypedEmitter<BotEvents> {
333
328
 
334
329
  craft: (
335
330
  recipe: Recipe,
336
- count: number | null,
337
- craftingTable: Block,
338
- callback?: () => void
331
+ count?: number,
332
+ craftingTable?: Block
339
333
  ) => Promise<void>
340
334
 
341
335
  writeBook: (
342
336
  slot: number,
343
- pages: string[],
344
- callback?: (err?: Error) => void
337
+ pages: string[]
345
338
  ) => Promise<void>
346
339
 
347
340
  openContainer: (chest: Block | Entity) => Promise<Chest | Furnace | Dispenser>
@@ -363,8 +356,7 @@ export interface Bot extends TypedEmitter<BotEvents> {
363
356
  trade: (
364
357
  villagerInstance: Villager,
365
358
  tradeIndex: string | number,
366
- times?: number,
367
- cb?: (err?: Error) => void
359
+ times?: number
368
360
  ) => Promise<void>
369
361
 
370
362
  setCommandBlock: (pos: Vec3, command: string, trackOutput: boolean) => void
@@ -372,23 +364,21 @@ export interface Bot extends TypedEmitter<BotEvents> {
372
364
  clickWindow: (
373
365
  slot: number,
374
366
  mouseButton: number,
375
- mode: number,
376
- cb?: (err?: Error) => void
367
+ mode: number
377
368
  ) => Promise<void>
378
369
 
379
370
  putSelectedItemRange: (
380
371
  start: number,
381
372
  end: number,
382
373
  window: Window,
383
- slot: any,
384
- cb?: (err?: Error) => void
374
+ slot: any
385
375
  ) => Promise<void>
386
376
 
387
- putAway: (slot: number, cb?: (err?: Error) => void) => Promise<void>
377
+ putAway: (slot: number) => Promise<void>
388
378
 
389
379
  closeWindow: (window: Window) => void
390
380
 
391
- transfer: (options: TransferOptions, cb?: (err?: Error) => void) => Promise<void>
381
+ transfer: (options: TransferOptions) => Promise<void>
392
382
 
393
383
  openBlock: (block: Block, Class: new () => EventEmitter) => Promise<void>
394
384
 
@@ -396,15 +386,14 @@ export interface Bot extends TypedEmitter<BotEvents> {
396
386
 
397
387
  moveSlotItem: (
398
388
  sourceSlot: number,
399
- destSlot: number,
400
- cb?: (err?: Error) => void
389
+ destSlot: number
401
390
  ) => Promise<void>
402
391
 
403
392
  updateHeldItem: () => void
404
393
 
405
394
  getEquipmentDestSlot: (destination: string) => number
406
395
 
407
- waitForChunksToLoad: (cb?: (err?: Error) => void) => Promise<void>
396
+ waitForChunksToLoad: () => Promise<void>
408
397
 
409
398
  nearestEntity: (filter?: (entity: Entity) => boolean) => Entity | null
410
399
 
@@ -579,11 +568,10 @@ export interface TransferOptions {
579
568
  export interface creativeMethods {
580
569
  setInventorySlot: (
581
570
  slot: number,
582
- item: Item | null,
583
- callback?: (error?: Error) => void
571
+ item: Item | null
584
572
  ) => Promise<void>
585
573
 
586
- flyTo: (destination: Vec3, cb?: () => void) => Promise<void>
574
+ flyTo: (destination: Vec3) => Promise<void>
587
575
 
588
576
  startFlying: () => void
589
577
 
@@ -634,15 +622,13 @@ export class Chest extends (EventEmitter as new () => TypedEmitter<StorageEvents
634
622
  deposit (
635
623
  itemType: number,
636
624
  metadata: number | null,
637
- count: number | null,
638
- cb?: (err?: Error) => void
625
+ count: number | null
639
626
  ): Promise<void>;
640
627
 
641
628
  withdraw (
642
629
  itemType: number,
643
630
  metadata: number | null,
644
- count: number | null,
645
- cb?: (err?: Error) => void
631
+ count: number | null
646
632
  ): Promise<void>;
647
633
 
648
634
  count (itemType: number, metadata: number | null): number;
@@ -658,24 +644,22 @@ export class Furnace extends (EventEmitter as new () => TypedEmitter<FurnaceEven
658
644
 
659
645
  close (): void;
660
646
 
661
- takeInput (cb: (err: Error | null, item: Item) => void): Promise<Item>;
647
+ takeInput (): Promise<Item>;
662
648
 
663
- takeFuel (cb: (err: Error | null, item: Item) => void): Promise<Item>;
649
+ takeFuel (): Promise<Item>;
664
650
 
665
- takeOutput (cb: (err: Error | null, item: Item) => void): Promise<Item>;
651
+ takeOutput (): Promise<Item>;
666
652
 
667
653
  putInput (
668
654
  itemType: number,
669
655
  metadata: number | null,
670
- count: number,
671
- cb?: (err?: Error) => void
656
+ count: number
672
657
  ): Promise<void>;
673
658
 
674
659
  putFuel (
675
660
  itemType: number,
676
661
  metadata: number | null,
677
- count: number,
678
- cb?: (err?: Error) => void
662
+ count: number
679
663
  ): Promise<void>;
680
664
 
681
665
  inputItem (): Item;
@@ -693,15 +677,13 @@ export class Dispenser extends (EventEmitter as new () => TypedEmitter<StorageEv
693
677
  deposit (
694
678
  itemType: number,
695
679
  metadata: number | null,
696
- count: number | null,
697
- cb?: (err?: Error) => void
680
+ count: number | null
698
681
  ): Promise<void>;
699
682
 
700
683
  withdraw (
701
684
  itemType: number,
702
685
  metadata: number | null,
703
- count: number | null,
704
- cb?: (err?: Error) => void
686
+ count: number | null
705
687
  ): Promise<void>;
706
688
 
707
689
  count (itemType: number, metadata: number | null): number;
@@ -719,15 +701,14 @@ export class EnchantmentTable extends (EventEmitter as new () => TypedEmitter<Co
719
701
  targetItem (): Item;
720
702
 
721
703
  enchant (
722
- choice: string | number,
723
- cb?: (err: Error | null, item: Item) => void
704
+ choice: string | number
724
705
  ): Promise<Item>;
725
706
 
726
- takeTargetItem (cb?: (err: Error | null, item: Item) => void): Promise<Item>;
707
+ takeTargetItem (): Promise<Item>;
727
708
 
728
- putTargetItem (item: Item, cb?: (err: Error | null) => void): Promise<Item>;
709
+ putTargetItem (item: Item): Promise<Item>;
729
710
 
730
- putLapis (item: Item, cb?: (err: Error | null) => void): Promise<Item>;
711
+ putLapis (item: Item): Promise<Item>;
731
712
  }
732
713
 
733
714
  export class Anvil {