Search in sources :

Example 16 with ServerRequest

use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.

the class Pokemon method usePotion.

/**
	 * use a potion on that pokemon. Will check if there is enough potions and if the pokemon need
	 * to be healed.
	 *
	 * @param itemId {@link ItemId} of the potion to use.
	 * @return Result, ERROR_CANNOT_USE if the requirements aren't met
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public UseItemPotionResponse.Result usePotion(ItemId itemId) throws RequestFailedException {
    Item potion = api.getInventories().getItemBag().getItem(itemId);
    //some sanity check, to prevent wrong use of this call
    if (!potion.isPotion() || potion.getCount() < 1 || !isInjured())
        return UseItemPotionResponse.Result.ERROR_CANNOT_USE;
    UseItemPotionMessageOuterClass.UseItemPotionMessage reqMsg = UseItemPotionMessageOuterClass.UseItemPotionMessage.newBuilder().setItemId(itemId).setPokemonId(getId()).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.USE_ITEM_POTION, reqMsg);
    api.getRequestHandler().sendServerRequests(serverRequest, true);
    UseItemPotionResponse response;
    try {
        response = UseItemPotionResponse.parseFrom(serverRequest.getData());
        if (response.getResult() == UseItemPotionResponse.Result.SUCCESS) {
            potion.setCount(potion.getCount() - 1);
            setStamina(response.getStamina());
        }
        return response.getResult();
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : Item(com.pokegoapi.api.inventory.Item) UseItemPotionMessageOuterClass(POGOProtos.Networking.Requests.Messages.UseItemPotionMessageOuterClass) UseItemPotionResponse(POGOProtos.Networking.Responses.UseItemPotionResponseOuterClass.UseItemPotionResponse) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest)

Example 17 with ServerRequest

use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.

the class Pokemon method powerUpAsync.

/**
	 * Powers up a pokemon with candy and stardust.
	 * After powering up this pokemon object will reflect the new changes.
	 *
	 * @return The result
	 */
public Observable<UpgradePokemonResponse.Result> powerUpAsync() {
    UpgradePokemonMessage reqMsg = UpgradePokemonMessage.newBuilder().setPokemonId(getId()).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.UPGRADE_POKEMON, reqMsg);
    return api.getRequestHandler().sendAsyncServerRequests(serverRequest, true).map(new Func1<ByteString, UpgradePokemonResponse.Result>() {

        @Override
        public UpgradePokemonResponse.Result call(ByteString result) {
            UpgradePokemonResponse response;
            try {
                response = UpgradePokemonResponse.parseFrom(result);
            } catch (InvalidProtocolBufferException e) {
                throw Exceptions.propagate(e);
            }
            //set new pokemon details
            applyProto(response.getUpgradedPokemon());
            return response.getResult();
        }
    });
}
Also used : UpgradePokemonMessage(POGOProtos.Networking.Requests.Messages.UpgradePokemonMessageOuterClass.UpgradePokemonMessage) ByteString(com.google.protobuf.ByteString) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) UpgradePokemonResponse(POGOProtos.Networking.Responses.UpgradePokemonResponseOuterClass.UpgradePokemonResponse) EvolutionResult(com.pokegoapi.api.map.pokemon.EvolutionResult) Result(POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse.Result)

Example 18 with ServerRequest

use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.

the class Gym method deployPokemon.

/**
	 * Deploy pokemon
	 *
	 * @param pokemon The pokemon to deploy
	 * @return Result of attempt to deploy pokemon
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public FortDeployPokemonResponse.Result deployPokemon(Pokemon pokemon) throws RequestFailedException {
    FortDeployPokemonMessage reqMsg = FortDeployPokemonMessage.newBuilder().setFortId(getId()).setPlayerLatitude(api.getLatitude()).setPlayerLongitude(api.getLongitude()).setPokemonId(pokemon.getId()).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.FORT_DEPLOY_POKEMON, reqMsg);
    api.getRequestHandler().sendServerRequests(serverRequest, true);
    try {
        return FortDeployPokemonResponse.parseFrom(serverRequest.getData()).getResult();
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException();
    }
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) FortDeployPokemonMessage(POGOProtos.Networking.Requests.Messages.FortDeployPokemonMessageOuterClass.FortDeployPokemonMessage) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest)

Example 19 with ServerRequest

use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.

the class EggIncubator method hatchEgg.

/**
	 * Hatch an egg.
	 *
	 * @param egg the egg
	 * @return status of putting egg in incubator
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public UseItemEggIncubatorResponse.Result hatchEgg(EggPokemon egg) throws RequestFailedException {
    UseItemEggIncubatorMessage reqMsg = UseItemEggIncubatorMessage.newBuilder().setItemId(proto.getId()).setPokemonId(egg.getId()).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.USE_ITEM_EGG_INCUBATOR, reqMsg);
    api.getRequestHandler().sendServerRequests(serverRequest, true);
    UseItemEggIncubatorResponse response;
    try {
        response = UseItemEggIncubatorResponse.parseFrom(serverRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    api.getInventories().updateInventories(true);
    return response.getResult();
}
Also used : UseItemEggIncubatorResponse(POGOProtos.Networking.Responses.UseItemEggIncubatorResponseOuterClass.UseItemEggIncubatorResponse) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) UseItemEggIncubatorMessage(POGOProtos.Networking.Requests.Messages.UseItemEggIncubatorMessageOuterClass.UseItemEggIncubatorMessage) ServerRequest(com.pokegoapi.main.ServerRequest)

Example 20 with ServerRequest

use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.

the class Hatchery method queryHatchedEggs.

/**
	 * Get if eggs has hatched.
	 *
	 * @return list of hatched eggs
	 * @throws RequestFailedException if an exception occurred while sending requests
	 * @deprecated Use getHatchedEggs()
	 */
@Deprecated
public List<HatchedEgg> queryHatchedEggs() throws RequestFailedException {
    GetHatchedEggsMessage msg = GetHatchedEggsMessage.newBuilder().build();
    ServerRequest serverRequest = new ServerRequest(RequestType.GET_HATCHED_EGGS, msg);
    api.getRequestHandler().sendServerRequests(serverRequest);
    GetHatchedEggsResponse response;
    try {
        response = GetHatchedEggsResponse.parseFrom(serverRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    api.getInventories().updateInventories();
    return updateHatchedEggs(response);
}
Also used : GetHatchedEggsResponse(POGOProtos.Networking.Responses.GetHatchedEggsResponseOuterClass.GetHatchedEggsResponse) GetHatchedEggsMessage(POGOProtos.Networking.Requests.Messages.GetHatchedEggsMessageOuterClass.GetHatchedEggsMessage) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest)

Aggregations

InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)42 ServerRequest (com.pokegoapi.main.ServerRequest)42 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)35 ByteString (com.google.protobuf.ByteString)13 GetPlayerMessage (POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage)3 Item (com.pokegoapi.api.inventory.Item)3 ItemBag (com.pokegoapi.api.inventory.ItemBag)3 TutorialListener (com.pokegoapi.api.listener.TutorialListener)3 FortDeployPokemonMessage (POGOProtos.Networking.Requests.Messages.FortDeployPokemonMessageOuterClass.FortDeployPokemonMessage)2 LevelUpRewardsMessage (POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage)2 ReleasePokemonMessage (POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage)2 UseItemEncounterMessage (POGOProtos.Networking.Requests.Messages.UseItemEncounterMessageOuterClass.UseItemEncounterMessage)2 LevelUpRewardsResponse (POGOProtos.Networking.Responses.LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse)2 UseItemEncounterResponse (POGOProtos.Networking.Responses.UseItemEncounterResponseOuterClass.UseItemEncounterResponse)2 EvolutionResult (com.pokegoapi.api.map.pokemon.EvolutionResult)2 BattleAction (POGOProtos.Data.Battle.BattleActionOuterClass.BattleAction)1 PlayerBadge (POGOProtos.Data.PlayerBadgeOuterClass.PlayerBadge)1 PokemonFamilyId (POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId)1 TutorialState (POGOProtos.Enums.TutorialStateOuterClass.TutorialState)1 Candy (POGOProtos.Inventory.CandyOuterClass.Candy)1