Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add anvil saver example #1222

Merged
merged 1 commit into from
Aug 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions examples/anvil_saver/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "mineflayer-example",
"version": "0.0.0",
"private": true,
"dependencies": {
"prismarine-provider-anvil": "^2.3.0"
},
"description": "A mineflayer example"
}
30 changes: 30 additions & 0 deletions examples/anvil_saver/saver.js
Original file line number Diff line number Diff line change
@@ -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 <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] : '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!')
})
})
20 changes: 17 additions & 3 deletions lib/plugins/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ const paintingFaceToVec = [
new Vec3(1, 0, 0)
]

function inject (bot, { version }) {
const dimensionNames = {
'-1': 'minecraft:nether',
0: 'minecraft:overworld',
1: 'minecraft:end'
}

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
const signs = {}
const paintingsByPos = {}
const paintingsById = {}
Expand Down Expand Up @@ -446,13 +451,22 @@ function inject (bot, { version }) {
// if we get a respawn packet and the dimension is changed,
// unload all chunks from memory.
let dimension

function dimensionToFolderName (dimension) {
if (bot.supportFeature('dimensionIsAnInt')) {
return dimensionNames[dimension]
} else if (bot.supportFeature('dimensionIsAString')) {
return dimension
}
}
bot._client.on('login', (packet) => {
dimension = packet.dimension
bot.world = new World(null, storageBuilder ? storageBuilder({ version: bot.version, worldName: dimensionToFolderName(packet.dimension) }) : null).sync
})
bot._client.on('respawn', (packet) => {
if (dimension === packet.dimension) return
dimension = packet.dimension
bot.world = new World().sync
bot.world = new World(null, storageBuilder ? storageBuilder({ version: bot.version, worldName: dimensionToFolderName(packet.dimension) }) : null).sync
})

bot.findBlock = findBlock
Expand Down