use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class Encounter method useItem.
/**
* Uses an item in this encounter
*
* @param itemId the item to use
* @return the result from this action
* @throws RequestFailedException if the use request fails
*/
public UseItemEncounterResponse.Status useItem(ItemId itemId) throws RequestFailedException {
if (isActive()) {
ItemBag bag = api.inventories.itemBag;
Item item = bag.getItem(itemId);
if (item.count > 0) {
if (getActiveItem() == null) {
UseItemEncounterMessage message = UseItemEncounterMessage.newBuilder().setEncounterId(pokemon.encounterId).setSpawnPointGuid(pokemon.spawnPointId).setItem(itemId).build();
ServerRequest request = new ServerRequest(RequestType.USE_ITEM_ENCOUNTER, message);
ByteString responseData = api.requestHandler.sendServerRequests(request, true);
try {
UseItemEncounterResponse response = UseItemEncounterResponse.parseFrom(responseData);
activeItem = response.getActiveItem();
captureProbabilities = response.getCaptureProbability();
if (response.getStatus() == Status.SUCCESS) {
item.setCount(item.count - 1);
}
return response.getStatus();
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
} else {
return UseItemEncounterResponse.Status.ACTIVE_ITEM_EXISTS;
}
} else {
return UseItemEncounterResponse.Status.NO_ITEM_IN_INVENTORY;
}
}
return UseItemEncounterResponse.Status.ALREADY_COMPLETED;
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class Encounter method encounter.
/**
* Encounters this pokemon
*
* @return the result from the attempted encounter
* @throws RequestFailedException if the encounter request fails
*/
protected EncounterResult encounter() throws RequestFailedException {
EncounterMessage message = EncounterMessage.newBuilder().setEncounterId(pokemon.encounterId).setSpawnPointId(pokemon.spawnPointId).setPlayerLatitude(api.latitude).setPlayerLongitude(api.longitude).build();
ServerRequest request = new ServerRequest(RequestType.ENCOUNTER, message);
ByteString responseData = api.requestHandler.sendServerRequests(request, true);
try {
EncounterResponse response = EncounterResponse.parseFrom(responseData);
encounterResult = EncounterResult.from(response.getStatus());
activeItem = response.getActiveItem();
captureProbabilities = response.getCaptureProbability();
encounteredPokemon = response.getWildPokemon().getPokemonData();
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
return encounterResult;
}
use of com.pokegoapi.exceptions.request.RequestFailedException in project PokeGOAPI-Java by Grover-c13.
the class Encounter method throwPokeball.
/**
* Throws a pokeball in this encounter
*
* @param pokeball the pokeball to throw
* @param throwProperties the throw properties for this throw
* @return the result from the pokeball throw
* @throws RequestFailedException if the throw request fails
* @throws NoSuchItemException if the requested pokeball does not exist
*/
public CatchPokemonResponse.CatchStatus throwPokeball(ItemId pokeball, ThrowProperties throwProperties) throws RequestFailedException, NoSuchItemException {
if (isActive()) {
ItemBag bag = api.inventories.itemBag;
Item item = bag.getItem(pokeball);
if (item.count > 0) {
CatchPokemonMessage message = CatchPokemonMessage.newBuilder().setEncounterId(pokemon.encounterId).setSpawnPointId(pokemon.spawnPointId).setPokeball(pokeball).setNormalizedHitPosition(throwProperties.normalizedHitPosition).setNormalizedReticleSize(throwProperties.normalizedReticleSize).setSpinModifier(throwProperties.spinModifier).setHitPokemon(throwProperties.shouldHitPokemon()).build();
ServerRequest request = new ServerRequest(RequestType.CATCH_POKEMON, message);
ByteString responseData = api.requestHandler.sendServerRequests(request, true);
try {
CatchPokemonResponse response = CatchPokemonResponse.parseFrom(responseData);
status = response.getStatus();
if (hasCaptured()) {
captureAward = response.getCaptureAward();
capturedPokemon = response.getCapturedPokemonId();
captureReason = response.getCaptureReason();
}
if (status == CatchStatus.CATCH_SUCCESS || status == CatchStatus.CATCH_FLEE) {
pokemon.despawned = true;
api.playerProfile.updateProfile();
}
if (status == CatchStatus.CATCH_ESCAPE) {
activeItem = ItemId.UNRECOGNIZED;
}
if (status != CatchStatus.CATCH_ERROR) {
item.setCount(item.count - 1);
}
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
} else {
throw new NoSuchItemException();
}
}
return status;
}
use of com.pokegoapi.exceptions.request.RequestFailedException 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.latitude).setPlayerLongitude(api.longitude).setPokemonId(pokemon.getId()).build();
ServerRequest serverRequest = new ServerRequest(RequestType.FORT_DEPLOY_POKEMON, reqMsg);
api.requestHandler.sendServerRequests(serverRequest, true);
try {
return FortDeployPokemonResponse.parseFrom(serverRequest.getData()).getResult();
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException();
}
}
use of com.pokegoapi.exceptions.request.RequestFailedException 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.requestHandler.sendServerRequests(serverRequest, true);
UseItemEggIncubatorResponse response;
try {
response = UseItemEggIncubatorResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
api.inventories.updateInventories();
return response.getResult();
}
Aggregations