use of POGOProtos.Inventory.CandyOuterClass.Candy in project PokeGOAPI-Java by Grover-c13.
the class PokeBank method releasePokemon.
/**
* Releases multiple pokemon in a single request
*
* @param releasePokemon the pokemon to release
* @return the amount of candies for each pokemon family
* @throws RequestFailedException if an exception occurred while sending requests
*/
public Map<PokemonFamilyId, Integer> releasePokemon(Pokemon... releasePokemon) throws RequestFailedException {
ReleasePokemonMessage.Builder releaseBuilder = ReleasePokemonMessage.newBuilder();
for (Pokemon pokemon : releasePokemon) {
if (!pokemon.isDeployed() && !pokemon.isFavorite()) {
releaseBuilder.addPokemonIds(pokemon.getId());
}
}
ServerRequestEnvelope envelope = ServerRequestEnvelope.createCommons();
ServerRequest releaseRequest = envelope.add(RequestType.RELEASE_POKEMON, releaseBuilder.build());
Map<PokemonFamilyId, Integer> lastCandies = new HashMap<>(api.getInventories().getCandyjar().getCandies());
ServerResponse response = api.getRequestHandler().sendServerRequests(envelope);
try {
GetInventoryResponse inventoryResponse = GetInventoryResponse.parseFrom(response.get(RequestType.GET_INVENTORY));
ReleasePokemonResponse releaseResponse = ReleasePokemonResponse.parseFrom(releaseRequest.getData());
Map<PokemonFamilyId, Integer> candyCount = new HashMap<>();
if (releaseResponse.getResult() == Result.SUCCESS && inventoryResponse.getSuccess()) {
synchronized (this.lock) {
for (Pokemon pokemon : releasePokemon) {
this.pokemons.remove(pokemon);
}
}
for (Pokemon pokemon : releasePokemon) {
api.getInventories().getPokebank().removePokemon(pokemon);
}
List<InventoryItem> items = inventoryResponse.getInventoryDelta().getInventoryItemsList();
for (InventoryItem item : items) {
InventoryItemData data = item.getInventoryItemData();
if (data != null && data.hasCandy()) {
Candy candy = data.getCandy();
PokemonFamilyId family = candy.getFamilyId();
Integer lastCandy = lastCandies.get(family);
if (lastCandy == null) {
lastCandy = 0;
}
candyCount.put(family, candy.getCandy() - lastCandy);
}
}
api.getInventories().updateInventories(inventoryResponse);
}
return candyCount;
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
}
Aggregations