Skip to content

Commit

Permalink
Made bot auto respawning togglable (#3010)
Browse files Browse the repository at this point in the history
* bot respawn can now be opt-out

* added respawn field to BotOptions

* added respawn field to docs

* fixed code style

* removed bot.respawn

* switched bot.respawn -> options.respawn
  • Loading branch information
Averagess committed May 20, 2023
1 parent acf14c8 commit 4415e73
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ Create and return an instance of the class bot.
* storageBuilder : an optional function, takes as argument version and worldName and return an instance of something with the same API as prismarine-provider-anvil. Will be used to save the world.
* client : an instance of node-minecraft-protocol, if not specified, mineflayer makes it's own client. This can be used to enable using mineflayer through a proxy of many clients or a vanilla client and a mineflayer client.
* brand : the brand name for the client to use. Defaults to vanilla. Can be used to simulate custom clients for servers that require it.
* respawn : when set to false disables bot from automatically respawning, defaults to true.
* plugins : object : defaults to {}
- pluginName : false : don't load internal plugin with given name ie. `pluginName`
- pluginName : true : load internal plugin with given name ie. `pluginName` even though loadInternalplugins is set to false
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface BotOptions extends ClientOptions {
client?: Client
brand?: string
defaultChatPatterns?: boolean
respawn?: boolean
}

export type ChatLevel = 'enabled' | 'commandsOnly' | 'disabled'
Expand Down
1 change: 1 addition & 0 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function createBot (options = {}) {
options.loadInternalPlugins = options.loadInternalPlugins ?? true
options.client = options.client ?? null
options.brand = options.brand ?? 'vanilla'
options.respawn = options.respawn ?? true
const bot = new EventEmitter()
bot._client = options.client
bot.end = (reason) => bot._client.end(reason)
Expand Down
3 changes: 2 additions & 1 deletion lib/plugins/health.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = inject

function inject (bot) {
function inject (bot, options) {
bot.isAlive = true

bot._client.on('respawn', (packet) => {
Expand All @@ -24,6 +24,7 @@ function inject (bot) {
bot.isAlive = false
bot.emit('death')
}
if (!options.respawn) return
bot._client.write('client_command', { payload: 0 })
} else if (bot.health > 0 && !bot.isAlive) {
bot.isAlive = true
Expand Down

0 comments on commit 4415e73

Please sign in to comment.