Search in sources :

Example 1 with UseItemEncounterResponse

use of POGOProtos.Networking.Responses.UseItemEncounterResponseOuterClass.UseItemEncounterResponse in project PokeGOAPI-Java by Grover-c13.

the class Encounter method useItem.

/**
 * Uses an item in this encounter
 *
 * @param itemId the item to use
 * @return the result from this action
 * @throws RequestFailedException if the use request fails
 */
public UseItemEncounterResponse.Status useItem(ItemId itemId) throws RequestFailedException {
    if (isActive()) {
        ItemBag bag = api.inventories.itemBag;
        Item item = bag.getItem(itemId);
        if (item.count > 0) {
            if (getActiveItem() == null) {
                UseItemEncounterMessage message = UseItemEncounterMessage.newBuilder().setEncounterId(pokemon.encounterId).setSpawnPointGuid(pokemon.spawnPointId).setItem(itemId).build();
                ServerRequest request = new ServerRequest(RequestType.USE_ITEM_ENCOUNTER, message);
                ByteString responseData = api.requestHandler.sendServerRequests(request, true);
                try {
                    UseItemEncounterResponse response = UseItemEncounterResponse.parseFrom(responseData);
                    activeItem = response.getActiveItem();
                    captureProbabilities = response.getCaptureProbability();
                    if (response.getStatus() == Status.SUCCESS) {
                        item.setCount(item.count - 1);
                    }
                    return response.getStatus();
                } catch (InvalidProtocolBufferException e) {
                    throw new RequestFailedException(e);
                }
            } else {
                return UseItemEncounterResponse.Status.ACTIVE_ITEM_EXISTS;
            }
        } else {
            return UseItemEncounterResponse.Status.NO_ITEM_IN_INVENTORY;
        }
    }
    return UseItemEncounterResponse.Status.ALREADY_COMPLETED;
}
Also used : Item(com.pokegoapi.api.inventory.Item) UseItemEncounterResponse(POGOProtos.Networking.Responses.UseItemEncounterResponseOuterClass.UseItemEncounterResponse) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) ByteString(com.google.protobuf.ByteString) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) UseItemEncounterMessage(POGOProtos.Networking.Requests.Messages.UseItemEncounterMessageOuterClass.UseItemEncounterMessage) ItemBag(com.pokegoapi.api.inventory.ItemBag)

Aggregations

UseItemEncounterMessage (POGOProtos.Networking.Requests.Messages.UseItemEncounterMessageOuterClass.UseItemEncounterMessage)1 UseItemEncounterResponse (POGOProtos.Networking.Responses.UseItemEncounterResponseOuterClass.UseItemEncounterResponse)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 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)1 ServerRequest (com.pokegoapi.main.ServerRequest)1