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.

156 lines
4.9 KiB

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: 54825, // 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.placeBlock('torch',isAir.position)
  70. }
  71. }
  72. bot.once('spawn', () => {
  73. bot.chat('I am connected')
  74. //delay(1000).then(() => bot.chat("...And I am The Bot."));
  75. bot.autoEat.options = {
  76. priority: "foodPoints",
  77. 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"],
  78. eatingTimeout: 2
  79. }
  80. setInterval(() => {
  81. if (bot.health >=6){
  82. const mobFilter = e => e.type === 'mob' && e.mobType === 'Slime' || 'Zombie'
  83. const mob = bot.nearestEntity(mobFilter)
  84. if (!mob) return
  85. const pos = mob.position.offset(0,mob.height,0)
  86. const dist = pos.distanceTo(bot.entity.position)
  87. bot.lookAt(pos, true)
  88. if (dist >= 20) return
  89. if (dist <= 10 && bot.heldItem.type !== 'diamond_sword' && bot.inventory.findInventoryItem('diamond_sword') !== null) {
  90. bot.equip(bot.inventory.findInventoryItem('diamond_sword'))
  91. }
  92. //console.log(dist)
  93. if (dist <= 5){
  94. bot.attack(mob)
  95. }// else{
  96. // console.log('is not Fighting')
  97. //}
  98. }
  99. }, 1000)
  100. })
  101. bot.on("autoeat_started", () => {
  102. console.log("Auto Eat started!")
  103. })
  104. bot.on("autoeat_stopped", () => {
  105. console.log("Auto Eat stopped!")
  106. bot.sw
  107. })
  108. bot.on("health", () => {
  109. if (bot.food === 20) bot.autoEat.disable()
  110. // Disable the plugin if the bot is at 20 food points
  111. else bot.autoEat.enable() // Else enable the plugin again
  112. })
  113. bot.on('chat', (username, message) => {
  114. if (message === 'hi') bot.chat(`Hi ${username}!`);
  115. if (message === (`${bot.username} off`)){
  116. bot.chat(`Bye ${username}!`)
  117. bot.quit()
  118. }
  119. if (message === 'bot spawnpoint') {
  120. bot.chat(`${bot.spawnPoint.x} ${bot.spawnPoint.y} ${bot.spawnPoint.z}`)
  121. }
  122. if (message === 'bot follow me') {
  123. followPlayer(username)
  124. }
  125. if (message === 'bot stop follow') {
  126. bot.chat(`CU later ${username}`)
  127. bot.pathfinder.stop()
  128. }
  129. if (message === 'bot find emerald block') {
  130. locateEmeraldBlock()
  131. }
  132. if (message === 'bot light up') {
  133. lightUp()
  134. }
  135. /*if (message === 'bot stop find') {
  136. bot.chat(`Ok...`)
  137. bot.pathfinder.stop()
  138. }*/
  139. })