Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. module.exports = inject
  2.  
  3. function inject (bot) {
  4. let swingInterval = null
  5. let waitTimeout = null
  6.  
  7. bot.targetDigBlock = null
  8. bot.lastDigTime = null
  9.  
  10. function dig (block, cb) {
  11. if (bot.targetDigBlock) bot.stopDigging()
  12. cb = cb || noop
  13. bot.lookAt(block.position.offset(0.5, 0.5, 0.5), false, () => {
  14. bot._client.write('block_dig', {
  15. status: 0, // start digging
  16. location: block.position,
  17. face: 1 // hard coded to always dig from the top
  18. })
  19. const waitTime = bot.digTime(block)
  20. waitTimeout = setTimeout(finishDigging, waitTime)
  21. bot.targetDigBlock = block
  22. bot._client.write('arm_animation', { hand: 0 })
  23. swingInterval = setInterval(() => {
  24. bot._client.write('arm_animation', { hand: 0 })
  25. }, 350)
  26. const eventName = `blockUpdate:${block.position}`
  27. bot.on(eventName, onBlockUpdate)
  28.  
  29. bot.stopDigging = () => {
  30. if (bot.targetDigBlock === null) return
  31. bot.removeListener(eventName, onBlockUpdate)
  32. clearInterval(swingInterval)
  33. clearTimeout(waitTimeout)
  34. swingInterval = null
  35. waitTimeout = null
  36. bot._client.write('block_dig', {
  37. status: 1, // cancel digging
  38. location: bot.targetDigBlock.position,
  39. face: 1 // hard coded to always dig from the top
  40. })
  41. const block = bot.targetDigBlock
  42. bot.targetDigBlock = null
  43. bot.lastDigTime = new Date()
  44. bot.emit('diggingAborted', block)
  45. bot.stopDigging = noop
  46. cb(new Error('Digging aborted'))
  47. }
  48.  
  49. function onBlockUpdate (oldBlock, newBlock) {
  50. // vanilla server never actually interrupt digging, but some server send block update when you start digging
  51. // so ignore block update if not air
  52. if (newBlock.type !== 0) return
  53. bot.removeListener(eventName, onBlockUpdate)
  54. clearInterval(swingInterval)
  55. clearTimeout(waitTimeout)
  56. swingInterval = null
  57. waitTimeout = null
  58. bot.targetDigBlock = null
  59. bot.lastDigTime = new Date()
  60. bot.emit('diggingCompleted', newBlock)
  61. cb()
  62. }
  63.  
  64. function finishDigging () {
  65. clearInterval(swingInterval)
  66. clearTimeout(waitTimeout)
  67. swingInterval = null
  68. waitTimeout = null
  69. bot._client.write('block_dig', {
  70. status: 2, // finish digging
  71. location: bot.targetDigBlock.position,
  72. face: 1 // hard coded to always dig from the top
  73. })
  74. bot.targetDigBlock = null
  75. bot.lastDigTime = new Date()
  76. bot._updateBlock(block.position, 0, 0)
  77. }
  78. })
  79. }
  80.  
  81. function canDigBlock (block) {
  82. return block && block.diggable && block.position.offset(0.5, 0.5, 0.5).distanceTo(bot.entity.position) < 6
  83. }
  84.  
  85. function digTime (block) {
  86. return 1500
  87. //return //block.digTime(bot.heldItem ? bot.heldItem.type : null, bot.game.gameMode === 'creative',
  88. //bot.blockAt(bot.entity.position).type === 9, !bot.entity.onGround) // only stationary water counts
  89. }
  90.  
  91. bot.dig = dig
  92. bot.stopDigging = noop
  93. bot.canDigBlock = canDigBlock
  94. bot.digTime = digTime
  95. }
  96.  
  97. function noop (err) {
  98. if (err) throw err
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement