-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
108 lines (96 loc) · 3.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const { mapToImage, supportedVersions } = require('./lib/util.js')
const { promises: fs } = require('fs')
const path = require('path')
const EventEmitter = require('events')
/**
* @typedef MapPacket
* @property {number} itemDamage
* @property {number} scale
* @property {boolean} trackingPosition
* @property {Array<unknown>} icons
* @property {number} columns
* @property {number?} rows
* @property {number?} x
* @property {number?} y
* @property {Buffer?} data
*/
/**
* @param {import('mineflayer').Bot} bot
*/
function mineflayerPlugin (bot, options = {}) {
if (!supportedVersions.includes(bot.majorVersion)) {
console.error('Map downloader: Version not supported')
return
}
const outputDir = options['mapDownloader-outputDir'] ?? path.join('.')
const saveToFile = options['mapDownloader-saveToFile'] ?? true
const saveInternal = options['mapDownloader-saveInternal'] ?? true
const filePrefix = options["mapDownloader-filePrefix"] ?? ''
const fileSuffix = options["mapDownloader-fileSuffix"] ?? ''
bot.mapDownloader = new MapSaver(bot.version, { outputDir, saveToFile, saveInternal, filePrefix, fileSuffix })
bot._client.on('map', (data) => {
bot.mapDownloader.onMapPacket(data)
})
bot.mapDownloader.on('new_map', data => {
bot.emit('new_map', data)
})
bot.mapDownloader.on('new_map_saved', data => {
bot.emit('new_map_saved', data)
})
}
class MapSaver extends EventEmitter {
constructor (version, options = {}) {
super()
this.isActive = true
this.outputDir = options.outputDir ?? path.join('.')
this.saveToFile = options.saveToFile ?? true
this.saveInternal = options.saveInternal ?? true
this.filePrefix = options.filePrefix ?? ''
this.fileSuffix = options.fileSuffix ?? ''
this.version = version
this.maps = {}
// Mojang fixed columns and rows having negative numbers for 1.17
this.fullColumnRow = require('minecraft-data')(this.version).version['>']('1.16.5') ? 128 : -128
this.majorVersion = require('minecraft-data')(this.version).version.majorVersion
}
/** @param {MapPacket} data */
onMapPacket (data) {
if (!this.isActive) return
const mapId = data.itemDamage
if (!(mapId in this.maps) && data.data && data.columns === this.fullColumnRow && data.rows === this.fullColumnRow) {
mapToImage(data.data, data.itemDamage, this.majorVersion)
.then((pngBuf) => {
const mapName = `${this.filePrefix}map_${mapId.toString().padStart(6, '0')}${this.fileSuffix}.png`
this.emit('new_map', {
name: mapName,
png: pngBuf,
id: mapId,
})
if (this.saveInternal) this.maps[mapId] = pngBuf
if (!this.saveToFile) return
fs.mkdir(this.outputDir, { recursive: true }).then(() => {
fs.writeFile(path.join(this.outputDir, mapName), pngBuf).then(() => {
this.emit('new_map_saved', {
name: mapName,
id: mapId,
})
});
})
})
.catch(console.error)
return true
}
return false
}
activate() {
this.isActive = true
}
deactivate() {
this.isActive = false
}
}
module.exports = {
mapDownloader: mineflayerPlugin,
MapSaver,
supportedVersions
}