/**
   * creates and returns a respawned player based on the provided PlayerEntity. Args are the
   * PlayerEntityMP to respawn, an INT for the dimension to respawn into (usually 0), and a boolean
   * value that is true if the player beat the game rather than dying
   */
  public EntityPlayerMP respawnPlayer(EntityPlayerMP par1EntityPlayerMP, int par2, boolean par3) {
    par1EntityPlayerMP
        .getServerForPlayer()
        .getEntityTracker()
        .removeAllTrackingPlayers(par1EntityPlayerMP);
    par1EntityPlayerMP
        .getServerForPlayer()
        .getEntityTracker()
        .removeEntityFromAllTrackingPlayers(par1EntityPlayerMP);
    par1EntityPlayerMP.getServerForPlayer().getPlayerManager().removePlayer(par1EntityPlayerMP);
    this.playerEntityList.remove(par1EntityPlayerMP);
    this.mcServer
        .worldServerForDimension(par1EntityPlayerMP.dimension)
        .removeEntity(par1EntityPlayerMP);
    ChunkCoordinates var4 = par1EntityPlayerMP.getBedLocation();
    boolean var5 = par1EntityPlayerMP.isSpawnForced();
    par1EntityPlayerMP.dimension = par2;
    Object var6;

    if (this.mcServer.isDemo()) {
      var6 =
          new DemoWorldManager(this.mcServer.worldServerForDimension(par1EntityPlayerMP.dimension));
    } else {
      var6 =
          new ItemInWorldManager(
              this.mcServer.worldServerForDimension(par1EntityPlayerMP.dimension));
    }

    EntityPlayerMP var7 =
        new EntityPlayerMP(
            this.mcServer,
            this.mcServer.worldServerForDimension(par1EntityPlayerMP.dimension),
            par1EntityPlayerMP.username,
            (ItemInWorldManager) var6);
    var7.playerNetServerHandler = par1EntityPlayerMP.playerNetServerHandler;
    var7.clonePlayer(par1EntityPlayerMP, par3);
    var7.entityId = par1EntityPlayerMP.entityId;
    WorldServer var8 = this.mcServer.worldServerForDimension(par1EntityPlayerMP.dimension);
    this.func_72381_a(var7, par1EntityPlayerMP, var8);
    ChunkCoordinates var9;

    if (var4 != null) {
      var9 =
          EntityPlayer.verifyRespawnCoordinates(
              this.mcServer.worldServerForDimension(par1EntityPlayerMP.dimension), var4, var5);

      if (var9 != null) {
        var7.setLocationAndAngles(
            (double) ((float) var9.posX + 0.5F),
            (double) ((float) var9.posY + 0.1F),
            (double) ((float) var9.posZ + 0.5F),
            0.0F,
            0.0F);
        var7.setSpawnChunk(var4, var5);
      } else {
        var7.playerNetServerHandler.sendPacketToPlayer(new Packet70GameEvent(0, 0));
      }
    }

    var8.theChunkProviderServer.loadChunk((int) var7.posX >> 4, (int) var7.posZ >> 4);

    while (!var8.getCollidingBoundingBoxes(var7, var7.boundingBox).isEmpty()) {
      var7.setPosition(var7.posX, var7.posY + 1.0D, var7.posZ);
    }

    var7.playerNetServerHandler.sendPacketToPlayer(
        new Packet9Respawn(
            var7.dimension,
            (byte) var7.worldObj.difficultySetting,
            var7.worldObj.getWorldInfo().getTerrainType(),
            var7.worldObj.getHeight(),
            var7.theItemInWorldManager.getGameType()));
    var9 = var8.getSpawnPoint();
    var7.playerNetServerHandler.setPlayerLocation(
        var7.posX, var7.posY, var7.posZ, var7.rotationYaw, var7.rotationPitch);
    var7.playerNetServerHandler.sendPacketToPlayer(
        new Packet6SpawnPosition(var9.posX, var9.posY, var9.posZ));
    var7.playerNetServerHandler.sendPacketToPlayer(
        new Packet43Experience(var7.experience, var7.experienceTotal, var7.experienceLevel));
    this.updateTimeAndWeatherForPlayer(var7, var8);
    var8.getPlayerManager().addPlayer(var7);
    var8.spawnEntityInWorld(var7);
    this.playerEntityList.add(var7);
    var7.addSelfToInternalCraftingInventory();
    return var7;
  }
  /** Called on respawn */
  public EntityPlayerMP recreatePlayerEntity(
      EntityPlayerMP playerIn, int dimension, boolean conqueredEnd) {
    playerIn.getServerForPlayer().getEntityTracker().removePlayerFromTrackers(playerIn);
    playerIn.getServerForPlayer().getEntityTracker().untrackEntity(playerIn);
    playerIn.getServerForPlayer().getPlayerManager().removePlayer(playerIn);
    this.playerEntityList.remove(playerIn);
    this.mcServer
        .worldServerForDimension(playerIn.dimension)
        .removePlayerEntityDangerously(playerIn);
    BlockPos var4 = playerIn.func_180470_cg();
    boolean var5 = playerIn.isSpawnForced();
    playerIn.dimension = dimension;
    Object var6;

    if (this.mcServer.isDemo()) {
      var6 = new DemoWorldManager(this.mcServer.worldServerForDimension(playerIn.dimension));
    } else {
      var6 = new ItemInWorldManager(this.mcServer.worldServerForDimension(playerIn.dimension));
    }

    EntityPlayerMP var7 =
        new EntityPlayerMP(
            this.mcServer,
            this.mcServer.worldServerForDimension(playerIn.dimension),
            playerIn.getGameProfile(),
            (ItemInWorldManager) var6);
    var7.playerNetServerHandler = playerIn.playerNetServerHandler;
    var7.clonePlayer(playerIn, conqueredEnd);
    var7.setEntityId(playerIn.getEntityId());
    var7.func_174817_o(playerIn);
    WorldServer var8 = this.mcServer.worldServerForDimension(playerIn.dimension);
    this.func_72381_a(var7, playerIn, var8);
    BlockPos var9;

    if (var4 != null) {
      var9 =
          EntityPlayer.func_180467_a(
              this.mcServer.worldServerForDimension(playerIn.dimension), var4, var5);

      if (var9 != null) {
        var7.setLocationAndAngles(
            (double) ((float) var9.getX() + 0.5F),
            (double) ((float) var9.getY() + 0.1F),
            (double) ((float) var9.getZ() + 0.5F),
            0.0F,
            0.0F);
        var7.func_180473_a(var4, var5);
      } else {
        var7.playerNetServerHandler.sendPacket(new S2BPacketChangeGameState(0, 0.0F));
      }
    }

    var8.theChunkProviderServer.loadChunk((int) var7.posX >> 4, (int) var7.posZ >> 4);

    while (!var8.getCollidingBoundingBoxes(var7, var7.getEntityBoundingBox()).isEmpty()
        && var7.posY < 256.0D) {
      var7.setPosition(var7.posX, var7.posY + 1.0D, var7.posZ);
    }

    var7.playerNetServerHandler.sendPacket(
        new S07PacketRespawn(
            var7.dimension,
            var7.worldObj.getDifficulty(),
            var7.worldObj.getWorldInfo().getTerrainType(),
            var7.theItemInWorldManager.getGameType()));
    var9 = var8.getSpawnPoint();
    var7.playerNetServerHandler.setPlayerLocation(
        var7.posX, var7.posY, var7.posZ, var7.rotationYaw, var7.rotationPitch);
    var7.playerNetServerHandler.sendPacket(new S05PacketSpawnPosition(var9));
    var7.playerNetServerHandler.sendPacket(
        new S1FPacketSetExperience(var7.experience, var7.experienceTotal, var7.experienceLevel));
    this.updateTimeAndWeatherForPlayer(var7, var8);
    var8.getPlayerManager().addPlayer(var7);
    var8.spawnEntityInWorld(var7);
    this.playerEntityList.add(var7);
    this.field_177454_f.put(var7.getUniqueID(), var7);
    var7.addSelfToInternalCraftingInventory();
    var7.setHealth(var7.getHealth());
    return var7;
  }
  private static Entity sendEntityToWorld(
      Entity entity, int newDimension, Vector3 newPos, Facing3 newLook) {
    MinecraftServer server = MinecraftServer.getServer();
    Entity currentEntity = entity;
    if (entity.dimension != newDimension) {
      if (entity instanceof EntityPlayerMP) {
        EntityPlayerMP player = (EntityPlayerMP) entity;
        ServerConfigurationManager scm = server.getConfigurationManager();
        int oldDimension = player.dimension;
        player.dimension = newDimension;
        WorldServer oldWorld = server.worldServerForDimension(oldDimension);
        WorldServer newWorld = server.worldServerForDimension(newDimension);

        DimensionRegisterMessage packet =
            new DimensionRegisterMessage(
                newDimension, DimensionManager.getProviderType(newDimension));
        LCRuntime.runtime.network().getPreferredPipe().sendForgeMessageTo(packet, player);

        player.closeScreen();
        player.playerNetServerHandler.sendPacket(
            new S07PacketRespawn(
                player.dimension,
                player.worldObj.difficultySetting,
                newWorld.getWorldInfo().getTerrainType(),
                player.theItemInWorldManager.getGameType()));
        oldWorld.removePlayerEntityDangerously(player);
        player.isDead = false;
        player.setLocationAndAngles(
            newPos.x, newPos.y, newPos.z, (float) newLook.yaw, (float) newLook.pitch);
        newWorld.spawnEntityInWorld(player);
        player.setWorld(newWorld);
        scm.func_72375_a(player, oldWorld);
        player.playerNetServerHandler.setPlayerLocation(
            newPos.x, newPos.y, newPos.z, (float) newLook.yaw, (float) newLook.pitch);
        player.theItemInWorldManager.setWorld(newWorld);
        scm.updateTimeAndWeatherForPlayer(player, newWorld);
        scm.syncPlayerInventory(player);
        Iterator<?> var6 = player.getActivePotionEffects().iterator();
        while (var6.hasNext())
          player.playerNetServerHandler.sendPacket(
              new S1DPacketEntityEffect(player.getEntityId(), (PotionEffect) var6.next()));
        player.playerNetServerHandler.sendPacket(
            new S1FPacketSetExperience(
                player.experience, player.experienceTotal, player.experienceLevel));
      } else {
        int oldDimension = entity.dimension;
        WorldServer oldWorld = server.worldServerForDimension(oldDimension);
        WorldServer newWorld = server.worldServerForDimension(newDimension);
        entity.dimension = newDimension;

        entity.worldObj.removeEntity(entity);
        entity.isDead = false;
        server
            .getConfigurationManager()
            .transferEntityToWorld(entity, oldDimension, oldWorld, newWorld);
        currentEntity = EntityList.createEntityByName(EntityList.getEntityString(entity), newWorld);

        if (currentEntity != null) {
          currentEntity.copyDataFrom(entity, true);
          currentEntity.setLocationAndAngles(
              newPos.x, newPos.y, newPos.z, (float) newLook.yaw, (float) newLook.pitch);
          newWorld.spawnEntityInWorld(currentEntity);
        }

        entity.isDead = true;
        oldWorld.resetUpdateEntityTick();
        newWorld.resetUpdateEntityTick();
      }
    } else {
      currentEntity.setLocationAndAngles(
          newPos.x, newPos.y, newPos.z, (float) newLook.yaw, (float) newLook.pitch);
      if (currentEntity instanceof EntityPlayerMP) {
        EntityPlayerMP mpEnt = (EntityPlayerMP) currentEntity;
        mpEnt.rotationYaw = (float) newLook.yaw;
        mpEnt.setPositionAndUpdate(newPos.x, newPos.y, newPos.z);
        mpEnt.worldObj.updateEntityWithOptionalForce(entity, false);
      }
    }
    return currentEntity;
  }