use of com.pokegoapi.api.inventory.ItemBag 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.ItemBag 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.ItemBag in project PokeGOAPI-Java by Grover-c13.
the class PlayerProfile method acceptLevelUpRewards.
/**
* Accept the rewards granted and the items unlocked by gaining a trainer level up. Rewards are retained by the
* server until a player actively accepts them.
* The rewarded items are automatically inserted into the players item bag.
*
* @param level the trainer level that you want to accept the rewards for
* @return a PlayerLevelUpRewards object containing information about the items rewarded and unlocked for this level
* @throws RequestFailedException if an exception occurred while sending requests
* @throws InsufficientLevelException if you have not yet reached the desired level
* @see PlayerLevelUpRewards
*/
public PlayerLevelUpRewards acceptLevelUpRewards(int level) throws RequestFailedException {
// Check if we even have achieved this level yet
if (level > stats.getLevel()) {
throw new InsufficientLevelException();
}
LevelUpRewardsMessage msg = LevelUpRewardsMessage.newBuilder().setLevel(level).build();
ServerRequest serverRequest = new ServerRequest(RequestType.LEVEL_UP_REWARDS, msg);
api.getRequestHandler().sendServerRequests(serverRequest, true);
LevelUpRewardsResponse response;
try {
response = LevelUpRewardsResponse.parseFrom(serverRequest.getData());
} catch (InvalidProtocolBufferException e) {
throw new RequestFailedException(e);
}
// Add the awarded items to our bag
ItemBag bag = api.getInventories().getItemBag();
bag.addAwardedItems(response);
// Build a new rewards object and return it
return new PlayerLevelUpRewards(response);
}
use of com.pokegoapi.api.inventory.ItemBag in project PokeGOAPI-Java by Grover-c13.
the class CatchPokemonAtAreaExample method catchArea.
private static void catchArea(PokemonGo api) throws RequestFailedException, NoSuchItemException {
ItemBag bag = api.getInventories().getItemBag();
try {
//Wait until map is updated for the current location
api.getMap().awaitUpdate();
Set<CatchablePokemon> catchablePokemon = api.getMap().getMapObjects().getPokemon();
System.out.println("Pokemon in area: " + catchablePokemon.size());
Random random = new Random();
PokeBank pokebank = api.getInventories().getPokebank();
for (CatchablePokemon cp : catchablePokemon) {
// Encounter this pokemon
Encounter encounter = cp.encounter();
// If the encounter was successful, attempt to catch this pokemon
if (encounter.isSuccessful()) {
System.out.println("Encountered: " + cp.getPokemonId());
List<Pokeball> usablePokeballs = bag.getUsablePokeballs();
if (usablePokeballs.size() > 0) {
//Select pokeball with smart selector to print what pokeball is used
double probability = encounter.getCaptureProbability();
Pokeball pokeball = PokeballSelector.SMART.select(usablePokeballs, probability);
System.out.println("Attempting to catch: " + cp.getPokemonId() + " with " + pokeball + " (" + probability + ")");
// Throw pokeballs until capture or flee
while (encounter.isActive()) {
// Wait between Pokeball throws
Thread.sleep(500 + random.nextInt(1000));
// If no item is active, use a razzberry
int razzberryCount = bag.getItem(ItemId.ITEM_RAZZ_BERRY).getCount();
if (encounter.getActiveItem() == null && razzberryCount > 0) {
encounter.useItem(ItemId.ITEM_RAZZ_BERRY);
}
// Throw pokeball with random properties
encounter.throwPokeball(PokeballSelector.SMART, ThrowProperties.random());
if (encounter.getStatus() == CatchStatus.CATCH_SUCCESS) {
// Print pokemon stats
Pokemon pokemon = pokebank.getPokemonById(encounter.getCapturedPokemon());
if (pokemon != null) {
double iv = pokemon.getIvInPercentage();
int number = pokemon.getPokemonId().getNumber();
String name = PokeDictionary.getDisplayName(number, Locale.ENGLISH);
System.out.println("====" + name + "====");
System.out.println("CP: " + pokemon.getCp());
System.out.println("IV: " + iv + "%");
System.out.println("Height: " + pokemon.getHeightM() + "m");
System.out.println("Weight: " + pokemon.getWeightKg() + "kg");
System.out.println("Move 1: " + pokemon.getMove1());
System.out.println("Move 2: " + pokemon.getMove2());
//Rename the pokemon to <Name> IV%
pokemon.renamePokemon(name + " " + iv + "%");
//Set pokemon with IV above 90% as favorite
if (iv > 90) {
pokemon.setFavoritePokemon(true);
}
}
}
}
} else {
System.out.println("Skipping Pokemon, we have no Pokeballs!");
}
// Wait for animation before catching next pokemon
Thread.sleep(3000 + random.nextInt(1000));
} else {
System.out.println("Failed to encounter pokemon: " + encounter.getEncounterResult());
}
}
} catch (InterruptedException e) {
return;
}
}
Aggregations