Search in sources :

Example 1 with NearbyPokemon

use of com.pokegoapi.api.map.pokemon.NearbyPokemon 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)

Aggregations

PokemonGo (com.pokegoapi.api.PokemonGo)1 MapObjects (com.pokegoapi.api.map.MapObjects)1 Point (com.pokegoapi.api.map.Point)1 Pokestop (com.pokegoapi.api.map.fort.Pokestop)1 NearbyPokemon (com.pokegoapi.api.map.pokemon.NearbyPokemon)1 PtcCredentialProvider (com.pokegoapi.auth.PtcCredentialProvider)1 NoSuchItemException (com.pokegoapi.exceptions.NoSuchItemException)1 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)1 HashProvider (com.pokegoapi.util.hash.HashProvider)1 Path (com.pokegoapi.util.path.Path)1 ArrayList (java.util.ArrayList)1 OkHttpClient (okhttp3.OkHttpClient)1