diff --git a/src/main/java/com/pokegoapi/api/map/pokemon/CatchItemResult.java b/src/main/java/com/pokegoapi/api/map/pokemon/CatchItemResult.java new file mode 100644 index 00000000..c4061af5 --- /dev/null +++ b/src/main/java/com/pokegoapi/api/map/pokemon/CatchItemResult.java @@ -0,0 +1,55 @@ +/* + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.pokegoapi.api.map.pokemon; + +import POGOProtos.Networking.Responses.UseItemCaptureResponseOuterClass; +import POGOProtos.Networking.Responses.UseItemCaptureResponseOuterClass.UseItemCaptureResponse; + +public class CatchItemResult { + private UseItemCaptureResponse proto; + + public CatchItemResult(UseItemCaptureResponse proto) { + this.proto = proto; + } + + public boolean getSuccess() { + return proto.getSuccess(); + } + + public double getItemCaptureMult() { + return proto.getItemCaptureMult(); + } + + public double getItemFleeMult() { + return proto.getItemFleeMult(); + } + + public boolean getStopMovement() { + return proto.getStopMovement(); + } + + public boolean getStopAttack() { + return proto.getStopAttack(); + } + + public boolean getTargetMax() { + return proto.getTargetMax(); + } + + public boolean getTargetSlow() { + return proto.getTargetSlow(); + } +} diff --git a/src/main/java/com/pokegoapi/api/map/pokemon/CatchablePokemon.java b/src/main/java/com/pokegoapi/api/map/pokemon/CatchablePokemon.java index 32ecd439..3f794d1f 100644 --- a/src/main/java/com/pokegoapi/api/map/pokemon/CatchablePokemon.java +++ b/src/main/java/com/pokegoapi/api/map/pokemon/CatchablePokemon.java @@ -22,10 +22,14 @@ import POGOProtos.Map.Pokemon.WildPokemonOuterClass.WildPokemon; import POGOProtos.Networking.Requests.Messages.CatchPokemonMessageOuterClass.CatchPokemonMessage; import POGOProtos.Networking.Requests.Messages.EncounterMessageOuterClass; +import POGOProtos.Networking.Requests.Messages.UseItemCaptureMessageOuterClass; +import POGOProtos.Networking.Requests.Messages.UseItemCaptureMessageOuterClass.UseItemCaptureMessage; import POGOProtos.Networking.Requests.RequestTypeOuterClass; import POGOProtos.Networking.Responses.CatchPokemonResponseOuterClass.CatchPokemonResponse; import POGOProtos.Networking.Responses.EncounterResponseOuterClass; import POGOProtos.Networking.Responses.EncounterResponseOuterClass.EncounterResponse; +import POGOProtos.Networking.Responses.UseItemCaptureResponseOuterClass; +import POGOProtos.Networking.Responses.UseItemCaptureResponseOuterClass.UseItemCaptureResponse; import com.google.protobuf.InvalidProtocolBufferException; import com.pokegoapi.api.PokemonGo; import com.pokegoapi.api.inventory.ItemBag; @@ -150,6 +154,36 @@ public EncounterResult encounterPokemon() throws LoginFailedException, return new EncounterResult(response); } + /** + * Tries to catch a pokemon (will attempt to use a pokeball, if you have + * none will use greatball etc) and uwill use a single razz berry if available. + * + * @return CatchResult + * @throws LoginFailedException + * if failed to login + * @throws RemoteServerException + * if the server failed to respond + */ + public CatchResult catchPokemonWithRazzBerry() throws LoginFailedException, + RemoteServerException { + Pokeball pokeball; + + ItemBag bag = api.getInventories().getItemBag(); + if (bag.getItem(ItemId.ITEM_POKE_BALL).getCount() > 0) { + pokeball = Pokeball.POKEBALL; + } else if (bag.getItem(ItemId.ITEM_GREAT_BALL).getCount() > 0) { + pokeball = Pokeball.GREATBALL; + } else if (bag.getItem(ItemId.ITEM_ULTRA_BALL).getCount() > 0) { + pokeball = Pokeball.ULTRABALL; + } else { + pokeball = Pokeball.MASTERBALL; + } + + useItem(ItemId.ITEM_RAZZ_BERRY); + return catchPokemon(pokeball, -1, -1); + } + + /** * Tries to catch a pokemon (will attempt to use a pokeball, if you have * none will use greatball etc). @@ -178,6 +212,8 @@ public CatchResult catchPokemon() throws LoginFailedException, return catchPokemon(pokeball); } + + /** * Tries to catch a pokeball with the given type. * @@ -213,6 +249,54 @@ public CatchResult catchPokemon(Pokeball pokeball, int amount) 0.85 + Math.random() * 0.15, pokeball, amount); } + /** + * Tried to catch a pokemon with given pokeball and max number of pokeballs. + * + * @param pokeball + * Type of pokeball + * @param amount + * Max number of pokeballs to use + * @param razberryLimit + * Max number of razberrys to use + * @return CatchResult + * @throws LoginFailedException + * if failed to login + * @throws RemoteServerException + * if the server failed to respond + */ + public CatchResult catchPokemon(Pokeball pokeball, int amount, int razberryLimit) + throws LoginFailedException, RemoteServerException { + return catchPokemon(1.0, 1.95 + Math.random() * 0.05, + 0.85 + Math.random() * 0.15, pokeball, razberryLimit); + } + + /** + * Tries to catch a pokemon. + * + * @param normalizedHitPosition + * the normalized hit position + * @param normalizedReticleSize + * the normalized hit reticle + * @param spinModifier + * the spin modifier + * @param type + * Type of pokeball to throw + * @param amount + * Max number of Pokeballs to throw, negative number for + * unlimited + * @return CatchResult of resulted try to catch pokemon + * @throws LoginFailedException + * if failed to login + * @throws RemoteServerException + * if the server failed to respond + */ + public CatchResult catchPokemon(double normalizedHitPosition, + double normalizedReticleSize, double spinModifier, Pokeball type, + int amount) throws LoginFailedException, RemoteServerException { + + return catchPokemon(normalizedHitPosition, normalizedReticleSize, spinModifier, type, amount, -1); + } + /** * Tries to catch a pokemon. * @@ -227,6 +311,8 @@ public CatchResult catchPokemon(Pokeball pokeball, int amount) * @param amount * Max number of Pokeballs to throw, negative number for * unlimited + * @param razberriesLimit + * The maximum amount of razberries to use, -1 for unlimited * @return CatchResult of resulted try to catch pokemon * @throws LoginFailedException * if failed to login @@ -235,14 +321,21 @@ public CatchResult catchPokemon(Pokeball pokeball, int amount) */ public CatchResult catchPokemon(double normalizedHitPosition, double normalizedReticleSize, double spinModifier, Pokeball type, - int amount) throws LoginFailedException, RemoteServerException { + int amount, int razberriesLimit) throws LoginFailedException, RemoteServerException { if (!isEncountered()) { return new CatchResult(); } + int razberries = 0; int numThrows = 0; CatchPokemonResponse response = null; do { + + if (razberries < razberriesLimit || razberriesLimit == -1) { + useItem(ItemId.ITEM_RAZZ_BERRY); + razberries++; + } + CatchPokemonMessage reqMsg = CatchPokemonMessage.newBuilder() .setEncounterId(getEncounterId()).setHitPokemon(true) .setNormalizedHitPosition(normalizedHitPosition) @@ -274,6 +367,38 @@ public CatchResult catchPokemon(double normalizedHitPosition, return new CatchResult(response); } + /** + * Tries to use an item on a catchable pokemon (ie razzberry). + * + * @param item + * the item ID + * @return CatchItemResult info about the new modifiers about the pokemon (can move, item capture multi) eg + * @throws LoginFailedException + * if failed to login + * @throws RemoteServerException + * if the server failed to respond + */ + public CatchItemResult useItem(ItemId item) throws LoginFailedException, RemoteServerException { + + UseItemCaptureMessage reqMsg = UseItemCaptureMessage + .newBuilder() + .setEncounterId(this.getEncounterId()) + .setSpawnPointGuid(this.getSpawnPointId()) + .setItemId(item) + .build(); + + ServerRequest serverRequest = new ServerRequest( + RequestTypeOuterClass.RequestType.USE_ITEM_CAPTURE, reqMsg); + api.getRequestHandler().sendServerRequests(serverRequest); + UseItemCaptureResponse response = null; + try { + response = UseItemCaptureResponse.parseFrom(serverRequest.getData()); + } catch (InvalidProtocolBufferException e) { + throw new RemoteServerException(e); + } + return new CatchItemResult(response); + } + @Override public boolean equals(Object obj) { if (obj == this) { diff --git a/src/main/java/com/pokegoapi/examples/CatchPokemonAtAreaExample.java b/src/main/java/com/pokegoapi/examples/CatchPokemonAtAreaExample.java index fedd05d9..3ea40914 100644 --- a/src/main/java/com/pokegoapi/examples/CatchPokemonAtAreaExample.java +++ b/src/main/java/com/pokegoapi/examples/CatchPokemonAtAreaExample.java @@ -36,6 +36,7 @@ import com.pokegoapi.api.map.pokemon.CatchResult; import com.pokegoapi.api.map.pokemon.CatchablePokemon; import com.pokegoapi.api.map.pokemon.EncounterResult; +import com.pokegoapi.auth.GoogleLogin; import com.pokegoapi.auth.PtcLogin; import com.pokegoapi.exceptions.LoginFailedException; import com.pokegoapi.exceptions.RemoteServerException; @@ -69,7 +70,7 @@ public static void main(String[] args) { // if encounter was succesful, catch if (encResult.wasSuccessful()) { System.out.println("Encounted:" + cp.getPokemonId()); - CatchResult result = cp.catchPokemon(); + CatchResult result = cp.catchPokemonWithRazzBerry(); System.out.println("Attempt to catch:" + cp.getPokemonId() + " " + result.getStatus()); }