Advertisement
Hcat

Untitled

Dec 29th, 2021
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mineflayer = require('mineflayer')
  2. const pvp = require('mineflayer-pvp').plugin
  3. const { pathfinder, Movements, goals} = require('mineflayer-pathfinder')
  4. const armorManager = require('mineflayer-armor-manager')
  5. const AutoAuth = require('mineflayer-auto-auth')
  6. const autoeat = require("mineflayer-auto-eat")
  7.  
  8. function createBot(){
  9.   const bot = mineflayer.createBot({
  10.     host: process.argv[2] ||"wolfxmc.org",
  11.     port: "25565",
  12.     username: 'Hcat_lbS68Y2k',
  13.     logErrors: true,
  14.     hideErrors: false,
  15.     version: "1.16.5",
  16.     plugins: [AutoAuth],
  17.     AutoAuth: {
  18.        logging: true,
  19.        password: 'AdminHcat',
  20.        ignoreRepeat: true
  21.      }
  22. })
  23.  
  24. bot.on('error', (err) => console.log(err))
  25. bot.on('end', createBot)
  26.  
  27. // const bot = mineflayer.createBot({
  28. //     host: process.argv[2],
  29. //     port: process.argv[3],
  30. //     username: process.argv[4] || 'Hcat_SuperBot',
  31. //     password: process.argv[5],
  32. //     logErrors: false,
  33. //     plugins: [AutoAuth],
  34. //     AutoAuth: {
  35. //       logging: true,
  36. //       password: 'AdminHcat',
  37. //       ignoreRepeat: true
  38. //     }
  39. // })
  40.  
  41. bot.loadPlugin(pvp)
  42. bot.loadPlugin(armorManager)
  43. bot.loadPlugin(pathfinder)
  44. bot.loadPlugin(autoeat)
  45.  
  46. bot.on('serverAuth', function() {
  47.     // 授权登录成功
  48.     bot.chat("授权登录成功")
  49.   });
  50.  
  51. bot.on('playerCollect', (collector, itemDrop) => {
  52.   if (collector !== bot.entity) return
  53.  
  54.   setTimeout(() => {
  55.     const sword = bot.inventory.items().find(item => item.name.includes('sword'))
  56.     if (sword) bot.equip(sword, 'hand')
  57.   }, 150)
  58. })
  59.  
  60. bot.on('playerCollect', (collector, itemDrop) => {
  61.   if (collector !== bot.entity) return
  62.   bot.armorManager.equipAll()
  63.   setTimeout(() => {
  64.     const shield = bot.inventory.items().find(item => item.name.includes('shield'))
  65.     if (shield) bot.equip(shield, 'off-hand')
  66.   }, 250)
  67. })
  68.  
  69. let guardPos = null
  70.  
  71. function guardArea (pos) {
  72.   guardPos = pos.clone()
  73.  
  74.   if (!bot.pvp.target) {
  75.     moveToGuardPos()
  76.   }
  77. }
  78.  
  79. function stopGuarding () {
  80.   guardPos = null
  81.   bot.pvp.stop()
  82.   bot.pathfinder.setGoal(null)
  83. }
  84.  
  85. function moveToGuardPos () {
  86.   const mcData = require('minecraft-data')(bot.version)
  87.   bot.pathfinder.setMovements(new Movements(bot, mcData))
  88.   bot.pathfinder.setGoal(new goals.GoalBlock(guardPos.x, guardPos.y, guardPos.z))
  89. }
  90.  
  91. function moveToEntityPos (entity) {
  92.   const mcData = require('minecraft-data')(bot.version)
  93.   bot.pathfinder.setMovements(new Movements(bot, mcData))
  94.   bot.pathfinder.setGoal(new goals.GoalBlock(entity.position.x, entity.position.y, entity.position.z))
  95. }
  96.  
  97. bot.on('stoppedAttacking', () => {
  98.   if (guardPos) {
  99.     moveToGuardPos()
  100.   }
  101. })
  102.  
  103. bot.on('physicTick', () => {
  104.   if (bot.pvp.target) return
  105.   if (bot.pathfinder.isMoving()) return
  106.   const entity = bot.nearestEntity()
  107.   if(!entity)return
  108.   if (entity.displayName !=="undefined" && entity.displayName !=="Arrow") bot.lookAt(entity.position.offset(0, entity.height, 0))
  109.   // console.log("跟踪到实体:" + entity.displayName)
  110.   if (entity.displayName === "Item") moveToEntityPos(entity)
  111. })
  112.  
  113. bot.on('physicTick', () => {
  114.   if (!guardPos) return
  115.  
  116.   const filter = e => e.type === 'mob' && e.position.distanceTo(bot.entity.position) < 16 &&
  117.                       e.mobType !== 'Armor Stand' // Mojang classifies armor stands as mobs for some reason?
  118.  
  119.   const entity = bot.nearestEntity(filter)
  120.   if (entity) {
  121.     bot.pvp.attack(entity)
  122.  
  123.   }
  124. })
  125.  
  126. bot.on('chat', (username, message) => {
  127.   if (message === 'guard' || message === '守护') {
  128.     const player = bot.players[username]
  129.  
  130.     if (!player) {
  131.       bot.chat("我看不到你")
  132.       return
  133.     }
  134.     if (!player.entity){
  135.       bot.chat("无法找到实体")
  136.       return
  137.     }
  138.     bot.chat('我会保护好这个地方哒')
  139.     guardArea(player.entity.position)
  140.   }
  141.  
  142.   if (message === 'fight me') {
  143.     const player = bot.players[username]
  144.  
  145.     if (!player) {
  146.       bot.chat("我看不到你")
  147.       return
  148.     }
  149.  
  150.     bot.chat('准备战斗!')
  151.     bot.pvp.attack(player.entity)
  152.   }
  153.  
  154.   if (message === 'stop') {
  155.     bot.chat('我将不再守卫这个地方')
  156.     stopGuarding()
  157.   }
  158. })
  159.  
  160. //控制台打印消息
  161. bot.on('message', (message) => {
  162.   console.log(message.toAnsi())
  163. })
  164. //控制台打印生命状态
  165. bot.on('health', () => {
  166.   // console.log(bot.food)
  167. })
  168.  
  169.  
  170. bot.once('spawn', () => {
  171.   bot.autoEat.options = {
  172.     priority: 'foodPoints',
  173.     startAt: 18,
  174.     setTimeout: 10,
  175.     bannedFood: ["golden_apple", "enchanted_golden_apple", "rotten_flesh"]
  176.   }
  177.   // bot.physics.sprintSpeed = 15;
  178. })
  179.  
  180.  
  181. bot.on('autoeat_started', () => {
  182.   console.log('自动进食已开始!')
  183. })
  184.  
  185. bot.on('autoeat_stopped', () => {
  186.   console.log('自动进食已停止!')
  187. })
  188.  
  189. bot.on('entityHurt', (entity) => {
  190.   if (entity===bot.entity){
  191.     guardArea(bot.entity.position)
  192.  
  193.       const sword = bot.inventory.items().find(item => item.name.includes('sword'))
  194.       if (sword) bot.equip(sword, 'hand')
  195.     console.log("焯!我被打了")
  196.   }
  197.  
  198. })
  199.  
  200. bot.on('entityEffect', (entity,effect) => {
  201.     console.log("获得buff:"+ entity.type+"BUFF:"+effect.id)
  202.   })
  203.  
  204.   var resend = false
  205.   var tpaccept = false
  206.  
  207.   bot.on('chat', (username, message) => {
  208.  
  209.     if (message === "BotTpME") {
  210.       if (username !== "Hcat") {
  211.         bot.chat("机器人暂仅支持Hcat使用哦")
  212.         return
  213.       }
  214.       bot.chat("/tpa " + username)
  215.       bot.chat("已发送传送请求")
  216.     }
  217.  
  218.     if (message === "切换复读机") {
  219.       if (username !== "Hcat") {
  220.         bot.chat("机器人暂仅支持Hcat使用哦")
  221.         return
  222.       }
  223.       resend = !resend
  224.       bot.chat("复读机开关:" + resend)
  225.     }
  226.  
  227.     if (message === "切换自动请求") {
  228.       if (username !== "Hcat") {
  229.         bot.chat("机器人暂仅支持Hcat使用哦")
  230.         return
  231.       }
  232.       tpaccept = !tpaccept
  233.       bot.chat("自动接受传送:" + tpaccept)
  234.     }
  235.  
  236.     // if (tpaccept) {
  237.     //   bot.chat("/tpaccept")
  238.     // } else {
  239.     //   bot.chat("/tpdeny")
  240.     // }
  241.  
  242.     if (message === '手动接受TP') {
  243.       bot.chat('/tpaccept')
  244.     }
  245.     if (message === '传送小黑塔') {
  246.       bot.chat('/res tp black_tower')
  247.     }
  248.  
  249.  
  250.     if (username === bot.username) return
  251.     if (resend === true) bot.chat(message)
  252.   })
  253.  
  254.  
  255. bot.on('health', () => {
  256.   if (bot.food === 20 || bot.pvp.target) {
  257.     bot.autoEat.disable()//如果机器人处于20个食物点,则自动进食
  258.   }else bot.autoEat.enable() // 否则再次启用插件
  259. })
  260.  
  261. bot.on('chat', async (username, message) => {
  262.   if (username === bot.username) return
  263.   switch (message) {
  264.     case 'loaded':
  265.       await bot.waitForChunksToLoad()
  266.       bot.chat('Ready!')
  267.       break
  268.     case 'list':
  269.       sayItems()
  270.       break
  271.     case 'dig':
  272.       dig()
  273.       break
  274.     case 'build':
  275.       build()
  276.       break
  277.     case 'equip dirt':
  278.       equipDirt()
  279.       break
  280.   }
  281. })
  282.  
  283. function sayItems (items = bot.inventory.items()) {
  284.   const output = items.map(itemToString).join(', ')
  285.   if (output) {
  286.     bot.chat(output)
  287.   } else {
  288.     bot.chat('背包内无物品')
  289.   }
  290. }
  291.  
  292. function dig () {
  293.   let target
  294.   if (bot.targetDigBlock) {
  295.     bot.chat(`already digging ${bot.targetDigBlock.name}`)
  296.   } else {
  297.     target = bot.blockAt(bot.entity.position.offset(0, -1, 0))
  298.     if (target && bot.canDigBlock(target)) {
  299.       bot.chat(`正在开始采集 ${target.name}`)
  300.       bot.dig(target, onDiggingCompleted)
  301.     } else {
  302.       bot.chat('cannot dig')
  303.     }
  304.   }
  305.  
  306.   function onDiggingCompleted (err) {
  307.     if (err) {
  308.       console.log(err.stack)
  309.       return
  310.     }
  311.     bot.chat(`finished digging ${target.name}`)
  312.   }
  313. }
  314.  
  315. function build () {
  316.   const referenceBlock = bot.blockAt(bot.entity.position.offset(0, -1, 0))
  317.   const jumpY = Math.floor(bot.entity.position.y) + 1.0
  318.   bot.setControlState('jump', true)
  319.   bot.on('move', placeIfHighEnough)
  320.  
  321.   let tryCount = 0
  322.  
  323.   function placeIfHighEnough () {
  324.     if (bot.entity.position.y > jumpY) {
  325.       bot.placeBlock(referenceBlock, vec3(0, 1, 0), (err) => {
  326.         if (err) {
  327.           tryCount++
  328.           if (tryCount > 10) {
  329.             bot.chat(err.message)
  330.             bot.setControlState('jump', false)
  331.             bot.removeListener('move', placeIfHighEnough)
  332.             return
  333.           }
  334.           return
  335.         }
  336.         bot.setControlState('jump', false)
  337.         bot.removeListener('move', placeIfHighEnough)
  338.         bot.chat('Placing a block was successful')
  339.       })
  340.     }
  341.   }
  342. }
  343.  
  344. function equipDirt () {
  345.   const mcData = require('minecraft-data')(bot.version)
  346.   let itemsByName
  347.   if (bot.supportFeature('itemsAreNotBlocks')) {
  348.     itemsByName = 'itemsByName'
  349.   } else if (bot.supportFeature('itemsAreAlsoBlocks')) {
  350.     itemsByName = 'blocksByName'
  351.   }
  352.   bot.equip(mcData[itemsByName].dirt.id, 'hand', (err) => {
  353.     if (err) {
  354.       bot.chat(`unable to equip dirt: ${err.message}`)
  355.     } else {
  356.       bot.chat('equipped dirt')
  357.     }
  358.   })
  359. }
  360.  
  361. function itemToString (item) {
  362.   if (item) {
  363.     return `${item.name} x ${item.count}`
  364.   } else {
  365.     return '(nothing)'
  366.   }
  367. }
  368.  
  369. }
  370. createBot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement