<commit_before>const assert = require('assert')
const Vec3 = require('vec3').Vec3

module.exports = inject

// ms to wait before clicking on a tool so the server can send the new
// damage information
const DIG_CLICK_TIMEOUT = 500

function inject (bot, { version }) {
  const Item = require('prismarine-item')(version)
  const windows = require('prismarine-windows')(version).windows

  let nextActionNumber = 0
  const windowClickQueue = []
  let lastEating
  let lastFishing
  let lastFloat
  let windowItems

  // 0-8, null = uninitialized
  // which quick bar slot is selected
  bot.quickBarSlot = null
  bot.inventory = new windows.InventoryWindow(0, 'Inventory', 44)
  bot.currentWindow = null
  bot.heldItem = null

  bot._client.on('entity_status', (packet) => {
    if (packet.entityId === bot.entity.id && packet.entityStatus === 9 && lastEating) {
      lastEating()
      lastEating = undefined
    }
  })

  bot._client.on('spawn_entity', (packet) => {
    if (packet.type === 90 && lastFishing && !lastFloat) {
      lastFloat = bot.entities[packet.entityId]
    }
  })

  bot._client.on('world_particles', (packet) => {
    if (!lastFloat || !lastFishing) return

    const pos = lastFloat.position
    if (packet.particleId === 4 && packet.x.toFixed(3) === pos.x.toFixed(3) && packet.z.toFixed(3) === pos.z.toFixed(3)) {
      activateItem()
      lastFishing()
      lastFishing = undefined
    }
  })

  function consume (cb) {
    activateItem()

    if (lastEating) {
      lastEating(new Error('Consuming cancelled due to calling bot.consume() again'))
    }

    lastEating = cb
  }

  function fish (cb) {
    activateItem()

    if (lastFishing) {
      lastFishing(new Error('Fishing cancelled due to calling bot.fish() again'))
    }

    lastFishing = cb
  }

  function activateItem () {
    if (bot.majorVersion === '1.8') {
      bot._client.write('block_place', {
        location: new Vec3(-1, 255, -1),
        direction: -1,
        heldItem: Item.toNotch(bot.heldItem),
        cursorX: -1,
        cursorY: -1,
        cursorZ: -1
      })
    }
<commit_msg>Fix bot.fish() if hook/float gets despawned<commit_after>const assert = require('assert')
const Vec3 = require('vec3').Vec3

module.exports = inject

// ms to wait before clicking on a tool so the server can send the new
// damage information
const DIG_CLICK_TIMEOUT = 500

function inject (bot, { version }) {
  const Item = require('prismarine-item')(version)
  const windows = require('prismarine-windows')(version).windows

  let nextActionNumber = 0
  const windowClickQueue = []
  let lastEating
  let lastFishing
  let lastFloat
  let windowItems

  // 0-8, null = uninitialized
  // which quick bar slot is selected
  bot.quickBarSlot = null
  bot.inventory = new windows.InventoryWindow(0, 'Inventory', 44)
  bot.currentWindow = null
  bot.heldItem = null

  bot._client.on('entity_status', (packet) => {
    if (packet.entityId === bot.entity.id && packet.entityStatus === 9 && lastEating) {
      lastEating()
      lastEating = undefined
    }
  })

  bot._client.on('spawn_entity', (packet) => {
    if (packet.type === 90 && lastFishing && !lastFloat) {
      lastFloat = bot.entities[packet.entityId]
    }
  })

  bot._client.on('world_particles', (packet) => {
    if (!lastFloat || !lastFishing) return

    const pos = lastFloat.position
    if (packet.particleId === 4 && packet.x.toFixed(3) === pos.x.toFixed(3) && packet.z.toFixed(3) === pos.z.toFixed(3)) {
      activateItem()
      lastFishing()
      lastFishing = undefined
    }
  })

  bot._client.on('entity_destroy', (packet) => {
    if (!lastFloat) return
    if (packet.entityIds.some(id => id === lastFloat.id)) {
      lastFishing(new Error('Fishing cancelled'))
      lastFishing = undefined
      lastFloat = undefined
    }
  })

  function consume (cb) {
    activateItem()

    if (lastEating) {
      lastEating(new Error('Consuming cancelled due to calling bot.consume() again'))
    }

    lastEating = cb
  }

  function fish (cb) {
    activateItem()

    if (lastFishing) {
      lastFishing(new Error('Fishing cancelled due to calling bot.fish() again'))
      lastFishing = undefined
      return
    }

    lastFishing = cb
  }

  function activateItem () {
    if (bot.majorVersion === '1.8') {
      bot._client.write('block_place', {
        location: new Vec3(-1, 255, -1),
        direction: -1,
        heldItem: Item.toNotch(bot.heldItem),
        cursorX: -1,
        cursorY: -1,
        cursorZ: -1
      })
    }

{'fraction_non_alphanumeric': 0.134764328391237, 'fraction_numerical': 0.014162425315335252, 'mean_word_length': 2.893195521102498, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5797576', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1441, 'n_words': 352}