Search in sources :

Example 1 with UseItemPotionResponse

use of POGOProtos.Networking.Responses.UseItemPotionResponseOuterClass.UseItemPotionResponse in project PokeGOAPI-Java by Grover-c13.

the class Pokemon method usePotion.

/**
 * use a potion on that pokemon. Will check if there is enough potions and if the pokemon need
 * to be healed.
 *
 * @param itemId {@link ItemId} of the potion to use.
 * @return Result, ERROR_CANNOT_USE if the requirements aren't met
 * @throws RequestFailedException if an exception occurred while sending requests
 */
public UseItemPotionResponse.Result usePotion(ItemId itemId) throws RequestFailedException {
    Item potion = api.inventories.itemBag.getItem(itemId);
    // some sanity check, to prevent wrong use of this call
    if (!potion.isPotion() || potion.count < 1 || !isInjured())
        return UseItemPotionResponse.Result.ERROR_CANNOT_USE;
    UseItemPotionMessageOuterClass.UseItemPotionMessage reqMsg = UseItemPotionMessageOuterClass.UseItemPotionMessage.newBuilder().setItemId(itemId).setPokemonId(getId()).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.USE_ITEM_POTION, reqMsg);
    api.requestHandler.sendServerRequests(serverRequest, true);
    UseItemPotionResponse response;
    try {
        response = UseItemPotionResponse.parseFrom(serverRequest.getData());
        if (response.getResult() == UseItemPotionResponse.Result.SUCCESS) {
            potion.setCount(potion.count - 1);
            this.stamina = response.getStamina();
        }
        return response.getResult();
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
}
Also used : Item(com.pokegoapi.api.inventory.Item) UseItemPotionMessageOuterClass(POGOProtos.Networking.Requests.Messages.UseItemPotionMessageOuterClass) UseItemPotionResponse(POGOProtos.Networking.Responses.UseItemPotionResponseOuterClass.UseItemPotionResponse) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest)

Aggregations

UseItemPotionMessageOuterClass (POGOProtos.Networking.Requests.Messages.UseItemPotionMessageOuterClass)1 UseItemPotionResponse (POGOProtos.Networking.Responses.UseItemPotionResponseOuterClass.UseItemPotionResponse)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