use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class Pokemon method usePotion.
/**
* use a potion on that pokemon. Will check if there is enough potions and if the pokemon need
* to be healed.
*
* @param itemId {@link ItemId} of the potion to use.
* @return Result, ERROR_CANNOT_USE if the requirements aren't met
* @throws RequestFailedException if an exception occurred while sending requests
*/
public UseItemPotionResponse.Result usePotion(ItemId itemId) throws RequestFailedException {
Item potion = api.inventories.itemBag.getItem(itemId);
// some sanity check, to prevent wrong use of this call
if (!potion.isPotion() || potion.count < 1 || !isInjured())
return UseItemPotionResponse.Result.ERROR_CANNOT_USE;
UseItemPotionMessageOuterClass.UseItemPotionMessage reqMsg = UseItemPotionMessageOuterClass.UseItemPotionMessage.newBuilder().setItemId(itemId).setPokemonId(getId()).build();
ServerRequest serverRequest = new ServerRequest(RequestType.USE_ITEM_POTION, reqMsg);
api.requestHandler.sendServerRequests(serverRequest, true);
UseItemPotionResponse response;
try {
response = UseItemPotionResponse.parseFrom(serverRequest.getData());
if (response.getResult() == UseItemPotionResponse.Result.SUCCESS) {
potion.setCount(potion.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 Settings method updateSettings.
/**
* Updates settings latest data.
*
* @throws RequestFailedException if an exception occurred while sending requests
*/
public void updateSettings() throws RequestFailedException {
DownloadSettingsMessageOuterClass.DownloadSettingsMessage msg = DownloadSettingsMessageOuterClass.DownloadSettingsMessage.newBuilder().build();
ServerRequest serverRequest = new ServerRequest(RequestTypeOuterClass.RequestType.DOWNLOAD_SETTINGS, msg);
//here you marked everything as read
api.getRequestHandler().sendServerRequests(serverRequest, false);
DownloadSettingsResponseOuterClass.DownloadSettingsResponse response;
try {
response = DownloadSettingsResponseOuterClass.DownloadSettingsResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
updateSettings(response);
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class Gym method details.
private GymGetInfoResponse details() throws RequestFailedException {
List<TutorialState> tutorialStates = api.playerProfile.getTutorialState().getTutorialStates();
if (!tutorialStates.contains(TutorialState.GYM_TUTORIAL)) {
api.playerProfile.visitGymComplete();
}
if (details == null) {
GymGetInfoMessage reqMsg = GymGetInfoMessage.newBuilder().setGymId(this.getId()).setGymLatDegrees(this.getLatitude()).setGymLngDegrees(this.getLongitude()).setPlayerLatDegrees(api.latitude).setPlayerLngDegrees(api.longitude).build();
ServerRequest serverRequest = new ServerRequest(RequestType.GYM_GET_INFO, reqMsg);
api.requestHandler.sendServerRequests(serverRequest, true);
try {
details = GymGetInfoResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException();
}
}
return details;
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class ItemBag method removeItem.
/**
* Discards the given item.
*
* @param id the id
* @param quantity the quantity
* @return the result
* @throws RequestFailedException if an exception occurred while sending requests
*/
public Result removeItem(ItemId id, int quantity) throws RequestFailedException {
Item item = getItem(id);
if (item.count < quantity) {
throw new IllegalArgumentException("You cannot remove more quantity than you have");
}
RecycleInventoryItemMessage msg = RecycleInventoryItemMessage.newBuilder().setItemId(id).setCount(quantity).build();
ServerRequest serverRequest = new ServerRequest(RequestType.RECYCLE_INVENTORY_ITEM, msg);
api.requestHandler.sendServerRequests(serverRequest, true);
RecycleInventoryItemResponse response;
try {
response = RecycleInventoryItemResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
if (response.getResult() == RecycleInventoryItemResponse.Result.SUCCESS) {
item.setCount(response.getNewCount());
if (item.count <= 0) {
removeItem(item.getItemId());
}
}
return response.getResult();
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class ItemBag method useIncense.
/**
* use an incense
*
* @param type type of item
* @throws RequestFailedException if an exception occurred while sending requests
*/
public void useIncense(ItemId type) throws RequestFailedException {
UseIncenseMessage useIncenseMessage = UseIncenseMessage.newBuilder().setIncenseType(type).setIncenseTypeValue(type.getNumber()).build();
ServerRequest useIncenseRequest = new ServerRequest(RequestType.USE_INCENSE, useIncenseMessage);
api.requestHandler.sendServerRequests(useIncenseRequest, true);
try {
UseIncenseResponse response = UseIncenseResponse.parseFrom(useIncenseRequest.getData());
Log.i("Main", "Use incense result: " + response.getResult());
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
}
Aggregations