use of com.pokegoapi.exceptions.NoSuchItemException in project PokeGOAPI-Java by Grover-c13.
the class CatchPokemonAtAreaExample method main.
/**
* Catches a pokemon at an area.
*
* @param args args
*/
public static void main(String[] args) {
OkHttpClient http = new OkHttpClient();
final PokemonGo api = new PokemonGo(http);
try {
HashProvider hasher = ExampleConstants.getHashProvider();
api.login(new PtcCredentialProvider(http, ExampleConstants.LOGIN, ExampleConstants.PASSWORD), hasher);
api.setLocation(ExampleConstants.LATITUDE, ExampleConstants.LONGITUDE, ExampleConstants.ALTITUDE);
// Catch all pokemon in the current area
catchArea(api);
MapObjects mapObjects = api.getMap().getMapObjects();
//Find all pokestops with pokemon nearby
List<Pokestop> travelPokestops = new ArrayList<>();
Set<NearbyPokemon> nearby = mapObjects.getNearby();
for (NearbyPokemon nearbyPokemon : nearby) {
String fortId = nearbyPokemon.getFortId();
//Check if nearby pokemon is near a pokestop
if (fortId != null && fortId.length() > 0) {
//Find the pokestop with the fort id of the nearby pokemon
Pokestop pokestop = mapObjects.getPokestop(fortId);
if (pokestop != null && !travelPokestops.contains(pokestop)) {
travelPokestops.add(pokestop);
}
}
}
//Sort from closest to farthest
Collections.sort(travelPokestops, new Comparator<Pokestop>() {
@Override
public int compare(Pokestop primary, Pokestop secondary) {
double lat = api.getLatitude();
double lng = api.getLongitude();
double distance1 = MapUtil.distFrom(primary.getLatitude(), primary.getLongitude(), lat, lng);
double distance2 = MapUtil.distFrom(secondary.getLatitude(), secondary.getLongitude(), lat, lng);
return Double.compare(distance1, distance2);
}
});
for (Pokestop pokestop : travelPokestops) {
Point destination = new Point(pokestop.getLatitude(), pokestop.getLongitude());
//Use the current player position as the source and the pokestop position as the destination
//Travel to Pokestop at 20KMPH
Path path = new Path(api.getPoint(), destination, 20.0);
System.out.println("Traveling to " + destination + " at 20KMPH!");
path.start(api);
try {
while (!path.isComplete()) {
//Calculate the desired intermediate point for the current time
Point point = path.calculateIntermediate(api);
//Set the API location to that point
api.setLatitude(point.getLatitude());
api.setLongitude(point.getLongitude());
//Sleep for 2 seconds before setting the location again
Thread.sleep(2000);
}
} catch (InterruptedException e) {
break;
}
System.out.println("Finished traveling to pokestop, catching pokemon.");
catchArea(api);
}
} catch (NoSuchItemException | RequestFailedException e) {
Log.e("Main", "An exception occurred while running example: ", e);
}
}
use of com.pokegoapi.exceptions.NoSuchItemException 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;
}
Aggregations