Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ export interface BotEvents {
teamCreated: (team: Team) => Promise<void> | void
teamRemoved: (team: Team) => Promise<void> | void
teamUpdated: (team: Team) => Promise<void> | void
teamMemberAdded: (team: Team) => Promise<void> | void
teamMemberRemoved: (team: Team) => Promise<void> | void
teamMemberAdded: (team: Team, members: string[]) => Promise<void> | void
teamMemberRemoved: (team: Team, members: string[]) => Promise<void> | void
bossBarCreated: (bossBar: BossBar) => Promise<void> | void
bossBarDeleted: (bossBar: BossBar) => Promise<void> | void
bossBarUpdated: (bossBar: BossBar) => Promise<void> | void
Expand Down
8 changes: 7 additions & 1 deletion lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ function createBot (options = {}) {
bot.version = versionData.minecraftVersion
options.version = versionData.minecraftVersion
bot.supportFeature = bot.registry.supportFeature
setTimeout(() => bot.emit('inject_allowed'), 0)
const stateListener = (newState) => {
if (newState !== 'handshaking') {
bot.emit('inject_allowed')
bot._client.removeListener('state', stateListener)
}
}
bot._client.on('state', stateListener)
}
return bot
}
1 change: 1 addition & 0 deletions lib/plugins/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ function inject (bot, { version, storageBuilder, hideErrors }) {
bot._getDimensionName = () => worldName

async function switchWorld () {
bot.emit('worldSwitch')
if (bot.world) {
if (storageBuilder) {
await bot.world.async.waitSaving()
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/digging.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function inject (bot) {
bot.targetDigFace = null
bot.lastDigTime = null

async function dig (block, forceLook, digFace) {
async function dig (block, forceLook, digFace, face) {
if (block === null || block === undefined) {
throw new Error('dig was called with an undefined or null block')
}
Expand All @@ -29,7 +29,7 @@ function inject (bot) {
throw new Error(`dig time for ${block?.name ?? block} is Infinity`)
}

bot.targetDigFace = 1 // Default (top)
bot.targetDigFace = face ?? 1 // Default (top)

if (forceLook !== 'ignore') {
if (digFace?.x || digFace?.y || digFace?.z) {
Expand Down Expand Up @@ -239,7 +239,7 @@ function inject (bot) {
const headEquippedItem = bot.inventory.slots[headEquipmentSlot]
if (headEquippedItem) {
const helmetEnchantments = headEquippedItem.enchants
enchantments = enchantments.concat(helmetEnchantments)
enchantments = [...(enchantments ?? []), ...(helmetEnchantments ?? [])]
}

const creative = bot.game.gameMode === 'creative'
Expand Down
58 changes: 43 additions & 15 deletions lib/plugins/entities.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Vec3 } = require('vec3')
const conv = require('../conversions')
const { performance } = require('perf_hooks')
// These values are only accurate for versions 1.14 and above (crouch hitbox changes)
// Todo: hitbox sizes for sleeping, swimming/crawling, and flying with elytra
const PLAYER_HEIGHT = 1.8
Expand Down Expand Up @@ -138,8 +139,9 @@ function inject (bot) {
// refactor to function
bot.emit('entityGone', entity)
entity.isValid = false
if (entity.username && bot.players[entity.username]) {
bot.players[entity.username].entity = null
if (entity.uuid) {
const player = bot._playerFromUUID(entity.uuid)
if (player) player.entity = null
}
delete bot.entities[entity.id]
}
Expand Down Expand Up @@ -219,8 +221,11 @@ function inject (bot) {
entity.eyeHeight = PLAYER_EYEHEIGHT
entity.height = PLAYER_HEIGHT
entity.width = PLAYER_WIDTH
if (bot.players[entity.username] !== undefined && !bot.players[entity.username].entity) {
bot.players[entity.username].entity = entity
if (uuid) {
const player = bot._playerFromUUID(uuid)
if (player && !player.entity) {
player.entity = entity
}
}
return entity
}
Expand Down Expand Up @@ -411,8 +416,9 @@ function inject (bot) {
const entity = fetchEntity(id)
bot.emit('entityGone', entity)
entity.isValid = false
if (entity.username && bot.players[entity.username]) {
bot.players[entity.username].entity = null
if (entity.uuid) {
const player = bot._playerFromUUID(entity.uuid)
if (player) player.entity = null
}
delete bot.entities[id]
})
Expand All @@ -432,8 +438,10 @@ function inject (bot) {
bot._client.on('entity_look', (packet) => {
// entity look
const entity = fetchEntity(packet.entityId)
entity.yaw = conv.fromNotchianYawByte(packet.yaw)
entity.pitch = conv.fromNotchianPitchByte(packet.pitch)
if (packet.yaw && packet.pitch) {
entity.yaw = conv.fromNotchianYawByte(packet.yaw)
entity.pitch = conv.fromNotchianPitchByte(packet.pitch)
}
bot.emit('entityMoved', entity)
})

Expand Down Expand Up @@ -471,8 +479,8 @@ function inject (bot) {
const entity = fetchEntity(packet.entityId)
entity.position.set(packet.x, packet.y, packet.z)
entity.velocity.set(packet.dx, packet.dy, packet.dz)
entity.yaw = packet.yaw
entity.pitch = packet.pitch
entity.yaw = conv.fromDegreesToYaw(packet.yaw)
entity.pitch = conv.fromDegreesToPitch(packet.pitch)
bot.emit('entityMoved', entity)
})

Expand Down Expand Up @@ -770,12 +778,12 @@ function inject (bot) {
}

if (newPlayer) {
if (!player.username) continue // Should be unreachable if add_player is always sent for new players
if (player.username == null) continue // Should be unreachable if add_player is always sent for new players
bot.players[player.username] = player
bot.uuidToUsername[player.uuid] = player.username
}

const playerEntity = Object.values(bot.entities).find(e => e.type === 'player' && e.username === player.username)
const playerEntity = Object.values(bot.entities).find(e => e.type === 'player' && e.uuid === player.uuid)
player.entity = playerEntity

if (playerEntity === bot.entity) {
Expand Down Expand Up @@ -818,7 +826,7 @@ function inject (bot) {
}
}

const playerEntity = Object.values(bot.entities).find(e => e.type === 'player' && e.username === item.name)
const playerEntity = Object.values(bot.entities).find(e => e.type === 'player' && e.uuid === item.uuid)
player.entity = playerEntity
if (playerEntity === bot.entity) {
bot.player = player
Expand Down Expand Up @@ -934,7 +942,7 @@ function inject (bot) {
})

// dismounting when the vehicle is gone
bot._client.on('entityGone', (entity) => {
bot.on('entityGone', (entity) => {
if (bot.vehicle === entity) {
bot.vehicle = null
bot.emit('dismount', (entity))
Expand Down Expand Up @@ -1072,6 +1080,25 @@ function inject (bot) {
function fetchEntity (id) {
return bot.entities[id] || (bot.entities[id] = new Entity(id))
}

let lastTick = performance.now()
bot.on('physicsTick', () => {
const now = performance.now()
const delta = (now - lastTick) / 50 // convert to ticks
for (const entity of Object.values(bot.entities)) {
if (entity.isValid && entity.velocity && (entity.name === "falling_block" || entity.name === 'snowball' || entity.name === 'egg' || entity.name === 'ender_pearl')) {
entity.velocity.x = entity.velocity.x * (1 - 0.01) ** delta
entity.velocity.y = entity.velocity.y * (1 - 0.01) ** delta
entity.velocity.z = entity.velocity.z * (1 - 0.01) ** delta
if (!entity.metadata[5] && entity.velocity.y > -3) { // check no gravity flag and terminal velocity (60 m/s -> 3m/tick)
entity.velocity.y -= 0.03 * ((1 - (1 - 0.01) ** delta) / 0.01)
}
entity.position.translate(entity.velocity.x * delta, entity.velocity.y * delta, entity.velocity.z * delta)
bot.emit('entityMoved', entity)
}
}
lastTick = now
})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code does not appear to be particularly stable. raw referencing metadata is version specific, which is not properly handled.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you have any suggestions? This code was added by my contributor but I don't see a better solution there. Reference metadata keys from mc-data is even worse since there is no data for <1.16.5

}

function parseMetadata (metadata, entityMetadata = {}) {
Expand All @@ -1097,11 +1124,12 @@ function extractSkinInformation (properties) {
const skinTexture = JSON.parse(Buffer.from(props.textures.value, 'base64').toString('utf8'))

const skinTextureUrl = skinTexture?.textures?.SKIN?.url ?? undefined
const capeTextureUrl = skinTexture?.textures?.CAPE?.url ?? undefined
const skinTextureModel = skinTexture?.textures?.SKIN?.metadata?.model ?? undefined

if (!skinTextureUrl) {
return undefined
}

return { url: skinTextureUrl, model: skinTextureModel }
return { url: skinTextureUrl, capeUrl: capeTextureUrl, model: skinTextureModel }
}
1 change: 0 additions & 1 deletion lib/plugins/health.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ function inject (bot, options) {
bot.isAlive = true

bot._client.on('respawn', (packet) => {
bot.isAlive = false
bot.emit('respawn')
})

Expand Down
6 changes: 5 additions & 1 deletion lib/plugins/inventory.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const assert = require('assert')
const { Vec3 } = require('vec3')
const { once, sleep, createDoneTask, createTask, withTimeout } = require('../promise_utils')
const conv = require('../conversions')

module.exports = inject

Expand Down Expand Up @@ -128,7 +129,10 @@ function inject (bot, { hideErrors }) {
bot._client.write('use_item', {
hand: offHand ? 1 : 0,
sequence,
rotation: { x: 0, y: 0 }
rotation: {
x: conv.toNotchianYaw(bot.entity.yaw),
y: conv.toNotchianPitch(bot.entity.pitch)
}
})
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/plugins/team.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function inject (bot) {
delete bot.teamMap[member]
})
delete teams[teamName]
bot.emit('teamRemoved', teams[teamName])
bot.emit('teamRemoved', team)
break

case 'change':
Expand All @@ -62,7 +62,7 @@ function inject (bot) {
team.add(player)
bot.teamMap[player] = team
}
bot.emit('teamMemberAdded', teams[teamName])
bot.emit('teamMemberAdded', teams[teamName], players)
break

case 'leave':
Expand All @@ -71,7 +71,7 @@ function inject (bot) {
team.remove(player)
delete bot.teamMap[player]
}
bot.emit('teamMemberRemoved', teams[teamName])
bot.emit('teamMemberRemoved', teams[teamName], players)
break

default:
Expand Down
Loading