Skip to content

Commit b2c9f2a

Browse files
authored
Fix tutorial name selection and fix pokeballs not being decreased when catching pokemon (#835)
1 parent e068351 commit b2c9f2a

File tree

2 files changed

+59
-50
lines changed

2 files changed

+59
-50
lines changed

library/src/main/java/com/pokegoapi/api/map/pokemon/CatchablePokemon.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.google.protobuf.InvalidProtocolBufferException;
4141
import com.pokegoapi.api.PokemonGo;
4242
import com.pokegoapi.api.inventory.Item;
43+
import com.pokegoapi.api.inventory.ItemBag;
4344
import com.pokegoapi.api.inventory.Pokeball;
4445
import com.pokegoapi.api.listener.PokemonListener;
4546
import com.pokegoapi.api.map.pokemon.encounter.DiskEncounterResult;
@@ -530,10 +531,13 @@ public CatchResult catchPokemon(double normalizedHitPosition,
530531
int amount, int razberriesLimit)
531532
throws LoginFailedException, CaptchaActiveException, RemoteServerException {
532533

533-
Item razzberriesInventory = api.getInventories().getItemBag().getItem(ItemId.ITEM_RAZZ_BERRY);
534+
ItemBag itemBag = api.getInventories().getItemBag();
535+
Item razzberriesInventory = itemBag.getItem(ItemId.ITEM_RAZZ_BERRY);
534536
int razzberriesCountInventory = razzberriesInventory.getCount();
535537
int razberries = 0;
536538
int numThrows = 0;
539+
Item pokeballItem = itemBag.getItem(type.getBallType());
540+
int pokeballCount = pokeballItem.getCount();
537541
CatchResult result;
538542

539543
if (razzberriesCountInventory < razberriesLimit) {
@@ -556,6 +560,13 @@ && useItem(ItemId.ITEM_RAZZ_BERRY).getSuccess()) {
556560
break;
557561
}
558562

563+
if (result.getStatus() != CatchStatus.CATCH_ERROR) {
564+
pokeballItem.setCount(--pokeballCount);
565+
if (pokeballCount <= 0) {
566+
break;
567+
}
568+
}
569+
559570
// continue for the following cases:
560571
// CatchStatus.CATCH_ESCAPE
561572
// CatchStatus.CATCH_MISSED
@@ -589,6 +600,7 @@ && useItem(ItemId.ITEM_RAZZ_BERRY).getSuccess()) {
589600
}
590601

591602
numThrows++;
603+
592604
}
593605
while (amount < 0 || numThrows < amount);
594606

library/src/main/java/com/pokegoapi/api/player/PlayerProfile.java

+46-49
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ public class PlayerProfile {
9999

100100
/**
101101
* @param api the api
102-
* @throws LoginFailedException when the auth is invalid
103-
* @throws RemoteServerException when the server is down/having issues
102+
* @throws LoginFailedException when the auth is invalid
103+
* @throws RemoteServerException when the server is down/having issues
104104
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
105105
*/
106106
public PlayerProfile(PokemonGo api) throws LoginFailedException, CaptchaActiveException, RemoteServerException {
@@ -111,8 +111,8 @@ public PlayerProfile(PokemonGo api) throws LoginFailedException, CaptchaActiveEx
111111
/**
112112
* Updates the player profile with the latest data.
113113
*
114-
* @throws LoginFailedException when the auth is invalid
115-
* @throws RemoteServerException when the server is down/having issues
114+
* @throws LoginFailedException when the auth is invalid
115+
* @throws RemoteServerException when the server is down/having issues
116116
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
117117
*/
118118
public void updateProfile() throws RemoteServerException, CaptchaActiveException, LoginFailedException {
@@ -174,9 +174,9 @@ public void updateProfile(PlayerData playerData) {
174174
/**
175175
* Performs a GET_PLAYER_PROFILE request.
176176
*
177-
* @throws RemoteServerException if the server has an issue or an invalid request is sent
177+
* @throws RemoteServerException if the server has an issue or an invalid request is sent
178178
* @throws CaptchaActiveException if a captcha is active, and the message cannot be sent
179-
* @throws LoginFailedException if login fails
179+
* @throws LoginFailedException if login fails
180180
*/
181181
public void getProfile() throws RemoteServerException, CaptchaActiveException, LoginFailedException {
182182
GetPlayerProfileMessage profileMessage = GetPlayerProfileMessage.newBuilder()
@@ -208,9 +208,9 @@ public void getProfile() throws RemoteServerException, CaptchaActiveException, L
208208
*
209209
* @param level the trainer level that you want to accept the rewards for
210210
* @return a PlayerLevelUpRewards object containing information about the items rewarded and unlocked for this level
211-
* @throws LoginFailedException when the auth is invalid
212-
* @throws RemoteServerException when the server is down/having issues
213-
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
211+
* @throws LoginFailedException when the auth is invalid
212+
* @throws RemoteServerException when the server is down/having issues
213+
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
214214
* @throws InsufficientLevelException if you have not yet reached the desired level
215215
* @see PlayerLevelUpRewards
216216
*/
@@ -241,7 +241,7 @@ public PlayerLevelUpRewards acceptLevelUpRewards(int level)
241241
/**
242242
* Add currency.
243243
*
244-
* @param name the name
244+
* @param name the name
245245
* @param amount the amount
246246
* @throws InvalidCurrencyException the invalid currency exception
247247
*/
@@ -258,8 +258,8 @@ public void addCurrency(String name, int amount) throws InvalidCurrencyException
258258
/**
259259
* Check and equip badges.
260260
*
261-
* @throws LoginFailedException when the auth is invalid
262-
* @throws RemoteServerException When a buffer exception is thrown
261+
* @throws LoginFailedException when the auth is invalid
262+
* @throws RemoteServerException When a buffer exception is thrown
263263
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
264264
* @deprecated use getMedals, which uses common requests to check for badges
265265
*/
@@ -297,8 +297,8 @@ public int getCurrency(Currency currency) {
297297
*
298298
* @param response the response to get badges from
299299
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
300-
* @throws LoginFailedException if login fails
301-
* @throws RemoteServerException if the server has an issue
300+
* @throws LoginFailedException if login fails
301+
* @throws RemoteServerException if the server has an issue
302302
*/
303303
public void updateAwardedMedals(CheckAwardedBadgesResponse response)
304304
throws CaptchaActiveException, LoginFailedException, RemoteServerException {
@@ -432,8 +432,8 @@ public boolean hasBuddy() {
432432
*
433433
* @param pokemon the pokemon to set as your buddy
434434
* @return if this task was successfull
435-
* @throws LoginFailedException when the auth is invalid
436-
* @throws RemoteServerException when the server is down/having issues
435+
* @throws LoginFailedException when the auth is invalid
436+
* @throws RemoteServerException when the server is down/having issues
437437
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
438438
*/
439439
public boolean setBuddy(Pokemon pokemon) throws CaptchaActiveException, LoginFailedException, RemoteServerException {
@@ -454,8 +454,8 @@ public boolean setBuddy(Pokemon pokemon) throws CaptchaActiveException, LoginFai
454454
/**
455455
* Set the account to legal screen in order to receive valid response
456456
*
457-
* @throws LoginFailedException when the auth is invalid
458-
* @throws RemoteServerException when the server is down/having issues
457+
* @throws LoginFailedException when the auth is invalid
458+
* @throws RemoteServerException when the server is down/having issues
459459
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
460460
*/
461461
public void activateAccount() throws LoginFailedException, CaptchaActiveException, RemoteServerException {
@@ -465,8 +465,8 @@ public void activateAccount() throws LoginFailedException, CaptchaActiveExceptio
465465
/**
466466
* Setup an avatar for the current account
467467
*
468-
* @throws LoginFailedException when the auth is invalid
469-
* @throws RemoteServerException when the server is down/having issues
468+
* @throws LoginFailedException when the auth is invalid
469+
* @throws RemoteServerException when the server is down/having issues
470470
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
471471
*/
472472
public void setupAvatar() throws LoginFailedException, CaptchaActiveException, RemoteServerException {
@@ -517,8 +517,8 @@ public void setupAvatar() throws LoginFailedException, CaptchaActiveException, R
517517
/**
518518
* Encounter tutorial complete. In other words, catch the first Pokémon
519519
*
520-
* @throws LoginFailedException when the auth is invalid
521-
* @throws RemoteServerException when the server is down/having issues
520+
* @throws LoginFailedException when the auth is invalid
521+
* @throws RemoteServerException when the server is down/having issues
522522
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
523523
*/
524524
public void encounterTutorialComplete() throws LoginFailedException, CaptchaActiveException, RemoteServerException {
@@ -558,23 +558,23 @@ public void encounterTutorialComplete() throws LoginFailedException, CaptchaActi
558558
/**
559559
* Setup an user name for our account
560560
*
561-
* @throws LoginFailedException when the auth is invalid
562-
* @throws RemoteServerException when the server is down/having issues
561+
* @throws LoginFailedException when the auth is invalid
562+
* @throws RemoteServerException when the server is down/having issues
563563
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
564564
*/
565-
public void claimCodeName() throws LoginFailedException, CaptchaActiveException, RemoteServerException {
566-
claimCodeName(null);
565+
public String claimCodeName() throws LoginFailedException, CaptchaActiveException, RemoteServerException {
566+
return claimCodeName(null);
567567
}
568568

569569
/**
570570
* Setup an user name for our account
571571
*
572572
* @param lastFailure the last name used that was already taken; null for first try.
573-
* @throws LoginFailedException when the auth is invalid
574-
* @throws RemoteServerException when the server is down/having issues
573+
* @throws LoginFailedException when the auth is invalid
574+
* @throws RemoteServerException when the server is down/having issues
575575
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
576576
*/
577-
public void claimCodeName(String lastFailure)
577+
public String claimCodeName(String lastFailure)
578578
throws LoginFailedException, CaptchaActiveException, RemoteServerException {
579579
if (getPlayerData().getRemainingCodenameClaims() <= 0) {
580580
throw new RuntimeException("You have no remaining codename claims!");
@@ -602,41 +602,38 @@ public void claimCodeName(String lastFailure)
602602
String updatedCodename = null;
603603
try {
604604
ClaimCodenameResponse claimCodenameResponse = ClaimCodenameResponse.parseFrom(request.getData());
605-
if (claimCodenameResponse.getStatus() == ClaimCodenameResponse.Status.SUCCESS) {
606-
updatedCodename = claimCodenameResponse.getCodename();
607-
} else {
608-
claimCodeName(name);
605+
if (claimCodenameResponse.getStatus() != ClaimCodenameResponse.Status.SUCCESS) {
606+
return claimCodeName(name);
609607
}
608+
updatedCodename = claimCodenameResponse.getCodename();
609+
610610
if (claimCodenameResponse.hasUpdatedPlayer()) {
611611
updateProfile(claimCodenameResponse.getUpdatedPlayer());
612612
}
613-
} catch (InvalidProtocolBufferException e) {
614-
throw new RemoteServerException(e);
615-
}
616613

617-
if (updatedCodename != null) {
618-
markTutorial(TutorialStateOuterClass.TutorialState.NAME_SELECTION);
614+
if (updatedCodename != null) {
615+
markTutorial(TutorialStateOuterClass.TutorialState.NAME_SELECTION);
619616

620-
final GetPlayerMessage getPlayerReqMsg = GetPlayerMessage.newBuilder()
621-
.setPlayerLocale(playerLocale.getPlayerLocale())
622-
.build();
623-
request = new ServerRequest(RequestType.GET_PLAYER, getPlayerReqMsg);
617+
final GetPlayerMessage getPlayerReqMsg = GetPlayerMessage.newBuilder()
618+
.setPlayerLocale(playerLocale.getPlayerLocale())
619+
.build();
620+
request = new ServerRequest(RequestType.GET_PLAYER, getPlayerReqMsg);
624621

625-
api.getRequestHandler().sendServerRequests(request.withCommons());
622+
api.getRequestHandler().sendServerRequests(request.withCommons());
626623

627-
try {
628624
updateProfile(GetPlayerResponse.parseFrom(request.getData()));
629-
} catch (InvalidProtocolBufferException e) {
630-
throw new RemoteServerException(e);
631625
}
626+
} catch (InvalidProtocolBufferException e) {
627+
throw new RemoteServerException(e);
632628
}
629+
return updatedCodename;
633630
}
634631

635632
/**
636633
* The last step, mark the last tutorial state as completed
637634
*
638-
* @throws LoginFailedException when the auth is invalid
639-
* @throws RemoteServerException when the server is down/having issues
635+
* @throws LoginFailedException when the auth is invalid
636+
* @throws RemoteServerException when the server is down/having issues
640637
* @throws CaptchaActiveException if a captcha is active and the message can't be sent
641638
*/
642639
public void firstTimeExperienceComplete()
@@ -674,4 +671,4 @@ private static String randomCodenameGenerator() {
674671
}
675672
return sb.toString();
676673
}
677-
}
674+
}

0 commit comments

Comments
 (0)