From acd034a9b93b54788e1a08efcb9c1ffd0e973a64 Mon Sep 17 00:00:00 2001 From: Romain Beaumont Date: Sun, 9 Aug 2020 03:18:37 +0200 Subject: [PATCH] Add anvil saver example #334 --- docs/api.md | 1 + examples/anvil_saver/package.json | 9 +++++++++ examples/anvil_saver/saver.js | 30 ++++++++++++++++++++++++++++++ lib/plugins/blocks.js | 6 +++--- 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 examples/anvil_saver/package.json create mode 100644 examples/anvil_saver/saver.js diff --git a/docs/api.md b/docs/api.md index fd43c30735..8b94266c4f 100644 --- a/docs/api.md +++ b/docs/api.md @@ -729,6 +729,7 @@ Create and return an instance of the class bot. * keepAlive : send keep alive packets : default to true * checkTimeoutInterval : default to `30*1000` (30s), check if keepalive received at that period, disconnect otherwise. * loadInternalPlugins : defaults to true + * 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. * 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 diff --git a/examples/anvil_saver/package.json b/examples/anvil_saver/package.json new file mode 100644 index 0000000000..a91391c0cc --- /dev/null +++ b/examples/anvil_saver/package.json @@ -0,0 +1,9 @@ +{ + "name": "mineflayer-example", + "version": "0.0.0", + "private": true, + "dependencies": { + "prismarine-provider-anvil": "^2.3.0" + }, + "description": "A mineflayer example" +} diff --git a/examples/anvil_saver/saver.js b/examples/anvil_saver/saver.js new file mode 100644 index 0000000000..bd59e6bbee --- /dev/null +++ b/examples/anvil_saver/saver.js @@ -0,0 +1,30 @@ +/* + * This example demonstrates how to save a world with mineflayer and + * https://github.com/PrismarineJS/prismarine-provider-anvil + */ + +const mineflayer = require('mineflayer') +const fs = require('fs') + +if (process.argv.length < 4 || process.argv.length > 6) { + console.log('Usage : node saver.js [] []') + process.exit(1) +} + +const bot = mineflayer.createBot({ + host: process.argv[2], + port: parseInt(process.argv[3]), + username: process.argv[4] ? process.argv[4] : 'saver', + password: process.argv[5], + storageBuilder: ({ version, worldName }) => { + const Anvil = require('prismarine-provider-anvil').Anvil(version) + fs.mkdirSync(worldName) + return new Anvil(worldName) + } +}) + +bot.on('spawn', () => { + bot.waitForChunksToLoad(() => { + console.log('World saved!') + }) +}) diff --git a/lib/plugins/blocks.js b/lib/plugins/blocks.js index c106cc2f51..236567b313 100644 --- a/lib/plugins/blocks.js +++ b/lib/plugins/blocks.js @@ -14,13 +14,13 @@ const paintingFaceToVec = [ new Vec3(1, 0, 0) ] -function inject (bot, { version }) { +function inject (bot, { version, storageBuilder }) { const nbt = require('prismarine-nbt') const Block = require('prismarine-block')(version) const Chunk = require('prismarine-chunk')(version) const ChatMessage = require('prismarine-chat')(version) const World = require('prismarine-world')(version) - bot.world = new World().sync + bot.world = new World(null, storageBuilder({ version: bot.version, worldName: 'overworld' })).sync const signs = {} const paintingsByPos = {} const paintingsById = {} @@ -452,7 +452,7 @@ function inject (bot, { version }) { bot._client.on('respawn', (packet) => { if (dimension === packet.dimension) return dimension = packet.dimension - bot.world = new World().sync + bot.world = new World(null, storageBuilder({ version: bot.version, worldName: 'overworld' })).sync }) bot.findBlock = findBlock