|
18 | 18 | import POGOProtos.Data.Player.CurrencyOuterClass; |
19 | 19 | import POGOProtos.Data.Player.EquippedBadgeOuterClass; |
20 | 20 | import POGOProtos.Data.Player.PlayerStatsOuterClass; |
| 21 | +import POGOProtos.Inventory.Item.ItemAwardOuterClass; |
21 | 22 | import POGOProtos.Networking.Requests.Messages.GetPlayerMessageOuterClass.GetPlayerMessage; |
| 23 | +import POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass; |
| 24 | +import POGOProtos.Networking.Requests.Messages.LevelUpRewardsMessageOuterClass.LevelUpRewardsMessage; |
| 25 | +import POGOProtos.Networking.Requests.RequestTypeOuterClass; |
22 | 26 | import POGOProtos.Networking.Requests.RequestTypeOuterClass.RequestType; |
23 | 27 | import POGOProtos.Networking.Responses.GetPlayerResponseOuterClass; |
| 28 | +import POGOProtos.Networking.Responses.LevelUpRewardsResponseOuterClass; |
24 | 29 | import com.google.protobuf.InvalidProtocolBufferException; |
25 | 30 | import com.pokegoapi.api.PokemonGo; |
| 31 | +import com.pokegoapi.api.inventory.Item; |
| 32 | +import com.pokegoapi.api.inventory.ItemBag; |
26 | 33 | import com.pokegoapi.exceptions.InvalidCurrencyException; |
27 | 34 | import com.pokegoapi.exceptions.LoginFailedException; |
28 | 35 | import com.pokegoapi.exceptions.RemoteServerException; |
@@ -128,6 +135,43 @@ public void updateProfile() throws RemoteServerException, LoginFailedException { |
128 | 135 |
|
129 | 136 | } |
130 | 137 |
|
| 138 | + /** |
| 139 | + * Accept the rewards granted and the items unlocked by gaining a trainer level up. Rewards are retained by the |
| 140 | + * server until a player actively accepts them. |
| 141 | + * The rewarded items are automatically inserted into the players item bag. |
| 142 | + * |
| 143 | + * @see PlayerLevelUpRewards |
| 144 | + * @param level the trainer level that you want to accept the rewards for |
| 145 | + * @return a PlayerLevelUpRewards object containing information about the items rewarded and unlocked for this level |
| 146 | + * @throws LoginFailedException if the login failed |
| 147 | + * @throws RemoteServerException if the server failed to respond |
| 148 | + */ |
| 149 | + public PlayerLevelUpRewards acceptLevelUpRewards(int level) throws RemoteServerException, LoginFailedException { |
| 150 | + // Check if we even have achieved this level yet |
| 151 | + if (level > stats.getLevel()) { |
| 152 | + return new PlayerLevelUpRewards(PlayerLevelUpRewards.Status.NOT_UNLOCKED_YET); |
| 153 | + } |
| 154 | + LevelUpRewardsMessage msg = LevelUpRewardsMessage.newBuilder() |
| 155 | + .setLevel(level) |
| 156 | + .build(); |
| 157 | + ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.LEVEL_UP_REWARDS, msg); |
| 158 | + api.getRequestHandler().sendServerRequests(serverRequest); |
| 159 | + LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse response; |
| 160 | + try { |
| 161 | + response = LevelUpRewardsResponseOuterClass.LevelUpRewardsResponse.parseFrom(serverRequest.getData()); |
| 162 | + } catch (InvalidProtocolBufferException e) { |
| 163 | + throw new RemoteServerException(e); |
| 164 | + } |
| 165 | + // Add the awarded items to our bag |
| 166 | + ItemBag bag = api.getInventories().getItemBag(); |
| 167 | + for (ItemAwardOuterClass.ItemAward itemAward : response.getItemsAwardedList()) { |
| 168 | + Item item = bag.getItem(itemAward.getItemId()); |
| 169 | + item.setCount(item.getCount() + itemAward.getItemCount()); |
| 170 | + } |
| 171 | + // Build a new rewards object and return it |
| 172 | + return new PlayerLevelUpRewards(response); |
| 173 | + } |
| 174 | + |
131 | 175 | /** |
132 | 176 | * Add currency. |
133 | 177 | * |
|
0 commit comments