Search in sources :

Example 1 with UseItemReviveMessage

use of POGOProtos.Networking.Requests.Messages.UseItemReviveMessageOuterClass.UseItemReviveMessage in project PokeGOAPI-Java by Grover-c13.

the class Pokemon method useRevive.

/**
 * Use a revive item on the pokemon. Will check if there is enough revive & if the pokemon need
 * to be revived.
 *
 * @param itemId {@link ItemId} of the Revive to use.
 * @return Result, ERROR_CANNOT_USE if the requirements aren't met
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public UseItemReviveResponse.Result useRevive(ItemId itemId) throws RequestFailedException {
    Item item = api.inventories.itemBag.getItem(itemId);
    if (!item.isRevive() || item.count < 1 || !isFainted())
        return UseItemReviveResponse.Result.ERROR_CANNOT_USE;
    UseItemReviveMessage reqMsg = UseItemReviveMessage.newBuilder().setItemId(itemId).setPokemonId(getId()).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.USE_ITEM_REVIVE, reqMsg);
    api.requestHandler.sendServerRequests(serverRequest, true);
    UseItemReviveResponse response;
    try {
        response = UseItemReviveResponse.parseFrom(serverRequest.getData());
        if (response.getResult() == UseItemReviveResponse.Result.SUCCESS) {
            item.setCount(item.count - 1);
            this.stamina = response.getStamina();
        }
        return response.getResult();
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : Item(com.pokegoapi.api.inventory.Item) UseItemReviveMessage(POGOProtos.Networking.Requests.Messages.UseItemReviveMessageOuterClass.UseItemReviveMessage) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) UseItemReviveResponse(POGOProtos.Networking.Responses.UseItemReviveResponseOuterClass.UseItemReviveResponse) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest)

Aggregations

UseItemReviveMessage (POGOProtos.Networking.Requests.Messages.UseItemReviveMessageOuterClass.UseItemReviveMessage)1 UseItemReviveResponse (POGOProtos.Networking.Responses.UseItemReviveResponseOuterClass.UseItemReviveResponse)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Item (com.pokegoapi.api.inventory.Item)1 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)1 ServerRequest (com.pokegoapi.main.ServerRequest)1