diff --git a/src/main/java/com/pokegoapi/api/inventory/EggIncubator.java b/src/main/java/com/pokegoapi/api/inventory/EggIncubator.java index 1521fa89..3b8c4acc 100644 --- a/src/main/java/com/pokegoapi/api/inventory/EggIncubator.java +++ b/src/main/java/com/pokegoapi/api/inventory/EggIncubator.java @@ -64,9 +64,7 @@ public int getUsesRemaining() { */ public UseItemEggIncubatorResponse.Result hatchEgg(EggPokemon egg) throws LoginFailedException, RemoteServerException { - if (!egg.getIsEgg()) { - return null; - } + UseItemEggIncubatorMessage reqMsg = UseItemEggIncubatorMessage.newBuilder() .setItemId(proto.getId()) .setPokemonId(egg.getId()) diff --git a/src/main/java/com/pokegoapi/api/inventory/Hatchery.java b/src/main/java/com/pokegoapi/api/inventory/Hatchery.java index 22b3cd89..19236139 100644 --- a/src/main/java/com/pokegoapi/api/inventory/Hatchery.java +++ b/src/main/java/com/pokegoapi/api/inventory/Hatchery.java @@ -15,9 +15,17 @@ package com.pokegoapi.api.inventory; +import POGOProtos.Networking.Requests.Messages.GetHatchedEggsMessageOuterClass.GetHatchedEggsMessage; +import POGOProtos.Networking.Requests.RequestTypeOuterClass.RequestType; +import POGOProtos.Networking.Responses.GetHatchedEggsResponseOuterClass.GetHatchedEggsResponse; +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.api.pokemon.HatchedEgg; +import com.pokegoapi.exceptions.LoginFailedException; +import com.pokegoapi.exceptions.RemoteServerException; +import com.pokegoapi.main.ServerRequest; + import lombok.Getter; import java.util.ArrayList; @@ -38,5 +46,34 @@ public Hatchery(PokemonGo instance) { public void addEgg(EggPokemon egg) { eggs.add(egg); } + + + /** + * Get if eggs has hatched. + * + * @return list of hatched eggs + * @throws RemoteServerException e + * @throws LoginFailedException e + */ + public List queryHatchedEggs() throws RemoteServerException, LoginFailedException { + GetHatchedEggsMessage msg = GetHatchedEggsMessage.newBuilder().build(); + ServerRequest serverRequest = new ServerRequest(RequestType.GET_HATCHED_EGGS, msg); + instance.getRequestHandler().sendServerRequests(serverRequest); + + GetHatchedEggsResponse response = null; + try { + response = GetHatchedEggsResponse.parseFrom(serverRequest.getData()); + } catch (InvalidProtocolBufferException e) { + throw new RemoteServerException(e); + } + List eggs = new ArrayList(); + for (int i = 0; i < response.getPokemonIdCount(); i++) { + eggs.add(new HatchedEgg(response.getPokemonId(i), + response.getExperienceAwarded(i), + response.getCandyAwarded(i), + response.getStardustAwarded(i))); + } + return eggs; + } } diff --git a/src/main/java/com/pokegoapi/api/pokemon/EggPokemon.java b/src/main/java/com/pokegoapi/api/pokemon/EggPokemon.java index 120744b6..4f611901 100644 --- a/src/main/java/com/pokegoapi/api/pokemon/EggPokemon.java +++ b/src/main/java/com/pokegoapi/api/pokemon/EggPokemon.java @@ -16,7 +16,13 @@ package com.pokegoapi.api.pokemon; import POGOProtos.Data.PokemonDataOuterClass.PokemonData; +import POGOProtos.Networking.Responses.UseItemEggIncubatorResponseOuterClass.UseItemEggIncubatorResponse; + import com.pokegoapi.api.PokemonGo; +import com.pokegoapi.api.inventory.EggIncubator; +import com.pokegoapi.exceptions.LoginFailedException; +import com.pokegoapi.exceptions.RemoteServerException; + import lombok.Setter; /** @@ -30,20 +36,39 @@ public class EggPokemon { private PokemonData proto; // API METHODS // + /** + * Incubate this egg. + * + * @param incubator : the incubator + * @return status of putting egg in incubator + * @throws LoginFailedException e + * @throws RemoteServerException e + */ + public UseItemEggIncubatorResponse.Result incubate(EggIncubator incubator) + throws LoginFailedException, RemoteServerException { + if (incubator.isInUse()) { + throw new IllegalArgumentException("Incubator already used"); + } + return incubator.hatchEgg(this); + } // DELEGATE METHODS BELOW // + /** + * Build a EggPokemon wrapper from the proto. + * + * @param proto : the prototype + */ public EggPokemon(PokemonData proto) { + if (!proto.getIsEgg()) { + throw new IllegalArgumentException("You cant build a EggPokemon without a valid PokemonData."); + } this.proto = proto; } public long getId() { return proto.getId(); } - - public boolean getIsEgg() { - return proto.getIsEgg(); - } - + public double getEggKmWalkedTarget() { return proto.getEggKmWalkedTarget(); } @@ -63,6 +88,10 @@ public long getCreationTimeMs() { public String getEggIncubatorId() { return proto.getEggIncubatorId(); } + + public boolean isIncubate() { + return proto.getEggIncubatorId().length() > 0; + } @Override public int hashCode() { diff --git a/src/main/java/com/pokegoapi/api/pokemon/HatchedEgg.java b/src/main/java/com/pokegoapi/api/pokemon/HatchedEgg.java new file mode 100644 index 00000000..396a4033 --- /dev/null +++ b/src/main/java/com/pokegoapi/api/pokemon/HatchedEgg.java @@ -0,0 +1,14 @@ +package com.pokegoapi.api.pokemon; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class HatchedEgg { + + private Long id; + private int experience; + private int candy; + private int stardust; +}