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

157 lines
4.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. const mineflayer = require('mineflayer')
  2. const { pathfinder, Movements, goals } = require('mineflayer-pathfinder')
  3. const GoalFollow = goals.GoalFollow
  4. const GoalBlock = goals.GoalBlock
  5. const { Vec3 } = require('vec3')
  6. const autoeat = require("mineflayer-auto-eat")
  7. // to add forge modules https://github.com/PrismarineJS/mineflayer/pull/472
  8. const options = {
  9. host: "127.0.0.1", // Server IP
  10. port: 50128, // Server port
  11. username: 'V5-Bot', // Bot name
  12. }
  13. const bot = mineflayer.createBot(options)
  14. bot.loadPlugin(pathfinder)
  15. bot.loadPlugin(autoeat)
  16. function delay(time) { // delay function
  17. return new Promise(resolve => setTimeout(resolve, time));
  18. }
  19. function followPlayer(username){
  20. const playerToFollow = bot.players[username]
  21. /*if (!playerFH) {
  22. bot.chat(`I can't see ${playerFH} !`)
  23. return
  24. }*/
  25. const mcData = require('minecraft-data')(bot.version)
  26. const movements = new Movements(bot, mcData)
  27. movements.scafoldingBlocks = []
  28. bot.pathfinder.setMovements(movements)
  29. const goal = new GoalFollow(playerToFollow.entity, 2)
  30. bot.pathfinder.setGoal(goal, true) // true # for checking Goal position each frame
  31. }
  32. function locateEmeraldBlock(){
  33. const mcData = require('minecraft-data')(bot.version)
  34. const movements = new Movements(bot, mcData)
  35. movements.scafoldingBlocks = []
  36. bot.pathfinder.setMovements(movements)
  37. const emeraldBlock = bot.findBlock({
  38. matching: mcData.blocksByName.emerald_block.id,
  39. maxDistance: 32
  40. })
  41. if (!emeraldBlock){
  42. bot.chat("I can't see any emerald blocks!")
  43. return
  44. }
  45. const x = emeraldBlock.position.x
  46. const y = emeraldBlock.position.y + 1
  47. const z = emeraldBlock.position.z
  48. const goal = new GoalBlock(x, y, z)
  49. bot.pathfinder.setGoal(goal)
  50. }
  51. function lightUp() {
  52. const mcData = require('minecraft-data')(bot.version)
  53. const movements = new Movements(bot, mcData)
  54. movements.scafoldingBlocks = []
  55. bot.pathfinder.setMovements(movements)
  56. const isAir = bot.findBlock({
  57. matching: mcData.blocksByName.air.id,
  58. maxDistance: 32
  59. })
  60. const x = isAir.position.x
  61. const y = isAir.position.y - 1
  62. const z = isAir.position.z
  63. const potentialLightableBlock = bot.blockAt({
  64. x,
  65. y,
  66. z
  67. })
  68. if (potentialLightableBlock.transparent === false && isAir.light <= 7){
  69. bot.equip(bot.inventory.findInventoryItem('torch'))
  70. bot.placeBlock('torch',isAir.position)
  71. }
  72. }
  73. bot.once('spawn', () => {
  74. bot.chat('I am connected')
  75. //delay(1000).then(() => bot.chat("...And I am The Bot."));
  76. bot.autoEat.options = {
  77. priority: "foodPoints",
  78. bannedFood: ['beetroot', 'cake', 'carrot', 'chorus_fruit', 'golden_carrot', 'honey_bottle', 'poisonous_potato', 'potato', 'pufferfish', 'beef', 'chicken', 'cod', 'mutton', 'porkchop', 'rabbit', 'salmon', 'rotten_flesh', 'spider_eye', 'suspicious_stew', 'sweet_berries', 'tropical_fish', "golden_apple", "enchanted_golden_apple"],
  79. eatingTimeout: 2
  80. }
  81. setInterval(() => {
  82. if (bot.health >=6){
  83. const mobFilter = e => (e.type === 'mob' && (e.mobType === 'Slime' || e.mobType === 'Zombie' || e.mobType === 'Spider'))
  84. const mob = bot.nearestEntity(mobFilter)
  85. if (!mob) return
  86. const pos = mob.position.offset(0,mob.height,0)
  87. const dist = pos.distanceTo(bot.entity.position)
  88. bot.lookAt(pos, true)
  89. if (dist >= 20) return
  90. if (dist <= 10 && bot.inventory.findInventoryItem('diamond_sword')) {
  91. bot.equip(bot.inventory.findInventoryItem('diamond_sword'))
  92. }
  93. //console.log(dist)
  94. if (dist <= 5){
  95. bot.attack(mob)
  96. }// else{
  97. // console.log('is not Fighting')
  98. //}
  99. }
  100. }, 800)
  101. })
  102. bot.on("autoeat_started", () => {
  103. console.log("Auto Eat started!")
  104. })
  105. bot.on("autoeat_stopped", () => {
  106. console.log("Auto Eat stopped!")
  107. bot.sw
  108. })
  109. bot.on("health", () => {
  110. if (bot.food === 20) bot.autoEat.disable()
  111. // Disable the plugin if the bot is at 20 food points
  112. else bot.autoEat.enable() // Else enable the plugin again
  113. })
  114. bot.on('chat', (username, message) => {
  115. if (message === 'hi') bot.chat(`Hi ${username}!`);
  116. if (message === (`${bot.username} off`)){
  117. bot.chat(`Bye ${username}!`)
  118. bot.quit()
  119. }
  120. if (message === 'bot spawnpoint') {
  121. bot.chat(`${bot.spawnPoint.x} ${bot.spawnPoint.y} ${bot.spawnPoint.z}`)
  122. }
  123. if (message === 'bot follow me') {
  124. followPlayer(username)
  125. }
  126. if (message === 'bot stop follow') {
  127. bot.chat(`CU later ${username}`)
  128. bot.pathfinder.stop()
  129. }
  130. if (message === 'bot find emerald block') {
  131. locateEmeraldBlock()
  132. }
  133. if (message === 'bot light up') {
  134. lightUp()
  135. }
  136. /*if (message === 'bot stop find') {
  137. bot.chat(`Ok...`)
  138. bot.pathfinder.stop()
  139. }*/
  140. })