You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.3 KiB
JavaScript

/*
IMPORTANT NOTE:
To run, do `yarn install` or `npm install` in your console
Then, replace your_email_here with your microsoft account on line 44
*/
const mineflayer = require('mineflayer')
const sharp = require('sharp')
const mergeImg = require('merge-img')
const fs = require('fs');
const path = require('path');
const { v4: uuidv4 } = require('uuid');
function combineVert(uuid) {
return new Promise((resolve, reject) => {
loop = 0
for (let i = 0; i < 10; i++) {
if (i % 2 === 0) { // if number is even
let iterations = loop
loop++
mergeImg([path.join(__dirname, uuid, `map${i}.png`), path.join(__dirname, uuid, `map${i + 1}.png`)], {
direction: true
}).then((img) => {
img.write(path.join(__dirname, uuid, `${iterations}.png`), () => {
if (i === 8) {
resolve()
}
});
});
}
}
})
}
function run() {
return new Promise((resolve) => {
const uuid = `data${uuidv4()}`;
let num = 0;
fs.mkdirSync(`./${uuid}`)
const bot = mineflayer.createBot({
username: "your_email_here",
host: 'minehut.com',
viewDistance: 'tiny',
auth: 'microsoft',
version: '1.17.1'
})
bot._client.on('map', async (map) => {
let data = map.data
function hexToByte(hex) {
debugger
if (hex.charAt(0) == '#') {
hex = hex.substr(1)
}
const a = parseInt(hex.slice(0, 2), 16)
const r = parseInt(hex.slice(2, 4), 16)
const g = parseInt(hex.slice(4, 6), 16)
const b = parseInt(hex.slice(6, 8), 16)
return [r, g, b, a]
}
const buf = new Buffer.from(data)
const imgBuf = new Uint8ClampedArray(128 * 128 * 4)
for (let index = 0; index < imgBuf.byteLength; index += 4) {
const colorMap = require('./colors.json')
const colorArr = hexToByte(colorMap[buf[index / 4]])
for (let k = 0; k < 4; k++) {
imgBuf[index + k] = colorArr[k]
}
}
let mapbuffer = await sharp(imgBuf, {
raw: {
width: 128,
height: 128,
channels: 4
}
})
.png()
.toBuffer()
fs.writeFileSync(path.join(__dirname, uuid, `map${num}.png`), mapbuffer)
if (num === 9) {
async function combineMaps() {
await combineVert(uuid)
mergeImg([path.join(__dirname, uuid, `0.png`), path.join(__dirname, uuid, `1.png`), path.join(__dirname, uuid, `2.png`), path.join(__dirname, uuid, `3.png`), path.join(__dirname, uuid, `4.png`)], {
direction: false
}).then((img) => {
img.write(path.join(__dirname, uuid, "resultraw.png"), () => {
for (let i = 0; i < 5; i++) {
fs.unlinkSync(path.join(__dirname, uuid, `${i}.png`))
}
for (let i = 0; i < 10; i++) {
fs.unlinkSync(path.join(__dirname, uuid, `map${i}.png`))
}
bot.end();
resolve();
});
}).catch((err) => {
console.log(`Error: ${err}`)
})
};
combineMaps()
}
num++
})
bot.on('error', (err) => console.log(err))
bot.on('kicked', reason => console.info('Kicked for', reason))
bot.on('end', (reason) => console.log('Disconnected ' + reason))
})
}
function sleep(m) {
return new Promise((resolve) => {
setTimeout(() => {
resolve()
}, m)
})
}
async function main() {
for (let i = 0; i < 100; i++) {
await run();
await sleep(5000)
console.log('completed ' + i)
}
process.exit(0)
};
main()