use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class Pokemon method setFavoritePokemon.
/**
* Function to mark the pokemon as favorite or not.
*
* @param markFavorite Mark Pokemon as Favorite?
* @return the SetFavoritePokemonResponse.Result
* @throws RequestFailedException if an exception occurred while sending requests
*/
public SetFavoritePokemonResponse.Result setFavoritePokemon(boolean markFavorite) throws RequestFailedException {
SetFavoritePokemonMessage reqMsg = SetFavoritePokemonMessage.newBuilder().setPokemonId(getId()).setIsFavorite(markFavorite).build();
ServerRequest serverRequest = new ServerRequest(RequestType.SET_FAVORITE_POKEMON, reqMsg);
api.requestHandler.sendServerRequests(serverRequest, true);
SetFavoritePokemonResponse response;
try {
response = SetFavoritePokemonResponse.parseFrom(serverRequest.getData());
if (response.getResult() == SetFavoritePokemonResponse.Result.SUCCESS) {
favorite = markFavorite ? 1 : 0;
}
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
api.inventories.pokebank.removePokemon(this);
return response.getResult();
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class Pokemon method renamePokemon.
/**
* Rename pokemon nickname pokemon response . result.
*
* @param nickname the nickname
* @return the nickname pokemon response . result
* @throws RequestFailedException if an exception occurred while sending requests
*/
public NicknamePokemonResponse.Result renamePokemon(String nickname) throws RequestFailedException {
NicknamePokemonMessage reqMsg = NicknamePokemonMessage.newBuilder().setPokemonId(getId()).setNickname(nickname).build();
ServerRequest serverRequest = new ServerRequest(RequestType.NICKNAME_POKEMON, reqMsg);
api.requestHandler.sendServerRequests(serverRequest, true);
NicknamePokemonResponse response;
try {
response = NicknamePokemonResponse.parseFrom(serverRequest.getData());
if (response.getResult() == NicknamePokemonResponse.Result.SUCCESS) {
this.nickname = nickname;
}
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
api.inventories.pokebank.removePokemon(this);
return response.getResult();
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class Pokemon method useRevive.
/**
* Use a revive item on the pokemon. Will check if there is enough revive & if the pokemon need
* to be revived.
*
* @param itemId {@link ItemId} of the Revive to use.
* @return Result, ERROR_CANNOT_USE if the requirements aren't met
* @throws RequestFailedException if an exception occurred while sending requests
*/
public UseItemReviveResponse.Result useRevive(ItemId itemId) throws RequestFailedException {
Item item = api.inventories.itemBag.getItem(itemId);
if (!item.isRevive() || item.count < 1 || !isFainted())
return UseItemReviveResponse.Result.ERROR_CANNOT_USE;
UseItemReviveMessage reqMsg = UseItemReviveMessage.newBuilder().setItemId(itemId).setPokemonId(getId()).build();
ServerRequest serverRequest = new ServerRequest(RequestType.USE_ITEM_REVIVE, reqMsg);
api.requestHandler.sendServerRequests(serverRequest, true);
UseItemReviveResponse response;
try {
response = UseItemReviveResponse.parseFrom(serverRequest.getData());
if (response.getResult() == UseItemReviveResponse.Result.SUCCESS) {
item.setCount(item.count - 1);
this.stamina = response.getStamina();
}
return response.getResult();
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class PokeBank method releasePokemon.
/**
* Releases multiple pokemon in a single request
*
* @param releasePokemon the pokemon to release
* @return the amount of candies for each pokemon family
* @throws RequestFailedException if an exception occurred while sending requests
*/
public Map<PokemonFamilyId, Integer> releasePokemon(Pokemon... releasePokemon) throws RequestFailedException {
ReleasePokemonMessage.Builder releaseBuilder = ReleasePokemonMessage.newBuilder();
for (Pokemon pokemon : releasePokemon) {
if (!pokemon.isDeployed() && !pokemon.isFavorite()) {
releaseBuilder.addPokemonIds(pokemon.getId());
}
}
ServerRequest releaseRequest = new ServerRequest(RequestType.RELEASE_POKEMON, releaseBuilder.build());
ServerRequestEnvelope envelope = ServerRequestEnvelope.createCommons(releaseRequest, api);
Map<PokemonFamilyId, Integer> lastCandies = new HashMap<>(api.inventories.candyjar.getCandies());
ServerResponse response = api.requestHandler.sendServerRequests(envelope);
try {
ByteString inventoryData = response.get(RequestType.GET_HOLOHOLO_INVENTORY);
GetHoloInventoryResponse inventoryResponse = GetHoloInventoryResponse.parseFrom(inventoryData);
ReleasePokemonResponse releaseResponse = ReleasePokemonResponse.parseFrom(releaseRequest.getData());
Map<PokemonFamilyId, Integer> candyCount = new HashMap<>();
if (releaseResponse.getResult() == Result.SUCCESS && inventoryResponse.getSuccess()) {
synchronized (this.lock) {
this.pokemons.removeAll(Arrays.asList(releasePokemon));
}
for (Pokemon pokemon : releasePokemon) {
api.inventories.pokebank.removePokemon(pokemon);
}
List<InventoryItem> items = inventoryResponse.getInventoryDelta().getInventoryItemsList();
for (InventoryItem item : items) {
InventoryItemData data = item.getInventoryItemData();
if (data != null && data.hasCandy()) {
Candy candy = data.getCandy();
PokemonFamilyId family = candy.getFamilyId();
Integer lastCandy = lastCandies.get(family);
if (lastCandy == null) {
lastCandy = 0;
}
candyCount.put(family, candy.getCandy() - lastCandy);
}
}
api.inventories.updateInventories(inventoryResponse);
}
return candyCount;
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class News method markUnreadNews.
/**
* Mark Unread News to read
*/
public void markUnreadNews() {
if (currentNews == null || currentNews.getNewsArticlesCount() <= 0) {
// do nothing
return;
}
// Stored enabled and un-read article
List<String> unReadNewsList = new ArrayList<>();
for (NewsArticle newsArticle : currentNews.getNewsArticlesList()) {
if (newsArticle.getEnabled() && !newsArticle.getArticleRead())
unReadNewsList.add(newsArticle.getId());
}
Log.i(TAG, "markUnreadNews total Article count:" + unReadNewsList.size());
if (unReadNewsList.size() > 0) {
MarkReadNewsArticleMessage msg = MarkReadNewsArticleMessage.newBuilder().addAllNewsIds(unReadNewsList).build();
ServerRequest request = new ServerRequest(RequestTypeOuterClass.RequestType.MARK_READ_NEWS_ARTICLE, msg);
ServerRequestEnvelope envelope = ServerRequestEnvelope.create(request);
try {
api.requestHandler.sendServerRequests(envelope);
MarkReadNewsArticleResponse response = MarkReadNewsArticleResponse.parseFrom(request.getData());
if (response.getResult() == MarkReadNewsArticleResponse.Result.SUCCESS) {
Log.i(TAG, "Mark News Article -> success");
} else {
Log.w(TAG, "Mark News Article -> !success");
}
} catch (RequestFailedException e) {
e.printStackTrace();
Log.e(TAG, "RequestFailedException: cause:" + e.getCause() + " message:" + e.getMessage());
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
Log.e(TAG, "InvalidProtocolBufferException: cause:" + e.getCause() + " message:" + e.getMessage());
}
} else {
Log.i(TAG, "no unmarked news found -> skipped");
}
}
Aggregations