mineflayer 3.14.0 → 3.17.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
  }
@@ -0,0 +1,45 @@
1
+ /*
2
+ *
3
+ * A bot that attacks the player that sends a message or the nearest entity (excluding players)
4
+ *
5
+ */
6
+ const mineflayer = require('mineflayer')
7
+
8
+ if (process.argv.length < 4 || process.argv.length > 6) {
9
+ console.log('Usage : node attack.js <host> <port> [<name>] [<password>]')
10
+ process.exit(1)
11
+ }
12
+
13
+ const bot = mineflayer.createBot({
14
+ host: process.argv[2],
15
+ port: parseInt(process.argv[3]),
16
+ username: process.argv[4] ? process.argv[4] : 'attack',
17
+ password: process.argv[5]
18
+ })
19
+
20
+ bot.on('spawn', () => {
21
+ bot.on('chat', (username, message) => {
22
+ if (message === 'attack me') attackPlayer(username)
23
+ else if (message === 'attack') attackEntity()
24
+ })
25
+ })
26
+
27
+ function attackPlayer (username) {
28
+ const player = bot.players[username]
29
+ if (!player || !player.entity) {
30
+ bot.chat('I can\'t see you')
31
+ } else {
32
+ bot.chat(`Attacking ${player.username}`)
33
+ bot.attack(player.entity)
34
+ }
35
+ }
36
+
37
+ function attackEntity () {
38
+ const entity = bot.nearestEntity()
39
+ if (!entity) {
40
+ bot.chat('No nearby entities')
41
+ } else {
42
+ bot.chat(`Attacking ${entity.name ?? entity.username}`)
43
+ bot.attack(entity)
44
+ }
45
+ }
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
 
@@ -427,6 +428,11 @@ export interface simpleClick {
427
428
  rightMouse: (slot: number) => Promise<void>
428
429
  }
429
430
 
431
+ export interface Tablist {
432
+ header: ChatMessage
433
+ footer: ChatMessage
434
+ }
435
+
430
436
  export interface chatPatternOptions {
431
437
  repeat: boolean
432
438
  parse: boolean
@@ -742,13 +748,18 @@ export class Villager extends (EventEmitter as new () => TypedEmitter<Conditiona
742
748
  }
743
749
 
744
750
  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
751
+ inputItem1: Item
752
+ outputItem: Item
753
+ inputItem2: Item | null
754
+ hasItem2: boolean
755
+ tradeDisabled: boolean
756
+ nbTradeUses: number
757
+ maximumNbTradeUses: number
758
+ xp?: number
759
+ specialPrice?: number
760
+ priceMultiplier?: number
761
+ demand?: number
762
+ realPrice?: number
752
763
  }
753
764
 
754
765
  export class ScoreBoard {
@@ -791,7 +802,7 @@ export class Team {
791
802
 
792
803
  update (name: string, friendlyFire: boolean, nameTagVisibility: string, collisionRule: string, formatting: number, prefix: string, suffix: string): void;
793
804
 
794
- displayName (member: string);
805
+ displayName (member: string): ChatMessage;
795
806
  }
796
807
 
797
808
  export type DisplaySlot =
package/lib/features.json CHANGED
@@ -7,7 +7,7 @@
7
7
  {
8
8
  "name": "spawner",
9
9
  "description": "spawner is called spawner",
10
- "versions": ["1.13", "1.17.1"]
10
+ "versions": ["1.13", "1.18.1"]
11
11
  },
12
12
  {
13
13
  "name": "blockMetadata",
@@ -17,12 +17,12 @@
17
17
  {
18
18
  "name": "blockStateId",
19
19
  "description": "block metadata is encoded as state id",
20
- "versions": ["1.13", "1.17.1"]
20
+ "versions": ["1.13", "1.18.1"]
21
21
  },
22
22
  {
23
23
  "name": "creativeSleepNearMobs",
24
24
  "description": "can sleep near mobs in creative",
25
- "versions": ["1.13", "1.17.1"]
25
+ "versions": ["1.13", "1.18.1"]
26
26
  },
27
27
  {
28
28
  "name": "fixedPointPosition",
@@ -32,7 +32,7 @@
32
32
  {
33
33
  "name": "doublePosition",
34
34
  "description": "Entity positions are represented with double",
35
- "versions": ["1.9", "1.17.1"]
35
+ "versions": ["1.9", "1.18.1"]
36
36
  },
37
37
  {
38
38
  "name": "fixedPointDelta",
@@ -42,7 +42,7 @@
42
42
  {
43
43
  "name": "fixedPointDelta128",
44
44
  "description": "Delta of position are represented with fixed point numbers times 128",
45
- "versions": ["1.9", "1.17.1"]
45
+ "versions": ["1.9", "1.18.1"]
46
46
  },
47
47
  {
48
48
  "name": "customChannelMCPrefixed",
@@ -52,12 +52,12 @@
52
52
  {
53
53
  "name": "customChannelIdentifier",
54
54
  "description": "custom channel is an identifier starting in minecraft namespace",
55
- "versions": ["1.13", "1.17.1"]
55
+ "versions": ["1.13", "1.18.1"]
56
56
  },
57
57
  {
58
58
  "name": "dimensionDataIsAvailable",
59
59
  "description": "dimensionData is available, providing an additional information about the current dimension",
60
- "versions": ["1.17", "1.17.1"]
60
+ "versions": ["1.17", "1.18.1"]
61
61
  },
62
62
  {
63
63
  "name": "useItemWithBlockPlace",
@@ -67,7 +67,7 @@
67
67
  {
68
68
  "name": "useItemWithOwnPacket",
69
69
  "description": "use item is done with its own packet",
70
- "versions": ["1.9", "1.17.1"]
70
+ "versions": ["1.9", "1.18.1"]
71
71
  },
72
72
  {
73
73
  "name": "blockPlaceHasHeldItem",
@@ -87,7 +87,7 @@
87
87
  {
88
88
  "name": "blockPlaceHasInsideBlock",
89
89
  "description": "block_place packet has inside block",
90
- "versions": ["1.14", "1.17.1"]
90
+ "versions": ["1.14", "1.18.1"]
91
91
  },
92
92
  {
93
93
  "name": "teleportUsesPositionPacket",
@@ -102,7 +102,7 @@
102
102
  {
103
103
  "name": "teleportUsesOwnPacket",
104
104
  "description": "teleport is done using its own packet",
105
- "versions": ["1.9", "1.17.1"]
105
+ "versions": ["1.9", "1.18.1"]
106
106
  },
107
107
  {
108
108
  "name": "oneBlockForSeveralVariations",
@@ -112,7 +112,7 @@
112
112
  {
113
113
  "name": "blockSchemeIsFlat",
114
114
  "description": "all variations of a packet have their own id",
115
- "versions": ["1.13", "1.17.1"]
115
+ "versions": ["1.13", "1.18.1"]
116
116
  },
117
117
  {
118
118
  "name": "tabCompleteHasNoToolTip",
@@ -122,7 +122,7 @@
122
122
  {
123
123
  "name": "tabCompleteHasAToolTip",
124
124
  "description": "tab complete has a tool tip",
125
- "versions": ["1.13", "1.17.1"]
125
+ "versions": ["1.13", "1.18.1"]
126
126
  },
127
127
  {
128
128
  "name": "effectAreMinecraftPrefixed",
@@ -132,7 +132,7 @@
132
132
  {
133
133
  "name": "effectAreNotPrefixed",
134
134
  "description": "effect are not prefixed",
135
- "versions": ["1.13", "1.17.1"]
135
+ "versions": ["1.13", "1.18.1"]
136
136
  },
137
137
  {
138
138
  "name": "itemsAreAlsoBlocks",
@@ -142,12 +142,12 @@
142
142
  {
143
143
  "name": "itemsAreNotBlocks",
144
144
  "description": "items are not block",
145
- "versions": ["1.13", "1.17.1"]
145
+ "versions": ["1.13", "1.18.1"]
146
146
  },
147
147
  {
148
148
  "name": "fishingBobberCorrectlyNamed",
149
149
  "description": "the fishing hook entity is named fishing_bobber",
150
- "versions": ["1.14", "1.17.1"]
150
+ "versions": ["1.14", "1.18.1"]
151
151
  },
152
152
  {
153
153
  "name": "editBookIsPluginChannel",
@@ -157,7 +157,7 @@
157
157
  {
158
158
  "name": "hasEditBookPacket",
159
159
  "description": "book editing is handled through a packet",
160
- "versions": ["1.13", "1.17.1"]
160
+ "versions": ["1.13", "1.18.1"]
161
161
  },
162
162
  {
163
163
  "name": "clientUpdateBookIdWhenSign",
@@ -197,12 +197,12 @@
197
197
  {
198
198
  "name": "dimensionIsAWorld",
199
199
  "description": "description is a world name (string)",
200
- "versions": ["1.16.2", "1.17.1"]
200
+ "versions": ["1.16.2", "1.18.1"]
201
201
  },
202
202
  {
203
203
  "name": "dimensionDataIsAvailable",
204
204
  "description": "dimensionData is available, describing additional dimension information",
205
- "versions": ["1.17", "1.17.1"]
205
+ "versions": ["1.17", "1.18.1"]
206
206
  },
207
207
  {
208
208
  "name": "doesntHaveChestType",
@@ -227,22 +227,22 @@
227
227
  {
228
228
  "name": "hasAttackCooldown",
229
229
  "description": "if there is a cooldown after attacks to deal full damage",
230
- "versions": ["1.9", "1.17.1"]
230
+ "versions": ["1.9", "1.18.1"]
231
231
  },
232
232
  {
233
233
  "name": "usesLoginPacket",
234
234
  "description": "uses the login packet as defined in mcData",
235
- "versions": ["1.16", "1.17.1"]
235
+ "versions": ["1.16", "1.18.1"]
236
236
  },
237
237
  {
238
238
  "name": "usesMultiblockSingleLong",
239
239
  "description": "in the multi_block_change packet is stored as a single number",
240
- "versions": ["1.16.2", "1.17.1"]
240
+ "versions": ["1.16.2", "1.18.1"]
241
241
  },
242
242
  {
243
243
  "name": "usesMultiblock3DChunkCoords",
244
244
  "description": "in the multi_block_change packet, all 3 axis coords are defined",
245
- "versions": ["1.16.2", "1.17.1"]
245
+ "versions": ["1.16.2", "1.18.1"]
246
246
  },
247
247
  {
248
248
  "name": "setBlockUsesMetadataNumber",
@@ -257,7 +257,7 @@
257
257
  {
258
258
  "name": "selectingTradeMovesItems",
259
259
  "description": "selecting a trade automatically puts the required items into trading slots",
260
- "versions": ["1.14", "1.17.1"]
260
+ "versions": ["1.14", "1.18.1"]
261
261
  },
262
262
  {
263
263
  "name": "resourcePackUsesHash",
@@ -272,7 +272,7 @@
272
272
  {
273
273
  "name": "teamUsesChatComponents",
274
274
  "description": "teams use chatcomponents for formatting",
275
- "versions": ["1.13", "1.17.1"]
275
+ "versions": ["1.13", "1.18.1"]
276
276
  },
277
277
  {
278
278
  "name": "teamUsesScoreboard",
@@ -287,7 +287,7 @@
287
287
  {
288
288
  "name": "enderCrystalNameNoCapsWithUnderscore",
289
289
  "description": "this is when the end_crystal's entity name is end_crystal",
290
- "versions": ["1.14", "1.17.1"]
290
+ "versions": ["1.14", "1.18.1"]
291
291
  },
292
292
  {
293
293
  "name": "entityNameUpperCaseNoUnderscore",
@@ -307,7 +307,7 @@
307
307
  {
308
308
  "name": "stateIdUsed",
309
309
  "description": "starting in 1.17.1, actionId has been replaced with stateId",
310
- "versions": ["1.17.1", "1.17.1"]
310
+ "versions": ["1.17.1", "1.18.1"]
311
311
  },
312
312
  {
313
313
  "name": "actionIdUsed",
@@ -317,6 +317,21 @@
317
317
  {
318
318
  "name": "setSlotAsTransaction",
319
319
  "description": "use setslot as transaction instead of just hoping it'll work",
320
- "versions": ["1.17", "1.17.1"]
320
+ "versions": ["1.17", "1.18.1"]
321
+ },
322
+ {
323
+ "name": "armAnimationBeforeUse",
324
+ "description": "arm animation packet sent before use entity packet",
325
+ "versions": ["1.8", "1.8.9"]
326
+ },
327
+ {
328
+ "name": "tallWorld",
329
+ "description": "world starts at -64 and ends at 384",
330
+ "versions": ["1.18", "1.18.1"]
331
+ },
332
+ {
333
+ "name": "sendStringifiedSignText",
334
+ "description": "sign text send when updating signs is send as stringified strings",
335
+ "versions": ["1.8", "1.8.9"]
321
336
  }
322
337
  ]
package/lib/loader.js CHANGED
@@ -63,7 +63,7 @@ function createBot (options = {}) {
63
63
  options.username = options.username ?? 'Player'
64
64
  options.version = options.version ?? false
65
65
  options.plugins = options.plugins ?? {}
66
- options.hideErrors = options.hideErrors ?? true
66
+ options.hideErrors = options.hideErrors ?? false
67
67
  options.logErrors = options.logErrors ?? true
68
68
  options.loadInternalPlugins = options.loadInternalPlugins ?? true
69
69
  options.client = options.client ?? null