Search in sources :

Example 1 with InsufficientLevelException

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

the class PlayerProfile method acceptLevelUpRewards.

/**
 * Accept the rewards granted and the items unlocked by gaining a trainer level up. Rewards are retained by the
 * server until a player actively accepts them.
 * The rewarded items are automatically inserted into the players item bag.
 *
 * @param level the trainer level that you want to accept the rewards for
 * @return a PlayerLevelUpRewards object containing information about the items rewarded and unlocked for this level
 * @throws RequestFailedException if an exception occurred while sending requests
 * @throws InsufficientLevelException if you have not yet reached the desired level
 * @see PlayerLevelUpRewards
 */
public PlayerLevelUpRewards acceptLevelUpRewards(int level) throws RequestFailedException {
    // Check if we even have achieved this level yet
    if (level > stats.getLevel()) {
        throw new InsufficientLevelException();
    }
    LevelUpRewardsMessage msg = LevelUpRewardsMessage.newBuilder().setLevel(level).build();
    ServerRequest serverRequest = new ServerRequest(RequestType.LEVEL_UP_REWARDS, msg);
    api.requestHandler.sendServerRequests(serverRequest, true);
    LevelUpRewardsResponse response;
    try {
        response = LevelUpRewardsResponse.parseFrom(serverRequest.getData());
    } catch (InvalidProtocolBufferException e) {
        throw new RequestFailedException(e);
    }
    // Add the awarded items to our bag
    ItemBag bag = api.inventories.itemBag;
    bag.addAwardedItems(response);
    // Build a new rewards object and return it
    return new PlayerLevelUpRewards(response);
}
Also used : InsufficientLevelException(com.pokegoapi.exceptions.InsufficientLevelException) LevelUpRewardsMessage(POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ServerRequest(com.pokegoapi.main.ServerRequest) LevelUpRewardsResponse(POGOProtos.Networking.Responses.LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse) ItemBag(com.pokegoapi.api.inventory.ItemBag)

Aggregations

LevelUpRewardsMessage (POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage)1 LevelUpRewardsResponse (POGOProtos.Networking.Responses.LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 ItemBag (com.pokegoapi.api.inventory.ItemBag)1 InsufficientLevelException (com.pokegoapi.exceptions.InsufficientLevelException)1 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)1 ServerRequest (com.pokegoapi.main.ServerRequest)1