Thanks to visit codestin.com
Credit goes to github.com

Skip to content

allow to hatch eggs; fully tested #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
26 changes: 20 additions & 6 deletions src/main/java/com/pokegoapi/api/inventory/Inventories.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +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 @@ -34,6 +34,9 @@
import com.pokegoapi.main.ServerRequest;
import lombok.Getter;

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


public class Inventories {

Expand All @@ -47,6 +50,8 @@ public class Inventories {
@Getter
private Pokedex pokedex;
@Getter
private List<EggIncubator> incubators;
@Getter
private Hatchery hatchery;

private long lastInventoryUpdate = 0;
Expand All @@ -64,6 +69,7 @@ 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 @@ -92,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 @@ -103,7 +111,7 @@ 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
Expand All @@ -123,28 +131,34 @@ public void updateInventories(boolean forceUpdate) throws LoginFailedException,
// 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
38 changes: 4 additions & 34 deletions src/main/java/com/pokegoapi/api/pokemon/EggPokemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,16 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/*
* 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.pokemon;

import POGOProtos.Data.PokemonDataOuterClass.PokemonData;
import POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId;
import POGOProtos.Enums.PokemonIdOuterClass;
import POGOProtos.Enums.PokemonMoveOuterClass;
import POGOProtos.Inventory.Item.ItemIdOuterClass.ItemId;
import POGOProtos.Networking.Requests.Messages.EvolvePokemonMessageOuterClass.EvolvePokemonMessage;
import POGOProtos.Networking.Requests.Messages.NicknamePokemonMessageOuterClass.NicknamePokemonMessage;
import POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage;
import POGOProtos.Networking.Requests.RequestTypeOuterClass.RequestType;
import POGOProtos.Networking.Responses.EvolvePokemonResponseOuterClass.EvolvePokemonResponse;
import POGOProtos.Networking.Responses.NicknamePokemonResponseOuterClass.NicknamePokemonResponse;
import POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse;
import POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse.Result;
import com.google.protobuf.InvalidProtocolBufferException;
import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.api.map.pokemon.EvolutionResult;
import com.pokegoapi.exceptions.LoginFailedException;
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.main.ServerRequest;
import com.pokegoapi.util.Log;
import lombok.Setter;

/**
* The egg pokemon.
*/
public class EggPokemon {
public class EggPokemon {

private static final String TAG = EggPokemon.class.getSimpleName();
@Setter
Expand Down Expand Up @@ -93,6 +60,9 @@ public long getCreationTimeMs() {
return proto.getCreationTimeMs();
}

public String getEggIncubatorId() {
return proto.getEggIncubatorId();
}

@Override
public int hashCode() {
Expand Down