Ejemplo n.º 1
0
 @Override
 public Entity getEntityById(int id) {
   synchronized (entities) {
     for (Entity entity : entities) if (id == entity.getId()) return entity;
     return null;
   }
 }
Ejemplo n.º 2
0
  @EventHandler
  public void onPacketProcess(PacketProcessEvent event) {
    Packet packet = event.getPacket();
    if (packet instanceof Packet5PlayerInventory) {
      Packet5PlayerInventory inventoryPacket = (Packet5PlayerInventory) packet;
      Entity entity = getEntityById(inventoryPacket.entityID);
      if (entity == null || !(entity instanceof LivingEntity)) return;
      LivingEntity livingEntity = (LivingEntity) entity;
      livingEntity.setWornItemAt(inventoryPacket.slot, inventoryPacket.item);
    } else if (packet instanceof Packet8UpdateHealth) {
      Packet8UpdateHealth updateHealthPacket = (Packet8UpdateHealth) packet;
      MainPlayerEntity player = bot.getPlayer();
      player.setHealth(updateHealthPacket.healthMP);
      player.setHunger(updateHealthPacket.food);
    } else if (packet instanceof Packet9Respawn) {
      synchronized (chunks) {
        chunks.clear();
      }
    } else if (packet instanceof Packet20NamedEntitySpawn) {
      Packet20NamedEntitySpawn spawnPacket = (Packet20NamedEntitySpawn) packet;
      PlayerEntity entity = new PlayerEntity(this, spawnPacket.entityId, spawnPacket.name);
      entity.setX(spawnPacket.xPosition / 32D);
      entity.setY(spawnPacket.yPosition / 32D);
      entity.setZ(spawnPacket.zPosition / 32D);
      entity.setYaw(spawnPacket.rotation);
      entity.setPitch(spawnPacket.pitch);
      entity.setWornItemAt(0, new BasicItemStack(spawnPacket.currentItem, 1, 0));
      spawnEntity(entity);
    } else if (packet instanceof Packet21PickupSpawn) {
      Packet21PickupSpawn spawnPacket = (Packet21PickupSpawn) packet;
      ItemEntity entity = new ItemEntity(this, spawnPacket.entityId, spawnPacket.item);
      entity.setX(spawnPacket.xPosition / 32D);
      entity.setY(spawnPacket.yPosition / 32D);
      entity.setZ(spawnPacket.zPosition / 32D);
      spawnEntity(entity);
    } else if (packet instanceof Packet22Collect) {
      Entity entity = getEntityById(((Packet22Collect) packet).collectedEntityId);
      if (entity != null) despawnEntity(entity);
    } else if (packet instanceof Packet23VehicleSpawn) {
      Packet23VehicleSpawn spawnPacket = (Packet23VehicleSpawn) packet;
      Entity entity = null;
      Class<? extends Entity> entityClass = EntityList.getObjectEntityClass(spawnPacket.type);
      if (entityClass == null) return;
      try {
        Constructor<? extends Entity> constructor =
            entityClass.getConstructor(World.class, Integer.TYPE);
        entity = constructor.newInstance(this, spawnPacket.entityId);
      } catch (Exception exception) {
        exception.printStackTrace();
        return;
      }
      entity.setX(spawnPacket.xPosition / 32D);
      entity.setY(spawnPacket.yPosition / 32D);
      entity.setZ(spawnPacket.zPosition / 32D);
      entity.setYaw(0);
      entity.setPitch(0);
      spawnEntity(entity);
    } else if (packet instanceof Packet24MobSpawn) {
      Packet24MobSpawn mobSpawnPacket = (Packet24MobSpawn) packet;
      LivingEntity entity = null;
      Class<? extends LivingEntity> entityClass =
          EntityList.getLivingEntityClass(mobSpawnPacket.type);
      if (entityClass == null) return;
      try {
        Constructor<? extends LivingEntity> constructor =
            entityClass.getConstructor(World.class, Integer.TYPE);
        entity = constructor.newInstance(this, mobSpawnPacket.entityId);
      } catch (Exception exception) {
        exception.printStackTrace();
        return;
      }
      entity.setX(mobSpawnPacket.xPosition / 32D);
      entity.setY(mobSpawnPacket.yPosition / 32D);
      entity.setZ(mobSpawnPacket.zPosition / 32D);
      entity.setYaw((mobSpawnPacket.yaw * 360) / 256F);
      entity.setPitch((mobSpawnPacket.pitch * 360) / 256F);
      entity.setHeadYaw((mobSpawnPacket.headYaw * 360) / 256F);

      if (mobSpawnPacket.getMetadata() != null) entity.updateMetadata(mobSpawnPacket.getMetadata());
      spawnEntity(entity);
    } else if (packet instanceof Packet25EntityPainting) {
      Packet25EntityPainting paintingPacket = (Packet25EntityPainting) packet;
      PaintingEntity entity =
          new PaintingEntity(
              this, paintingPacket.entityId, ArtType.getArtTypeByName(paintingPacket.title));
      entity.setX(paintingPacket.xPosition);
      entity.setY(paintingPacket.yPosition);
      entity.setZ(paintingPacket.zPosition);
      entity.setDirection(paintingPacket.direction);
      spawnEntity(entity);
    } else if (packet instanceof Packet26EntityExpOrb) {

    } else if (packet instanceof Packet29DestroyEntity) {
      Packet29DestroyEntity destroyEntityPacket = (Packet29DestroyEntity) packet;
      for (int id : destroyEntityPacket.entityIds) {
        Entity entity = getEntityById(id);
        if (entity != null) {
          despawnEntity(entity);
          entity.setDead(true);
        }
      }
    } else if (packet instanceof Packet30Entity) {
      Packet30Entity entityPacket = (Packet30Entity) packet;
      Entity entity = getEntityById(entityPacket.entityId);
      if (entity == null) return;
      entity.setX(entity.getX() + (entityPacket.xPosition / 32D));
      entity.setY(entity.getY() + (entityPacket.yPosition / 32D));
      entity.setZ(entity.getZ() + (entityPacket.zPosition / 32D));
      if (packet instanceof Packet31RelEntityMove || packet instanceof Packet33RelEntityMoveLook) {
        entity.setYaw((entityPacket.yaw * 360) / 256F);
        entity.setPitch((entityPacket.pitch * 360) / 256F);
      }
    } else if (packet instanceof Packet34EntityTeleport) {
      Packet34EntityTeleport teleportPacket = (Packet34EntityTeleport) packet;
      Entity entity = getEntityById(teleportPacket.entityId);
      if (entity == null) return;
      entity.setX(teleportPacket.xPosition / 32D);
      entity.setY(teleportPacket.yPosition / 32D);
      entity.setZ(teleportPacket.zPosition / 32D);
      entity.setYaw((teleportPacket.yaw * 360) / 256F);
      entity.setPitch((teleportPacket.pitch * 360) / 256F);
    } else if (packet instanceof Packet35EntityHeadRotation) {
      Packet35EntityHeadRotation headRotatePacket = (Packet35EntityHeadRotation) packet;
      Entity entity = getEntityById(headRotatePacket.entityId);
      if (entity == null || !(entity instanceof LivingEntity)) return;
      ((LivingEntity) entity).setHeadYaw((headRotatePacket.headRotationYaw * 360) / 256F);
    } else if (packet instanceof Packet39AttachEntity) {
      Packet39AttachEntity attachEntityPacket = (Packet39AttachEntity) packet;
      Entity rider = getEntityById(attachEntityPacket.entityId);
      if (rider == null) return;
      Entity riding = null;
      if (attachEntityPacket.vehicleEntityId == -1) {
        if (rider.getRiding() != null) {
          rider.getRiding().setRider(null);
          rider.setRiding(null);
        }
      } else {
        riding = getEntityById(attachEntityPacket.vehicleEntityId);
        if (riding == null) return;
        rider.setRiding(riding);
        riding.setRider(rider);
      }
    } else if (packet instanceof Packet40EntityMetadata) {
      Packet40EntityMetadata metadataPacket = (Packet40EntityMetadata) packet;
      Entity entity = getEntityById(metadataPacket.entityId);
      if (entity == null) return;
      entity.updateMetadata(metadataPacket.getMetadata());
    } else if (packet instanceof Packet43Experience) {
      Packet43Experience experiencePacket = (Packet43Experience) packet;
      MainPlayerEntity player = bot.getPlayer();
      player.setExperienceLevel(experiencePacket.experienceLevel);
      player.setExperienceTotal(experiencePacket.experienceTotal);
    } else if (packet instanceof Packet51MapChunk) {
      if (bot.isMovementDisabled()) return;
      Packet51MapChunk mapChunkPacket = (Packet51MapChunk) packet;
      processChunk(
          mapChunkPacket.x,
          mapChunkPacket.z,
          mapChunkPacket.chunkData,
          mapChunkPacket.bitmask,
          mapChunkPacket.additionalBitmask,
          true,
          mapChunkPacket.biomes);
    } else if (packet instanceof Packet52MultiBlockChange) {
      Packet52MultiBlockChange multiBlockChangePacket = (Packet52MultiBlockChange) packet;
      if (multiBlockChangePacket.metadataArray == null) return;
      DataInputStream datainputstream =
          new DataInputStream(new ByteArrayInputStream(multiBlockChangePacket.metadataArray));
      try {
        for (int i = 0; i < multiBlockChangePacket.size; i++) {
          short word0 = datainputstream.readShort();
          short word1 = datainputstream.readShort();
          int id = (word1 & 0xfff) >> 4;
          int metadata = word1 & 0xf;
          int x = word0 >> 12 & 0xf;
          int z = word0 >> 8 & 0xf;
          int y = word0 & 0xff;
          setBlockIdAt(
              id,
              (multiBlockChangePacket.xPosition * 16) + x,
              y,
              (multiBlockChangePacket.zPosition * 16) + z);
          setBlockMetadataAt(
              metadata,
              (multiBlockChangePacket.xPosition * 16) + x,
              y,
              (multiBlockChangePacket.zPosition * 16) + z);
        }
      } catch (IOException exception) {
        exception.printStackTrace();
      }
    } else if (packet instanceof Packet53BlockChange) {
      Packet53BlockChange blockChangePacket = (Packet53BlockChange) packet;
      setBlockIdAt(
          blockChangePacket.type,
          blockChangePacket.xPosition,
          blockChangePacket.yPosition,
          blockChangePacket.zPosition);
      setBlockMetadataAt(
          blockChangePacket.metadata,
          blockChangePacket.xPosition,
          blockChangePacket.yPosition,
          blockChangePacket.zPosition);
    } else if (packet instanceof Packet56MapChunks) {
      if (bot.isMovementDisabled()) return;
      Packet56MapChunks chunkPacket = (Packet56MapChunks) packet;
      for (int i = 0; i < chunkPacket.primaryBitmap.length; i++)
        processChunk(
            chunkPacket.chunkX[i],
            chunkPacket.chunkZ[i],
            chunkPacket.chunkData[i],
            chunkPacket.primaryBitmap[i],
            chunkPacket.secondaryBitmap[i],
            chunkPacket.skylight,
            true);
    }
  }