mineflayer 3.18.0 → 4.2.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.
Files changed (43) hide show
  1. package/.github/workflows/ci.yml +1 -1
  2. package/README.md +1 -1
  3. package/docs/README.md +1 -1
  4. package/docs/api.md +38 -9
  5. package/docs/es/README_ES.md +6 -6
  6. package/docs/history.md +20 -0
  7. package/docs/ru/README_RU.md +1 -2
  8. package/docs/tutorial.md +27 -78
  9. package/examples/inventory.js +1 -1
  10. package/index.d.ts +8 -4
  11. package/lib/bossbar.js +1 -1
  12. package/lib/loader.js +4 -4
  13. package/lib/plugins/anvil.js +4 -4
  14. package/lib/plugins/bed.js +3 -5
  15. package/lib/plugins/block_actions.js +2 -2
  16. package/lib/plugins/blocks.js +2 -2
  17. package/lib/plugins/book.js +8 -10
  18. package/lib/plugins/chat.js +3 -22
  19. package/lib/plugins/chest.js +5 -6
  20. package/lib/plugins/craft.js +3 -4
  21. package/lib/plugins/creative.js +15 -13
  22. package/lib/plugins/digging.js +2 -25
  23. package/lib/plugins/enchantment_table.js +5 -6
  24. package/lib/plugins/entities.js +9 -9
  25. package/lib/plugins/fishing.js +4 -6
  26. package/lib/plugins/furnace.js +6 -7
  27. package/lib/plugins/game.js +4 -1
  28. package/lib/plugins/generic_place.js +2 -2
  29. package/lib/plugins/inventory.js +23 -25
  30. package/lib/plugins/physics.js +6 -8
  31. package/lib/plugins/place_block.js +2 -2
  32. package/lib/plugins/place_entity.js +2 -2
  33. package/lib/plugins/settings.js +25 -5
  34. package/lib/plugins/simple_inventory.js +4 -5
  35. package/lib/plugins/tablist.js +1 -1
  36. package/lib/plugins/villager.js +5 -6
  37. package/lib/promise_utils.js +1 -25
  38. package/lib/scoreboard.js +1 -1
  39. package/lib/team.js +2 -2
  40. package/lib/version.js +1 -1
  41. package/package.json +4 -3
  42. package/lib/features.json +0 -337
  43. package/lib/supportFeature.js +0 -27
@@ -1,5 +1,4 @@
1
1
  const { once } = require('events')
2
- const { printCallbackDepreciation } = require('../promise_utils')
3
2
 
4
3
  module.exports = inject
5
4
 
@@ -34,7 +33,7 @@ function inject (bot, options) {
34
33
  bot.addChatPattern = (name, pattern, opts = {}) => {
35
34
  if (!(pattern instanceof RegExp)) throw new Error('Pattern parameter should be of type RegExp')
36
35
  const { repeat = true, deprecated = false, parse = false } = opts
37
- _patterns[_length++] = {
36
+ _patterns[_length] = {
38
37
  name,
39
38
  patterns: [pattern],
40
39
  position: 0,
@@ -44,7 +43,7 @@ function inject (bot, options) {
44
43
  repeat,
45
44
  parse
46
45
  }
47
- return _length
46
+ return _length++ // increment length after we give it back to the user
48
47
  }
49
48
 
50
49
  bot.removeChatPattern = name => {
@@ -170,7 +169,7 @@ function inject (bot, options) {
170
169
  chatWithHeader('', message)
171
170
  }
172
171
 
173
- bot.tabComplete = callbackify(tabComplete)
172
+ bot.tabComplete = tabComplete
174
173
 
175
174
  function addDefaultPatterns () {
176
175
  if (!defaultChatPatterns) return
@@ -194,21 +193,3 @@ function inject (bot, options) {
194
193
  }
195
194
  bot.awaitMessage = awaitMessage
196
195
  }
197
-
198
- function callbackify (f) { // specifically for this function because cb isn't the last parameter
199
- return function (...args) {
200
- const cb = args[1]
201
- args.splice(1, 1)
202
- return f(...args).then(r => {
203
- if (cb) {
204
- printCallbackDepreciation()
205
- cb(undefined, r)
206
- } return r
207
- }, err => {
208
- if (cb) {
209
- printCallbackDepreciation()
210
- cb(err)
211
- } else throw err
212
- })
213
- }
214
- }
@@ -1,9 +1,8 @@
1
- const { callbackify } = require('../promise_utils')
2
1
 
3
2
  module.exports = inject
4
3
 
5
4
  function inject (bot) {
6
- const allowedWindowTypes = ['minecraft:generic', 'minecraft:chest', 'minecraft:dispenser', 'minecraft:shulker_box', 'minecraft:hopper', 'minecraft:container', 'minecraft:dropper']
5
+ const allowedWindowTypes = ['minecraft:generic', 'minecraft:chest', 'minecraft:dispenser', 'minecraft:shulker_box', 'minecraft:hopper', 'minecraft:container', 'minecraft:dropper', 'minecraft:trapped_chest']
7
6
 
8
7
  function matchWindowType (window) {
9
8
  for (const type of allowedWindowTypes) {
@@ -14,7 +13,7 @@ function inject (bot) {
14
13
 
15
14
  async function openContainer (containerToOpen) {
16
15
  let chest
17
- if (containerToOpen.constructor.name === 'Block') {
16
+ if (containerToOpen.constructor.name === 'Block' && allowedWindowTypes.map(name => name.replace('minecraft:', '')).includes(containerToOpen.name)) {
18
17
  chest = await bot.openBlock(containerToOpen)
19
18
  } else if (containerToOpen.constructor.name === 'Entity') {
20
19
  chest = await bot.openEntity(containerToOpen)
@@ -26,7 +25,7 @@ function inject (bot) {
26
25
  return chest
27
26
  }
28
27
 
29
- bot.openContainer = callbackify(openContainer)
30
- bot.openChest = callbackify(openContainer)
31
- bot.openDispenser = callbackify(openContainer)
28
+ bot.openContainer = openContainer
29
+ bot.openChest = openContainer
30
+ bot.openDispenser = openContainer
32
31
  }
@@ -1,12 +1,11 @@
1
1
  const assert = require('assert')
2
2
  const { once } = require('events')
3
- const { callbackify } = require('../promise_utils')
4
3
 
5
4
  module.exports = inject
6
5
 
7
6
  function inject (bot, { version }) {
8
- const Item = require('prismarine-item')(version)
9
- const Recipe = require('prismarine-recipe')(version).Recipe
7
+ const Item = require('prismarine-item')(bot.version)
8
+ const Recipe = require('prismarine-recipe')(version).Recipe // TODO: update for prismarine-registry
10
9
 
11
10
  async function craft (recipe, count, craftingTable) {
12
11
  assert.ok(recipe)
@@ -226,7 +225,7 @@ function inject (bot, { version }) {
226
225
  return true
227
226
  }
228
227
 
229
- bot.craft = callbackify(craft)
228
+ bot.craft = craft
230
229
  bot.recipesFor = recipesFor
231
230
  bot.recipesAll = recipesAll
232
231
  }
@@ -1,32 +1,30 @@
1
1
  const assert = require('assert')
2
2
  const { Vec3 } = require('vec3')
3
- const { callbackify, sleep } = require('../promise_utils')
3
+ const { sleep, onceWithCleanup } = require('../promise_utils')
4
4
  const { once } = require('events')
5
5
 
6
6
  module.exports = inject
7
7
 
8
- function inject (bot, { version }) {
9
- const Item = require('prismarine-item')(version)
8
+ function inject (bot) {
9
+ const Item = require('prismarine-item')(bot.version)
10
10
 
11
11
  // these features only work when you are in creative mode.
12
12
  bot.creative = {
13
- setInventorySlot: callbackify(setInventorySlot),
14
- flyTo: callbackify(flyTo),
13
+ setInventorySlot,
14
+ flyTo: flyTo,
15
15
  startFlying,
16
- stopFlying
16
+ stopFlying,
17
+ clearSlot: slotNum => setInventorySlot(slotNum, null),
18
+ clearInventory
17
19
  }
18
20
 
19
21
  const creativeSlotsUpdates = []
20
- bot._client.on('set_slot', ({ windowId, slot, item }) => {
21
- if (windowId === 0 && creativeSlotsUpdates[slot]) {
22
- bot.emit(`set_creative_slot_${slot}`)
23
- }
24
- })
25
22
 
26
- // WARN: This method should not be called twice on the same slot before first callback succeeds
23
+ // WARN: This method should not be called twice on the same slot before first promise succeeds
27
24
  async function setInventorySlot (slot, item) {
28
25
  assert(slot >= 0 && slot <= 44)
29
26
 
27
+ if (Item.equal(bot.inventory.slots[slot], item, true)) return
30
28
  if (creativeSlotsUpdates[slot]) {
31
29
  throw new Error(`Setting slot ${slot} cancelled due to calling bot.creative.setInventorySlot(${slot}, ...) again`)
32
30
  }
@@ -37,10 +35,14 @@ function inject (bot, { version }) {
37
35
  item: Item.toNotch(item)
38
36
  })
39
37
 
40
- await once(bot, `set_creative_slot_${slot}`)
38
+ await onceWithCleanup(bot.inventory, `updateSlot:${slot}`, { checkCondition: (oldItem, newItem) => item === null ? newItem === null : newItem?.name === item.name && newItem?.count === item.count && newItem?.metadata === item.metadata })
41
39
  creativeSlotsUpdates[slot] = false
42
40
  }
43
41
 
42
+ async function clearInventory () {
43
+ return Promise.all(bot.inventory.slots.filter(item => item).map(item => setInventorySlot(item.slot, null)))
44
+ }
45
+
44
46
  let normalGravity = null
45
47
  const flyingSpeedPerUpdate = 0.5
46
48
 
@@ -1,5 +1,5 @@
1
1
  const { performance } = require('perf_hooks')
2
- const { createDoneTask, createTask, printCallbackDepreciation } = require('../promise_utils')
2
+ const { createDoneTask, createTask } = require('../promise_utils')
3
3
  const BlockFaces = require('prismarine-world').iterators.BlockFace
4
4
  const { Vec3 } = require('vec3')
5
5
 
@@ -198,35 +198,12 @@ function inject (bot) {
198
198
  return block.digTime(type, creative, bot.entity.isInWater, !bot.entity.onGround, enchantments, bot.entity.effects)
199
199
  }
200
200
 
201
- bot.dig = callbackify(dig)
201
+ bot.dig = dig
202
202
  bot.stopDigging = noop
203
203
  bot.canDigBlock = canDigBlock
204
204
  bot.digTime = digTime
205
205
  }
206
206
 
207
- function callbackify (f) { // specifically for this function because cb could be the non-last parameter
208
- return function (...args) {
209
- const cbIndex = typeof args[1] === 'function' ? 1 : (typeof args[2] === 'function' ? 2 : 3)
210
- const cb = args[cbIndex]
211
-
212
- if (cbIndex === 1) args[1] = true
213
- else if (typeof args[1] === 'string') args[1] = args[1] === 'ignore' ? args[1] : false
214
- else args[1] = !!args[1] // coerce to boolean
215
-
216
- return f(...args).then(r => {
217
- if (cb) {
218
- printCallbackDepreciation()
219
- cb(undefined, r)
220
- } return r
221
- }, err => {
222
- if (cb) {
223
- printCallbackDepreciation()
224
- cb(err)
225
- } else throw err
226
- })
227
- }
228
- }
229
-
230
207
  function noop (err) {
231
208
  if (err) throw err
232
209
  }
@@ -1,6 +1,5 @@
1
1
  const assert = require('assert')
2
2
  const { once } = require('events')
3
- const { callbackify } = require('../promise_utils')
4
3
 
5
4
  module.exports = inject
6
5
 
@@ -15,10 +14,10 @@ function inject (bot) {
15
14
 
16
15
  resetEnchantmentOptions()
17
16
 
18
- enchantmentTable.enchant = callbackify(enchant)
19
- enchantmentTable.takeTargetItem = callbackify(takeTargetItem)
20
- enchantmentTable.putTargetItem = callbackify(putTargetItem)
21
- enchantmentTable.putLapis = callbackify(putLapis)
17
+ enchantmentTable.enchant = enchant
18
+ enchantmentTable.takeTargetItem = takeTargetItem
19
+ enchantmentTable.putTargetItem = putTargetItem
20
+ enchantmentTable.putLapis = putLapis
22
21
  enchantmentTable.targetItem = function () { return this.slots[0] }
23
22
 
24
23
  bot._client.on('craft_progress_bar', onUpdateWindowProperty)
@@ -100,5 +99,5 @@ function inject (bot) {
100
99
  }
101
100
  }
102
101
 
103
- bot.openEnchantmentTable = callbackify(openEnchantmentTable)
102
+ bot.openEnchantmentTable = openEnchantmentTable
104
103
  }
@@ -24,13 +24,12 @@ const entityStatusEvents = {
24
24
  10: 'entityEatingGrass'
25
25
  }
26
26
 
27
- function inject (bot, { version }) {
28
- const Entity = require('prismarine-entity')(version)
29
- const objects = require('minecraft-data')(version).objects
30
- const mobs = require('minecraft-data')(version).mobs
31
- const entitiesArray = require('minecraft-data')(version).entitiesArray
32
- const Item = require('prismarine-item')(version)
33
- const ChatMessage = require('prismarine-chat')(version)
27
+ function inject (bot) {
28
+ const { objects, mobs, entitiesArray } = bot.registry
29
+ const Entity = require('prismarine-entity')(bot.version) // TODO: update for prismarine-registry
30
+ const Item = require('prismarine-item')(bot.version)
31
+ const ChatMessage = require('prismarine-chat')(bot.version) // TODO: update for prismarine-registry
32
+
34
33
  // ONLY 1.17 has this destroy_entity packet which is the same thing as entity_destroy packet except the entity is singular
35
34
  // 1.17.1 reverted this change so this is just a simpler fix
36
35
  bot._client.on('destroy_entity', (packet) => {
@@ -588,6 +587,7 @@ function inject (bot, { version }) {
588
587
  }
589
588
 
590
589
  function useEntity (target, leftClick, x, y, z) {
590
+ const sneaking = bot.getControlState('sneak')
591
591
  if (x && y && z) {
592
592
  bot._client.write('use_entity', {
593
593
  target: target.id,
@@ -595,13 +595,13 @@ function inject (bot, { version }) {
595
595
  x,
596
596
  y,
597
597
  z,
598
- sneaking: false
598
+ sneaking
599
599
  })
600
600
  } else {
601
601
  bot._client.write('use_entity', {
602
602
  target: target.id,
603
603
  mouse: leftClick,
604
- sneaking: false
604
+ sneaking
605
605
  })
606
606
  }
607
607
  }
@@ -1,17 +1,15 @@
1
1
  const { Vec3 } = require('vec3')
2
- const { callbackify, createDoneTask, createTask } = require('../promise_utils')
2
+ const { createDoneTask, createTask } = require('../promise_utils')
3
3
 
4
4
  module.exports = inject
5
5
 
6
6
  function inject (bot) {
7
- const mcData = require('minecraft-data')(bot.version)
8
-
9
7
  let bobberId = 90
10
8
  // Before 1.14 the bobber entity keep changing name at each version (but the id stays 90)
11
9
  // 1.14 changes the id, but hopefully we can stick with the name: fishing_bobber
12
10
  // the alternative would be to rename it in all version of mcData
13
11
  if (bot.supportFeature('fishingBobberCorrectlyNamed')) {
14
- bobberId = mcData.entitiesByName.fishing_bobber.id
12
+ bobberId = bot.registry.entitiesByName.fishing_bobber.id
15
13
  }
16
14
 
17
15
  let fishingTask = createDoneTask()
@@ -27,7 +25,7 @@ function inject (bot) {
27
25
  if (!lastBobber || fishingTask.done) return
28
26
 
29
27
  const pos = lastBobber.position
30
- const parts = mcData.particlesByName
28
+ const parts = bot.registry.particlesByName
31
29
  if (packet.particleId === (parts?.fishing ?? parts.bubble).id && packet.particles === 6 && pos.distanceTo(new Vec3(packet.x, pos.y, packet.z)) <= 1.23) {
32
30
  bot.activateItem()
33
31
  lastBobber = undefined
@@ -54,5 +52,5 @@ function inject (bot) {
54
52
  await fishingTask.promise
55
53
  }
56
54
 
57
- bot.fish = callbackify(fish)
55
+ bot.fish = fish
58
56
  }
@@ -1,5 +1,4 @@
1
1
  const assert = require('assert')
2
- const { callbackify } = require('../promise_utils')
3
2
 
4
3
  module.exports = inject
5
4
 
@@ -25,11 +24,11 @@ function inject (bot) {
25
24
  furnace.totalProgress = null
26
25
  furnace.progress = null
27
26
  furnace.progressSeconds = null
28
- furnace.takeInput = callbackify(takeInput)
29
- furnace.takeFuel = callbackify(takeFuel)
30
- furnace.takeOutput = callbackify(takeOutput)
31
- furnace.putInput = callbackify(putInput)
32
- furnace.putFuel = callbackify(putFuel)
27
+ furnace.takeInput = takeInput
28
+ furnace.takeFuel = takeFuel
29
+ furnace.takeOutput = takeOutput
30
+ furnace.putInput = putInput
31
+ furnace.putFuel = putFuel
33
32
  furnace.inputItem = function () { return this.slots[0] }
34
33
  furnace.fuelItem = function () { return this.slots[1] }
35
34
  furnace.outputItem = function () { return this.slots[2] }
@@ -118,5 +117,5 @@ function inject (bot) {
118
117
  return ticks * 0.05
119
118
  }
120
119
 
121
- bot.openFurnace = callbackify(openFurnace)
120
+ bot.openFurnace = openFurnace
122
121
  }
@@ -67,10 +67,13 @@ function inject (bot, options) {
67
67
 
68
68
  bot.emit('login')
69
69
  bot.emit('game')
70
- bot._client.write('held_item_slot', { slotId: 0 })
71
70
 
72
71
  // varint length-prefixed string as data
73
72
  bot._client.writeChannel(brandChannel, options.brand)
73
+
74
+ if (packet.dimensionCodec) {
75
+ bot.registry.loadDimensionCodec(packet.dimensionCodec)
76
+ }
74
77
  })
75
78
 
76
79
  bot._client.on('respawn', (packet) => {
@@ -1,8 +1,8 @@
1
1
  const assert = require('assert')
2
2
  module.exports = inject
3
3
 
4
- function inject (bot, { version }) {
5
- const Item = require('prismarine-item')(version)
4
+ function inject (bot) {
5
+ const Item = require('prismarine-item')(bot.version)
6
6
  /**
7
7
  *
8
8
  * @param {import('prismarine-block').Block} referenceBlock
@@ -1,7 +1,7 @@
1
1
  const assert = require('assert')
2
2
  const { Vec3 } = require('vec3')
3
3
  const { once } = require('events')
4
- const { callbackify, sleep, createDoneTask, createTask, withTimeout } = require('../promise_utils')
4
+ const { sleep, createDoneTask, createTask, withTimeout } = require('../promise_utils')
5
5
 
6
6
  module.exports = inject
7
7
 
@@ -20,10 +20,9 @@ const ALWAYS_CONSUMABLES = [
20
20
  'golden_apple'
21
21
  ]
22
22
 
23
- function inject (bot, { version, hideErrors }) {
24
- const Item = require('prismarine-item')(version)
25
- const windows = require('prismarine-windows')(version)
26
- const mcData = require('minecraft-data')(version)
23
+ function inject (bot, { hideErrors }) {
24
+ const Item = require('prismarine-item')(bot.version)
25
+ const windows = require('prismarine-windows')(bot.version) // TODO: update for prismarine-registry
27
26
 
28
27
  let eatingTask = createDoneTask()
29
28
 
@@ -251,7 +250,7 @@ function inject (bot, { version, hideErrors }) {
251
250
  (nbt != null && window.selectedItem.nbt !== nbt)) {
252
251
  // we are not holding the item we need. click it.
253
252
  const sourceItem = window.findItemRange(sourceStart, sourceEnd, itemType, metadata, false, nbt)
254
- const mcDataEntry = mcData.itemsArray.find(x => x.id === itemType)
253
+ const mcDataEntry = bot.registry.itemsArray.find(x => x.id === itemType)
255
254
  assert(mcDataEntry, 'Invalid itemType')
256
255
  if (!sourceItem) throw new Error(`Can't find ${mcDataEntry.name} in slots [${sourceStart} - ${sourceEnd}], (item id: ${itemType})`)
257
256
  if (firstSourceSlot === null) firstSourceSlot = sourceItem.slot
@@ -303,39 +302,41 @@ function inject (bot, { version, hideErrors }) {
303
302
 
304
303
  function extendWindow (window) {
305
304
  window.close = () => {
306
- window.emit('close')
307
305
  closeWindow(window)
306
+ window.emit('close')
308
307
  }
309
- window.withdraw = callbackify(async (itemType, metadata, count) => {
308
+
309
+ window.withdraw = async (itemType, metadata, count, nbt) => {
310
310
  if (bot.inventory.emptySlotCount() === 0) {
311
311
  throw new Error('Unable to withdraw, Bot inventory is full.')
312
312
  }
313
-
314
313
  const options = {
315
314
  window,
316
315
  itemType,
317
316
  metadata,
318
317
  count,
318
+ nbt,
319
319
  sourceStart: 0,
320
320
  sourceEnd: window.inventoryStart,
321
321
  destStart: window.inventoryStart,
322
322
  destEnd: window.inventoryEnd
323
323
  }
324
324
  await transfer(options)
325
- })
326
- window.deposit = callbackify(async (itemType, metadata, count) => {
325
+ }
326
+ window.deposit = async (itemType, metadata, count, nbt) => {
327
327
  const options = {
328
328
  window: window,
329
329
  itemType,
330
330
  metadata,
331
331
  count,
332
+ nbt,
332
333
  sourceStart: window.inventoryStart,
333
334
  sourceEnd: window.inventoryEnd,
334
335
  destStart: 0,
335
336
  destEnd: window.inventoryStart
336
337
  }
337
338
  await transfer(options)
338
- })
339
+ }
339
340
  }
340
341
 
341
342
  async function openBlock (block) {
@@ -380,7 +381,7 @@ function inject (bot, { version, hideErrors }) {
380
381
  if (item) {
381
382
  item.slot = slot
382
383
  }
383
- bot.inventory.slots[slot] = item
384
+ if (!Item.equal(bot.inventory.slots[slot], item, true)) bot.inventory.updateSlot(slot, item)
384
385
  }
385
386
  }
386
387
 
@@ -437,9 +438,6 @@ function inject (bot, { version, hideErrors }) {
437
438
  accepted: true
438
439
  // bot.emit(`confirmTransaction${click.id}`, false)
439
440
  })
440
- if (!hideErrors) {
441
- console.log(`WARNING : unknown transaction confirmation for window ${windowId}, action ${actionId} and accepted ${accepted}`)
442
- }
443
441
  return
444
442
  }
445
443
  // shift it later if packets are sent out of order
@@ -643,21 +641,21 @@ function inject (bot, { version, hideErrors }) {
643
641
  bot.emit(`setWindowItems:${window.id}`)
644
642
  })
645
643
 
646
- bot.activateBlock = callbackify(activateBlock)
647
- bot.activateEntity = callbackify(activateEntity)
648
- bot.activateEntityAt = callbackify(activateEntityAt)
649
- bot.consume = callbackify(consume)
644
+ bot.activateBlock = activateBlock
645
+ bot.activateEntity = activateEntity
646
+ bot.activateEntityAt = activateEntityAt
647
+ bot.consume = consume
650
648
  bot.activateItem = activateItem
651
649
  bot.deactivateItem = deactivateItem
652
650
 
653
651
  // not really in the public API
654
- bot.clickWindow = callbackify(clickWindow)
652
+ bot.clickWindow = clickWindow
655
653
  bot.putSelectedItemRange = putSelectedItemRange
656
654
  bot.putAway = putAway
657
655
  bot.closeWindow = closeWindow
658
- bot.transfer = callbackify(transfer)
659
- bot.openBlock = callbackify(openBlock)
660
- bot.openEntity = callbackify(openEntity)
661
- bot.moveSlotItem = callbackify(moveSlotItem)
656
+ bot.transfer = transfer
657
+ bot.openBlock = openBlock
658
+ bot.openEntity = openEntity
659
+ bot.moveSlotItem = moveSlotItem
662
660
  bot.updateHeldItem = updateHeldItem
663
661
  }
@@ -3,7 +3,7 @@ const assert = require('assert')
3
3
  const math = require('../math')
4
4
  const conv = require('../conversions')
5
5
  const { performance } = require('perf_hooks')
6
- const { callbackify, createDoneTask, createTask } = require('../promise_utils')
6
+ const { createDoneTask, createTask } = require('../promise_utils')
7
7
 
8
8
  const { Physics, PlayerState } = require('prismarine-physics')
9
9
 
@@ -15,10 +15,8 @@ const PHYSICS_INTERVAL_MS = 50
15
15
  const PHYSICS_TIMESTEP = PHYSICS_INTERVAL_MS / 1000
16
16
 
17
17
  function inject (bot, { physicsEnabled }) {
18
- const mcData = require('minecraft-data')(bot.version)
19
-
20
18
  const world = { getBlock: (pos) => { return bot.blockAt(pos, false) } }
21
- const physics = Physics(mcData, world)
19
+ const physics = Physics(bot.registry, world)
22
20
 
23
21
  bot.jumpQueued = false
24
22
  bot.jumpTicks = 0 // autojump cooldown
@@ -223,7 +221,7 @@ function inject (bot, { physicsEnabled }) {
223
221
  }
224
222
  })
225
223
 
226
- bot.look = callbackify(async (yaw, pitch, force) => {
224
+ bot.look = async (yaw, pitch, force) => {
227
225
  if (!lookingTask.done) {
228
226
  lookingTask.finish() // finish the previous one
229
227
  }
@@ -245,15 +243,15 @@ function inject (bot, { physicsEnabled }) {
245
243
  }
246
244
 
247
245
  await lookingTask.promise
248
- })
246
+ }
249
247
 
250
- bot.lookAt = callbackify(async (point, force) => {
248
+ bot.lookAt = async (point, force) => {
251
249
  const delta = point.minus(bot.entity.position.offset(0, bot.entity.height, 0))
252
250
  const yaw = Math.atan2(-delta.x, -delta.z)
253
251
  const groundDistance = Math.sqrt(delta.x * delta.x + delta.z * delta.z)
254
252
  const pitch = Math.atan2(delta.y, groundDistance)
255
253
  await bot.look(yaw, pitch, force)
256
- })
254
+ }
257
255
 
258
256
  // player position and look (clientbound)
259
257
  bot._client.on('position', (packet) => {
@@ -1,4 +1,4 @@
1
- const { onceWithCleanup, callbackify } = require('../promise_utils')
1
+ const { onceWithCleanup } = require('../promise_utils')
2
2
 
3
3
  module.exports = inject
4
4
 
@@ -24,6 +24,6 @@ function inject (bot) {
24
24
  await placeBlockWithOptions(referenceBlock, faceVector, { swingArm: 'right' })
25
25
  }
26
26
 
27
- bot.placeBlock = callbackify(placeBlock)
27
+ bot.placeBlock = placeBlock
28
28
  bot._placeBlockWithOptions = placeBlockWithOptions
29
29
  }
@@ -2,8 +2,8 @@ const assert = require('assert')
2
2
 
3
3
  module.exports = inject
4
4
 
5
- function inject (bot, { version }) {
6
- const Item = require('prismarine-item')(version)
5
+ function inject (bot) {
6
+ const Item = require('prismarine-item')(bot.version)
7
7
 
8
8
  /**
9
9
  *
@@ -23,13 +23,27 @@ const viewDistanceToBits = {
23
23
  function inject (bot, options) {
24
24
  function setSettings (settings) {
25
25
  extend(bot.settings, settings)
26
+
27
+ // chat
26
28
  const chatBits = chatToBits[bot.settings.chat]
27
29
  assert.ok(chatBits != null, `invalid chat setting: ${bot.settings.chat}`)
28
- const viewDistanceBits = viewDistanceToBits[bot.settings.viewDistance]
30
+
31
+ // view distance
32
+ let viewDistanceBits = null
33
+ if (typeof bot.settings.viewDistance === 'string') {
34
+ viewDistanceBits = viewDistanceToBits[bot.settings.viewDistance]
35
+ } else if (typeof bot.settings.viewDistance === 'number' && bot.settings.viewDistance > 0) { // Make sure view distance is a valid # || should be 2 or more
36
+ viewDistanceBits = bot.settings.viewDistance
37
+ }
29
38
  assert.ok(viewDistanceBits != null, `invalid view distance setting: ${bot.settings.viewDistance}`)
39
+
40
+ // hand
30
41
  const handBits = handToBits[bot.settings.mainHand]
31
42
  assert.ok(handBits != null, `invalid main hand: ${bot.settings.mainHand}`)
32
- bot.settings.showCape = !!bot.settings.showCape
43
+
44
+ // skin
45
+ // cape is inverted, not used at all (legacy?)
46
+ // bot.settings.showCape = !!bot.settings.showCape
33
47
  const skinParts = bot.settings.skinParts.showCape << 0 |
34
48
  bot.settings.skinParts.showJacket << 1 |
35
49
  bot.settings.skinParts.showLeftSleeve << 2 |
@@ -37,13 +51,17 @@ function inject (bot, options) {
37
51
  bot.settings.skinParts.showLeftPants << 4 |
38
52
  bot.settings.skinParts.showRightPants << 5 |
39
53
  bot.settings.skinParts.showHat << 6
54
+
55
+ // write the packet
40
56
  bot._client.write('settings', {
41
57
  locale: bot.settings.locale || 'en_US',
42
58
  viewDistance: viewDistanceBits,
43
59
  chatFlags: chatBits,
44
60
  chatColors: bot.settings.colorsEnabled,
45
61
  skinParts: skinParts,
46
- mainHand: handBits
62
+ mainHand: handBits,
63
+ enableTextFiltering: bot.settings.enableTextFiltering,
64
+ enableServerListing: bot.settings.enableServerListing
47
65
  })
48
66
  }
49
67
 
@@ -67,10 +85,12 @@ function inject (bot, options) {
67
85
  showHat: true
68
86
  }
69
87
  : options.skinParts,
70
- mainHand: options.mainHand || 'right'
88
+ mainHand: options.mainHand || 'right',
89
+ enableTextFiltering: options.enableTextFiltering || false,
90
+ enableServerListing: options.enableServerListing || true
71
91
  }
72
92
 
73
- bot._client.once('login', () => {
93
+ bot._client.on('login', () => {
74
94
  setSettings({})
75
95
  })
76
96
 
@@ -1,5 +1,4 @@
1
1
  const assert = require('assert')
2
- const { callbackify } = require('../promise_utils')
3
2
 
4
3
  module.exports = inject
5
4
 
@@ -144,10 +143,10 @@ function inject (bot) {
144
143
  return bot.clickWindow(slot, 1, 0)
145
144
  }
146
145
 
147
- bot.equip = callbackify(equip)
148
- bot.unequip = callbackify(unequip)
149
- bot.toss = callbackify(toss)
150
- bot.tossStack = callbackify(tossStack)
146
+ bot.equip = equip
147
+ bot.unequip = unequip
148
+ bot.toss = toss
149
+ bot.tossStack = tossStack
151
150
  bot.setQuickBarSlot = setQuickBarSlot
152
151
  bot.getEquipmentDestSlot = getDestSlot
153
152
  bot.simpleClick = { leftMouse, rightMouse }