mineflayer 3.14.1 → 3.18.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.
package/examples/anvil.js CHANGED
@@ -63,24 +63,22 @@ bot.on('chat', async (username, message) => {
63
63
  }
64
64
  })
65
65
 
66
- function tossItem (name, amount) {
66
+ async function tossItem (name, amount) {
67
67
  amount = parseInt(amount, 10)
68
68
  const item = itemByName(name)
69
69
  if (!item) {
70
70
  bot.chat(`I have no ${name}`)
71
- } else if (amount) {
72
- bot.toss(item.type, null, amount, checkIfTossed)
73
71
  } else {
74
- bot.tossStack(item, checkIfTossed)
75
- }
76
-
77
- function checkIfTossed (err) {
78
- if (err) {
72
+ try {
73
+ if (amount) {
74
+ await bot.toss(item.type, null, amount)
75
+ bot.chat(`tossed ${amount} x ${name}`)
76
+ } else {
77
+ await bot.tossStack(item)
78
+ bot.chat(`tossed ${name}`)
79
+ }
80
+ } catch (err) {
79
81
  bot.chat(`unable to toss: ${err.message}`)
80
- } else if (amount) {
81
- bot.chat(`tossed ${amount} x ${name}`)
82
- } else {
83
- bot.chat(`tossed ${name}`)
84
82
  }
85
83
  }
86
84
  }
package/examples/chest.js CHANGED
@@ -220,38 +220,36 @@ async function watchFurnace () {
220
220
  bot.removeListener('chat', onChat)
221
221
  }
222
222
 
223
- function putInFurnace (where, name, amount) {
223
+ async function putInFurnace (where, name, amount) {
224
224
  const item = itemByName(furnace.items(), name)
225
225
  if (item) {
226
226
  const fn = {
227
227
  input: furnace.putInput,
228
228
  fuel: furnace.putFuel
229
229
  }[where]
230
- fn.call(furnace, item.type, null, amount, (err) => {
231
- if (err) {
232
- bot.chat(`unable to put ${amount} ${item.name}`)
233
- } else {
234
- bot.chat(`put ${amount} ${item.name}`)
235
- }
236
- })
230
+ try {
231
+ await fn.call(furnace, item.type, null, amount)
232
+ bot.chat(`put ${amount} ${item.name}`)
233
+ } catch (err) {
234
+ bot.chat(`unable to put ${amount} ${item.name}`)
235
+ }
237
236
  } else {
238
237
  bot.chat(`unknown item ${name}`)
239
238
  }
240
239
  }
241
240
 
242
- function takeFromFurnace (what) {
241
+ async function takeFromFurnace (what) {
243
242
  const fn = {
244
243
  input: furnace.takeInput,
245
244
  fuel: furnace.takeFuel,
246
245
  output: furnace.takeOutput
247
246
  }[what]
248
- fn.call(furnace, (err, item) => {
249
- if (err) {
250
- bot.chat(`unable to take ${item.name}`)
251
- } else {
252
- bot.chat(`took ${item.name}`)
253
- }
254
- })
247
+ try {
248
+ const item = await fn.call(furnace)
249
+ bot.chat(`took ${item.name}`)
250
+ } catch (err) {
251
+ bot.chat('unable to take')
252
+ }
255
253
  }
256
254
  }
257
255
  }
@@ -56,7 +56,7 @@ function sayItems (items = bot.inventory.items()) {
56
56
  }
57
57
  }
58
58
 
59
- function dig () {
59
+ async function dig () {
60
60
  let target
61
61
  if (bot.targetDigBlock) {
62
62
  bot.chat(`already digging ${bot.targetDigBlock.name}`)
@@ -64,19 +64,16 @@ function dig () {
64
64
  target = bot.blockAt(bot.entity.position.offset(0, -1, 0))
65
65
  if (target && bot.canDigBlock(target)) {
66
66
  bot.chat(`starting to dig ${target.name}`)
67
- bot.dig(target, onDiggingCompleted)
67
+ try {
68
+ await bot.dig(target)
69
+ bot.chat(`finished digging ${target.name}`)
70
+ } catch (err) {
71
+ console.log(err.stack)
72
+ }
68
73
  } else {
69
74
  bot.chat('cannot dig')
70
75
  }
71
76
  }
72
-
73
- function onDiggingCompleted (err) {
74
- if (err) {
75
- console.log(err.stack)
76
- return
77
- }
78
- bot.chat(`finished digging ${target.name}`)
79
- }
80
77
  }
81
78
 
82
79
  function build () {
@@ -87,28 +84,26 @@ function build () {
87
84
 
88
85
  let tryCount = 0
89
86
 
90
- function placeIfHighEnough () {
87
+ async function placeIfHighEnough () {
91
88
  if (bot.entity.position.y > jumpY) {
92
- bot.placeBlock(referenceBlock, vec3(0, 1, 0), (err) => {
93
- if (err) {
94
- tryCount++
95
- if (tryCount > 10) {
96
- bot.chat(err.message)
97
- bot.setControlState('jump', false)
98
- bot.removeListener('move', placeIfHighEnough)
99
- return
100
- }
101
- return
102
- }
89
+ try {
90
+ await bot.placeBlock(referenceBlock, vec3(0, 1, 0))
103
91
  bot.setControlState('jump', false)
104
92
  bot.removeListener('move', placeIfHighEnough)
105
93
  bot.chat('Placing a block was successful')
106
- })
94
+ } catch (err) {
95
+ tryCount++
96
+ if (tryCount > 10) {
97
+ bot.chat(err.message)
98
+ bot.setControlState('jump', false)
99
+ bot.removeListener('move', placeIfHighEnough)
100
+ }
101
+ }
107
102
  }
108
103
  }
109
104
  }
110
105
 
111
- function equipDirt () {
106
+ async function equipDirt () {
112
107
  const mcData = require('minecraft-data')(bot.version)
113
108
  let itemsByName
114
109
  if (bot.supportFeature('itemsAreNotBlocks')) {
@@ -116,13 +111,12 @@ function equipDirt () {
116
111
  } else if (bot.supportFeature('itemsAreAlsoBlocks')) {
117
112
  itemsByName = 'blocksByName'
118
113
  }
119
- bot.equip(mcData[itemsByName].dirt.id, 'hand', (err) => {
120
- if (err) {
121
- bot.chat(`unable to equip dirt: ${err.message}`)
122
- } else {
123
- bot.chat('equipped dirt')
124
- }
125
- })
114
+ try {
115
+ await bot.equip(mcData[itemsByName].dirt.id, 'hand')
116
+ bot.chat('equipped dirt')
117
+ } catch (err) {
118
+ bot.chat(`unable to equip dirt: ${err.message}`)
119
+ }
126
120
  }
127
121
 
128
122
  function itemToString (item) {
@@ -77,24 +77,22 @@ function sayItems (items = null) {
77
77
  }
78
78
  }
79
79
 
80
- function tossItem (name, amount) {
80
+ async function tossItem (name, amount) {
81
81
  amount = parseInt(amount, 10)
82
82
  const item = itemByName(name)
83
83
  if (!item) {
84
84
  bot.chat(`I have no ${name}`)
85
- } else if (amount) {
86
- bot.toss(item.type, null, amount, checkIfTossed)
87
85
  } else {
88
- bot.tossStack(item, checkIfTossed)
89
- }
90
-
91
- function checkIfTossed (err) {
92
- if (err) {
86
+ try {
87
+ if (amount) {
88
+ await bot.toss(item.type, null, amount)
89
+ bot.chat(`tossed ${amount} x ${name}`)
90
+ } else {
91
+ await bot.tossStack(item)
92
+ bot.chat(`tossed ${name}`)
93
+ }
94
+ } catch (err) {
93
95
  bot.chat(`unable to toss: ${err.message}`)
94
- } else if (amount) {
95
- bot.chat(`tossed ${amount} x ${name}`)
96
- } else {
97
- bot.chat(`tossed ${name}`)
98
96
  }
99
97
  }
100
98
  }
package/index.d.ts CHANGED
@@ -189,6 +189,7 @@ export interface Bot extends TypedEmitter<BotEvents> {
189
189
  heldItem: Item | null
190
190
  currentWindow: Window | null
191
191
  simpleClick: simpleClick
192
+ tablist: Tablist
192
193
 
193
194
  connect: (options: BotOptions) => void
194
195
 
@@ -228,7 +229,6 @@ export interface Bot extends TypedEmitter<BotEvents> {
228
229
 
229
230
  tabComplete: (
230
231
  str: string,
231
- cb: (matches: string[]) => void,
232
232
  assumeCommand?: boolean,
233
233
  sendBlockInSight?: boolean
234
234
  ) => Promise<string[]>
@@ -247,11 +247,11 @@ export interface Bot extends TypedEmitter<BotEvents> {
247
247
 
248
248
  hasPlugin: (plugin: Plugin) => boolean
249
249
 
250
- sleep: (bedBlock: Block, cb?: (err?: Error) => void) => Promise<void>
250
+ sleep: (bedBlock: Block) => Promise<void>
251
251
 
252
252
  isABed: (bedBlock: Block) => void
253
253
 
254
- wake: (cb?: (err?: Error) => void) => Promise<void>
254
+ wake: () => Promise<void>
255
255
 
256
256
  setControlState: (control: ControlState, state: boolean) => void
257
257
 
@@ -261,38 +261,34 @@ export interface Bot extends TypedEmitter<BotEvents> {
261
261
 
262
262
  getExplosionDamages: (targetEntity: Entity, position: Vec3, radius: number, rawDamages?: boolean) => number | null
263
263
 
264
- lookAt: (point: Vec3, force?: boolean, callback?: () => void) => Promise<void>
264
+ lookAt: (point: Vec3, force?: boolean) => Promise<void>
265
265
 
266
266
  look: (
267
267
  yaw: number,
268
268
  pitch: number,
269
- force?: boolean,
270
- callback?: () => void
269
+ force?: boolean
271
270
  ) => Promise<void>
272
271
 
273
272
  updateSign: (block: Block, text: string) => void
274
273
 
275
274
  equip: (
276
275
  item: Item | number,
277
- destination: EquipmentDestination | null,
278
- callback?: (error?: Error) => void
276
+ destination: EquipmentDestination | null
279
277
  ) => Promise<void>
280
278
 
281
279
  unequip: (
282
- destination: EquipmentDestination | null,
283
- callback?: () => void
280
+ destination: EquipmentDestination | null
284
281
  ) => Promise<void>
285
282
 
286
- tossStack: (item: Item, callback?: (error?: Error) => void) => Promise<void>
283
+ tossStack: (item: Item) => Promise<void>
287
284
 
288
285
  toss: (
289
286
  itemType: number,
290
287
  metadata: number | null,
291
- count: number | null,
292
- callback?: (err?: Error) => void
288
+ count: number | null
293
289
  ) => Promise<void>
294
290
 
295
- 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>)
296
292
 
297
293
  stopDigging: () => void
298
294
 
@@ -302,15 +298,15 @@ export interface Bot extends TypedEmitter<BotEvents> {
302
298
 
303
299
  placeEntity: (referenceBlock: Block, faceVector: Vec3) => Promise<Entity>
304
300
 
305
- activateBlock: (block: Block, callback?: (err?: Error) => void) => Promise<void>
301
+ activateBlock: (block: Block) => Promise<void>
306
302
 
307
- activateEntity: (block: Entity, callback?: (err?: Error) => void) => Promise<void>
303
+ activateEntity: (block: Entity) => Promise<void>
308
304
 
309
- activateEntityAt: (block: Entity, position: Vec3, callback?: (err?: Error) => void) => Promise<void>
305
+ activateEntityAt: (block: Entity, position: Vec3) => Promise<void>
310
306
 
311
- consume: (callback?: (err?: Error) => void) => Promise<void>
307
+ consume: () => Promise<void>
312
308
 
313
- fish: (callback?: (err?: Error) => void) => Promise<void>
309
+ fish: () => Promise<void>
314
310
 
315
311
  activateItem: (offhand?: boolean) => void
316
312
 
@@ -333,14 +329,12 @@ export interface Bot extends TypedEmitter<BotEvents> {
333
329
  craft: (
334
330
  recipe: Recipe,
335
331
  count: number | null,
336
- craftingTable: Block,
337
- callback?: () => void
332
+ craftingTable: Block
338
333
  ) => Promise<void>
339
334
 
340
335
  writeBook: (
341
336
  slot: number,
342
- pages: string[],
343
- callback?: (err?: Error) => void
337
+ pages: string[]
344
338
  ) => Promise<void>
345
339
 
346
340
  openContainer: (chest: Block | Entity) => Promise<Chest | Furnace | Dispenser>
@@ -362,8 +356,7 @@ export interface Bot extends TypedEmitter<BotEvents> {
362
356
  trade: (
363
357
  villagerInstance: Villager,
364
358
  tradeIndex: string | number,
365
- times?: number,
366
- cb?: (err?: Error) => void
359
+ times?: number
367
360
  ) => Promise<void>
368
361
 
369
362
  setCommandBlock: (pos: Vec3, command: string, trackOutput: boolean) => void
@@ -371,23 +364,21 @@ export interface Bot extends TypedEmitter<BotEvents> {
371
364
  clickWindow: (
372
365
  slot: number,
373
366
  mouseButton: number,
374
- mode: number,
375
- cb?: (err?: Error) => void
367
+ mode: number
376
368
  ) => Promise<void>
377
369
 
378
370
  putSelectedItemRange: (
379
371
  start: number,
380
372
  end: number,
381
373
  window: Window,
382
- slot: any,
383
- cb?: (err?: Error) => void
374
+ slot: any
384
375
  ) => Promise<void>
385
376
 
386
- putAway: (slot: number, cb?: (err?: Error) => void) => Promise<void>
377
+ putAway: (slot: number) => Promise<void>
387
378
 
388
379
  closeWindow: (window: Window) => void
389
380
 
390
- transfer: (options: TransferOptions, cb?: (err?: Error) => void) => Promise<void>
381
+ transfer: (options: TransferOptions) => Promise<void>
391
382
 
392
383
  openBlock: (block: Block, Class: new () => EventEmitter) => Promise<void>
393
384
 
@@ -395,15 +386,14 @@ export interface Bot extends TypedEmitter<BotEvents> {
395
386
 
396
387
  moveSlotItem: (
397
388
  sourceSlot: number,
398
- destSlot: number,
399
- cb?: (err?: Error) => void
389
+ destSlot: number
400
390
  ) => Promise<void>
401
391
 
402
392
  updateHeldItem: () => void
403
393
 
404
394
  getEquipmentDestSlot: (destination: string) => number
405
395
 
406
- waitForChunksToLoad: (cb?: (err?: Error) => void) => Promise<void>
396
+ waitForChunksToLoad: () => Promise<void>
407
397
 
408
398
  nearestEntity: (filter?: (entity: Entity) => boolean) => Entity | null
409
399
 
@@ -427,6 +417,11 @@ export interface simpleClick {
427
417
  rightMouse: (slot: number) => Promise<void>
428
418
  }
429
419
 
420
+ export interface Tablist {
421
+ header: ChatMessage
422
+ footer: ChatMessage
423
+ }
424
+
430
425
  export interface chatPatternOptions {
431
426
  repeat: boolean
432
427
  parse: boolean
@@ -573,11 +568,10 @@ export interface TransferOptions {
573
568
  export interface creativeMethods {
574
569
  setInventorySlot: (
575
570
  slot: number,
576
- item: Item | null,
577
- callback?: (error?: Error) => void
571
+ item: Item | null
578
572
  ) => Promise<void>
579
573
 
580
- flyTo: (destination: Vec3, cb?: () => void) => Promise<void>
574
+ flyTo: (destination: Vec3) => Promise<void>
581
575
 
582
576
  startFlying: () => void
583
577
 
@@ -628,15 +622,13 @@ export class Chest extends (EventEmitter as new () => TypedEmitter<StorageEvents
628
622
  deposit (
629
623
  itemType: number,
630
624
  metadata: number | null,
631
- count: number | null,
632
- cb?: (err?: Error) => void
625
+ count: number | null
633
626
  ): Promise<void>;
634
627
 
635
628
  withdraw (
636
629
  itemType: number,
637
630
  metadata: number | null,
638
- count: number | null,
639
- cb?: (err?: Error) => void
631
+ count: number | null
640
632
  ): Promise<void>;
641
633
 
642
634
  count (itemType: number, metadata: number | null): number;
@@ -652,24 +644,22 @@ export class Furnace extends (EventEmitter as new () => TypedEmitter<FurnaceEven
652
644
 
653
645
  close (): void;
654
646
 
655
- takeInput (cb: (err: Error | null, item: Item) => void): Promise<Item>;
647
+ takeInput (): Promise<Item>;
656
648
 
657
- takeFuel (cb: (err: Error | null, item: Item) => void): Promise<Item>;
649
+ takeFuel (): Promise<Item>;
658
650
 
659
- takeOutput (cb: (err: Error | null, item: Item) => void): Promise<Item>;
651
+ takeOutput (): Promise<Item>;
660
652
 
661
653
  putInput (
662
654
  itemType: number,
663
655
  metadata: number | null,
664
- count: number,
665
- cb?: (err?: Error) => void
656
+ count: number
666
657
  ): Promise<void>;
667
658
 
668
659
  putFuel (
669
660
  itemType: number,
670
661
  metadata: number | null,
671
- count: number,
672
- cb?: (err?: Error) => void
662
+ count: number
673
663
  ): Promise<void>;
674
664
 
675
665
  inputItem (): Item;
@@ -687,15 +677,13 @@ export class Dispenser extends (EventEmitter as new () => TypedEmitter<StorageEv
687
677
  deposit (
688
678
  itemType: number,
689
679
  metadata: number | null,
690
- count: number | null,
691
- cb?: (err?: Error) => void
680
+ count: number | null
692
681
  ): Promise<void>;
693
682
 
694
683
  withdraw (
695
684
  itemType: number,
696
685
  metadata: number | null,
697
- count: number | null,
698
- cb?: (err?: Error) => void
686
+ count: number | null
699
687
  ): Promise<void>;
700
688
 
701
689
  count (itemType: number, metadata: number | null): number;
@@ -713,15 +701,14 @@ export class EnchantmentTable extends (EventEmitter as new () => TypedEmitter<Co
713
701
  targetItem (): Item;
714
702
 
715
703
  enchant (
716
- choice: string | number,
717
- cb?: (err: Error | null, item: Item) => void
704
+ choice: string | number
718
705
  ): Promise<Item>;
719
706
 
720
- takeTargetItem (cb?: (err: Error | null, item: Item) => void): Promise<Item>;
707
+ takeTargetItem (): Promise<Item>;
721
708
 
722
- putTargetItem (item: Item, cb?: (err: Error | null) => void): Promise<Item>;
709
+ putTargetItem (item: Item): Promise<Item>;
723
710
 
724
- putLapis (item: Item, cb?: (err: Error | null) => void): Promise<Item>;
711
+ putLapis (item: Item): Promise<Item>;
725
712
  }
726
713
 
727
714
  export class Anvil {
@@ -742,13 +729,18 @@ export class Villager extends (EventEmitter as new () => TypedEmitter<Conditiona
742
729
  }
743
730
 
744
731
  export interface VillagerTrade {
745
- firstInput: Item
746
- output: Item
747
- hasSecondItem: boolean
748
- secondaryInput: Item | null
749
- disabled: boolean
750
- tooluses: number
751
- maxTradeuses: number
732
+ inputItem1: Item
733
+ outputItem: Item
734
+ inputItem2: Item | null
735
+ hasItem2: boolean
736
+ tradeDisabled: boolean
737
+ nbTradeUses: number
738
+ maximumNbTradeUses: number
739
+ xp?: number
740
+ specialPrice?: number
741
+ priceMultiplier?: number
742
+ demand?: number
743
+ realPrice?: number
752
744
  }
753
745
 
754
746
  export class ScoreBoard {
@@ -791,7 +783,7 @@ export class Team {
791
783
 
792
784
  update (name: string, friendlyFire: boolean, nameTagVisibility: string, collisionRule: string, formatting: number, prefix: string, suffix: string): void;
793
785
 
794
- displayName (member: string);
786
+ displayName (member: string): ChatMessage;
795
787
  }
796
788
 
797
789
  export type DisplaySlot =