about summary refs log tree commit diff
path: root/index.js
blob: 7b654da0840801c709a97c4a7136d1ae6c78dc44 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
exports.plugin = (bot) => {
  var originalFunction = bot._client.write;

  // Create a new function that wraps the original function
  var newFunction = function (name, params) {
    if (name === "entity_action") {
      if (params.actionId === 3 || params.actionId === 4) {
        // cancel sprint start and sprint stop packets
        return;
      }
    }
    if (params.onGround !== undefined) {
      if (bot.targetDigBlock === null && yDistanceFallen <= 0.0) {
        params.onGround = false
      }
      else
        params.onGround = true
    }
    return originalFunction.apply(this, [...arguments])
  }

  // Replace the original function with the new function
  bot._client.write = newFunction;

  let startingFallingPosition = null
  let yDistanceFallen = 0.0
  bot.on('move', () => {
    //we are falling (or going up)
    if (bot.entity.onGround === false) {
      //console.log("not on ground!")
      if (startingFallingPosition === null) {
        startingFallingPosition = bot.entity.position.clone()
        //console.log("just started falling, so setting starting position: " + startingFallingPosition)
      }

      yDistanceFallen = Math.abs(startingFallingPosition.y - bot.entity.position.y)
      //console.log("Y distance fallen:" + yDistanceFallen + " -- curr pos: " + bot.entity.position)
    }
    else if (bot.entity.onGround === true) {
      startingFallingPosition = null;
      yDistanceFallen = 0.0
    }
  })
}