use of com.pokegoapi.main.ServerRequest 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.getInventories().getItemBag().getItem(itemId);
//some sanity check, to prevent wrong use of this call
if (!potion.isPotion() || potion.getCount() < 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.getRequestHandler().sendServerRequests(serverRequest, true);
UseItemPotionResponse response;
try {
response = UseItemPotionResponse.parseFrom(serverRequest.getData());
if (response.getResult() == UseItemPotionResponse.Result.SUCCESS) {
potion.setCount(potion.getCount() - 1);
setStamina(response.getStamina());
}
return response.getResult();
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
}
use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.
the class Pokemon method powerUpAsync.
/**
* Powers up a pokemon with candy and stardust.
* After powering up this pokemon object will reflect the new changes.
*
* @return The result
*/
public Observable<UpgradePokemonResponse.Result> powerUpAsync() {
UpgradePokemonMessage reqMsg = UpgradePokemonMessage.newBuilder().setPokemonId(getId()).build();
ServerRequest serverRequest = new ServerRequest(RequestType.UPGRADE_POKEMON, reqMsg);
return api.getRequestHandler().sendAsyncServerRequests(serverRequest, true).map(new Func1<ByteString, UpgradePokemonResponse.Result>() {
@Override
public UpgradePokemonResponse.Result call(ByteString result) {
UpgradePokemonResponse response;
try {
response = UpgradePokemonResponse.parseFrom(result);
} catch (InvalidProtocolBufferException e) {
throw Exceptions.propagate(e);
}
//set new pokemon details
applyProto(response.getUpgradedPokemon());
return response.getResult();
}
});
}
use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.
the class Gym method deployPokemon.
/**
* Deploy pokemon
*
* @param pokemon The pokemon to deploy
* @return Result of attempt to deploy pokemon
* @throws RequestFailedException if an exception occurred while sending requests
*/
public FortDeployPokemonResponse.Result deployPokemon(Pokemon pokemon) throws RequestFailedException {
FortDeployPokemonMessage reqMsg = FortDeployPokemonMessage.newBuilder().setFortId(getId()).setPlayerLatitude(api.getLatitude()).setPlayerLongitude(api.getLongitude()).setPokemonId(pokemon.getId()).build();
ServerRequest serverRequest = new ServerRequest(RequestType.FORT_DEPLOY_POKEMON, reqMsg);
api.getRequestHandler().sendServerRequests(serverRequest, true);
try {
return FortDeployPokemonResponse.parseFrom(serverRequest.getData()).getResult();
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException();
}
}
use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.
the class EggIncubator method hatchEgg.
/**
* Hatch an egg.
*
* @param egg the egg
* @return status of putting egg in incubator
* @throws RequestFailedException if an exception occurred while sending requests
*/
public UseItemEggIncubatorResponse.Result hatchEgg(EggPokemon egg) throws RequestFailedException {
UseItemEggIncubatorMessage reqMsg = UseItemEggIncubatorMessage.newBuilder().setItemId(proto.getId()).setPokemonId(egg.getId()).build();
ServerRequest serverRequest = new ServerRequest(RequestType.USE_ITEM_EGG_INCUBATOR, reqMsg);
api.getRequestHandler().sendServerRequests(serverRequest, true);
UseItemEggIncubatorResponse response;
try {
response = UseItemEggIncubatorResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
api.getInventories().updateInventories(true);
return response.getResult();
}
use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.
the class Hatchery method queryHatchedEggs.
/**
* Get if eggs has hatched.
*
* @return list of hatched eggs
* @throws RequestFailedException if an exception occurred while sending requests
* @deprecated Use getHatchedEggs()
*/
@Deprecated
public List<HatchedEgg> queryHatchedEggs() throws RequestFailedException {
GetHatchedEggsMessage msg = GetHatchedEggsMessage.newBuilder().build();
ServerRequest serverRequest = new ServerRequest(RequestType.GET_HATCHED_EGGS, msg);
api.getRequestHandler().sendServerRequests(serverRequest);
GetHatchedEggsResponse response;
try {
response = GetHatchedEggsResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
api.getInventories().updateInventories();
return updateHatchedEggs(response);
}
Aggregations