Search in sources :

Example 16 with RequestFailedException

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

the class CatchPokemonAtAreaExample method main.

/**
 * Catches a pokemon at an area.
 *
 * @param args args
 */
public static void main(String[] args) {
    OkHttpClient http = new OkHttpClient();
    final PokemonGo api = new PokemonGo(http);
    try {
        HashProvider hasher = ExampleConstants.getHashProvider();
        api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
        api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
        // Catch all pokemon in the current area
        catchArea(api);
        MapObjects mapObjects = api.getMap().mapObjects;
        // Find all pokestops with pokemon nearby
        List<Pokestop> travelPokestops = new ArrayList<>();
        Set<NearbyPokemon> nearby = mapObjects.nearby;
        for (NearbyPokemon nearbyPokemon : nearby) {
            String fortId = nearbyPokemon.getFortId();
            // Check if nearby pokemon is near a pokestop
            if (fortId != null && fortId.length() > 0) {
                // Find the pokestop with the fort id of the nearby pokemon
                Pokestop pokestop = mapObjects.getPokestop(fortId);
                if (pokestop != null && !travelPokestops.contains(pokestop)) {
                    travelPokestops.add(pokestop);
                }
            }
        }
        // Sort from closest to farthest
        Collections.sort(travelPokestops, new Comparator<Pokestop>() {

            @Override
            public int compare(Pokestop primary, Pokestop secondary) {
                double lat = api.latitude;
                double lng = api.longitude;
                double distance1 = MapUtil.distFrom(primary.getLatitude(), primary.getLongitude(), lat, lng);
                double distance2 = MapUtil.distFrom(secondary.getLatitude(), secondary.getLongitude(), lat, lng);
                return Double.compare(distance1, distance2);
            }
        });
        for (Pokestop pokestop : travelPokestops) {
            Point destination = new Point(pokestop.getLatitude(), pokestop.getLongitude());
            // Use the current player position as the source and the pokestop position as the destination
            // Travel to Pokestop at 20KMPH
            Path path = new Path(api.getPoint(), destination, 20.0);
            System.out.println("Traveling to " + destination + " at 20KMPH!");
            path.start(api);
            try {
                while (!path.complete) {
                    // Calculate the desired intermediate point for the current time
                    Point point = path.calculateIntermediate(api);
                    // Set the API location to that point
                    api.setLatitude(point.getLatitude());
                    api.setLongitude(point.getLongitude());
                    // Sleep for 2 seconds before setting the location again
                    Thread.sleep(2000);
                }
            } catch (InterruptedException e) {
                break;
            }
            System.out.println("Finished traveling to pokestop, catching pokemon.");
            catchArea(api);
        }
    } catch (NoSuchItemException | RequestFailedException e) {
        Log.e("Main", "An exception occurred while running example: ", e);
    }
}
Also used : Path(com.pokegoapi.util.path.Path) PtcCredentialProvider(com.pokegoapi.auth.PtcCredentialProvider) OkHttpClient(okhttp3.OkHttpClient) ArrayList(java.util.ArrayList) Point(com.pokegoapi.api.map.Point) MapObjects(com.pokegoapi.api.map.MapObjects) NoSuchItemException(com.pokegoapi.exceptions.NoSuchItemException) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) PokemonGo(com.pokegoapi.api.PokemonGo) Pokestop(com.pokegoapi.api.map.fort.Pokestop) HashProvider(com.pokegoapi.util.hash.HashProvider) NearbyPokemon(com.pokegoapi.api.map.pokemon.NearbyPokemon)

Example 17 with RequestFailedException

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

the class FightGymExample method main.

/**
 * Fights gyms in the nearby area.
 */
public static void main(String[] args) {
    OkHttpClient http = new OkHttpClient();
    final PokemonGo api = new PokemonGo(http);
    try {
        // Login and set location
        HashProvider hasher = ExampleConstants.getHashProvider();
        api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
        api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
        List<Pokemon> pokemons = api.inventories.pokebank.pokemons;
        // List to put all pokemon that can be used in a gym battle
        List<Pokemon> possiblePokemon = new ArrayList<>();
        for (Pokemon pokemon : pokemons) {
            // Check if pokemon has full health and is not deployed in a gym
            if (pokemon.getDeployedFortId().length() == 0) {
                if (pokemon.getStamina() < pokemon.getMaxStamina()) {
                    healPokemonFull(pokemon);
                    if (!(pokemon.isInjured() || pokemon.isFainted())) {
                        possiblePokemon.add(pokemon);
                    }
                    Thread.sleep(1000);
                } else {
                    possiblePokemon.add(pokemon);
                }
            } else {
                System.out.println(pokemon.getPokemonId() + " already deployed.");
            }
        }
        // Sort by highest CP
        Collections.sort(possiblePokemon, new Comparator<Pokemon>() {

            @Override
            public int compare(Pokemon primary, Pokemon secondary) {
                return Integer.compare(secondary.getCp(), primary.getCp());
            }
        });
        // Pick the top 6 pokemon from the possible list
        final Pokemon[] attackers = new Pokemon[6];
        for (int i = 0; i < 6; i++) {
            attackers[i] = possiblePokemon.get(i);
        }
        // Sort from closest to farthest
        MapObjects mapObjects = api.getMap().mapObjects;
        List<Gym> gyms = new ArrayList<>(mapObjects.gyms);
        Collections.sort(gyms, new Comparator<Gym>() {

            @Override
            public int compare(Gym primary, Gym secondary) {
                double lat = api.latitude;
                double lng = api.longitude;
                double distance1 = MapUtil.distFrom(primary.getLatitude(), primary.getLongitude(), lat, lng);
                double distance2 = MapUtil.distFrom(secondary.getLatitude(), secondary.getLongitude(), lat, lng);
                return Double.compare(distance1, distance2);
            }
        });
        for (Gym gym : gyms) {
            // Check if gym is attackable, and check if it is not owned by your team
            if (gym.isAttackable() && gym.getOwnedByTeam() != api.playerProfile.getPlayerData().getTeam()) {
                // Walk to gym; Documented pathing in TravelToPokestopExample
                Point destination = new Point(gym.getLatitude(), gym.getLongitude());
                Path path = new Path(api.getPoint(), destination, 50.0);
                System.out.println("Traveling to " + destination + " at 50KMPH!");
                path.start(api);
                try {
                    while (!path.complete) {
                        Point point = path.calculateIntermediate(api);
                        api.setLatitude(point.getLatitude());
                        api.setLongitude(point.getLongitude());
                        System.out.println("Time left: " + (int) (path.getTimeLeft(api) / 1000) + " seconds.");
                        Thread.sleep(2000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Beginning battle with gym.");
                // Create battle object
                Battle battle = gym.battle();
                // Start battle
                battle.start(new FightHandler(attackers));
                while (battle.active) {
                    handleAttack(api, battle);
                }
                // Heal all pokemon after battle
                for (Pokemon pokemon : possiblePokemon) {
                    if (pokemon.getStamina() < pokemon.getMaxStamina()) {
                        healPokemonFull(pokemon);
                        Thread.sleep(1000);
                    }
                }
                // If prestige reaches 0, deploy your pokemon
                if (battle.gym.getPoints() <= 0) {
                    Pokemon best = possiblePokemon.get(0);
                    System.out.println("Deploying " + best.getPokemonId() + " to gym.");
                    battle.gym.deployPokemon(best);
                }
            }
        }
    } catch (RequestFailedException | InterruptedException e) {
        // failed to login, invalid credentials, auth issue or server issue.
        Log.e("Main", "Failed to login, captcha or server issue: ", e);
    }
}
Also used : Path(com.pokegoapi.util.path.Path) PtcCredentialProvider(com.pokegoapi.auth.PtcCredentialProvider) OkHttpClient(okhttp3.OkHttpClient) ArrayList(java.util.ArrayList) Point(com.pokegoapi.api.map.Point) MapObjects(com.pokegoapi.api.map.MapObjects) Point(com.pokegoapi.api.map.Point) Battle(com.pokegoapi.api.gym.Battle) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) PokemonGo(com.pokegoapi.api.PokemonGo) HashProvider(com.pokegoapi.util.hash.HashProvider) Gym(com.pokegoapi.api.gym.Gym) Pokemon(com.pokegoapi.api.pokemon.Pokemon)

Example 18 with RequestFailedException

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

the class TransferOnePidgeyExample method main.

/**
 * Transfers one pidgey from the player's inventory.
 */
public static void main(String[] args) {
    OkHttpClient http = new OkHttpClient();
    PokemonGo api = new PokemonGo(http);
    try {
        HashProvider hasher = ExampleConstants.getHashProvider();
        api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
        api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
        List<Pokemon> pidgeys = api.inventories.pokebank.getPokemonByPokemonId(PokemonIdOuterClass.PokemonId.PIDGEY);
        if (pidgeys.size() > 0) {
            Pokemon pest = pidgeys.get(0);
            // print the pokemon data
            pest.debug();
            ReleasePokemonResponseOuterClass.ReleasePokemonResponse.Result result = pest.transferPokemon();
            Log.i("Main", "Transfered Pidgey result:" + result);
        } else {
            Log.i("Main", "You have no pidgeys :O");
        }
    } catch (RequestFailedException e) {
        // failed to login, invalid credentials, auth issue or server issue.
        Log.e("Main", "Failed to login. Invalid credentials, captcha or server issue: ", e);
    }
}
Also used : PtcCredentialProvider(com.pokegoapi.auth.PtcCredentialProvider) OkHttpClient(okhttp3.OkHttpClient) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) PokemonGo(com.pokegoapi.api.PokemonGo) HashProvider(com.pokegoapi.util.hash.HashProvider) Pokemon(com.pokegoapi.api.pokemon.Pokemon)

Example 19 with RequestFailedException

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

the class UseIncenseExample method main.

/**
 * Catches a pokemon at an area.
 */
public static void main(String[] args) {
    OkHttpClient http = new OkHttpClient();
    PokemonGo api = new PokemonGo(http, new SystemTimeImpl());
    try {
        HashProvider hasher = ExampleConstants.getHashProvider();
        api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
        api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
        api.inventories.itemBag.useIncense();
    } catch (RequestFailedException e) {
        // failed to login, invalid credentials, auth issue or server issue.
        Log.e("Main", "Failed to login, captcha or server issue: ", e);
    }
}
Also used : PtcCredentialProvider(com.pokegoapi.auth.PtcCredentialProvider) OkHttpClient(okhttp3.OkHttpClient) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) PokemonGo(com.pokegoapi.api.PokemonGo) SystemTimeImpl(com.pokegoapi.util.SystemTimeImpl) HashProvider(com.pokegoapi.util.hash.HashProvider)

Example 20 with RequestFailedException

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

the class CheckEvolutionExample method main.

/**
 * Displays pokemon evolutions
 *
 * @param args Not used
 */
public static void main(String[] args) {
    OkHttpClient http = new OkHttpClient();
    final PokemonGo api = new PokemonGo(http);
    try {
        // Login and set location
        HashProvider hasher = ExampleConstants.getHashProvider();
        api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
        api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
        // Get the evolution meta from the item templates received from the game server
        Evolutions evolutionMeta = api.itemTemplates.evolutions;
        System.out.println("Evolutions: ");
        for (PokemonId pokemon : PokemonId.values()) {
            List<PokemonId> evolutions = evolutionMeta.getEvolutions(pokemon);
            if (evolutions.size() > 0) {
                System.out.println(pokemon + " -> " + evolutions);
            }
        }
        System.out.println();
        System.out.println("Most basic: ");
        for (PokemonId pokemon : PokemonId.values()) {
            List<PokemonId> basic = evolutionMeta.getBasic(pokemon);
            if (basic.size() > 0) {
                // Check this is not the most basic pokemon
                if (!(basic.size() == 1 && basic.contains(pokemon))) {
                    System.out.println(pokemon + " -> " + basic);
                }
            }
        }
        System.out.println();
        System.out.println("Highest: ");
        for (PokemonId pokemon : PokemonId.values()) {
            List<PokemonId> highest = evolutionMeta.getHighest(pokemon);
            if (highest.size() > 0) {
                // Check this is not the highest pokemon
                if (!(highest.size() == 1 && highest.contains(pokemon))) {
                    System.out.println(pokemon + " -> " + highest);
                }
            }
        }
    } catch (RequestFailedException e) {
        // failed to login, invalid credentials, auth issue or server issue.
        Log.e("Main", "Failed to login, captcha or server issue: ", e);
    }
}
Also used : PtcCredentialProvider(com.pokegoapi.auth.PtcCredentialProvider) OkHttpClient(okhttp3.OkHttpClient) PokemonId(POGOProtos.Enums.PokemonIdOuterClass.PokemonId) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) PokemonGo(com.pokegoapi.api.PokemonGo) HashProvider(com.pokegoapi.util.hash.HashProvider) Evolutions(com.pokegoapi.api.pokemon.Evolutions)

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