use of com.pokegoapi.main.ServerRequest 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.getRequestHandler().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);
}
}
use of com.pokegoapi.main.ServerRequest 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.getCount() < 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.getRequestHandler().sendServerRequests(serverRequest);
RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse response;
try {
response = RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
if (response.getResult() == RecycleInventoryItemResponseOuterClass.RecycleInventoryItemResponse.Result.SUCCESS) {
item.setCount(response.getNewCount());
if (item.getCount() <= 0) {
removeItem(item.getItemId());
}
}
return response.getResult();
}
use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.
the class PokeBank method releasePokemon.
/**
* Releases multiple pokemon in a single request
*
* @param releasePokemon the pokemon to release
* @return the amount of candies for each pokemon family
* @throws RequestFailedException if an exception occurred while sending requests
*/
public Map<PokemonFamilyId, Integer> releasePokemon(Pokemon... releasePokemon) throws RequestFailedException {
ReleasePokemonMessage.Builder releaseBuilder = ReleasePokemonMessage.newBuilder();
for (Pokemon pokemon : releasePokemon) {
if (!pokemon.isDeployed() && !pokemon.isFavorite()) {
releaseBuilder.addPokemonIds(pokemon.getId());
}
}
ServerRequestEnvelope envelope = ServerRequestEnvelope.createCommons();
ServerRequest releaseRequest = envelope.add(RequestType.RELEASE_POKEMON, releaseBuilder.build());
Map<PokemonFamilyId, Integer> lastCandies = new HashMap<>(api.getInventories().getCandyjar().getCandies());
ServerResponse response = api.getRequestHandler().sendServerRequests(envelope);
try {
GetInventoryResponse inventoryResponse = GetInventoryResponse.parseFrom(response.get(RequestType.GET_INVENTORY));
ReleasePokemonResponse releaseResponse = ReleasePokemonResponse.parseFrom(releaseRequest.getData());
Map<PokemonFamilyId, Integer> candyCount = new HashMap<>();
if (releaseResponse.getResult() == Result.SUCCESS && inventoryResponse.getSuccess()) {
synchronized (this.lock) {
for (Pokemon pokemon : releasePokemon) {
this.pokemons.remove(pokemon);
}
}
for (Pokemon pokemon : releasePokemon) {
api.getInventories().getPokebank().removePokemon(pokemon);
}
List<InventoryItem> items = inventoryResponse.getInventoryDelta().getInventoryItemsList();
for (InventoryItem item : items) {
InventoryItemData data = item.getInventoryItemData();
if (data != null && data.hasCandy()) {
Candy candy = data.getCandy();
PokemonFamilyId family = candy.getFamilyId();
Integer lastCandy = lastCandies.get(family);
if (lastCandy == null) {
lastCandy = 0;
}
candyCount.put(family, candy.getCandy() - lastCandy);
}
}
api.getInventories().updateInventories(inventoryResponse);
}
return candyCount;
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
}
use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.
the class Battle method sendActions.
/**
* Sends all currently queued actions to the server
*
* @param handler to handle this battle
* @return if this battle should switch to the next defender
* @throws RequestFailedException if an exception occurred while sending requests
*/
private boolean sendActions(BattleHandler handler) throws RequestFailedException {
AttackGymMessage.Builder builder = AttackGymMessage.newBuilder().setGymId(gym.getId()).setBattleId(battleId).setPlayerLatitude(api.getLatitude()).setPlayerLongitude(api.getLongitude());
while (queuedActions.size() > 0) {
ClientAction action = queuedActions.element();
if (action.getEndTime() < lastSendTime) {
queuedActions.remove();
long activePokemon = activeAttacker.getPokemon().getId();
if (action.getPokemon() != null) {
activePokemon = action.getPokemon().getId();
}
long start = action.getStartTime();
BattleAction.Builder actionBuilder = BattleAction.newBuilder().setActionStartMs(start).setDurationMs(action.getDuration()).setTargetIndex(-1).setActivePokemonId(activePokemon).setType(action.getType());
if (action.isHasDamageWindow()) {
long damageWindowsStart = start + action.getDamageWindowStart();
long damageWindowEnd = start + action.getDamageWindowEnd();
actionBuilder.setDamageWindowsStartTimestampMs(damageWindowsStart);
actionBuilder.setDamageWindowsEndTimestampMs(damageWindowEnd);
}
builder.addAttackActions(actionBuilder.build());
} else {
break;
}
}
if (lastRetrievedAction != null && sentActions) {
builder.setLastRetrievedAction(lastRetrievedAction);
}
if (builder.getAttackActionsCount() > 0) {
AttackGymMessage message = builder.build();
ServerRequest request = new ServerRequest(RequestType.ATTACK_GYM, message);
api.getRequestHandler().sendServerRequests(request, true);
boolean nextDefender;
try {
AttackGymResponse response = AttackGymResponse.parseFrom(request.getData());
nextDefender = handleAttackResponse(handler, response);
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
sentActions = true;
return nextDefender;
}
return false;
}
use of com.pokegoapi.main.ServerRequest in project PokeGOAPI-Java by Grover-c13.
the class PlayerProfile method updateProfile.
/**
* Updates the player profile with the latest data.
*
* @throws RequestFailedException if an exception occurred while sending requests
*/
public void updateProfile() throws RequestFailedException {
GetPlayerMessage message = GetPlayerMessage.newBuilder().setPlayerLocale(playerLocale.getPlayerLocale()).build();
ServerRequest request = new ServerRequest(RequestType.GET_PLAYER, message);
api.getRequestHandler().sendServerRequests(request);
try {
updateProfile(GetPlayerResponse.parseFrom(request.getData()));
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
}
Aggregations