mineflayer 4.0.0 → 4.3.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.
@@ -1,32 +1,30 @@
1
1
  const assert = require('assert')
2
2
  const { Vec3 } = require('vec3')
3
- const { 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: setInventorySlot,
13
+ setInventorySlot,
14
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
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
 
@@ -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) => {
@@ -4,14 +4,12 @@ const { createDoneTask, createTask } = require('../promise_utils')
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
@@ -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
@@ -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,19 +302,20 @@ 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 = 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,
@@ -323,12 +323,13 @@ function inject (bot, { version, hideErrors }) {
323
323
  }
324
324
  await transfer(options)
325
325
  }
326
- window.deposit = async (itemType, metadata, count) => {
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,
@@ -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
@@ -15,10 +15,10 @@ 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)
20
+
21
+ const positionUpdateSentEveryTick = bot.supportFeature('positionUpdateSentEveryTick')
22
22
 
23
23
  bot.jumpQueued = false
24
24
  bot.jumpTicks = 0 // autojump cooldown
@@ -150,7 +150,7 @@ function inject (bot, { physicsEnabled }) {
150
150
  // Send a position packet every second, even if no update was made
151
151
  sendPacketPosition(position, onGround)
152
152
  lastSent.time = performance.now()
153
- } else if (bot.supportFeature('positionUpdateSentEveryTick') && bot.isAlive) {
153
+ } else if (positionUpdateSentEveryTick && bot.isAlive) {
154
154
  // For versions < 1.12, one player packet should be sent every tick
155
155
  // for the server to update health correctly
156
156
  bot._client.write('flying', { onGround: bot.entity.onGround })
@@ -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
 
@@ -5,7 +5,7 @@ const escapeValueNewlines = str => {
5
5
  }
6
6
 
7
7
  function inject (bot) {
8
- const ChatMessage = require('prismarine-chat')(bot.version)
8
+ const ChatMessage = require('prismarine-chat')(bot.version) // TODO: update for prismarine-registry
9
9
 
10
10
  bot.tablist = {
11
11
  header: new ChatMessage(''),
@@ -29,8 +29,11 @@ function inject (bot) {
29
29
  }
30
30
  if (team !== undefined) {
31
31
  if (mode === 1) {
32
- bot.emit('teamRemoved', teams[teamName])
32
+ team.members.forEach(member => {
33
+ delete bot.teamMap[member]
34
+ })
33
35
  delete teams[teamName]
36
+ bot.emit('teamRemoved', teams[teamName])
34
37
  }
35
38
  if (mode === 2) {
36
39
  team.update(
@@ -4,8 +4,8 @@ const { once } = require('events')
4
4
  module.exports = inject
5
5
 
6
6
  function inject (bot, { version }) {
7
- const mcData = require('minecraft-data')(version)
8
- const Item = require('prismarine-item')(version)
7
+ const { entitiesByName } = bot.registry
8
+ const Item = require('prismarine-item')(bot.version)
9
9
 
10
10
  let selectTrade
11
11
  if (bot.supportFeature('useMCTrSel')) {
@@ -70,7 +70,7 @@ function inject (bot, { version }) {
70
70
  }
71
71
 
72
72
  async function openVillager (villagerEntity) {
73
- const villagerType = mcData.entitiesByName.villager ? mcData.entitiesByName.villager.id : mcData.entitiesByName.Villager.id
73
+ const villagerType = entitiesByName.villager ? entitiesByName.villager.id : entitiesByName.Villager.id
74
74
  assert.strictEqual(villagerEntity.entityType, villagerType)
75
75
  let ready = false
76
76
 
package/lib/scoreboard.js CHANGED
@@ -5,7 +5,7 @@ const sortItems = (a, b) => {
5
5
  }
6
6
 
7
7
  module.exports = (bot) => {
8
- const ChatMessage = require('prismarine-chat')(bot.version)
8
+ const ChatMessage = require('prismarine-chat')(bot.version) // TODO: update for prismarine-registry
9
9
 
10
10
  class ScoreBoard {
11
11
  constructor (packet) {
package/lib/team.js CHANGED
@@ -3,8 +3,8 @@ let MessageBuilder
3
3
 
4
4
  module.exports = loader
5
5
 
6
- function loader (mcVersion) {
7
- ChatMessage = require('prismarine-chat')(mcVersion)
6
+ function loader (version) {
7
+ ChatMessage = require('prismarine-chat')(version) // TODO: update for prismarine-registry
8
8
  MessageBuilder = ChatMessage.MessageBuilder
9
9
  return Team
10
10
  }
package/lib/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  module.exports = {
2
2
  supportedVersions: ['1.8', '1.9', '1.10', '1.11', '1.12', '1.13', '1.14', '1.15', '1.16', '1.17', '1.18'],
3
- testedVersions: ['1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.1']
3
+ testedVersions: ['1.8.8', '1.9.4', '1.10.2', '1.11.2', '1.12.2', '1.13.2', '1.14.4', '1.15.2', '1.16.5', '1.17.1', '1.18.2']
4
4
  } // when updating testedVersions, make sure to update CI.yml
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mineflayer",
3
- "version": "4.0.0",
3
+ "version": "4.3.0",
4
4
  "description": "create minecraft bots with a stable, high level API",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -21,17 +21,18 @@
21
21
  },
22
22
  "license": "MIT",
23
23
  "dependencies": {
24
- "minecraft-data": "^2.109.0",
25
- "minecraft-protocol": "^1.26.5",
24
+ "minecraft-data": "^3.0.0",
25
+ "minecraft-protocol": "^1.31.0",
26
26
  "prismarine-biome": "^1.1.1",
27
27
  "prismarine-block": "^1.13.1",
28
28
  "prismarine-chat": "^1.3.3",
29
29
  "prismarine-chunk": "^1.29.0",
30
30
  "prismarine-entity": "^2.0.0",
31
- "prismarine-item": "^1.11.0",
31
+ "prismarine-item": "^1.11.5",
32
32
  "prismarine-nbt": "^2.0.0",
33
33
  "prismarine-physics": "^1.3.1",
34
34
  "prismarine-recipe": "^1.1.0",
35
+ "prismarine-registry": "^1.0.0",
35
36
  "prismarine-windows": "^2.4.2",
36
37
  "prismarine-world": "^3.6.0",
37
38
  "protodef": "^1.14.0",