Skip to content

Commit

Permalink
fix digging and spawning
Browse files Browse the repository at this point in the history
  • Loading branch information
extremeheat committed Jan 5, 2025
1 parent 0b9850c commit 0b36d9f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/lib/plugins/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ module.exports.server = function (serv) {
})
}

module.exports.entity = function (entity) {
module.exports.entity = function (entity, serv) {
entity.sendMetadata = (data) => {
if (serv.registry.version['>=']('1.20.2')) {
// todo: fix in mcdata
return
}
entity._writeOthersNearby('entity_metadata', {
entityId: entity.id,
metadata: data
Expand Down
3 changes: 2 additions & 1 deletion src/lib/plugins/spawn.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ module.exports.entity = function (entity, serv) {
entity.bornTime = Date.now()
serv.entities[entity.id] = entity

if (entity.type === 'player') entity.spawnPacketName = 'named_entity_spawn'
if (serv.supportFeature('unifiedPlayerAndEntitySpawnPacket')) entity.spawnPacketName = 'spawn_entity'
else if (entity.type === 'player') entity.spawnPacketName = 'named_entity_spawn'
else if (entity.type === 'object') entity.spawnPacketName = 'spawn_entity'
else if (entity.type === 'mob') {
if (serv.supportFeature('consolidatedEntitySpawnPacket')) entity.spawnPacketName = 'spawn_entity'
Expand Down
10 changes: 6 additions & 4 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ function onceWithTimeout (emitter, event, timeout = 10000, checkCondition) {
const timeoutId = setTimeout(() => {
reject(new Error(`Timeout waiting for '${event}' event`))
}, timeout)
emitter.once(event, (data) => {
if (checkCondition && !checkCondition(data)) return
function onEvent (...data) {
if (checkCondition && !checkCondition(...data)) return
clearTimeout(timeoutId)
resolve(data)
})
resolve([...data])
emitter.off(event, onEvent)
}
emitter.on(event, onEvent)
})
}

Expand Down
20 changes: 14 additions & 6 deletions test/mineflayer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const { Vec3 } = require('vec3')
const { onceWithTimeout } = require('../src/lib/utils')
const expect = require('expect').default

const DEBUG_PACKET_IO = true
const DEBUG_PACKET_IO = false

function assertPosEqual (actual, expected, precision = 1) {
expect(actual.distanceTo(expected)).toBeLessThan(precision)
Expand Down Expand Up @@ -176,12 +176,14 @@ squid.testedVersions.forEach((testedVersion, i) => {
await once(bot, `blockUpdate:${pos}`, 4000)
console.log('Block at', pos, bot.blockAt(pos))

const p = once(bot2, 'blockUpdate')
bot.dig(bot.blockAt(pos))
const p = onceWithTimeout(bot2, 'blockUpdate', 4000, (old, now) => {
return now.type === 0
})
await bot.dig(bot.blockAt(pos))
console.log('Digging...')

const [, newBlock] = await p
console.log('Dug.')
console.log('Dug.', newBlock)
assertPosEqual(newBlock.position, pos)
expect(newBlock.type).toEqual(0)
})
Expand Down Expand Up @@ -388,16 +390,19 @@ squid.testedVersions.forEach((testedVersion, i) => {
}).timeout(100 * 1000)
})

BigInt.prototype.toJSON = function () {
BigInt.prototype.toJSON = function () { // eslint-disable-line no-extend-native
return this.toString()
}
const SKIP_PACKETS = ['update_time']
function logBotEvents (serv, bot, prefix) {
bot._client.on('packet', (data, meta) => {
if (serv.isReady && !SKIP_PACKETS.includes(meta.name)) {
console.log(prefix, 'Packet', meta.name, JSON.stringify(data)?.slice(0, 60))
console.log(prefix, 'Packet', meta, JSON.stringify(data)?.slice(0, 60))
}
})
bot._client.on('state', (now, old) => {
console.log(prefix, '~ Client State Change', now, old)
})
bot.on('kicked', (...a) => {
console.warn(prefix, '*Bot kicked', a)
})
Expand All @@ -421,6 +426,9 @@ function logBotEvents (serv, bot, prefix) {
}
function logClientboundEvents (serv, prefix = '') {
serv._server.on('connection', (client) => {
client.on('state', (now, old) => {
console.log(prefix, '~ Server State Change', now, old)
})
const oldWrite = client.write
client.write = (name, param) => {
if (SKIP_PACKETS.includes(name)) return
Expand Down

0 comments on commit 0b36d9f

Please sign in to comment.