use of POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse in project PokeGOAPI-Java by Grover-c13.
the class Inventories method updateInventories.
/**
* Updates the inventories with the latest data.
*
* @param forceUpdate For a full update if true
* @return the response to the update message
* @throws RequestFailedException if an exception occurred while sending requests
*/
public GetInventoryResponse updateInventories(boolean forceUpdate) throws RequestFailedException {
if (forceUpdate) {
lastInventoryUpdate = 0;
itemBag.reset();
pokebank.reset();
candyjar.reset();
pokedex.reset();
synchronized (this.lock) {
incubators.clear();
}
hatchery.reset();
}
GetInventoryMessage invReqMsg = GetInventoryMessage.newBuilder().setLastTimestampMs(lastInventoryUpdate).build();
ServerRequest inventoryRequest = new ServerRequest(RequestTypeOuterClass.RequestType.GET_INVENTORY, invReqMsg);
api.getRequestHandler().sendServerRequests(inventoryRequest);
GetInventoryResponse response;
try {
response = GetInventoryResponse.parseFrom(inventoryRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
return response;
}
use of POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse 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