use of com.pokegoapi.api.inventory.Item 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.getInventories().getItemBag();
Item item = bag.getItem(pokeball);
if (item.getCount() > 0) {
CatchPokemonMessage message = CatchPokemonMessage.newBuilder().setEncounterId(pokemon.getEncounterId()).setSpawnPointId(pokemon.getSpawnPointId()).setPokeball(pokeball).setNormalizedHitPosition(throwProperties.getNormalizedHitPosition()).setNormalizedReticleSize(throwProperties.getNormalizedReticleSize()).setSpinModifier(throwProperties.getSpinModifier()).setHitPokemon(throwProperties.shouldHitPokemon()).build();
ServerRequest request = new ServerRequest(RequestType.CATCH_POKEMON, message);
ByteString responseData = api.getRequestHandler().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.setDespawned(true);
}
if (status == CatchStatus.CATCH_SUCCESS) {
api.getPlayerProfile().updateProfile();
}
if (status != CatchStatus.CATCH_ERROR) {
item.setCount(item.getCount() - 1);
}
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
} else {
throw new NoSuchItemException();
}
}
return status;
}
use of com.pokegoapi.api.inventory.Item 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.getInventories().getItemBag();
Item item = bag.getItem(itemId);
if (item.getCount() > 0) {
if (activeItem == null) {
UseItemEncounterMessage message = UseItemEncounterMessage.newBuilder().setEncounterId(pokemon.getEncounterId()).setSpawnPointGuid(pokemon.getSpawnPointId()).setItem(itemId).build();
ServerRequest request = new ServerRequest(RequestType.USE_ITEM_ENCOUNTER, message);
ByteString responseData = api.getRequestHandler().sendServerRequests(request, true);
try {
UseItemEncounterResponse response = UseItemEncounterResponse.parseFrom(responseData);
activeItem = response.getActiveItem();
captureProbabilities = response.getCaptureProbability();
if (response.getStatus() == Status.SUCCESS) {
item.setCount(item.getCount() - 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.api.inventory.Item 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.api.inventory.Item 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.getInventories().getItemBag().getItem(itemId);
if (!item.isRevive() || item.getCount() < 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.getRequestHandler().sendServerRequests(serverRequest, true);
UseItemReviveResponse response;
try {
response = UseItemReviveResponse.parseFrom(serverRequest.getData());
if (response.getResult() == UseItemReviveResponse.Result.SUCCESS) {
item.setCount(item.getCount() - 1);
setStamina(response.getStamina());
}
return response.getResult();
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
}
Aggregations