Skip to content

Commit

Permalink
Add anvil saver example
Browse files Browse the repository at this point in the history
  • Loading branch information
rom1504 committed Aug 9, 2020
1 parent e81bbd0 commit 6619d4c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
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

0 comments on commit 6619d4c

Please sign in to comment.