Skip to content

Commit

Permalink
Proper title event (#3498)
Browse files Browse the repository at this point in the history
* titles event

* only 1 event

* remove chatmsg

* add docs and example

* LINT
  • Loading branch information
SMEDjs authored Nov 16, 2024
1 parent fdba037 commit 3829a25
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 26 deletions.
13 changes: 7 additions & 6 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
- ["respawn"](#respawn)
- ["game"](#game)
- ["resourcePack" (url, hash)](#resourcepack-url-hash)
- ["title"](#title)
- ["title" (title, type)](#title-title-type)
- ["rain"](#rain)
- ["weatherUpdate"](#weatherupdate)
- ["time"](#time)
Expand Down Expand Up @@ -1237,11 +1237,12 @@ Emitted when the server changes any of the game properties.

Emitted when the server sends a resource pack.

#### "title"
#### "title" (title, type)

Emitted when the server sends a title

* `text` - title's text
* `title` - title's text
* `type` - title's type "subtitle" or "title"

#### "rain"

Expand Down Expand Up @@ -1771,7 +1772,7 @@ Checks if the given plugin is loaded (or scheduled to be loaded) on this bot.

This function returns a `Promise`, with `void` as its argument upon completion.

Sleep in a bed. `bedBlock` should be a `Block` instance which is a bed.
Sleep in a bed. `bedBlock` should be a `Block` instance which is a bed.

#### bot.isABed(bedBlock)

Expand All @@ -1781,7 +1782,7 @@ Return true if `bedBlock` is a bed

This function returns a `Promise`, with `void` as its argument upon completion.

Get out of bed.
Get out of bed.

#### bot.setControlState(control, state)

Expand Down Expand Up @@ -2120,7 +2121,7 @@ These are lower level methods for the inventory, they can be useful sometimes bu
#### bot.clickWindow(slot, mouseButton, mode)

This function returns a `Promise`, with `void` as its argument upon completion.

The only valid mode option at the moment is 0. Shift clicking or mouse dragging is not implemented.

Click on the current window. See details at https://wiki.vg/Protocol#Click_Container
Expand Down
9 changes: 5 additions & 4 deletions docs/br/api_br.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
- ["respawn"](#respawn)
- ["game"](#game)
- ["resourcePack" (url, hash)](#resourcepack-url-hash)
- ["title"](#title)
- ["title" (title, type)](#title-title-type)
- ["rain"](#rain)
- ["weatherUpdate"](#weatherupdate)
- ["time"](#time)
Expand Down Expand Up @@ -1060,11 +1060,12 @@ Este evento é emitido quando o arquivo index é carregado. Você pode carregar

É emitido quando o servidor envia um pacote de recursos.

#### "title"
#### "title" (title, type)

É emitido quando o servidor exibe um título.

* `text` - texto do título
* `title` - texto do título
* `type` - tipo do título "subtitle" ou "title"

#### "rain"

Expand Down Expand Up @@ -2035,4 +2036,4 @@ Observação: enquanto você voa, `bot.entity.velocity` não é preciso.

#### bot.creative.stopFlying()

Restaura `bot.physics.gravity` ao seu valor original.
Restaura `bot.physics.gravity` ao seu valor original.
7 changes: 4 additions & 3 deletions docs/es/api_es.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
- ["respawn"](#respawn)
- ["game"](#game)
- ["resourcePack" (url, hash)](#resourcepack-url-hash)
- ["title"](#title)
- ["title" (title, type)](#title-title-type)
- ["rain"](#rain)
- ["weatherUpdate"](#weatherupdate)
- ["time"](#time)
Expand Down Expand Up @@ -1071,11 +1071,12 @@ Se emite cuando el servidor cambia cualquiera de sus propiedades

Se emite cuando el servidor manda un paquete de recursos

#### "title"
#### "title" (title, type)

Se emite cuando el servidor manda/muestra un título

* `text` - texto del título
* `title` - texto del título
* `type` - tipo del título "subtitle" o "title"

#### "rain"

Expand Down
11 changes: 6 additions & 5 deletions docs/ru/api_ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
- ["respawn"](#respawn)
- ["game"](#game)
- ["resourcePack" (url, hash)](#resourcepack-url-hash)
- ["title"](#title)
- ["title" (title, type)](#title-title-type)
- ["rain"](#rain)
- ["weatherUpdate"](#weatherupdate)
- ["time"](#time)
Expand Down Expand Up @@ -1264,11 +1264,12 @@ UUID существа, который определяется боссом.

Срабатывает, когда сервер отправляет ресурспак.

#### "title"
#### "title" (title, type)

Срабатывает, когда сервер отправляет текст по центру экрана.

* `text` - Текст на экране.
* `title` - Текст на экране.
* `type` - Тип текста "subtitle" или "title"

#### "rain"

Expand Down Expand Up @@ -1634,7 +1635,7 @@ UUID существа, который определяется боссом.

#### bot.recipesFor(itemType, metadata, minResultCount, craftingTable)

Возвращает список рецептов(`Recipe`), которые вы можете использовать для крафта
Возвращает список рецептов(`Recipe`), которые вы можете использовать для крафта
предмета(`itemType`) с мета-данными(`metadata`).

* `itemType` - Числовой ID предмета, который вы хотите создать.
Expand Down Expand Up @@ -2116,7 +2117,7 @@ bot.once('login', () => {
#### bot.clickWindow(slot, mouseButton, mode)

Эта функция возвращает `Promise` с `void` в качестве аргумента при завершении.

Единственное действительное значение для `mode` - 0. Нажатие с шифтом или перемещение через мышь не реализовано.

Нажимает на текущее окно. Подробнее - https://wiki.vg/Protocol#Click_Container
Expand Down
11 changes: 6 additions & 5 deletions docs/zh/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
- ["respawn"](#respawn)
- ["game"](#game)
- ["resourcePack" (url, hash)](#resourcepack-url-hash)
- ["title"](#title)
- ["title" (title, type)](#title-title-type)
- ["rain"](#rain)
- ["weatherUpdate"](#weatherupdate)
- ["time"](#time)
Expand Down Expand Up @@ -1127,11 +1127,12 @@ Emitted for every server message, including chats.

当服务器发送资源包时触发

#### "title"
#### "title" (title, type)

当服务器发送标题时触发

* `text` - 标题文本
* `title` - 标题文本
* `type` - 标题类型 "subtitle" 或 "title"

#### "rain"

Expand All @@ -1150,11 +1151,11 @@ If you join a server where it is already raining, this event will fire.

当bot从服务器被踢出时触发

`reason`是一条解释你被踢的原因的聊天信息.
`reason`是一条解释你被踢的原因的聊天信息.

`loggedIn`
如果客户端在成功登录后被踢出则为`true`
如果kick发生在登录阶段则为 `false`
如果kick发生在登录阶段则为 `false`

#### "end" (reason)

Expand Down
22 changes: 22 additions & 0 deletions examples/titles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* An example of how to handle title events from the server.
*/
const mineflayer = require('mineflayer')

if (process.argv.length < 4 || process.argv.length > 6) {
console.log('Usage : node titles.js <host> <port> [<name>] [<password>]')
process.exit(1)
}

const bot = mineflayer.createBot({
host: process.argv[2],
port: parseInt(process.argv[3]),
username: process.argv[4] ? process.argv[4] : 'titles',
password: process.argv[5]
})

// This event is triggered when the server sends a title to the client.
bot.on('title', (text, type) => {
// type is either "title" or "subtitle"
console.log(`Received ${type}: ${text}`)
})
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface BotEvents {
spawn: () => Promise<void> | void
respawn: () => Promise<void> | void
game: () => Promise<void> | void
title: (text: string) => Promise<void> | void
title: (text: string, type: "subtitle" | "title") => Promise<void> | void
rain: () => Promise<void> | void
time: () => Promise<void> | void
kicked: (reason: string, loggedIn: boolean) => Promise<void> | void
Expand Down
9 changes: 7 additions & 2 deletions lib/plugins/title.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
module.exports = inject

function inject (bot) {
bot._client.on('title', (packet) => {
if (packet.action === 0 || packet.action === 1) {
bot.emit('title', packet.text)
bot.emit('title', packet.text, 'title')
}
})
bot._client.on('set_title_text', packet => {
bot.emit('title', packet.text, 'title')
})
bot._client.on('set_title_subtitle', packet => {
bot.emit('title', packet.text, 'subtitle')
})
}

0 comments on commit 3829a25

Please sign in to comment.