Skip to content

Commit

Permalink
feat: bot.respawn, fix respawn with flying squid (#3206)
Browse files Browse the repository at this point in the history
* feat: bot.respawn, fix respawn with flying squid

* add typing

(cherry picked from commit 66e34da)

* add description for .respawn()

* up header
  • Loading branch information
zardoy committed Jan 6, 2024
1 parent 91108d3 commit 3a6ce54
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
9 changes: 7 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
- ["blockBreakProgressEnd" (block, entity)](#blockbreakprogressend-block-entity)
- ["diggingCompleted" (block)](#diggingcompleted-block)
- ["diggingAborted" (block)](#diggingaborted-block)
- ["usedFirework"](#usedfirework)
- ["usedfirework"](#usedfirework)
- ["move"](#move)
- ["forcedMove"](#forcedmove)
- ["mount"](#mount)
Expand Down Expand Up @@ -291,7 +291,7 @@
- [bot.getExplosionDamages(entity, position, radius, [rawDamages])](#botgetexplosiondamagesentity-position-radius-rawdamages)
- [bot.lookAt(point, [force])](#botlookatpoint-force)
- [bot.look(yaw, pitch, [force])](#botlookyaw-pitch-force)
- [bot.updateSign(block, text)](#botupdatesignblock-text)
- [bot.updateSign(block, text, back = false)](#botupdatesignblock-text-back--false)
- [bot.equip(item, destination)](#botequipitem-destination)
- [bot.unequip(destination)](#botunequipdestination)
- [bot.tossStack(item)](#bottossstackitem)
Expand Down Expand Up @@ -331,6 +331,7 @@
- [bot.setCommandBlock(pos, command, [options])](#botsetcommandblockpos-command-options)
- [bot.supportFeature(name)](#botsupportfeaturename)
- [bot.waitForTicks(ticks)](#botwaitforticksticks)
- [bot.respawn()](#botrespawn)
- [Lower level inventory methods](#lower-level-inventory-methods)
- [bot.clickWindow(slot, mouseButton, mode)](#botclickwindowslot-mousebutton-mode)
- [bot.putSelectedItemRange(start, end, window, slot)](#botputselecteditemrangestart-end-window-slot)
Expand Down Expand Up @@ -2103,6 +2104,10 @@ The list of available features can be found inside the [./lib/features.json](htt

This is a promise-based function that waits for a given number of in-game ticks to pass before continuing. This is useful for quick timers that need to function with specific timing, regardless of the given physics tick speed of the bot. This is similar to the standard Javascript setTimeout function, but runs on the physics timer of the bot specifically.

#### bot.respawn()

When `respawn` option is disabled, you can call this method manually to respawn.

### Lower level inventory methods

These are lower level methods for the inventory, they can be useful sometimes but prefer the inventory methods presented above if you can.
Expand Down
3 changes: 3 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface BotEvents {
unmatchedMessage: (stringMsg: string, jsonMsg: ChatMessage) => Promise<void> | void
inject_allowed: () => Promise<void> | void
login: () => Promise<void> | void
/** When `respawn` option is disabled, you can call this method manually to respawn. */
spawn: () => Promise<void> | void
respawn: () => Promise<void> | void
game: () => Promise<void> | void
Expand Down Expand Up @@ -427,6 +428,8 @@ export interface Bot extends TypedEmitter<BotEvents> {
acceptResourcePack: () => void

denyResourcePack: () => void

respawn: () => void
}

export interface simpleClick {
Expand Down
9 changes: 8 additions & 1 deletion lib/plugins/health.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,17 @@ function inject (bot, options) {
bot.emit('death')
}
if (!options.respawn) return
bot._client.write('client_command', { payload: 0 })
bot.respawn()
} else if (bot.health > 0 && !bot.isAlive) {
bot.isAlive = true
bot.emit('spawn')
}
})

const respawn = () => {
if (bot.isAlive) return
bot._client.write('client_command', bot.supportFeature('respawnIsPayload') ? { payload: 0 } : { actionId: 0 })
}

bot.respawn = respawn
}

0 comments on commit 3a6ce54

Please sign in to comment.