mineflayer-database v0.0.5
npm install mineflayer-database
This creates a bot that stores the bot position in a table inside a generated Database, so you can retrieve it for later use. Even after the program restarts.
/*This line requires the modules to run this script,
in this case, mineflayer to create the bot,
and mineflayer-database to manage the databases*/
const mineflayer = require('mineflayer');
const { database } = require('mineflayer-database');
/*This line creates and spawns the bot*/
const bot = mineflayer.createBot()
/*This line waits for the bot to spawn*/
bot.on('spawn', async () => {
//This line loads the plugin.
bot.loadPlugin(database);
/*This line creates a new database called 'botData',
and assings it to the constant 'botDatabase'.*/
const botDatabase = await bot.database.setDatabase('botData');
/*This line waits for the plugin to add a table that's called 'TestTable',
and assings the bot's username to the table as the value.*/
await bot.database.addTable(botDatabase, 'TestTable', bot.username);
/*This line waits for the plugin to retrieve a table that's called 'TestTable',
and assigns its value (in this case the username of the bot) to the constant 'botData'.*/
const botData = await bot.database.getTable(botDatabase, 'TestTable');
/*This line prints the value of the constant 'botData',
(in this case the username of the bot) to the terminal.*/
console.log(botData);
});
Methods
BotInstance.database.setDatabase(name)
Starts a database with name as identifier. To access its tables you need to initialize the database with the same name. Also returns a Promise with the database as the value. Recommended usage:
const botDatabase = await bot.database.setDatabase(name)
BotInstance.database.addTable(database, name, value)
Adds a table named name and assings value to it. database is the database initialized with .setDatabase(). Also returns a Promise with the value as the value. Recommended usage:
await bot.database.addTable(database, name, value)
BotInstance.database.removeTable(database, name)
Removes the table named name from database.
Also returns a Promise with null
as the value.
Recommended usage:
await bot.database.removeTable(database, name)
BotInstance.database.getTable(database, name)
Returns the value of the table named name from the database. Also returns a Promise with the value of the table named name as the value. Recommended usage:
const tableValue = await bot.database.getTable(database, name)