use of POGOProtos.Networking.Responses.UseItemEncounterResponseOuterClass.UseItemEncounterResponse 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;
}
Aggregations