-
-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example for using the node:readline module (#2907)
* add example for using the node:readline module * Rename examples/readline.js to examples/cli/readline.js
- Loading branch information
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* This example is an easy way to connect mineflayer to the node:readline module | ||
* See: https://nodejs.org/api/readline.html | ||
* Using this, we can make a simple terminal-to-ingame-chat interface | ||
* | ||
* Made by Jovan04 01/24/2023 | ||
*/ | ||
|
||
if (process.argv.length < 4 || process.argv.length > 6) { | ||
console.log('Usage : node readline.js <host> <port> [<name>] [<auth>]') | ||
process.exit(1) | ||
} | ||
|
||
const mineflayer = require('mineflayer') // load mineflayer library | ||
const readline = require('node:readline') // load the node.js readline module | ||
|
||
// bot options | ||
const options = { | ||
host: process.argv[2], | ||
port: parseInt(process.argv[3]), | ||
username: process.argv[4] || 'readline', | ||
auth: process.argv[5] || 'offline' | ||
} | ||
|
||
const bot = mineflayer.createBot(options) // join the minecraft server | ||
|
||
const rl = readline.createInterface({ // creates our readline interface with our console as input and output | ||
input: process.stdin, | ||
output: process.stdout | ||
}) | ||
|
||
bot.once('spawn', () => { | ||
console.log(`Bot joined the game with username ${bot.username}.`) | ||
rl.setPrompt('> '); rl.prompt() // gives us a little arrow at the bottom for the input line | ||
}) | ||
|
||
bot.on('message', (message) => { | ||
readline.moveCursor(process.stdout, -2, 0) // we move the cursor to the left two places because our cursor is already two positions in (because of the input arrow) | ||
console.log(message.toAnsi()) // convert our message to ansi to preserve chat formatting | ||
rl.prompt() // regenerate our little arrow on the input line | ||
}) | ||
|
||
rl.on('line', (line) => { | ||
readline.moveCursor(process.stdout, 0, -1) // move cursor up one line | ||
readline.clearScreenDown(process.stdout) // clear all the lines below the cursor (i.e. the last line we entered) | ||
bot.chat(line.toString()) // sends the line we entered to ingame chat | ||
}) | ||
|
||
bot.on('kicked', console.log) | ||
bot.on('error', console.log) |