Search in sources :

Example 11 with RequestFailedException

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

the class Inventories method updateInventories.

/**
 * Updates the inventories with the latest data.
 *
 * @param forceUpdate For a full update if true
 * @return the response to the update message
 * @throws RequestFailedException if an exception occurred while sending requests
 * @deprecated Inventory is updated as a common request
 */
@Deprecated
public GetHoloInventoryResponse updateInventories(boolean forceUpdate) throws RequestFailedException {
    if (forceUpdate) {
        lastInventoryUpdate = 0;
        itemBag.reset();
        pokebank.reset();
        candyjar.reset();
        pokedex.reset();
        synchronized (this.lock) {
            incubators.clear();
        }
        hatchery.reset();
    }
    GetHoloInventoryMessage invReqMsg = GetHoloInventoryMessage.newBuilder().setLastTimestampMs(lastInventoryUpdate).build();
    ServerRequest inventoryRequest = new ServerRequest(RequestType.GET_HOLOHOLO_INVENTORY, invReqMsg);
    api.requestHandler.sendServerRequests(inventoryRequest, false);
    GetHoloInventoryResponse response;
    try {
        response = GetHoloInventoryResponse.parseFrom(inventoryRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    return response;
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) GetHoloInventoryMessage(POGOProtos.Networking.Requests.Messages.GetHoloInventoryMessageOuterClass.GetHoloInventoryMessage) ServerRequest(com.pokegoapi.main.ServerRequest) GetHoloInventoryResponse(POGOProtos.Networking.Responses.GetHoloInventoryResponseOuterClass.GetHoloInventoryResponse)

Example 12 with RequestFailedException

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

the class ItemBag method useLuckyEgg.

/**
 * use a lucky egg
 *
 * @return the xp boost response
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public UseItemXpBoostResponse useLuckyEgg() throws RequestFailedException {
    UseItemXpBoostMessage xpMsg = UseItemXpBoostMessage.newBuilder().setItemId(ItemId.ITEM_LUCKY_EGG).build();
    ServerRequest req = new ServerRequest(RequestType.USE_ITEM_XP_BOOST, xpMsg);
    api.requestHandler.sendServerRequests(req, true);
    try {
        UseItemXpBoostResponse response = UseItemXpBoostResponse.parseFrom(req.getData());
        Log.i("Main", "Use incense result: " + response.getResult());
        return response;
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) UseItemXpBoostResponse(POGOProtos.Networking.Responses.UseItemXpBoostResponseOuterClass.UseItemXpBoostResponse) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) UseItemXpBoostMessage(POGOProtos.Networking.Requests.Messages.UseItemXpBoostMessageOuterClass.UseItemXpBoostMessage)

Example 13 with RequestFailedException

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

the class Battle method beginDefenderBattle.

/**
 * Starts this battle with an individual defender
 *
 * @param handler to handle this battle
 * @throws RequestFailedException if an exception occurred while sending requests
 */
private void beginDefenderBattle(final BattleHandler handler) throws RequestFailedException {
    lastRetrievedAction = null;
    queuedActions.clear();
    battleState = BattleState.STATE_UNSET;
    lastServerTime = api.currentTimeMillis();
    lastSendTime = lastServerTime;
    sentActions = false;
    List<Pokemon> attackers = new ArrayList<>();
    for (Pokemon pokemon : team) {
        if (!faintedPokemon.contains(pokemon.getId())) {
            attackers.add(pokemon);
        }
    }
    if (attackers.size() > 0 && defenderIndex < defenderCount) {
        StartGymBattleMessage.Builder builder = StartGymBattleMessage.newBuilder().setPlayerLatitude(api.latitude).setPlayerLongitude(api.longitude).setGymId(gym.getId()).setDefendingPokemonId(gym.getDefendingPokemon().get(defenderIndex).getPokemon().getId());
        for (Pokemon pokemon : attackers) {
            builder.addAttackingPokemonIds(pokemon.getId());
            if (pokemon.getStamina() < pokemon.getMaxStamina()) {
                throw new IllegalArgumentException("Pokemon must have full stamina to battle in a gym!");
            } else {
                String deployedFortId = pokemon.getDeployedFortId();
                if (pokemon.getFromFort() && deployedFortId != null && deployedFortId.length() > 0) {
                    throw new IllegalArgumentException("Cannot deploy Pokemon that is already in a gym!");
                }
            }
        }
        try {
            StartGymBattleMessage message = builder.build();
            ServerRequest request = new ServerRequest(RequestType.GYM_START_SESSION, message);
            api.requestHandler.sendServerRequests(request, true);
            StartGymBattleResponse response = StartGymBattleResponse.parseFrom(request.getData());
            if (response.getResult() == StartGymBattleResponse.Result.SUCCESS) {
                battleId = response.getBattleId();
                attacker = response.getAttacker();
                defender = response.getDefender();
                activeDefender = new BattlePokemon(defender.getActivePokemon());
                activeAttacker = new BattlePokemon(attacker.getActivePokemon());
                updateLog(handler, response.getBattleLog());
            }
            sendActions(handler);
            handler.onStart(api, this, response.getResult());
        } catch (InvalidProtocolBufferException e) {
            battleId = "";
            throw new RequestFailedException(e);
        }
    } else {
        active = false;
    }
}
Also used : StartGymBattleResponse(POGOProtos.Networking.Responses.StartGymBattleResponseOuterClass.StartGymBattleResponse) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) StartGymBattleMessage(POGOProtos.Networking.Requests.Messages.StartGymBattleMessageOuterClass.StartGymBattleMessage) Pokemon(com.pokegoapi.api.pokemon.Pokemon) ServerRequest(com.pokegoapi.main.ServerRequest)

Example 14 with RequestFailedException

use of com.pokegoapi.exceptions.request.RequestFailedException 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.requestHandler.sendServerRequests(serverRequest, false);
    GetHatchedEggsResponse response;
    try {
        response = GetHatchedEggsResponse.parseFrom(serverRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    api.inventories.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)

Example 15 with RequestFailedException

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

the class DiskEncounter method encounter.

@Override
public EncounterResult encounter() throws RequestFailedException {
    DiskEncounterMessage message = DiskEncounterMessage.newBuilder().setEncounterId(pokemon.encounterId).setFortId(pokemon.spawnPointId).setPlayerLatitude(api.latitude).setPlayerLongitude(api.longitude).build();
    ServerRequest request = new ServerRequest(RequestType.DISK_ENCOUNTER, message);
    ByteString responseData = api.requestHandler.sendServerRequests(request, true);
    try {
        DiskEncounterResponse response = DiskEncounterResponse.parseFrom(responseData);
        encounterResult = EncounterResult.from(response.getResult());
        activeItem = response.getActiveItem();
        captureProbabilities = response.getCaptureProbability();
        encounteredPokemon = response.getPokemonData();
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    return encounterResult;
}
Also used : RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) ByteString(com.google.protobuf.ByteString) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) DiskEncounterMessage(POGOProtos.Networking.Requests.Messages.DiskEncounterMessageOuterClass.DiskEncounterMessage) DiskEncounterResponse(POGOProtos.Networking.Responses.DiskEncounterResponseOuterClass.DiskEncounterResponse) ServerRequest(com.pokegoapi.main.ServerRequest)

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