<commit_before>var Vec3 = require('vec3').Vec3;
var _ = require('lodash');
var Entity = require('prismarine-entity');
var conv = require('../conversions');
var NAMED_ENTITY_HEIGHT = 1.62;
var CROUCH_HEIGHT = NAMED_ENTITY_HEIGHT - 0.08;
var entitiesData = require('../minecraft-data').entities;
var version=require("../version");
var Item = require("prismarine-item")(version);

module.exports = inject;

var animationEvents = {
  0: 'entitySwingArm',
  1: 'entityHurt',
  2: 'entityWake',
  3: 'entityEat',
  4: 'entityCriticalEffect',
  5: 'entityMagicCriticalEffect'
};

var entityStatusEvents = {
  2: 'entityHurt',
  3: 'entityDead',
  6: 'entityTaming',
  7: 'entityTamed',
  8: 'entityShakingOffWater',
  10: 'entityEatingGrass',
};

function inject(bot) {

  bot.findPlayer = bot.findPlayers = function(filter) {
    var filterFn = function(entity) {
      if(entity.type !== 'player') return false;
      if(filter == null) return true;
      if(typeof filter === 'object' && filter instanceof RegExp) {
        return entity.username.search(filter) !== -1;
      } else if(typeof filter === 'function') {
        return filter(entity);
      } else if(typeof filter === 'string') {
        return entity.username.toLowerCase() === filter.toLowerCase();
      }
      return false;
    };
    var resultSet = _.transform(bot.entities, function(players, entity) {
      if(filterFn(entity)) players.push(entity);
    }, []);

    if(typeof filter === 'string') {
      switch(resultSet.length) {
        case 0:
          return null;
        case 1:
          return resultSet[0];
        default:
          return resultSet;
      }
    }
    return resultSet;
  };

  bot.players = {};
  bot.uuidToUsername = {};
  bot.entities = {};

  bot._client.once('login', function(packet) {
    // login
    bot.entity = fetchEntity(packet.entityId);
    bot.entity.username = bot.username;
    bot.entity.type = 'player';
  });

  bot._client.on('entity_equipment', function(packet) {
    // entity equipment
    var entity = fetchEntity(packet.entityId);
    entity.setEquipment(packet.slot, packet.item ? Item.fromNotch(packet.item) : null);
    bot.emit('entityEquip', entity);
  });

  bot._client.on('bed', function(packet) {
    // use bed
    var entity = fetchEntity(packet.entityId);
    entity.position.set(packet.location.x, packet.location.y, packet.location.z);
    bot.emit('entitySleep', entity);
  });

  bot._client.on('animation', function(packet) {
    // animation
    var entity = fetchEntity(packet.entityId);
    var eventName = animationEvents[packet.animation];
    if(eventName) bot.emit(eventName, entity);
  });

  bot._client.on('named_entity_spawn', function(packet) {
    // in case player_info packet was not sent before named_entity_spawn : ignore named_entity_spawn (see #213)
    if(packet.playerUUID in bot.uuidToUsername) {
      // spawn named entity
      var entity = fetchEntity(packet.entityId);
      entity.type = 'player';
      entity.username = bot.uuidToUsername[packet.playerUUID];
      entity.uuid = packet.playerUUID;
      entity.dataBlobs = packet.data;
      entity.position.set(packet.x / 32, packet.y / 32, packet.z / 32);
      entity.yaw = conv.fromNotchianYawByte(packet.yaw);
      entity.pitch = conv.fromNotchianPitchByte(packet.pitch);
      entity.height = NAMED_ENTITY_HEIGHT;
      entity.metadata = parseMetadata(packet.metadata);
      if(!bot.players[entity.username].entity) {
        bot.players[entity.username].entity = entity;
      }
      bot.emit('entitySpawn', entity);
    }
  });
  bot.on("entityCrouch", function(entity) {
    entity.height = CROUCH_HEIGHT;
  });
  bot.on("entityUncrouch", function(entity) {
    entity.height = NAMED_ENTITY_HEIGHT;
  });

  bot._client.on('collect', function(packet) {
    // collect item
    var collector = fetchEntity(packet.collectorEntityId);
    var collected = fetchEntity(packet.collectedEntityId);
    bot.emit('playerCollect', collector, collected);
  });

  bot._client.on('spawn_entity', function(packet) {
    // spawn object/vehicle
    var entity = fetchEntity(packet.entityId);
    var entityData = entitiesData[packet.type];
    if(entityData) {
      entity.type = 'object';
      entity.objectType = entityData.displayName;
      entity.displayName = entityData.displayName;
      entity.entityType = entityData.id;
      entity.name = entityData.name;
      entity.kind = entityData.type;
    } else {
      // unknown entity
      entity.type = 'other';
      entity.entityType = packet.type;
    }
    entity.position.set(packet.x / 32, packet.y / 32, packet.z / 32);
    entity.yaw = conv.fromNotchianYawByte(packet.yaw);
    entity.pitch = conv.fromNotchianPitchByte(packet.pitch);
    entity.objectData = packet.objectData;
    bot.emit('entitySpawn', entity);
  });

  bot._client.on('spawn_entity_experience_orb', function(packet) {
    var entity = fetchEntity(packet.entityId);
    entity.type = 'orb';
    entity.position.set(packet.x / 32, packet.y / 32, packet.z / 32);
    entity.count = packet.count;
    bot.emit('entitySpawn', entity);
  });

  bot._client.on('spawn_entity_living', function(packet) {
    // spawn mob
    var entity = fetchEntity(packet.entityId);
    entity.type = 'mob';
    var entityData = entitiesData[packet.type];
    entity.mobType = entityData.displayName;
    entity.displayName = entityData.displayName;
    entity.entityType = entityData.id;
    entity.name = entityData.name;
    entity.kind = entityData.type;
    entity.position.set(packet.x / 32, packet.y / 32, packet.z / 32);
    entity.yaw = conv.fromNotchianYawByte(packet.yaw);
    entity.pitch = conv.fromNotchianPitchByte(packet.pitch);
    entity.headPitch = conv.fromNotchianPitchByte(packet.headPitch);
    var notchVel = new Vec3(packet.velocityX, packet.velocityY, packet.velocityZ);
    entity.velocity.update(conv.fromNotchVelocity(notchVel));
    entity.metadata = parseMetadata(packet.metadata);
    bot.emit('entitySpawn', entity);
  });<commit_msg>adapt to new minecraft data entities format<commit_after>var Vec3 = require('vec3').Vec3;
var _ = require('lodash');
var Entity = require('prismarine-entity');
var conv = require('../conversions');
var NAMED_ENTITY_HEIGHT = 1.62;
var CROUCH_HEIGHT = NAMED_ENTITY_HEIGHT - 0.08;
var objects = require('../minecraft-data').objects;
var mobs = require('../minecraft-data').mobs;
var version=require("../version");
var Item = require("prismarine-item")(version);

module.exports = inject;

var animationEvents = {
  0: 'entitySwingArm',
  1: 'entityHurt',
  2: 'entityWake',
  3: 'entityEat',
  4: 'entityCriticalEffect',
  5: 'entityMagicCriticalEffect'
};

var entityStatusEvents = {
  2: 'entityHurt',
  3: 'entityDead',
  6: 'entityTaming',
  7: 'entityTamed',
  8: 'entityShakingOffWater',
  10: 'entityEatingGrass',
};

function inject(bot) {

  bot.findPlayer = bot.findPlayers = function(filter) {
    var filterFn = function(entity) {
      if(entity.type !== 'player') return false;
      if(filter == null) return true;
      if(typeof filter === 'object' && filter instanceof RegExp) {
        return entity.username.search(filter) !== -1;
      } else if(typeof filter === 'function') {
        return filter(entity);
      } else if(typeof filter === 'string') {
        return entity.username.toLowerCase() === filter.toLowerCase();
      }
      return false;
    };
    var resultSet = _.transform(bot.entities, function(players, entity) {
      if(filterFn(entity)) players.push(entity);
    }, []);

    if(typeof filter === 'string') {
      switch(resultSet.length) {
        case 0:
          return null;
        case 1:
          return resultSet[0];
        default:
          return resultSet;
      }
    }
    return resultSet;
  };

  bot.players = {};
  bot.uuidToUsername = {};
  bot.entities = {};

  bot._client.once('login', function(packet) {
    // login
    bot.entity = fetchEntity(packet.entityId);
    bot.entity.username = bot.username;
    bot.entity.type = 'player';
  });

  bot._client.on('entity_equipment', function(packet) {
    // entity equipment
    var entity = fetchEntity(packet.entityId);
    entity.setEquipment(packet.slot, packet.item ? Item.fromNotch(packet.item) : null);
    bot.emit('entityEquip', entity);
  });

  bot._client.on('bed', function(packet) {
    // use bed
    var entity = fetchEntity(packet.entityId);
    entity.position.set(packet.location.x, packet.location.y, packet.location.z);
    bot.emit('entitySleep', entity);
  });

  bot._client.on('animation', function(packet) {
    // animation
    var entity = fetchEntity(packet.entityId);
    var eventName = animationEvents[packet.animation];
    if(eventName) bot.emit(eventName, entity);
  });

  bot._client.on('named_entity_spawn', function(packet) {
    // in case player_info packet was not sent before named_entity_spawn : ignore named_entity_spawn (see #213)
    if(packet.playerUUID in bot.uuidToUsername) {
      // spawn named entity
      var entity = fetchEntity(packet.entityId);
      entity.type = 'player';
      entity.username = bot.uuidToUsername[packet.playerUUID];
      entity.uuid = packet.playerUUID;
      entity.dataBlobs = packet.data;
      entity.position.set(packet.x / 32, packet.y / 32, packet.z / 32);
      entity.yaw = conv.fromNotchianYawByte(packet.yaw);
      entity.pitch = conv.fromNotchianPitchByte(packet.pitch);
      entity.height = NAMED_ENTITY_HEIGHT;
      entity.metadata = parseMetadata(packet.metadata);
      if(!bot.players[entity.username].entity) {
        bot.players[entity.username].entity = entity;
      }
      bot.emit('entitySpawn', entity);
    }
  });
  bot.on("entityCrouch", function(entity) {
    entity.height = CROUCH_HEIGHT;
  });
  bot.on("entityUncrouch", function(entity) {
    entity.height = NAMED_ENTITY_HEIGHT;
  });

  bot._client.on('collect', function(packet) {
    // collect item
    var collector = fetchEntity(packet.collectorEntityId);
    var collected = fetchEntity(packet.collectedEntityId);
    bot.emit('playerCollect', collector, collected);
  });

  bot._client.on('spawn_entity', function(packet) {
    // spawn object/vehicle
    var entity = fetchEntity(packet.entityId);
    var entityData = objects[packet.type];
    if(entityData) {
      entity.type = 'object';
      entity.objectType = entityData.displayName;
      entity.displayName = entityData.displayName;
      entity.entityType = entityData.id;
      entity.name = entityData.name;
      entity.kind = entityData.category;
    } else {
      // unknown entity
      entity.type = 'other';
      entity.entityType = packet.type;
    }
    entity.position.set(packet.x / 32, packet.y / 32, packet.z / 32);
    entity.yaw = conv.fromNotchianYawByte(packet.yaw);
    entity.pitch = conv.fromNotchianPitchByte(packet.pitch);
    entity.objectData = packet.objectData;
    bot.emit('entitySpawn', entity);
  });

  bot._client.on('spawn_entity_experience_orb', function(packet) {
    var entity = fetchEntity(packet.entityId);
    entity.type = 'orb';
    entity.position.set(packet.x / 32, packet.y / 32, packet.z / 32);
    entity.count = packet.count;
    bot.emit('entitySpawn', entity);
  });

  bot._client.on('spawn_entity_living', function(packet) {
    // spawn mob
    var entity = fetchEntity(packet.entityId);
    entity.type = 'mob';
    var entityData = mobs[packet.type];
    entity.mobType = entityData.displayName;
    entity.displayName = entityData.displayName;
    entity.entityType = entityData.id;
    entity.name = entityData.name;
    entity.kind = entityData.category;
    entity.position.set(packet.x / 32, packet.y / 32, packet.z / 32);
    entity.yaw = conv.fromNotchianYawByte(packet.yaw);
    entity.pitch = conv.fromNotchianPitchByte(packet.pitch);
    entity.headPitch = conv.fromNotchianPitchByte(packet.headPitch);
    var notchVel = new Vec3(packet.velocityX, packet.velocityY, packet.velocityZ);
    entity.velocity.update(conv.fromNotchVelocity(notchVel));
    entity.metadata = parseMetadata(packet.metadata);
    bot.emit('entitySpawn', entity);
  });
{'fraction_non_alphanumeric': 0.1411774447296514, 'fraction_numerical': 0.008942618199884077, 'mean_word_length': 3.3824383164005805, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '82042', 'n_tokens_mistral': 3940, 'n_tokens_neox': 3814, 'n_words': 703}