Skip to content

Commit

Permalink
Merge branch 'Sadye-issue132'
Browse files Browse the repository at this point in the history
  • Loading branch information
Grover-c13 committed Jul 23, 2016
2 parents df13856 + 4e9042f commit f7afed6
Show file tree
Hide file tree
Showing 7 changed files with 343 additions and 69 deletions.
91 changes: 91 additions & 0 deletions src/main/java/com/pokegoapi/api/inventory/EggIncubator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.pokegoapi.api.inventory;

import POGOProtos.Inventory.EggIncubatorOuterClass;
import POGOProtos.Networking.Requests.Messages.UseItemEggIncubatorMessageOuterClass.UseItemEggIncubatorMessage;
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
import POGOProtos.Networking.Responses.UseItemEggIncubatorResponseOuterClass.UseItemEggIncubatorResponse;
import com.google.protobuf.InvalidProtocolBufferException;
import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.api.pokemon.EggPokemon;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.main.ServerRequest;
import lombok.Getter;

public class EggIncubator {
private final EggIncubatorOuterClass.EggIncubator proto;
private final PokemonGo pgo;
@Getter
private boolean inUse = false;

/**
* Create new EggIncubator with given proto.
*
* @param pgo the api
* @param proto the proto
*/
public EggIncubator(PokemonGo pgo, EggIncubatorOuterClass.EggIncubator proto) {
this.pgo = pgo;
this.proto = proto;
this.inUse = proto.getPokemonId() != 0;
}

/**
* Returns the remaining uses.
*
* @return uses remaining
*/
public int getUsesRemaining() {
return proto.getUsesRemaining();
}

/**
* Hatch an egg.
*
* @param egg the egg
* @return status of putting egg in incubator
* @throws RemoteServerException the remote server exception
* @throws LoginFailedException the login failed exception
*/
public UseItemEggIncubatorResponse.Result hatchEgg(EggPokemon egg)
throws LoginFailedException, RemoteServerException {
if (!egg.getIsEgg()) {
return null;
}
UseItemEggIncubatorMessage reqMsg = UseItemEggIncubatorMessage.newBuilder()
.setItemId(proto.getId())
.setPokemonId(egg.getId())
.build();

ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.USE_ITEM_EGG_INCUBATOR, reqMsg);
pgo.getRequestHandler().sendServerRequests(serverRequest);

UseItemEggIncubatorResponse response;
try {
response = UseItemEggIncubatorResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RemoteServerException(e);
}

pgo.getInventories().updateInventories(true);

this.inUse = true;

return response.getResult();
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/pokegoapi/api/inventory/Hatchery.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@

package com.pokegoapi.api.inventory;

import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.api.pokemon.EggPokemon;
import com.pokegoapi.api.pokemon.Pokemon;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Hatchery {
@Getter
Set<EggPokemon> eggs = new HashSet<EggPokemon>();
@Getter
PokemonGo instance;

public Hatchery(PokemonGo instance) {
this.instance = instance;
}

public void addEgg(EggPokemon egg) {
eggs.add(egg);
}

}
45 changes: 38 additions & 7 deletions src/main/java/com/pokegoapi/api/inventory/Inventories.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
package com.pokegoapi.api.inventory;

import POGOProtos.Enums.PokemonFamilyIdOuterClass;
import POGOProtos.Enums.PokemonIdOuterClass;
import POGOProtos.Enums.PokemonIdOuterClass.PokemonId;
import POGOProtos.Inventory.EggIncubatorOuterClass;
import POGOProtos.Inventory.InventoryItemDataOuterClass;
import POGOProtos.Inventory.InventoryItemOuterClass;
import POGOProtos.Inventory.Item.ItemDataOuterClass.ItemData;
Expand All @@ -26,12 +27,16 @@
import POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse;
import com.google.protobuf.InvalidProtocolBufferException;
import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.api.pokemon.EggPokemon;
import com.pokegoapi.api.pokemon.Pokemon;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.main.ServerRequest;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;


public class Inventories {

Expand All @@ -44,6 +49,10 @@ public class Inventories {
private CandyJar candyjar;
@Getter
private Pokedex pokedex;
@Getter
private List<EggIncubator> incubators;
@Getter
private Hatchery hatchery;

private long lastInventoryUpdate = 0;

Expand All @@ -60,6 +69,8 @@ public Inventories(PokemonGo api) throws LoginFailedException, RemoteServerExcep
pokebank = new PokeBank(api);
candyjar = new CandyJar(api);
pokedex = new Pokedex(api);
incubators = new ArrayList<>();
hatchery = new Hatchery(api);
updateInventories();
}

Expand Down Expand Up @@ -87,6 +98,8 @@ public void updateInventories(boolean forceUpdate) throws LoginFailedException,
pokebank = new PokeBank(api);
candyjar = new CandyJar(api);
pokedex = new Pokedex(api);
incubators = new ArrayList<>();
hatchery = new Hatchery(api);
}
GetInventoryMessage invReqMsg = GetInventoryMessage.newBuilder()
.setLastTimestampMs(lastInventoryUpdate)
Expand All @@ -98,36 +111,54 @@ public void updateInventories(boolean forceUpdate) throws LoginFailedException,
try {
response = GetInventoryResponse.parseFrom(inventoryRequest.getData());
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
throw new RemoteServerException(e);
}

for (InventoryItemOuterClass.InventoryItem inventoryItem
: response.getInventoryDelta().getInventoryItemsList()) {
InventoryItemDataOuterClass.InventoryItemData itemData = inventoryItem.getInventoryItemData();

if (itemData.getPokemonData().getPokemonId() != PokemonIdOuterClass.PokemonId.MISSINGNO) {
// hatchery
if (itemData.getPokemonData().getPokemonId() == PokemonId.MISSINGNO && itemData.getPokemonData().getIsEgg()) {
hatchery.addEgg(new EggPokemon(itemData.getPokemonData()));
}

// pokebank
if (itemData.getPokemonData().getPokemonId() != PokemonId.MISSINGNO) {
pokebank.addPokemon(new Pokemon(inventoryItem.getInventoryItemData().getPokemonData()));
}

// items
if (itemData.getItem().getItemId() != ItemId.UNRECOGNIZED
&& itemData.getItem().getItemId() != ItemId.ITEM_UNKNOWN) {
ItemData item = inventoryItem.getInventoryItemData().getItem();
ItemData item = itemData.getItem();
itemBag.addItem(new Item(item));
}

// candyjar
if (itemData.getPokemonFamily().getFamilyId() != PokemonFamilyIdOuterClass.PokemonFamilyId.UNRECOGNIZED
&& itemData.getPokemonFamily().getFamilyId() != PokemonFamilyIdOuterClass.PokemonFamilyId.FAMILY_UNSET) {
candyjar.setCandy(
inventoryItem.getInventoryItemData().getPokemonFamily().getFamilyId(),
inventoryItem.getInventoryItemData().getPokemonFamily().getCandy()
itemData.getPokemonFamily().getFamilyId(),
itemData.getPokemonFamily().getCandy()
);
}
// player stats
if (itemData.hasPlayerStats()) {
api.getPlayerProfile().setStats(inventoryItem.getInventoryItemData().getPlayerStats());
api.getPlayerProfile().setStats(itemData.getPlayerStats());
}

// pokedex
if (itemData.hasPokedexEntry()) {
pokedex.add(itemData.getPokedexEntry());
}

if (itemData.hasEggIncubators()) {
for (EggIncubatorOuterClass.EggIncubator incubator : itemData.getEggIncubators().getEggIncubatorList()) {
incubators.add(new EggIncubator(api, incubator));
}
}

lastInventoryUpdate = System.currentTimeMillis();
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/com/pokegoapi/api/inventory/PokeBank.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ public boolean test(Pokemon testPokemon) {
}
}



/**
* Gets pokemon by pokemon id.
*
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/pokegoapi/api/map/Map.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import POGOProtos.Networking.Responses.FortDetailsResponseOuterClass;
import POGOProtos.Networking.Responses.FortSearchResponseOuterClass.FortSearchResponse;
import POGOProtos.Networking.Responses.GetMapObjectsResponseOuterClass;

import com.annimon.stream.Collectors;
import com.annimon.stream.Stream;
import com.annimon.stream.function.Function;
Expand All @@ -49,11 +50,14 @@
import com.pokegoapi.google.common.geometry.S2CellId;
import com.pokegoapi.google.common.geometry.S2LatLng;
import com.pokegoapi.main.ServerRequest;

import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class Map {
Expand Down Expand Up @@ -93,7 +97,7 @@ public void clearCache() {
* @return a List of CatchablePokemon at your current location
*/
public List<CatchablePokemon> getCatchablePokemon() throws LoginFailedException, RemoteServerException {
List<CatchablePokemon> catchablePokemons = new ArrayList<>();
Set<CatchablePokemon> catchablePokemons = new HashSet<>();
MapObjects objects = getMapObjects();

for (MapPokemon mapPokemon : objects.getCatchablePokemons()) {
Expand All @@ -111,7 +115,7 @@ public List<CatchablePokemon> getCatchablePokemon() throws LoginFailedException,
}
}*/

return catchablePokemons;
return new ArrayList<>(catchablePokemons);
}

/**
Expand Down
Loading

0 comments on commit f7afed6

Please sign in to comment.