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.

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