Search in sources :

Example 6 with Pokemon

use of com.pokegoapi.api.pokemon.Pokemon in project PokeGOAPI-Java by Grover-c13.

the class PokeBank method releasePokemon.

/**
	 * Releases multiple pokemon in a single request
	 *
	 * @param releasePokemon the pokemon to release
	 * @return the amount of candies for each pokemon family
	 * @throws RequestFailedException if an exception occurred while sending requests
	 */
public Map<PokemonFamilyId, Integer> releasePokemon(Pokemon... releasePokemon) throws RequestFailedException {
    ReleasePokemonMessage.Builder releaseBuilder = ReleasePokemonMessage.newBuilder();
    for (Pokemon pokemon : releasePokemon) {
        if (!pokemon.isDeployed() && !pokemon.isFavorite()) {
            releaseBuilder.addPokemonIds(pokemon.getId());
        }
    }
    ServerRequestEnvelope envelope = ServerRequestEnvelope.createCommons();
    ServerRequest releaseRequest = envelope.add(RequestType.RELEASE_POKEMON, releaseBuilder.build());
    Map<PokemonFamilyId, Integer> lastCandies = new HashMap<>(api.getInventories().getCandyjar().getCandies());
    ServerResponse response = api.getRequestHandler().sendServerRequests(envelope);
    try {
        GetInventoryResponse inventoryResponse = GetInventoryResponse.parseFrom(response.get(RequestType.GET_INVENTORY));
        ReleasePokemonResponse releaseResponse = ReleasePokemonResponse.parseFrom(releaseRequest.getData());
        Map<PokemonFamilyId, Integer> candyCount = new HashMap<>();
        if (releaseResponse.getResult() == Result.SUCCESS && inventoryResponse.getSuccess()) {
            synchronized (this.lock) {
                for (Pokemon pokemon : releasePokemon) {
                    this.pokemons.remove(pokemon);
                }
            }
            for (Pokemon pokemon : releasePokemon) {
                api.getInventories().getPokebank().removePokemon(pokemon);
            }
            List<InventoryItem> items = inventoryResponse.getInventoryDelta().getInventoryItemsList();
            for (InventoryItem item : items) {
                InventoryItemData data = item.getInventoryItemData();
                if (data != null && data.hasCandy()) {
                    Candy candy = data.getCandy();
                    PokemonFamilyId family = candy.getFamilyId();
                    Integer lastCandy = lastCandies.get(family);
                    if (lastCandy == null) {
                        lastCandy = 0;
                    }
                    candyCount.put(family, candy.getCandy() - lastCandy);
                }
            }
            api.getInventories().updateInventories(inventoryResponse);
        }
        return candyCount;
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : ServerResponse(com.pokegoapi.main.ServerResponse) InventoryItem(POGOProtos.Inventory.InventoryItemOuterClass.InventoryItem) HashMap(java.util.HashMap) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) PokemonFamilyId(POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId) GetInventoryResponse(POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse) ServerRequestEnvelope(com.pokegoapi.main.ServerRequestEnvelope) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) ReleasePokemonMessage(POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage) Candy(POGOProtos.Inventory.CandyOuterClass.Candy) ReleasePokemonResponse(POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse) Pokemon(com.pokegoapi.api.pokemon.Pokemon) ServerRequest(com.pokegoapi.main.ServerRequest) InventoryItemData(POGOProtos.Inventory.InventoryItemDataOuterClass.InventoryItemData)

Example 7 with Pokemon

use of com.pokegoapi.api.pokemon.Pokemon in project PokeGOAPI-Java by Grover-c13.

the class CatchPokemonAtAreaExample method catchArea.

private static void catchArea(PokemonGo api) throws RequestFailedException, NoSuchItemException {
    ItemBag bag = api.getInventories().getItemBag();
    try {
        //Wait until map is updated for the current location
        api.getMap().awaitUpdate();
        Set<CatchablePokemon> catchablePokemon = api.getMap().getMapObjects().getPokemon();
        System.out.println("Pokemon in area: " + catchablePokemon.size());
        Random random = new Random();
        PokeBank pokebank = api.getInventories().getPokebank();
        for (CatchablePokemon cp : catchablePokemon) {
            // Encounter this pokemon
            Encounter encounter = cp.encounter();
            // If the encounter was successful, attempt to catch this pokemon
            if (encounter.isSuccessful()) {
                System.out.println("Encountered: " + cp.getPokemonId());
                List<Pokeball> usablePokeballs = bag.getUsablePokeballs();
                if (usablePokeballs.size() > 0) {
                    //Select pokeball with smart selector to print what pokeball is used
                    double probability = encounter.getCaptureProbability();
                    Pokeball pokeball = PokeballSelector.SMART.select(usablePokeballs, probability);
                    System.out.println("Attempting to catch: " + cp.getPokemonId() + " with " + pokeball + " (" + probability + ")");
                    // Throw pokeballs until capture or flee
                    while (encounter.isActive()) {
                        // Wait between Pokeball throws
                        Thread.sleep(500 + random.nextInt(1000));
                        // If no item is active, use a razzberry
                        int razzberryCount = bag.getItem(ItemId.ITEM_RAZZ_BERRY).getCount();
                        if (encounter.getActiveItem() == null && razzberryCount > 0) {
                            encounter.useItem(ItemId.ITEM_RAZZ_BERRY);
                        }
                        // Throw pokeball with random properties
                        encounter.throwPokeball(PokeballSelector.SMART, ThrowProperties.random());
                        if (encounter.getStatus() == CatchStatus.CATCH_SUCCESS) {
                            // Print pokemon stats
                            Pokemon pokemon = pokebank.getPokemonById(encounter.getCapturedPokemon());
                            if (pokemon != null) {
                                double iv = pokemon.getIvInPercentage();
                                int number = pokemon.getPokemonId().getNumber();
                                String name = PokeDictionary.getDisplayName(number, Locale.ENGLISH);
                                System.out.println("====" + name + "====");
                                System.out.println("CP: " + pokemon.getCp());
                                System.out.println("IV: " + iv + "%");
                                System.out.println("Height: " + pokemon.getHeightM() + "m");
                                System.out.println("Weight: " + pokemon.getWeightKg() + "kg");
                                System.out.println("Move 1: " + pokemon.getMove1());
                                System.out.println("Move 2: " + pokemon.getMove2());
                                //Rename the pokemon to <Name> IV%
                                pokemon.renamePokemon(name + " " + iv + "%");
                                //Set pokemon with IV above 90% as favorite
                                if (iv > 90) {
                                    pokemon.setFavoritePokemon(true);
                                }
                            }
                        }
                    }
                } else {
                    System.out.println("Skipping Pokemon, we have no Pokeballs!");
                }
                // Wait for animation before catching next pokemon
                Thread.sleep(3000 + random.nextInt(1000));
            } else {
                System.out.println("Failed to encounter pokemon: " + encounter.getEncounterResult());
            }
        }
    } catch (InterruptedException e) {
        return;
    }
}
Also used : PokeBank(com.pokegoapi.api.inventory.PokeBank) CatchablePokemon(com.pokegoapi.api.map.pokemon.CatchablePokemon) Point(com.pokegoapi.api.map.Point) ItemBag(com.pokegoapi.api.inventory.ItemBag) Random(java.util.Random) Encounter(com.pokegoapi.api.map.pokemon.Encounter) NearbyPokemon(com.pokegoapi.api.map.pokemon.NearbyPokemon) Pokemon(com.pokegoapi.api.pokemon.Pokemon) CatchablePokemon(com.pokegoapi.api.map.pokemon.CatchablePokemon) Pokeball(com.pokegoapi.api.inventory.Pokeball)

Aggregations

Pokemon (com.pokegoapi.api.pokemon.Pokemon)7 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)5 PokemonGo (com.pokegoapi.api.PokemonGo)3 PtcCredentialProvider (com.pokegoapi.auth.PtcCredentialProvider)3 HashProvider (com.pokegoapi.util.hash.HashProvider)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 OkHttpClient (okhttp3.OkHttpClient)3 PokemonFamilyId (POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 PokeBank (com.pokegoapi.api.inventory.PokeBank)2 Point (com.pokegoapi.api.map.Point)2 ServerRequest (com.pokegoapi.main.ServerRequest)2 Map (java.util.Map)2 PokemonData (POGOProtos.Data.PokemonDataOuterClass.PokemonData)1 PokemonId (POGOProtos.Enums.PokemonIdOuterClass.PokemonId)1 AppliedItem (POGOProtos.Inventory.AppliedItemOuterClass.AppliedItem)1 AppliedItems (POGOProtos.Inventory.AppliedItemsOuterClass.AppliedItems)1 Candy (POGOProtos.Inventory.CandyOuterClass.Candy)1 EggIncubatorOuterClass (POGOProtos.Inventory.EggIncubatorOuterClass)1