Skip to content

Commit 19eec40

Browse files
authored
General improvements and fixes (#820)
* Improve common requests, add new common request handling, add medal and buddy handling & new listeners for buddies, medals and level ups * Fix PokemonGo#checkChallenge accepting empty challenge URLs * Add path utils and example to move from one point to another at the given speed * Fix multiple captchas being received
1 parent a80d422 commit 19eec40

23 files changed

+804
-156
lines changed

library/src/main/java/com/pokegoapi/api/PokemonGo.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
2424
import POGOProtos.Networking.Requests.RequestTypeOuterClass.RequestType;
2525
import POGOProtos.Networking.Responses.CheckChallengeResponseOuterClass.CheckChallengeResponse;
26-
import POGOProtos.Networking.Responses.DownloadSettingsResponseOuterClass.DownloadSettingsResponse;
27-
import POGOProtos.Networking.Responses.GetInventoryResponseOuterClass.GetInventoryResponse;
2826
import POGOProtos.Networking.Responses.VerifyChallengeResponseOuterClass.VerifyChallengeResponse;
2927
import com.google.protobuf.ByteString;
3028
import com.google.protobuf.InvalidProtocolBufferException;
@@ -36,6 +34,7 @@
3634
import com.pokegoapi.api.listener.Listener;
3735
import com.pokegoapi.api.listener.LoginListener;
3836
import com.pokegoapi.api.map.Map;
37+
import com.pokegoapi.api.map.Point;
3938
import com.pokegoapi.api.player.PlayerProfile;
4039
import com.pokegoapi.api.settings.Settings;
4140
import com.pokegoapi.auth.CredentialProvider;
@@ -188,9 +187,9 @@ public void login(CredentialProvider credentialProvider)
188187
}
189188
this.credentialProvider = credentialProvider;
190189
startTime = currentTimeMillis();
191-
playerProfile = new PlayerProfile(this);
192-
settings = new Settings(this);
193190
inventories = new Inventories(this);
191+
settings = new Settings(this);
192+
playerProfile = new PlayerProfile(this);
194193

195194
initialize();
196195

@@ -247,15 +246,7 @@ private void initialize() throws RemoteServerException, CaptchaActiveException,
247246
*/
248247
private void fireRequestBlock(ServerRequest request)
249248
throws RemoteServerException, CaptchaActiveException, LoginFailedException {
250-
ServerRequest[] requests = CommonRequests.fillRequest(request, this);
251-
252-
getRequestHandler().sendServerRequests(requests);
253-
try {
254-
inventories.updateInventories(GetInventoryResponse.parseFrom(requests[3].getData()));
255-
settings.updateSettings(DownloadSettingsResponse.parseFrom(requests[5].getData()));
256-
} catch (InvalidProtocolBufferException e) {
257-
throw new RemoteServerException();
258-
}
249+
getRequestHandler().sendServerRequests(request.withCommons());
259250
}
260251

261252
/**
@@ -540,10 +531,17 @@ public String checkChallenge()
540531
AsyncHelper.toBlocking(getRequestHandler().sendAsyncServerRequests(request));
541532
CheckChallengeResponse response = CheckChallengeResponse.parseFrom(responseData);
542533
String newChallenge = response.getChallengeUrl();
543-
if (newChallenge != null && newChallenge.length() > 0) {
534+
if (response.getShowChallenge() && newChallenge != null && newChallenge.length() > 0) {
544535
updateChallenge(newChallenge, true);
545536
return newChallenge;
546537
}
547538
return null;
548539
}
540+
541+
/**
542+
* @return the current player position in Point form
543+
*/
544+
public Point getPoint() {
545+
return new Point(this.getLatitude(), this.getLongitude());
546+
}
549547
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.pokegoapi.api.listener;
2+
3+
import com.pokegoapi.api.PokemonGo;
4+
import com.pokegoapi.api.player.Medal;
5+
import com.pokegoapi.api.player.PlayerProfile;
6+
7+
/**
8+
* Listener for all player related events.
9+
*/
10+
public interface PlayerListener extends Listener {
11+
/**
12+
* Called when the player levels up
13+
* @param api the current api instance
14+
* @param oldLevel the old player level
15+
* @param newLevel the new player level
16+
* @return true if you want to accept level up rewards
17+
*/
18+
boolean onLevelUp(PokemonGo api, int oldLevel, int newLevel);
19+
20+
/**
21+
* Called when a new medal is awarded or leveled up for the current player
22+
* @param api the current api
23+
* @param profile the player receiving this medal
24+
* @param medal the medal awarded
25+
*/
26+
void onMedalAwarded(PokemonGo api, PlayerProfile profile, Medal medal);
27+
}

library/src/main/java/com/pokegoapi/api/listener/PokemonListener.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.pokegoapi.api.listener;
22

33
import POGOProtos.Enums.EncounterTypeOuterClass.EncounterType;
4+
import POGOProtos.Enums.PokemonFamilyIdOuterClass.PokemonFamilyId;
45
import com.pokegoapi.api.PokemonGo;
56
import com.pokegoapi.api.inventory.Pokeball;
67
import com.pokegoapi.api.map.pokemon.CatchablePokemon;
@@ -36,4 +37,12 @@ public interface PokemonListener extends Listener {
3637
* @return true to abort the capture and false to retry
3738
*/
3839
boolean onCatchEscape(PokemonGo api, CatchablePokemon pokemon, Pokeball pokeball, int throwCount);
40+
41+
/**
42+
* Called when your buddy pokemon finds candies
43+
* @param api the current api
44+
* @param family the candy family type
45+
* @param candyCount the amount of candies found
46+
*/
47+
void onBuddyFindCandy(PokemonGo api, PokemonFamilyId family, int candyCount);
3948
}

library/src/main/java/com/pokegoapi/api/map/Point.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ public Point(SpawnPointOuterClass.SpawnPoint spawnpoint) {
3939
this.latitude = spawnpoint.getLatitude();
4040
this.longitude = spawnpoint.getLongitude();
4141
}
42+
43+
@Override
44+
public String toString() {
45+
return this.latitude + ", " + this.longitude;
46+
}
4247
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
package com.pokegoapi.api.player;
17+
18+
import POGOProtos.Data.PlayerBadgeOuterClass.PlayerBadge;
19+
import POGOProtos.Enums.BadgeTypeOuterClass.BadgeType;
20+
import lombok.Getter;
21+
import lombok.Setter;
22+
23+
public class Medal {
24+
@Getter
25+
@Setter
26+
private int rank;
27+
@Getter
28+
private BadgeType type;
29+
30+
@Getter
31+
private final int startValue;
32+
@Getter
33+
private final double currentValue;
34+
@Getter
35+
private final int endValue;
36+
37+
/**
38+
* Creates a Medal with a PlayerBadge proto
39+
* @param badge the proto to inititialize with
40+
*/
41+
public Medal(PlayerBadge badge) {
42+
this.type = badge.getBadgeType();
43+
this.rank = badge.getRank();
44+
this.startValue = badge.getStartValue();
45+
this.currentValue = badge.getCurrentValue();
46+
this.endValue = badge.getEndValue();
47+
}
48+
49+
@Override
50+
public int hashCode() {
51+
return type.getNumber();
52+
}
53+
54+
@Override
55+
public boolean equals(Object obj) {
56+
return obj instanceof Medal && ((Medal) obj).type.equals(type);
57+
}
58+
}

0 commit comments

Comments
 (0)