use of com.google.protobuf.InvalidProtocolBufferException 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.getAvatar()).build();
ServerRequest request = new ServerRequest(RequestType.SET_AVATAR, setAvatarMessage);
api.getRequestHandler().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();
}
use of com.google.protobuf.InvalidProtocolBufferException 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.getRequestHandler().sendServerRequests(request);
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.google.protobuf.InvalidProtocolBufferException 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.getRequestHandler().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.getRequestHandler().sendServerRequests(request, true);
updateProfile(GetPlayerResponse.parseFrom(request.getData()));
}
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
return updatedCodename;
}
use of com.google.protobuf.InvalidProtocolBufferException in project PokeGOAPI-Java by Grover-c13.
the class PlayerProfile method acceptLevelUpRewards.
/**
* Accept the rewards granted and the items unlocked by gaining a trainer level up. Rewards are retained by the
* server until a player actively accepts them.
* The rewarded items are automatically inserted into the players item bag.
*
* @param level the trainer level that you want to accept the rewards for
* @return a PlayerLevelUpRewards object containing information about the items rewarded and unlocked for this level
* @throws RequestFailedException if an exception occurred while sending requests
* @throws InsufficientLevelException if you have not yet reached the desired level
* @see PlayerLevelUpRewards
*/
public PlayerLevelUpRewards acceptLevelUpRewards(int level) throws RequestFailedException {
// Check if we even have achieved this level yet
if (level > stats.getLevel()) {
throw new InsufficientLevelException();
}
LevelUpRewardsMessage msg = LevelUpRewardsMessage.newBuilder().setLevel(level).build();
ServerRequest serverRequest = new ServerRequest(RequestType.LEVEL_UP_REWARDS, msg);
api.getRequestHandler().sendServerRequests(serverRequest, true);
LevelUpRewardsResponse response;
try {
response = LevelUpRewardsResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
// Add the awarded items to our bag
ItemBag bag = api.getInventories().getItemBag();
bag.addAwardedItems(response);
// Build a new rewards object and return it
return new PlayerLevelUpRewards(response);
}
use of com.google.protobuf.InvalidProtocolBufferException in project PokeGOAPI-Java by Grover-c13.
the class Pokemon method transferPokemon.
/**
* Transfers the pokemon.
*
* @return the result
* @throws RequestFailedException if an exception occurred while sending requests
*/
public Result transferPokemon() throws RequestFailedException {
if (this.isFavorite() || this.isDeployed()) {
return Result.FAILED;
}
ReleasePokemonMessage reqMsg = ReleasePokemonMessage.newBuilder().setPokemonId(getId()).build();
ServerRequest serverRequest = new ServerRequest(RequestType.RELEASE_POKEMON, reqMsg);
api.getRequestHandler().sendServerRequests(serverRequest, true);
ReleasePokemonResponse response;
try {
response = ReleasePokemonResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
return ReleasePokemonResponse.Result.FAILED;
}
if (response.getResult() == Result.SUCCESS) {
api.getInventories().getPokebank().removePokemon(this);
}
return response.getResult();
}
Aggregations