use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class TransferMultiplePokemon method main.
/**
* Transfers all bad pokemon from the player's inventory.
*/
public static void main(String[] args) {
OkHttpClient http = new OkHttpClient();
PokemonGo api = new PokemonGo(http);
try {
HashProvider hasher = ExampleConstants.getHashProvider();
api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
PokeBank pokebank = api.inventories.pokebank;
List<Pokemon> pokemons = pokebank.pokemons;
List<Pokemon> transferPokemons = new ArrayList<>();
// Find all pokemon of bad types or with IV less than 25%
for (Pokemon pokemon : pokemons) {
PokemonId id = pokemon.getPokemonId();
double iv = pokemon.getIvInPercentage();
if (iv < 90) {
if (id == PokemonId.RATTATA || id == PokemonId.PIDGEY || id == PokemonId.CATERPIE || id == PokemonId.WEEDLE || id == PokemonId.MAGIKARP || id == PokemonId.ZUBAT || iv < 25) {
transferPokemons.add(pokemon);
}
}
}
System.out.println("Releasing " + transferPokemons.size() + " pokemon.");
Pokemon[] transferArray = transferPokemons.toArray(new Pokemon[transferPokemons.size()]);
Map<PokemonFamilyId, Integer> responses = pokebank.releasePokemon(transferArray);
// Loop through all responses and find the total amount of candies earned for each family
Map<PokemonFamilyId, Integer> candies = new HashMap<>();
for (Map.Entry<PokemonFamilyId, Integer> entry : responses.entrySet()) {
int candyAwarded = entry.getValue();
PokemonFamilyId family = entry.getKey();
Integer candy = candies.get(family);
if (candy == null) {
// candies map does not yet contain the amount if null, so set it to 0
candy = 0;
}
// Add the awarded candies from this request
candy += candyAwarded;
candies.put(family, candy);
}
for (Map.Entry<PokemonFamilyId, Integer> entry : candies.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue() + " candies awarded");
}
} catch (RequestFailedException e) {
// failed to login, invalid credentials, auth issue or server issue.
Log.e("Main", "Failed to login. Invalid credentials, captcha or server issue: ", e);
}
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class TutorialHandleExample method main.
/**
* Goes through the tutorial with custom responses.
*
* @param args args
*/
public static void main(String[] args) {
OkHttpClient http = new OkHttpClient();
final PokemonGo api = new PokemonGo(http);
try {
// Add listener to listen for all tutorial related events, must be registered before login is called,
// otherwise it will not be used
api.addListener(new TutorialListener() {
@Override
public String claimName(PokemonGo api, String lastFailure) {
// Last attempt to set a codename failed, set a random one by returning null
if (lastFailure != null) {
System.out.println("Codename \"" + lastFailure + "\" is already taken. Using random name.");
return null;
}
System.out.println("Selecting codename");
// Set the PTC name as the POGO username
return ExampleConstants.LOGIN;
}
@Override
public StarterPokemon selectStarter(PokemonGo api) {
// Catch Charmander as your starter pokemon
System.out.println("Selecting starter pokemon");
return StarterPokemon.CHARMANDER;
}
@Override
public PlayerAvatar selectAvatar(PokemonGo api) {
System.out.println("Selecting player avatar");
return new PlayerAvatar(PlayerGender.FEMALE, Avatar.Skin.YELLOW.id(), Avatar.Hair.BLACK.id(), Avatar.FemaleShirt.BLUE.id(), Avatar.FemalePants.BLACK_PURPLE_STRIPE.id(), Avatar.FemaleHat.BLACK_YELLOW_POKEBALL.id(), Avatar.FemaleShoes.BLACK_YELLOW_STRIPE.id(), Avatar.Eye.BROWN.id(), Avatar.FemaleBackpack.GRAY_BLACK_YELLOW_POKEBALL.id());
}
});
HashProvider hasher = ExampleConstants.getHashProvider();
api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
} catch (RequestFailedException e) {
Log.e("Main", "Failed to login!", e);
}
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class PlayerProfile method claimCodeName.
/**
* Setup an user name for our account
*
* @param lastFailure the last name used that was already taken; null for first try.
* @return the claimed codename
* @throws RequestFailedException if an exception occurred while sending requests
*/
public String claimCodeName(String lastFailure) throws RequestFailedException {
if (getPlayerData().getRemainingCodenameClaims() <= 0) {
throw new RuntimeException("You have no remaining codename claims!");
}
String name = randomCodenameGenerator();
List<TutorialListener> listeners = api.getListeners(TutorialListener.class);
for (TutorialListener listener : listeners) {
String listenerName = listener.claimName(api, lastFailure);
if (listenerName != null) {
name = listenerName;
break;
}
}
ClaimCodenameMessage claimCodenameMessage = ClaimCodenameMessage.newBuilder().setCodename(name).build();
ServerRequest request = new ServerRequest(RequestType.CLAIM_CODENAME, claimCodenameMessage);
api.requestHandler.sendServerRequests(request, true);
String updatedCodename;
try {
ClaimCodenameResponse claimCodenameResponse = ClaimCodenameResponse.parseFrom(request.getData());
if (claimCodenameResponse.getStatus() != ClaimCodenameResponse.Status.SUCCESS) {
return claimCodeName(name);
}
updatedCodename = claimCodenameResponse.getCodename();
if (claimCodenameResponse.hasUpdatedPlayer()) {
updateProfile(claimCodenameResponse.getUpdatedPlayer());
}
if (updatedCodename != null) {
markTutorial(TutorialStateOuterClass.TutorialState.NAME_SELECTION);
final GetPlayerMessage getPlayerReqMsg = GetPlayerMessage.newBuilder().setPlayerLocale(playerLocale.getPlayerLocale()).build();
request = new ServerRequest(RequestType.GET_PLAYER, getPlayerReqMsg);
api.requestHandler.sendServerRequests(request, true);
updateProfile(GetPlayerResponse.parseFrom(request.getData()));
}
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
return updatedCodename;
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class PlayerProfile method setBuddy.
/**
* Sets the current buddy
*
* @param pokemon the pokemon to set as your buddy
* @return if this task was successfull
* @throws RequestFailedException if an exception occurred while sending requests
*/
public boolean setBuddy(Pokemon pokemon) throws RequestFailedException {
SetBuddyPokemonMessageOuterClass.SetBuddyPokemonMessage message = SetBuddyPokemonMessageOuterClass.SetBuddyPokemonMessage.newBuilder().setPokemonId(pokemon.getId()).build();
ServerRequest request = new ServerRequest(RequestType.SET_BUDDY_POKEMON, message);
api.requestHandler.sendServerRequests(request, true);
try {
SetBuddyPokemonResponse response = SetBuddyPokemonResponse.parseFrom(request.getData());
buddy = new Buddy(api, response.getUpdatedBuddy());
return response.hasUpdatedBuddy();
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class PlayerProfile method setupAvatar.
/**
* Setup an avatar for the current account
*
* @throws RequestFailedException if an exception occurred while sending requests
*/
public void setupAvatar() throws RequestFailedException {
SecureRandom random = new SecureRandom();
PlayerGender gender = random.nextInt(100) % 2 == 0 ? PlayerGender.FEMALE : PlayerGender.MALE;
PlayerAvatar avatar = new PlayerAvatar(gender, random.nextInt(PlayerAvatar.getAvailableSkins()), random.nextInt(PlayerAvatar.getAvailableHair()), random.nextInt(PlayerAvatar.getAvailableShirts(gender)), random.nextInt(PlayerAvatar.getAvailablePants(gender)), random.nextInt(PlayerAvatar.getAvailableHats()), random.nextInt(PlayerAvatar.getAvailableShoes()), random.nextInt(PlayerAvatar.getAvailableEyes()), random.nextInt(PlayerAvatar.getAvailableBags(gender)));
List<TutorialListener> listeners = api.getListeners(TutorialListener.class);
for (TutorialListener listener : listeners) {
PlayerAvatar listenerAvatar = listener.selectAvatar(api);
if (listenerAvatar != null) {
avatar = listenerAvatar;
break;
}
}
final SetAvatarMessage setAvatarMessage = SetAvatarMessage.newBuilder().setPlayerAvatar(avatar.avatar).build();
ServerRequest request = new ServerRequest(RequestType.SET_AVATAR, setAvatarMessage);
api.requestHandler.sendServerRequests(request, true);
try {
SetAvatarResponse setAvatarResponse = SetAvatarResponse.parseFrom(request.getData());
playerData = setAvatarResponse.getPlayerData();
updateProfile(playerData);
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
markTutorial(TutorialStateOuterClass.TutorialState.AVATAR_SELECTION);
api.getAssetDigest();
}
Aggregations