Search in sources :

Example 1 with NoSuchItemException

use of com.pokegoapi.exceptions.NoSuchItemException 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().getMapObjects();
        //Find all pokestops with pokemon nearby
        List<Pokestop> travelPokestops = new ArrayList<>();
        Set<NearbyPokemon> nearby = mapObjects.getNearby();
        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.getLatitude();
                double lng = api.getLongitude();
                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.isComplete()) {
                    //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 2 with NoSuchItemException

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

the class Encounter method throwPokeball.

/**
	 * Throws a pokeball in this encounter
	 *
	 * @param pokeball the pokeball to throw
	 * @param throwProperties the throw properties for this throw
	 * @return the result from the pokeball throw
	 * @throws RequestFailedException if the throw request fails
	 * @throws NoSuchItemException if the requested pokeball does not exist
	 */
public CatchPokemonResponse.CatchStatus throwPokeball(ItemId pokeball, ThrowProperties throwProperties) throws RequestFailedException, NoSuchItemException {
    if (isActive()) {
        ItemBag bag = api.getInventories().getItemBag();
        Item item = bag.getItem(pokeball);
        if (item.getCount() > 0) {
            CatchPokemonMessage message = CatchPokemonMessage.newBuilder().setEncounterId(pokemon.getEncounterId()).setSpawnPointId(pokemon.getSpawnPointId()).setPokeball(pokeball).setNormalizedHitPosition(throwProperties.getNormalizedHitPosition()).setNormalizedReticleSize(throwProperties.getNormalizedReticleSize()).setSpinModifier(throwProperties.getSpinModifier()).setHitPokemon(throwProperties.shouldHitPokemon()).build();
            ServerRequest request = new ServerRequest(RequestType.CATCH_POKEMON, message);
            ByteString responseData = api.getRequestHandler().sendServerRequests(request, true);
            try {
                CatchPokemonResponse response = CatchPokemonResponse.parseFrom(responseData);
                status = response.getStatus();
                if (hasCaptured()) {
                    captureAward = response.getCaptureAward();
                    capturedPokemon = response.getCapturedPokemonId();
                    captureReason = response.getCaptureReason();
                }
                if (status == CatchStatus.CATCH_SUCCESS || status == CatchStatus.CATCH_FLEE) {
                    pokemon.setDespawned(true);
                }
                if (status == CatchStatus.CATCH_SUCCESS) {
                    api.getPlayerProfile().updateProfile();
                }
                if (status != CatchStatus.CATCH_ERROR) {
                    item.setCount(item.getCount() - 1);
                }
            } catch (InvalidProtocolBufferException e) {
                throw new RequestFailedException(e);
            }
        } else {
            throw new NoSuchItemException();
        }
    }
    return status;
}
Also used : Item(com.pokegoapi.api.inventory.Item) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) ByteString(com.google.protobuf.ByteString) CatchPokemonResponse(POGOProtos.Networking.Responses.CatchPokemonResponseOuterClass.CatchPokemonResponse) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) CatchPokemonMessage(POGOProtos.Networking.Requests.Messages.CatchPokemonMessageOuterClass.CatchPokemonMessage) NoSuchItemException(com.pokegoapi.exceptions.NoSuchItemException) ItemBag(com.pokegoapi.api.inventory.ItemBag)

Aggregations

NoSuchItemException (com.pokegoapi.exceptions.NoSuchItemException)2 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)2 CatchPokemonMessage (POGOProtos.Networking.Requests.Messages.CatchPokemonMessageOuterClass.CatchPokemonMessage)1 CatchPokemonResponse (POGOProtos.Networking.Responses.CatchPokemonResponseOuterClass.CatchPokemonResponse)1 ByteString (com.google.protobuf.ByteString)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 PokemonGo (com.pokegoapi.api.PokemonGo)1 Item (com.pokegoapi.api.inventory.Item)1 ItemBag (com.pokegoapi.api.inventory.ItemBag)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 ServerRequest (com.pokegoapi.main.ServerRequest)1 HashProvider (com.pokegoapi.util.hash.HashProvider)1 Path (com.pokegoapi.util.path.Path)1 ArrayList (java.util.ArrayList)1 OkHttpClient (okhttp3.OkHttpClient)1