Skip to content

Commit 1ac8fa3

Browse files
committed
add query from hatched eggs + rewrite stuff around EggPokemon
1 parent 2e14fab commit 1ac8fa3

File tree

4 files changed

+87
-9
lines changed

4 files changed

+87
-9
lines changed

src/main/java/com/pokegoapi/api/inventory/EggIncubator.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ public int getUsesRemaining() {
6464
*/
6565
public UseItemEggIncubatorResponse.Result hatchEgg(EggPokemon egg)
6666
throws LoginFailedException, RemoteServerException {
67-
if (!egg.getIsEgg()) {
68-
return null;
69-
}
67+
7068
UseItemEggIncubatorMessage reqMsg = UseItemEggIncubatorMessage.newBuilder()
7169
.setItemId(proto.getId())
7270
.setPokemonId(egg.getId())

src/main/java/com/pokegoapi/api/inventory/Hatchery.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@
1515

1616
package com.pokegoapi.api.inventory;
1717

18+
import POGOProtos.Networking.Requests.Messages.GetHatchedEggsMessageOuterClass.GetHatchedEggsMessage;
19+
import POGOProtos.Networking.Requests.RequestTypeOuterClass.RequestType;
20+
import POGOProtos.Networking.Responses.GetHatchedEggsResponseOuterClass.GetHatchedEggsResponse;
21+
import com.google.protobuf.InvalidProtocolBufferException;
1822
import com.pokegoapi.api.PokemonGo;
1923
import com.pokegoapi.api.pokemon.EggPokemon;
20-
import com.pokegoapi.api.pokemon.Pokemon;
24+
import com.pokegoapi.api.pokemon.HatchedEgg;
25+
import com.pokegoapi.exceptions.LoginFailedException;
26+
import com.pokegoapi.exceptions.RemoteServerException;
27+
import com.pokegoapi.main.ServerRequest;
28+
2129
import lombok.Getter;
2230

2331
import java.util.ArrayList;
@@ -38,5 +46,34 @@ public Hatchery(PokemonGo instance) {
3846
public void addEgg(EggPokemon egg) {
3947
eggs.add(egg);
4048
}
49+
50+
51+
/**
52+
* Get if eggs has hatched.
53+
*
54+
* @return list of hatched eggs
55+
* @throws RemoteServerException e
56+
* @throws LoginFailedException e
57+
*/
58+
public List<HatchedEgg> queryHatchedEggs() throws RemoteServerException, LoginFailedException {
59+
GetHatchedEggsMessage msg = GetHatchedEggsMessage.newBuilder().build();
60+
ServerRequest serverRequest = new ServerRequest(RequestType.GET_HATCHED_EGGS, msg);
61+
instance.getRequestHandler().sendServerRequests(serverRequest);
62+
63+
GetHatchedEggsResponse response = null;
64+
try {
65+
response = GetHatchedEggsResponse.parseFrom(serverRequest.getData());
66+
} catch (InvalidProtocolBufferException e) {
67+
throw new RemoteServerException(e);
68+
}
69+
List<HatchedEgg> eggs = new ArrayList<HatchedEgg>();
70+
for (int i = 0; i < response.getPokemonIdCount(); i++) {
71+
eggs.add(new HatchedEgg(response.getPokemonId(i),
72+
response.getExperienceAwarded(i),
73+
response.getCandyAwarded(i),
74+
response.getStardustAwarded(i)));
75+
}
76+
return eggs;
77+
}
4178

4279
}

src/main/java/com/pokegoapi/api/pokemon/EggPokemon.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
package com.pokegoapi.api.pokemon;
1717

1818
import POGOProtos.Data.PokemonDataOuterClass.PokemonData;
19+
import POGOProtos.Networking.Responses.UseItemEggIncubatorResponseOuterClass.UseItemEggIncubatorResponse;
20+
1921
import com.pokegoapi.api.PokemonGo;
22+
import com.pokegoapi.api.inventory.EggIncubator;
23+
import com.pokegoapi.exceptions.LoginFailedException;
24+
import com.pokegoapi.exceptions.RemoteServerException;
25+
2026
import lombok.Setter;
2127

2228
/**
@@ -30,20 +36,39 @@ public class EggPokemon {
3036
private PokemonData proto;
3137

3238
// API METHODS //
39+
/**
40+
* Incubate this egg.
41+
*
42+
* @param incubator : the incubator
43+
* @return status of putting egg in incubator
44+
* @throws LoginFailedException e
45+
* @throws RemoteServerException e
46+
*/
47+
public UseItemEggIncubatorResponse.Result incubate(EggIncubator incubator)
48+
throws LoginFailedException, RemoteServerException {
49+
if (incubator.isInUse()) {
50+
throw new IllegalArgumentException("Incubator already used");
51+
}
52+
return incubator.hatchEgg(this);
53+
}
3354

3455
// DELEGATE METHODS BELOW //
56+
/**
57+
* Build a EggPokemon wrapper from the proto.
58+
*
59+
* @param proto : the prototype
60+
*/
3561
public EggPokemon(PokemonData proto) {
62+
if (!proto.getIsEgg()) {
63+
throw new IllegalArgumentException("You cant build a EggPokemon without a valid PokemonData.");
64+
}
3665
this.proto = proto;
3766
}
3867

3968
public long getId() {
4069
return proto.getId();
4170
}
42-
43-
public boolean getIsEgg() {
44-
return proto.getIsEgg();
45-
}
46-
71+
4772
public double getEggKmWalkedTarget() {
4873
return proto.getEggKmWalkedTarget();
4974
}
@@ -63,6 +88,10 @@ public long getCreationTimeMs() {
6388
public String getEggIncubatorId() {
6489
return proto.getEggIncubatorId();
6590
}
91+
92+
public boolean isIncubate() {
93+
return proto.getEggIncubatorId().length() > 0;
94+
}
6695

6796
@Override
6897
public int hashCode() {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.pokegoapi.api.pokemon;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
8+
public class HatchedEgg {
9+
10+
private Long id;
11+
private int experience;
12+
private int candy;
13+
private int stardust;
14+
}

0 commit comments

Comments
 (0)