Advertisement
acxl

code base version 1.1 (CREDIT: snowcatman)

Dec 11th, 2020
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mineflayer = require('mineflayer', 'minecraft-protocol')
  2. const Vec3 = require('vec3').Vec3
  3. const fs = require('fs')
  4. const { version } = require('os')
  5. const pvp = require('mineflayer-pvp').plugin
  6. const { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
  7. const armorManager = require('mineflayer-armor-manager')
  8. const autoeat = require('mineflayer-auto-eat')
  9. const minecraftHawkEye = require('minecrafthawkeye')
  10.  
  11. function jsonReader(filePath, cb) {
  12.     fs.readFile(filePath, (err, fileData) => {
  13.         if (err) {
  14.             return cb && cb(err)
  15.         }
  16.         try {
  17.             const object = JSON.parse(fileData)
  18.             return cb && cb(null, object)
  19.         } catch (err) {
  20.             return cb && cb(err)
  21.         }
  22.     })
  23. }
  24.  
  25. jsonReader('./mc_login_info.json', (err, mc_login_info) => {
  26.     if (err) {
  27.         console.log(err)
  28.         return
  29.     }
  30.  
  31.     const bot = mineflayer.createBot({
  32.         version: mc_login_info.version,
  33.         host: mc_login_info.host,
  34.         port: mc_login_info.port,
  35.         username: mc_login_info.username,
  36.         password: mc_login_info.password,
  37.         // give time for communication to hosts&client with longer ping times
  38.         checkTimeoutInterval: 30 * 1000, // 30 * 1000 is equal to 30 seconds
  39.         logErrors: true
  40.     })
  41. })
  42.  
  43.     bot._client.on('custom_payload', (packet) => {
  44.         // console.log(packet)
  45.         if (packet.channel === "minecraft:brand") {
  46.             let data = packet.data
  47.             let brand = String.fromCharCode.apply(null, data)
  48.             console.log(brand)
  49.         }
  50.     })
  51.  
  52.     bot.once('login', () => {
  53.         console.log('logged in')
  54.         console.log(`curent client version ${bot.version}`)
  55.         // console.log(`curent server version is ${server.version}`)
  56.     })
  57.  
  58.     bot.loadPlugin(autoeat)
  59.     bot.loadPlugin(armorManager)
  60.     bot.loadPlugin(minecraftHawkEye)
  61.     bot.loadPlugin(pathfinder)
  62.     bot.loadPlugin(pvp)
  63.  
  64.    
  65.     bot.on('playerCollect', (collector, itemDrop) => {
  66.         if (collector !== bot.entity) return
  67.        
  68.         setTimeout(() => {
  69.             const sword = bot.inventory.items().find(item => item.name.includes('sword'))
  70.             if (sword) bot.equip(sword, 'hand')
  71.         }, 150)
  72.     })
  73.    
  74.     bot.on('playerCollect', (collector, itemDrop) => {
  75.         if (collector !== bot.entity) return
  76.        
  77.         setTimeout(() => {
  78.             const shield = bot.inventory.items().find(item => item.name.includes('shield'))
  79.             if (shield) bot.equip(shield, 'off-hand')
  80.         }, 250)
  81.     })
  82.    
  83.     let guardPos = null
  84.     let busy = false
  85.  
  86.     bot.on('spawn', () => { // gives error when bot spawns
  87.         console.log('spawned in server')
  88.         bot.hawkEye.stop()
  89.         stopGuarding()
  90.         bot.chat('I spawned, watch out!')
  91.     })
  92.    
  93.     function canSee(pos) {
  94.         const block = bot.blockAt(pos)
  95.         const r = bot.canSeeBlock(block)
  96.         if (r) {
  97.             bot.chat(`I can see the block of ${block.displayName} at ${pos}`)
  98.         } else {
  99.             bot.chat(`I cannot see the block of ${block.displayName} at ${pos}`)
  100.         }
  101.     }
  102.    
  103.     function sayPosition(username) {
  104.         //bot.chat(`My puplic position is disabled`)
  105.         bot.chat(`I am at ${bot.entity.position}`)
  106.         //bot.chat(`You are at ${bot.players[username].entity.position}`)
  107.         //bot.chat(`I don't know your position.`)
  108.         bot.chat(`${username} is at ${bot.players[username].entity.position} from me.`)
  109.     }
  110.  
  111.     function sayEquipment() {
  112.         const eq = bot.players[username].entity.equipment
  113.         const eqText = []
  114.         if (eq[0]) eqText.push(`holding a ${eq[0].displayName}`)
  115.         if (eq[1]) eqText.push(`wearing a ${eq[1].displayName} on your feet`)
  116.         if (eq[2]) eqText.push(`wearing a ${eq[2].displayName} on your legs`)
  117.         if (eq[3]) eqText.push(`wearing a ${eq[3].displayName} on your torso`)
  118.         if (eq[4]) eqText.push(`wearing a ${eq[4].displayName} on your head`)
  119.         if (eqText.length) {
  120.             bot.chat(`You are ${eqText.join(', ')}.`)
  121.         } else {
  122.             bot.chat('You are without armor!')
  123.         }
  124.     }
  125.  
  126.     function sayVersion() { // work in prograss not working yet
  127.         bot.chat('/version')
  128.         console.log(`${command.message} confermed ${brand}`)
  129.     }
  130.  
  131.     function saySpawnPoint() {
  132.         bot.chat(`Spawn is at ${bot.spawnPoint}`)
  133.     }
  134.  
  135.     function sayBlockUnder() {
  136.         const block = bot.blockAt(bot.players[username].entity.position.offset(0, -1, 0))
  137.         bot.chat(`Block under you is ${block.displayName} in the ${block.biome.name} biome`)
  138.         console.log(block)
  139.     }
  140.  
  141.     function quit(username) {
  142.         console.log('quit command used')
  143.         bot.quit(`${username} told me too`)
  144.     }
  145.  
  146.     function sayNick() {
  147.         bot.chat(`My name is ${bot.player.displayName}`)
  148.     }
  149.    
  150.     // begin of autoeats paste
  151.     bot.once('spawn', () => {
  152.         bot.autoEat.options = {
  153.             priority: 'foodPoints',
  154.             startAt: 14,
  155.             bannedFood: []
  156.         }
  157.     })
  158.     // The bot eats food automatically and emits these events when it starts eating and stops eating.
  159.     bot.on('autoeat_started', () => {
  160.         console.log('Auto Eat started!')
  161.     })
  162.  
  163.     bot.on('autoeat_stopped', () => {
  164.         console.log('Auto Eat stopped!')
  165.     })
  166.  
  167.     bot.on('health', () => {
  168.         if (bot.food === 20) bot.autoEat.disable()
  169.         // Disable the plugin if the bot is at 20 food points
  170.         else bot.autoEat.enable() // Else enable the plugin again
  171.     })
  172.     // end of paste autoeat
  173.  
  174.     bot.on('whisper', (username, message, rawMessage) => {
  175.         console.log(`I received a message from ${username}: ${message}`)
  176.         bot.whisper(username, 'I can tell secrets too.')
  177.     })
  178.  
  179.     bot.on('nonSpokenChat', (message) => {
  180.         console.log(`Non spoken chat: ${message}`)
  181.     })
  182.    
  183.     bot.on('login', () => {
  184.         bot.chat('Hi everyone!')
  185.     })
  186.    
  187.     bot.on('spawnReset', (message) => {
  188.         console.log('spawnReset command used')
  189.         stopGuarding()
  190.         bot.hawkEye.stop()
  191.         bot.chat('Oh noez! My bed is broken.')
  192.     })
  193.    
  194.     bot.on('forcedMove', () => {
  195.         console.log(`I have been forced to move to ${bot.entity.position}`)
  196.         bot.chat(`I have been forced to move to ${bot.entity.position}`)
  197.     })
  198.    
  199.     bot.on('health', () => {
  200.         console.log(`I have ${bot.health} health and ${bot.food} food`)
  201.         bot.chat(`I have ${bot.health} health and ${bot.food} food`)
  202.     })
  203.  
  204.     bot.on('death', () => {
  205.         console.log('I died, stopping and respawning')
  206.         stopGuarding()
  207.         bot.hawkEye.stop()
  208.         bot.respawn
  209.         bot.chat('I died x.x ugh!!')
  210.         // need instructions to return to
  211.         // what i was doing.
  212.     })
  213.  
  214.     bot.on('kicked', (reason) => {
  215.         console.log(`I got kicked for ${reason}`)
  216.     })
  217.    
  218.     bot.on('rain', () => {
  219.         if (bot.isRaining) {
  220.             console.log('It started raining.')
  221.             //bot.chat('It started raining.')
  222.         } else {
  223.             console.log('It stopped raining.')
  224.             //bot.chat('It stopped raining.')
  225.         }
  226.     })
  227.  
  228.     bot.on('noteHeard', (block, instrument, pitch) => {
  229.         bot.chat(`Music for my ears! I just heard a ${instrument.name}`)
  230.     })
  231.  
  232.     bot.on('chestLidMove', (block, isOpen) => {
  233.         const action = isOpen ? 'open' : 'close'
  234.         bot.chat(`Hey, did someone just ${action} a chest?`)
  235.     })
  236.  
  237.     bot.on('pistonMove', (block, isPulling, direction) => {
  238.         const action = isPulling ? 'pulling' : 'pushing'
  239.         bot.chat(`A piston is ${action} near me, i can hear it.`)
  240.     })
  241.  
  242.     bot.on('playerJoined', (player) => {
  243.         if (player.username !== bot.username) {
  244.             console.log(`${player.username}! Joined the server.`)
  245.             //bot.chat(`Hello, ${player.username}! Welcome to the server.`)
  246.         }
  247.     })
  248.  
  249.     bot.on('playerLeft', (player) => {
  250.         if (player.username === bot.username) return
  251.         console.log(`${player.username} left the server`)
  252.         // bot.chat(`Bye ${player.username}`)
  253.     })
  254.     /*
  255.     bot.on('playerCollect', (collector, collected) => {
  256.         if (collector.type === 'player' && collected.type === 'object') {
  257.             const rawItem = collected.metadata[10]
  258.             const item = mineflayer.Item.fromNotch(rawItem)
  259.             bot.chat(`${collector.username !== bot.username ? ("I'm so jealous. " + collector.username) : 'I '} collected ${item.count} ${item.displayName}`)
  260.         }
  261.     })
  262.     */
  263.     /*    
  264.     bot.on('entitySpawn', (entity) => {
  265.         if (entity.type === 'mob') {
  266.             console.log(`Look out! A ${entity.mobType} spawned at ${entity.position}`)
  267.         } else if (entity.type === 'player') {
  268.             console.log(`Look who decided to show up: ${entity.username}`)
  269.         } else if (entity.type === 'object') {
  270.             console.log(`There's a ${entity.objectType} at ${entity.position}`)
  271.         } else if (entity.type === 'global') {
  272.             console.log('Ooh lightning!')
  273.         } else if (entity.type === 'orb') {
  274.             console.log('Gimme dat exp orb!')
  275.         }
  276.     })
  277.     */    
  278.  
  279.     const target = bot.hawkEye.getPlayer()
  280.     console.log(target)
  281.     if (!target) {
  282.         return false
  283.     }
  284.  
  285.     function Start() {
  286.         bot.hawkEye.autoAttack(target)
  287.     }
  288.  
  289.     bot.on('entityHurt', (entity) => {
  290.         if (entity.type === 'mob') {
  291.             bot.chat(`Haha! The ${entity.mobType} got hurt!`)
  292.         } else if (entity.type === 'player') {
  293.             bot.chat(`Aww, poor ${entity.username} got hurt. Maybe you shouldn't have a ping of ${bot.players[entity.username].ping}`)
  294.        }
  295.    })
  296.  
  297.    bot.on('entitySwingArm', (entity) => {
  298.        bot.chat(`${entity.username}, I see that your arm is working fine.`)
  299.    })
  300.  
  301.    bot.on('entityCrouch', (entity) => {
  302.        bot.chat(`${entity.username}: you so sneaky.`)
  303.    })
  304.  
  305.    bot.on('entityUncrouch', (entity) => {
  306.        bot.chat(`${entity.username}: welcome back from the land of hunchbacks.`)
  307.    })
  308.  
  309.    bot.on('entitySleep', (entity) => {
  310.        bot.chat(`Good night, ${entity.username}`)
  311.    })
  312.  
  313.    bot.on('entityWake', (entity) => {
  314.        bot.chat(`Top of the morning, ${entity.username}`)
  315.    })
  316.  
  317.    bot.on('entityEat', (entity) => {
  318.        bot.chat(`${entity.username}: OM NOM NOM NOMONOM. That's what you sound like.`)
  319.     })
  320.  
  321.     bot.on('entityAttach', (entity, vehicle) => {
  322.         if (entity.type === 'player' && vehicle.type === 'object') {
  323.             bot.chat(`Sweet, ${entity.username} is riding that ${vehicle.objectType}`)
  324.         }
  325.     })
  326.  
  327.     bot.on('entityDetach', (entity, vehicle) => {
  328.         if (entity.type === 'player' && vehicle.type === 'object') {
  329.             bot.chat(`Lame, ${entity.username} stopped riding the ${vehicle.objectType}`)
  330.         }
  331.     })
  332.  
  333.     bot.on('entityEquipmentChange', (entity) => {
  334.         console.log('entityEquipmentChange', entity)
  335.     })
  336.  
  337.     bot.on('entityEffect', (entity, effect) => {
  338.         console.log('entityEffect', entity, effect)
  339.     })
  340.  
  341.     bot.on('entityEffectEnd', (entity, effect) => {
  342.         console.log('entityEffectEnd', entity, effect)
  343.     })
  344.  
  345.     function guardArea(pos) {
  346.         guardPos = pos.clone()
  347.        
  348.         if (!bot.pvp.target) {
  349.             moveToGuardPos()
  350.         }
  351.     }
  352.    
  353.     function stopGuarding() {
  354.         guardPos = null
  355.         bot.pvp.stop()
  356.         bot.pathfinder.setGoal(null)
  357.     }
  358.    
  359.     function moveToGuardPos() {
  360.         const mcData = require('minecraft-data')(bot.version)
  361.         bot.pathfinder.setMovements(new Movements(bot, mcData))
  362.         bot.pathfinder.setGoal(new goals.GoalBlock(guardPos.x, guardPos.y, guardPos.z))
  363.     }
  364.    
  365.     bot.on('stoppedAttacking', () => {
  366.         if (guardPos) {
  367.             moveToGuardPos()
  368.         }
  369.     })
  370.    
  371.     bot.on('physicTick', () => {
  372.         if (bot.pvp.target) return
  373.         if (bot.pathfinder.isMoving()) return
  374.        
  375.         const entity = bot.nearestEntity()
  376.         if (entity) bot.lookAt(entity.position.offset(0, entity.height, 0))
  377.     })
  378.    
  379.     bot.on('physicTick', () => {
  380.         if (!guardPos) return
  381.        
  382.         const filter = e => e.type === 'mob' && e.position.distanceTo(bot.entity.position) < 8 &&
  383.         e.mobType !== 'Armor Stand' // Mojang classifies armor stands as mobs for some reason?
  384.        
  385.         const entity = bot.nearestEntity(filter)
  386.         if (entity) {
  387.             bot.pvp.attack(entity)
  388.         }
  389.     })
  390.     /*
  391.     bot.on('chat', (username, message) => {
  392.         if (username === bot.username) return
  393.         const result = /canSee (-?[0-9]+),(-?[0-9]+),(-?[0-9]+)/.exec(message)
  394.         if (result) {
  395.             canSee(new Vec3(result[1], result[2], result[3]))
  396.             return
  397.         }
  398.         switch (message) {
  399.             case 'pos':
  400.                 console.log("pos command used")
  401.                 sayPosition(username)
  402.                 break
  403.             case 'wearing':
  404.                 console.log("wearing command used")
  405.                 sayEquipment()
  406.                 break
  407.             case 'nick':
  408.                 console.log("saynick command used")
  409.                 sayNick()
  410.                 break
  411.             case 'spawn':
  412.                 console.log("spawn command used")
  413.                 saySpawnPoint()
  414.                 break
  415.             case 'block':
  416.                 console.log("block command used")
  417.                 sayBlockUnder(username)
  418.                 break
  419.             default:
  420.                 console.log("I am ready!")
  421.                 bot.chat("I am ready!")
  422.     }
  423.     */
  424.  
  425.     // text of bots username and help will give a list of bot commands
  426.     bot.on('chat', (username, message) => { // work in progress....
  427.         if (message === `${bot.player.displayName} help`)
  428.             bot.chat('command reminders are:  stop, leave, fight me, guard, ')
  429.             bot.chat('pos, wearing, nick, spawn, block ')
  430.             bot.chat('arch-start, Version.(wip not all commands work...)')
  431.             bot.chat('Bot may just leave if it errors out.')
  432.     })
  433.  
  434.     bot.on('chat', (username, message) => { // work in progress....
  435.         if (message === 'Version') {
  436.             console.log("Version command used just used")
  437.             console.log(`version check! The version of the server is ${command.message} confermed ${brand}`)
  438.             bot.chat(`version check! The version of the server is ${command.message} confermed ${brand}`)
  439.             console.log("sayVersion command used")
  440.             sayVersion(username)
  441.             return
  442.         }
  443.     })
  444.  
  445.     bot.on('chat', (username, message) => {
  446.         if (message === 'leave') {
  447.                 console.log("leave command used")
  448.                 bot.chat("Yes sir right away!")
  449.                 bot.quit(username)
  450.                 return
  451.         }
  452.     })
  453.  
  454.     bot.on('chat', (username, message) => {
  455.         if (message === 'arch-start') {
  456.             console.log("arch-start command used")
  457.             bot.chat("Arrows away!! Ya, geting them... whahoo!!!")
  458.             Start()
  459.             return
  460.         }
  461.     })
  462.  
  463.     bot.on('chat', (username, message) => {
  464.         if (message === 'guard') {          // if message guard do gaurd
  465.             const player = bot.players[username]
  466.  
  467.             if (!player) {
  468.                 bot.chat("I can't see you.")
  469.                 return
  470.             }
  471.         bot.chat('I will guard that location.')
  472.             guardArea(player.entity.position)
  473.             }
  474.  
  475.             if (message === 'fight me') {
  476.                 const player = bot.players[username]
  477.  
  478.                 if (!player) {
  479.                     bot.chat("I can't see you.")
  480.                     return
  481.                 }
  482.  
  483.             bot.chat('Prepare to fight!')
  484.             bot.chat('5')
  485.             bot.chat('4')
  486.             bot.chat('3')
  487.             bot.chat('2')
  488.             bot.chat('1')
  489.             bot.chat('GO!!!')
  490.             bot.pvp.attack(player.entity)
  491.             }
  492.  
  493.             if (message === 'stop') {
  494.                 bot.chat('I will stop!')
  495.                 bot.hawkEye.stop()
  496.                 stopGuarding()
  497.             }
  498.     })
  499.  
  500.     function not_busy() {
  501.         guardPos = pos.clone()
  502.  
  503.         if (guardPos == null) {
  504.             return false
  505.        
  506.         } else { (guardPos == !null)
  507.                 return true
  508.             }
  509.         }
  510.    
  511.    
  512.     bot.on('not_busy', () => { // if bot is not busy than say "I am ready"
  513.         if (busy == true) {
  514.         console.log("I am busy!")
  515.         bot.chat("I am busy!")
  516.             } else { if (busy == false)
  517.             console.log('I am ready!')
  518.             bot.chat('I am ready!')
  519.             }
  520.    
  521.     })
  522.  
  523.     bot.on('sleep', () => {
  524.         bot.chat('Good night!')
  525.     })
  526.  
  527.     bot.on('wake', () => {
  528.         bot.chat('Good morning!')
  529.     })
  530.  
  531.     function goToSleep() {
  532.         const bed = bot.findBlock({
  533.             matching: block => bot.isABed(block)
  534.         })
  535.         if (bed) {
  536.             bot.sleep(bed, (err) => {
  537.                 if (err) {
  538.                     bot.chat(`I can't sleep: ${err.message}`)
  539.                } else {
  540.                    bot.chat("I'm sleeping")
  541.                }
  542.            })
  543.        } else {
  544.            bot.chat('No nearby bed')
  545.        }
  546.    }
  547.  
  548.    function wakeUp() {
  549.        bot.wake((err) => {
  550.            if (err) {
  551.                bot.chat(`I can't wake up: ${err.message}`)
  552.            } else {
  553.                bot.chat('I woke up')
  554.            }
  555.        }
  556.        
  557.    )
  558. }
  559. // syntax, syntax, syntax ???
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement