Search in sources :

Example 1 with CatchPokemonMessage

use of POGOProtos.Networking.Requests.Messages.CatchPokemonMessageOuterClass.CatchPokemonMessage 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.inventories.itemBag;
        Item item = bag.getItem(pokeball);
        if (item.count > 0) {
            CatchPokemonMessage message = CatchPokemonMessage.newBuilder().setEncounterId(pokemon.encounterId).setSpawnPointId(pokemon.spawnPointId).setPokeball(pokeball).setNormalizedHitPosition(throwProperties.normalizedHitPosition).setNormalizedReticleSize(throwProperties.normalizedReticleSize).setSpinModifier(throwProperties.spinModifier).setHitPokemon(throwProperties.shouldHitPokemon()).build();
            ServerRequest request = new ServerRequest(RequestType.CATCH_POKEMON, message);
            ByteString responseData = api.requestHandler.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.despawned = true;
                    api.playerProfile.updateProfile();
                }
                if (status == CatchStatus.CATCH_ESCAPE) {
                    activeItem = ItemId.UNRECOGNIZED;
                }
                if (status != CatchStatus.CATCH_ERROR) {
                    item.setCount(item.count - 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

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 Item (com.pokegoapi.api.inventory.Item)1 ItemBag (com.pokegoapi.api.inventory.ItemBag)1 NoSuchItemException (com.pokegoapi.exceptions.NoSuchItemException)1 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)1 ServerRequest (com.pokegoapi.main.ServerRequest)1