Search in sources :

Example 1 with InventoryItemData

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();
        }
    }
}
Also used : InventoryItem(POGOProtos.Inventory.InventoryItemOuterClass.InventoryItem) EggIncubators(POGOProtos.Inventory.EggIncubatorsOuterClass.EggIncubators) EggIncubatorOuterClass(POGOProtos.Inventory.EggIncubatorOuterClass) ItemId(POGOProtos.Inventory.Item.ItemIdOuterClass.ItemId) AppliedItem(POGOProtos.Inventory.AppliedItemOuterClass.AppliedItem) InventoryItem(POGOProtos.Inventory.InventoryItemOuterClass.InventoryItem) EggPokemon(com.pokegoapi.api.pokemon.EggPokemon) Candy(POGOProtos.Inventory.CandyOuterClass.Candy) AppliedItems(POGOProtos.Inventory.AppliedItemsOuterClass.AppliedItems) InventoryItemData(POGOProtos.Inventory.InventoryItemDataOuterClass.InventoryItemData) EggPokemon(com.pokegoapi.api.pokemon.EggPokemon) Pokemon(com.pokegoapi.api.pokemon.Pokemon) PokemonData(POGOProtos.Data.PokemonDataOuterClass.PokemonData) HashMap(java.util.HashMap) Map(java.util.Map) AppliedItem(POGOProtos.Inventory.AppliedItemOuterClass.AppliedItem) ItemData(POGOProtos.Inventory.Item.ItemDataOuterClass.ItemData) InventoryItemData(POGOProtos.Inventory.InventoryItemDataOuterClass.InventoryItemData) HashSet(java.util.HashSet)

Example 2 with InventoryItemData

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);
    }
}
Also used : ServerResponse(com.pokegoapi.main.ServerResponse) InventoryItem(POGOProtos.Inventory.InventoryItemOuterClass.InventoryItem) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) PokemonFamilyId(POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId) ServerRequestEnvelope(com.pokegoapi.main.ServerRequestEnvelope) RequestFailedException(com.pokegoapi.exceptions.request.RequestFailedException) ReleasePokemonMessage(POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage) Candy(POGOProtos.Inventory.CandyOuterClass.Candy) ReleasePokemonResponse(POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse) Pokemon(com.pokegoapi.api.pokemon.Pokemon) ServerRequest(com.pokegoapi.main.ServerRequest) InventoryItemData(POGOProtos.Inventory.InventoryItemDataOuterClass.InventoryItemData) GetHoloInventoryResponse(POGOProtos.Networking.Responses.GetHoloInventoryResponseOuterClass.GetHoloInventoryResponse)

Aggregations

Candy (POGOProtos.Inventory.CandyOuterClass.Candy)2 InventoryItemData (POGOProtos.Inventory.InventoryItemDataOuterClass.InventoryItemData)2 InventoryItem (POGOProtos.Inventory.InventoryItemOuterClass.InventoryItem)2 Pokemon (com.pokegoapi.api.pokemon.Pokemon)2 HashMap (java.util.HashMap)2 PokemonData (POGOProtos.Data.PokemonDataOuterClass.PokemonData)1 PokemonFamilyId (POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId)1 AppliedItem (POGOProtos.Inventory.AppliedItemOuterClass.AppliedItem)1 AppliedItems (POGOProtos.Inventory.AppliedItemsOuterClass.AppliedItems)1 EggIncubatorOuterClass (POGOProtos.Inventory.EggIncubatorOuterClass)1 EggIncubators (POGOProtos.Inventory.EggIncubatorsOuterClass.EggIncubators)1 ItemData (POGOProtos.Inventory.Item.ItemDataOuterClass.ItemData)1 ItemId (POGOProtos.Inventory.Item.ItemIdOuterClass.ItemId)1 ReleasePokemonMessage (POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage)1 GetHoloInventoryResponse (POGOProtos.Networking.Responses.GetHoloInventoryResponseOuterClass.GetHoloInventoryResponse)1 ReleasePokemonResponse (POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse)1 ByteString (com.google.protobuf.ByteString)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 EggPokemon (com.pokegoapi.api.pokemon.EggPokemon)1 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)1