Advertisement
Guest User

Untitled

a guest
May 16th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. /*
  2. * Jumping is fun. Riding pigs is even funnier!
  3. *
  4. * Learn how to make your bot interactive with this example.
  5. *
  6. * This bot can move, jump, ride vehicles, attack nearby entities and much more.
  7. */
  8. const mineflayer = require('mineflayer')
  9.  
  10. if (process.argv.length < 4 || process.argv.length > 6) {
  11. console.log('Usage : node jumper.js <host> <port> [<name>] [<password>]')
  12. process.exit(1)
  13. }
  14.  
  15. let bots = []
  16. let i = 0
  17.  
  18. function next() {
  19. if (i < 20) {
  20. i++
  21. setTimeout(() => {
  22. createBot(`mineflayer-bot${i}`)
  23. next()
  24. }, 4000)
  25. } else {
  26. //registerBots();
  27. }
  28. }
  29.  
  30. next();
  31.  
  32.  
  33. let tempBot;
  34.  
  35. function createBot() {
  36. tempBot = mineflayer.createBot({
  37. host: process.argv[2],
  38. port: parseInt(process.argv[3]),
  39. username: (process.argv[4] ? process.argv[4] : 'jumper') + i,
  40. password: process.argv[5],
  41. version: "1.12",
  42. verbose: true
  43. });
  44. console.log('created bot #' + i);
  45. registerBot(tempBot);
  46. bots.push(tempBot);
  47. }
  48.  
  49. let target = null
  50.  
  51. //function registerBots() {
  52. // bots.forEach(bot => {
  53. // registerBot(bot);
  54. // })
  55. //}
  56.  
  57. function registerBot(bot) {
  58. console.log('registering bot:', bot.username);
  59. bot.on('chat', (username, message) => {
  60. if (username === bot.username) return
  61. target = bot.players[username].entity
  62. let entity
  63. if(message.startsWith('command')) {
  64. bot.chat(message.replace('command ', '/'));
  65. } else {
  66. switch (message) {
  67. case 'bots online':
  68. bot.chat('Total bots online: ' + bots.length);
  69. break
  70. case 'forward':
  71. bot.setControlState('forward', true)
  72. break
  73. case 'back':
  74. bot.setControlState('back', true)
  75. break
  76. case 'left':
  77. bot.setControlState('left', true)
  78. break
  79. case 'right':
  80. bot.setControlState('right', true)
  81. break
  82. case 'sprint':
  83. bot.setControlState('sprint', true)
  84. break
  85. case 'stop':
  86. bot.clearControlStates()
  87. break
  88. case 'jump':
  89. bot.setControlState('jump', true)
  90. bot.setControlState('jump', false)
  91. break
  92. case 'jump a lot':
  93. bot.setControlState('jump', true)
  94. break
  95. case 'stop jumping':
  96. bot.setControlState('jump', false)
  97. break
  98. case 'attack':
  99. entity = nearestEntity(bot)
  100. if (entity) {
  101. bot.attack(entity, true)
  102. } else {
  103. bot.chat('no nearby entities')
  104. }
  105. break
  106. case 'mount':
  107. entity = nearestEntity(bot, 'object')
  108. if (entity) {
  109. bot.mount(entity)
  110. } else {
  111. bot.chat('no nearby objects')
  112. }
  113. break
  114. case 'dismount':
  115. bot.dismount()
  116. break
  117. case 'move vehicle forward':
  118. bot.moveVehicle(0.0, 1.0)
  119. break
  120. case 'move vehicle backward':
  121. bot.moveVehicle(0.0, -1.0)
  122. break
  123. case 'move vehicle left':
  124. bot.moveVehicle(1.0, 0.0)
  125. break
  126. case 'move vehicle right':
  127. bot.moveVehicle(-1.0, 0.0)
  128. break
  129. case 'tp':
  130. bot.entity.position.y += 10
  131. break
  132. case 'pos':
  133. bot.chat(bot.entity.position.toString())
  134. break
  135. case 'yp':
  136. bot.chat(`Yaw ${bot.entity.yaw}, pitch: ${bot.entity.pitch}`)
  137. break
  138. }
  139. }
  140. });
  141.  
  142. bot.once('spawn', () => {
  143. // keep your eyes on the target, so creepy!
  144. setInterval(watchTarget, 50);
  145.  
  146. function watchTarget() {
  147. if (!target) return;
  148. bot.lookAt(target.position.offset(0, target.height, 0))
  149. }
  150. });
  151.  
  152. bot.on('mount', () => {
  153. bot.chat(`mounted ${bot.vehicle.objectType}`)
  154. });
  155.  
  156. bot.on('dismount', (vehicle) => {
  157. bot.chat(`dismounted ${vehicle.objectType}`)
  158. });
  159.  
  160. }
  161.  
  162. function nearestEntity(bot, type) {
  163. let id
  164. let entity
  165. let dist
  166. let best = null
  167. let bestDistance = null
  168. for (id in bot.entities) {
  169. entity = bot.entities[id]
  170. if (type && entity.type !== type) continue
  171. if (entity === bot.entity) continue
  172. dist = bot.entity.position.distanceTo(entity.position)
  173. if (!best || dist < bestDistance) {
  174. best = entity
  175. bestDistance = dist
  176. }
  177. }
  178. return best
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement