Search in sources :

Example 31 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException 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.inventories.itemBag.getItem(itemId);
    // some sanity check, to prevent wrong use of this call
    if (!potion.isPotion() || potion.count < 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.requestHandler.sendServerRequests(serverRequest, true);
    UseItemPotionResponse response;
    try {
        response = UseItemPotionResponse.parseFrom(serverRequest.getData());
        if (response.getResult() == UseItemPotionResponse.Result.SUCCESS) {
            potion.setCount(potion.count - 1);
            this.stamina = 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 32 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.

the class Settings method updateSettings.

/**
	 * Updates settings latest data.
	 *
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public void updateSettings() throws RequestFailedException {
    DownloadSettingsMessageOuterClass.DownloadSettingsMessage msg = DownloadSettingsMessageOuterClass.DownloadSettingsMessage.newBuilder().build();
    ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.DOWNLOAD_SETTINGS, msg);
    //here you marked everything as read
    api.getRequestHandler().sendServerRequests(serverRequest, false);
    DownloadSettingsResponseOuterClass.DownloadSettingsResponse response;
    try {
        response = DownloadSettingsResponseOuterClass.DownloadSettingsResponse.parseFrom(serverRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    updateSettings(response);
}
Also used : DownloadSettingsResponse(POGOProtos.Networking.Responses.DownloadSettingsResponseOuterClass.DownloadSettingsResponse) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) DownloadSettingsMessageOuterClass(POGOProtos.Networking.Requests.Messages.DownloadSettingsMessageOuterClass) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) DownloadSettingsResponseOuterClass(POGOProtos.Networking.Responses.DownloadSettingsResponseOuterClass) ServerRequest(com.pokegoapi.main.ServerRequest)

Example 33 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.

the class Gym method details.

private GymGetInfoResponse details() throws RequestFailedException {
    List<TutorialState> tutorialStates = api.playerProfile.getTutorialState().getTutorialStates();
    if (!tutorialStates.contains(TutorialState.GYM_TUTORIAL)) {
        api.playerProfile.visitGymComplete();
    }
    if (details == null) {
        GymGetInfoMessage reqMsg = GymGetInfoMessage.newBuilder().setGymId(this.getId()).setGymLatDegrees(this.getLatitude()).setGymLngDegrees(this.getLongitude()).setPlayerLatDegrees(api.latitude).setPlayerLngDegrees(api.longitude).build();
        ServerRequest serverRequest = new ServerRequest(RequestType.GYM_GET_INFO, reqMsg);
        api.requestHandler.sendServerRequests(serverRequest, true);
        try {
            details = GymGetInfoResponse.parseFrom(serverRequest.getData());
        } catch (InvalidProtocolBufferException e) {
            throw new RequestFailedException();
        }
    }
    return details;
}
Also used : GymGetInfoMessage(POGOProtos.Networking.Requests.Messages.GymGetInfoMessageOuterClass.GymGetInfoMessage) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) TutorialState(POGOProtos.Enums.TutorialStateOuterClass.TutorialState)

Example 34 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.

the class ItemBag method removeItem.

/**
 * Discards the given item.
 *
 * @param id the id
 * @param quantity the quantity
 * @return the result
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public Result removeItem(ItemId id, int quantity) throws RequestFailedException {
    Item item = getItem(id);
    if (item.count < quantity) {
        throw new IllegalArgumentException("You cannot remove more quantity than you have");
    }
    RecycleInventoryItemMessage msg = RecycleInventoryItemMessage.newBuilder().setItemId(id).setCount(quantity).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.RECYCLE_INVENTORY_ITEM, msg);
    api.requestHandler.sendServerRequests(serverRequest, true);
    RecycleInventoryItemResponse response;
    try {
        response = RecycleInventoryItemResponse.parseFrom(serverRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    if (response.getResult() == RecycleInventoryItemResponse.Result.SUCCESS) {
        item.setCount(response.getNewCount());
        if (item.count <= 0) {
            removeItem(item.getItemId());
        }
    }
    return response.getResult();
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) RecycleInventoryItemResponse(POGOProtos.Networking.Responses.RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) RecycleInventoryItemMessage(POGOProtos.Networking.Requests.Messages.RecycleInventoryItemMessageOuterClass.RecycleInventoryItemMessage)

Example 35 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.

the class ItemBag method useIncense.

/**
 * use an incense
 *
 * @param type type of item
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public void useIncense(ItemId type) throws RequestFailedException {
    UseIncenseMessage useIncenseMessage = UseIncenseMessage.newBuilder().setIncenseType(type).setIncenseTypeValue(type.getNumber()).build();
    ServerRequest useIncenseRequest = new ServerRequest(RequestType.USE_INCENSE, useIncenseMessage);
    api.requestHandler.sendServerRequests(useIncenseRequest, true);
    try {
        UseIncenseResponse response = UseIncenseResponse.parseFrom(useIncenseRequest.getData());
        Log.i("Main", "Use incense result: " + response.getResult());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) UseIncenseMessage(POGOProtos.Networking.Requests.Messages.UseIncenseMessageOuterClass.UseIncenseMessage) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) UseIncenseResponse(POGOProtos.Networking.Responses.UseIncenseResponseOuterClass.UseIncenseResponse)

Aggregations

RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)50 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)39 ServerRequest (com.pokegoapi.main.ServerRequest)37 ByteString (com.google.protobuf.ByteString)10 OkHttpClient (okhttp3.OkHttpClient)9 PokemonGo (com.pokegoapi.api.PokemonGo)7 PtcCredentialProvider (com.pokegoapi.auth.PtcCredentialProvider)7 HashProvider (com.pokegoapi.util.hash.HashProvider)7 ArrayList (java.util.ArrayList)6 TutorialListener (com.pokegoapi.api.listener.TutorialListener)4 Pokemon (com.pokegoapi.api.pokemon.Pokemon)4 GetPlayerMessage (POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage)3 Item (com.pokegoapi.api.inventory.Item)3 ItemBag (com.pokegoapi.api.inventory.ItemBag)3 Point (com.pokegoapi.api.map.Point)3 IOException (java.io.IOException)3 PokemonFamilyId (POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId)2 PokemonId (POGOProtos.Enums.PokemonIdOuterClass.PokemonId)2 TutorialState (POGOProtos.Enums.TutorialStateOuterClass.TutorialState)2 LevelUpRewardsMessage (POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage)2