use of POGOProtos.Inventory.InventoryItemDataOuterClass.InventoryItemData in project PokeGOAPI-Java by Grover-c13.
the class Inventories method updateInventories.
/**
* Updates the inventories with the latest data.
*
* @param response the get inventory response
* @throws RequestFailedException if a request fails while sending a request
*/
public void updateInventories(GetHoloInventoryResponse response) throws RequestFailedException {
lastInventoryUpdate = api.currentTimeMillis();
for (InventoryItem inventoryItem : response.getInventoryDelta().getInventoryItemsList()) {
// Remove released Pokemon from bag.
if (inventoryItem.getDeletedItem().getPokemonId() != 0) {
pokebank.removePokemon(inventoryItem.getDeletedItem().getPokemonId());
}
InventoryItemData itemData = inventoryItem.getInventoryItemData();
if (itemData.hasPokemonData()) {
PokemonData pokemonData = itemData.getPokemonData();
if (pokemonData.getPokemonId() == PokemonId.MISSINGNO && pokemonData.getIsEgg()) {
hatchery.addEgg(new EggPokemon(pokemonData));
}
if (pokemonData.getPokemonId() != PokemonId.MISSINGNO) {
pokebank.addPokemon(new Pokemon(api, pokemonData));
}
}
if (itemData.hasItem()) {
ItemData item = itemData.getItem();
if (item.getCount() > 0) {
itemBag.addItem(new Item(api, item, itemBag));
}
}
if (itemData.hasCandy()) {
Candy candy = itemData.getCandy();
if (candy.getFamilyId() != PokemonFamilyId.UNRECOGNIZED && candy.getFamilyId() != PokemonFamilyId.FAMILY_UNSET) {
candyjar.setCandy(candy.getFamilyId(), candy.getCandy());
}
}
// player stats
if (itemData.hasPlayerStats()) {
api.playerProfile.setStats(new Stats(itemData.getPlayerStats()));
}
// pokedex
if (itemData.hasPokedexEntry()) {
pokedex.add(itemData.getPokedexEntry());
}
if (itemData.hasEggIncubators()) {
EggIncubators eggIncubators = itemData.getEggIncubators();
for (EggIncubatorOuterClass.EggIncubator incubator : eggIncubators.getEggIncubatorList()) {
EggIncubator eggIncubator = new EggIncubator(api, incubator);
synchronized (this.lock) {
incubators.remove(eggIncubator);
incubators.add(eggIncubator);
}
}
}
if (itemData.hasAppliedItems()) {
AppliedItems appliedItems = itemData.getAppliedItems();
for (AppliedItem appliedItem : appliedItems.getItemList()) {
this.appliedItems.put(appliedItem.getItemId(), appliedItem);
}
}
Set<ItemId> stale = new HashSet<>();
for (Map.Entry<ItemId, AppliedItem> entry : appliedItems.entrySet()) {
ItemId itemId = entry.getKey();
AppliedItem applied = entry.getValue();
if (api.currentTimeMillis() >= applied.getExpireMs()) {
stale.add(itemId);
} else {
Item item = itemBag.getItem(itemId);
item.setApplied(applied);
itemBag.addItem(item);
}
}
for (ItemId item : stale) {
appliedItems.remove(item);
itemBag.getItem(item).removeApplied();
}
}
}
use of POGOProtos.Inventory.InventoryItemDataOuterClass.InventoryItemData 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());
}
}
ServerRequest releaseRequest = new ServerRequest(RequestType.RELEASE_POKEMON, releaseBuilder.build());
ServerRequestEnvelope envelope = ServerRequestEnvelope.createCommons(releaseRequest, api);
Map<PokemonFamilyId, Integer> lastCandies = new HashMap<>(api.inventories.candyjar.getCandies());
ServerResponse response = api.requestHandler.sendServerRequests(envelope);
try {
ByteString inventoryData = response.get(RequestType.GET_HOLOHOLO_INVENTORY);
GetHoloInventoryResponse inventoryResponse = GetHoloInventoryResponse.parseFrom(inventoryData);
ReleasePokemonResponse releaseResponse = ReleasePokemonResponse.parseFrom(releaseRequest.getData());
Map<PokemonFamilyId, Integer> candyCount = new HashMap<>();
if (releaseResponse.getResult() == Result.SUCCESS && inventoryResponse.getSuccess()) {
synchronized (this.lock) {
this.pokemons.removeAll(Arrays.asList(releasePokemon));
}
for (Pokemon pokemon : releasePokemon) {
api.inventories.pokebank.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.inventories.updateInventories(inventoryResponse);
}
return candyCount;
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
}
Aggregations