-
Notifications
You must be signed in to change notification settings - Fork 326
added the ability to use potions & revive on pokemon #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cf909d1
fbf28fb
e7c8afd
8ca754d
7ec1fb8
61bf612
c0043be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,4 +38,28 @@ public ItemId getItemId() { | |
| public boolean isUnseen() { | ||
| return proto.getUnseen(); | ||
| } | ||
|
|
||
| /** | ||
| * Check if the item it's a potion | ||
| * | ||
| * @return true if the item it's a potion | ||
| */ | ||
| public boolean isPotion() { | ||
| return getItemId() == ItemId.ITEM_POTION | ||
| || getItemId() == ItemId.ITEM_SUPER_POTION | ||
| || getItemId() == ItemId.ITEM_HYPER_POTION | ||
| || getItemId() == ItemId.ITEM_MAX_POTION | ||
| ; | ||
| } | ||
|
|
||
| /** | ||
| * Check if the item it's a revive | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing dot |
||
| * | ||
| * @return true if the item it's a revive | ||
| */ | ||
| public boolean isRevive() { | ||
| return getItemId() == ItemId.ITEM_REVIVE | ||
| || getItemId() == ItemId.ITEM_MAX_REVIVE | ||
| ; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,23 +24,28 @@ | |
| import POGOProtos.Networking.Requests.Messages.NicknamePokemonMessageOuterClass.NicknamePokemonMessage; | ||
| import POGOProtos.Networking.Requests.Messages.ReleasePokemonMessageOuterClass.ReleasePokemonMessage; | ||
| import POGOProtos.Networking.Requests.Messages.SetFavoritePokemonMessageOuterClass.SetFavoritePokemonMessage; | ||
| import POGOProtos.Networking.Requests.Messages.UpgradePokemonMessageOuterClass; | ||
| import POGOProtos.Networking.Requests.Messages.UpgradePokemonMessageOuterClass.UpgradePokemonMessage; | ||
| import POGOProtos.Networking.Requests.Messages.UseItemPotionMessageOuterClass; | ||
| import POGOProtos.Networking.Requests.Messages.UseItemReviveMessageOuterClass; | ||
| import POGOProtos.Networking.Requests.RequestTypeOuterClass.RequestType; | ||
| import POGOProtos.Networking.Responses.EvolvePokemonResponseOuterClass.EvolvePokemonResponse; | ||
| import POGOProtos.Networking.Responses.NicknamePokemonResponseOuterClass.NicknamePokemonResponse; | ||
| import POGOProtos.Networking.Responses.RecycleInventoryItemResponseOuterClass; | ||
| import POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse; | ||
| import POGOProtos.Networking.Responses.ReleasePokemonResponseOuterClass.ReleasePokemonResponse.Result; | ||
| import POGOProtos.Networking.Responses.SetFavoritePokemonResponseOuterClass.SetFavoritePokemonResponse; | ||
| import POGOProtos.Networking.Responses.UpgradePokemonResponseOuterClass; | ||
| import POGOProtos.Networking.Responses.UpgradePokemonResponseOuterClass.UpgradePokemonResponse; | ||
| import POGOProtos.Networking.Responses.UseItemPotionResponseOuterClass; | ||
| import POGOProtos.Networking.Responses.UseItemReviveResponseOuterClass; | ||
| import com.google.protobuf.InvalidProtocolBufferException; | ||
| import com.pokegoapi.api.PokemonGo; | ||
| import com.pokegoapi.api.inventory.Item; | ||
| import com.pokegoapi.api.map.pokemon.EvolutionResult; | ||
| import com.pokegoapi.exceptions.LoginFailedException; | ||
| import com.pokegoapi.exceptions.RemoteServerException; | ||
| import com.pokegoapi.main.ServerRequest; | ||
| import com.pokegoapi.util.Log; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| /** | ||
|
|
@@ -52,13 +57,17 @@ public class Pokemon { | |
| private final PokemonGo pgo; | ||
| private PokemonData proto; | ||
| private PokemonMeta meta; | ||
| @Getter | ||
| @Setter | ||
| private int stamina; | ||
|
|
||
| // API METHODS // | ||
|
|
||
| // DELEGATE METHODS BELOW // | ||
| public Pokemon(PokemonGo api, PokemonData proto) { | ||
| this.pgo = api; | ||
| this.proto = proto; | ||
| this.stamina = proto.getStamina(); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -124,14 +133,14 @@ public NicknamePokemonResponse.Result renamePokemon(String nickname) | |
| } | ||
|
|
||
| /** | ||
| * Function to mark the pokemon as favorite or not. | ||
| * | ||
| * @param markFavorite Mark Pokemon as Favorite? | ||
| * @return the SetFavoritePokemonResponse.Result | ||
| * @throws LoginFailedException the login failed exception | ||
| * @throws RemoteServerException the remote server exception | ||
| */ | ||
| public SetFavoritePokemonResponse.Result setFavoritePokemon(boolean markFavorite) | ||
| * Function to mark the pokemon as favorite or not. | ||
| * | ||
| * @param markFavorite Mark Pokemon as Favorite? | ||
| * @return the SetFavoritePokemonResponse.Result | ||
| * @throws LoginFailedException the login failed exception | ||
| * @throws RemoteServerException the remote server exception | ||
| */ | ||
| public SetFavoritePokemonResponse.Result setFavoritePokemon(boolean markFavorite) | ||
| throws LoginFailedException, RemoteServerException { | ||
| SetFavoritePokemonMessage reqMsg = SetFavoritePokemonMessage.newBuilder() | ||
| .setPokemonId(getId()) | ||
|
|
@@ -250,10 +259,6 @@ public int getCp() { | |
| return proto.getCp(); | ||
| } | ||
|
|
||
| public int getStamina() { | ||
| return proto.getStamina(); | ||
| } | ||
|
|
||
| public int getMaxStamina() { | ||
| return proto.getStaminaMax(); | ||
| } | ||
|
|
@@ -305,9 +310,10 @@ public int getIndividualDefense() { | |
| public int getIndividualStamina() { | ||
| return proto.getIndividualStamina(); | ||
| } | ||
|
|
||
| /** | ||
| * Calculates the pokemons IV ratio. | ||
| * | ||
| * @return the pokemons IV ratio as a double between 0 and 1.0, 1.0 being perfect IVs | ||
| */ | ||
| public double getIvRatio() { | ||
|
|
@@ -387,4 +393,138 @@ public double getBaseFleeRate() { | |
| public PokemonIdOuterClass.PokemonId getParent() { | ||
| return getMeta().getParentId(); | ||
| } | ||
|
|
||
| /** | ||
| * Check if pokemon its injured but not fainted. need potions to heal | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dot |
||
| * | ||
| * @return true if pokemon is injured | ||
| */ | ||
| public boolean isInjured() { | ||
| return !isFainted() && getStamina() < getMaxStamina(); | ||
| } | ||
|
|
||
| /** | ||
| * check if a pokemon it's died (fainted). need a revive to resurrect | ||
| * | ||
| * @return true if a pokemon is fainted | ||
| */ | ||
| public boolean isFainted() { | ||
| return getStamina() == 0; | ||
| } | ||
|
|
||
| /** | ||
| * Heal a pokemon, using various fallbacks for potions | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dot |
||
| * | ||
| * @return Result, ERROR_CANNOT_USE if the requirements arent met | ||
| */ | ||
| public UseItemPotionResponseOuterClass.UseItemPotionResponse.Result heal() | ||
| throws LoginFailedException, RemoteServerException { | ||
|
|
||
| if (!isInjured()) | ||
| return UseItemPotionResponseOuterClass.UseItemPotionResponse.Result.ERROR_CANNOT_USE; | ||
|
|
||
| if (pgo.getInventories().getItemBag().getItem(ItemId.ITEM_POTION).getCount() > 0) | ||
| return usePotion(ItemId.ITEM_POTION); | ||
|
|
||
| if (pgo.getInventories().getItemBag().getItem(ItemId.ITEM_SUPER_POTION).getCount() > 0) | ||
| return usePotion(ItemId.ITEM_SUPER_POTION); | ||
|
|
||
| if (pgo.getInventories().getItemBag().getItem(ItemId.ITEM_HYPER_POTION).getCount() > 0) | ||
| return usePotion(ItemId.ITEM_HYPER_POTION); | ||
|
|
||
| if (pgo.getInventories().getItemBag().getItem(ItemId.ITEM_MAX_POTION).getCount() > 0) | ||
| return usePotion(ItemId.ITEM_MAX_POTION); | ||
|
|
||
| return UseItemPotionResponseOuterClass.UseItemPotionResponse.Result.ERROR_CANNOT_USE; | ||
| } | ||
|
|
||
| /** | ||
| * use a potion on that pokemon. Will check if there is enough potions & if the pokemon need | ||
| * to be healed. | ||
| * | ||
| * @return Result, ERROR_CANNOT_USE if the requirements arent met | ||
| */ | ||
| public UseItemPotionResponseOuterClass.UseItemPotionResponse.Result usePotion(ItemId itemId) | ||
| throws LoginFailedException, RemoteServerException { | ||
|
|
||
| Item potion = pgo.getInventories().getItemBag().getItem(itemId); | ||
| //some sanity check, to prevent wrong use of this call | ||
| if (!potion.isPotion() || potion.getCount() < 1 || !isInjured()) | ||
| return UseItemPotionResponseOuterClass.UseItemPotionResponse.Result.ERROR_CANNOT_USE; | ||
|
|
||
| UseItemPotionMessageOuterClass.UseItemPotionMessage reqMsg = UseItemPotionMessageOuterClass.UseItemPotionMessage | ||
| .newBuilder() | ||
| .setItemId(itemId) | ||
| .setPokemonId(getId()) | ||
| .build(); | ||
|
|
||
| ServerRequest serverRequest = new ServerRequest(RequestType.USE_ITEM_POTION, reqMsg); | ||
| pgo.getRequestHandler().sendServerRequests(serverRequest); | ||
|
|
||
| UseItemPotionResponseOuterClass.UseItemPotionResponse response; | ||
| try { | ||
| response = UseItemPotionResponseOuterClass.UseItemPotionResponse.parseFrom(serverRequest.getData()); | ||
| if (response.getResult() == UseItemPotionResponseOuterClass.UseItemPotionResponse.Result.SUCCESS) { | ||
| setStamina(response.getStamina()); | ||
| } | ||
| return response.getResult(); | ||
| } catch (InvalidProtocolBufferException e) { | ||
| throw new RemoteServerException(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Revive a pokemon, using various fallbacks for revive items | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dot |
||
| * | ||
| * @return Result, ERROR_CANNOT_USE if the requirements arent met | ||
| */ | ||
| public UseItemReviveResponseOuterClass.UseItemReviveResponse.Result revive() | ||
| throws LoginFailedException, RemoteServerException { | ||
|
|
||
| if (!isFainted()) | ||
| return UseItemReviveResponseOuterClass.UseItemReviveResponse.Result.ERROR_CANNOT_USE; | ||
|
|
||
| if (pgo.getInventories().getItemBag().getItem(ItemId.ITEM_REVIVE).getCount() > 0) | ||
| return useRevive(ItemId.ITEM_REVIVE); | ||
|
|
||
| if (pgo.getInventories().getItemBag().getItem(ItemId.ITEM_MAX_REVIVE).getCount() > 0) | ||
| return useRevive(ItemId.ITEM_MAX_REVIVE); | ||
|
|
||
| return UseItemReviveResponseOuterClass.UseItemReviveResponse.Result.ERROR_CANNOT_USE; | ||
| } | ||
|
|
||
| /** | ||
| * Use a revive item on the pokemon. Will check if there is enough revive & if the pokemon need | ||
| * to be revived. | ||
| * | ||
| * @return Result, ERROR_CANNOT_USE if the requirements arent met | ||
| */ | ||
| public UseItemReviveResponseOuterClass.UseItemReviveResponse.Result useRevive(ItemId itemId) | ||
| throws LoginFailedException, RemoteServerException { | ||
|
|
||
| Item item = pgo.getInventories().getItemBag().getItem(itemId); | ||
| if (!item.isRevive() || item.getCount() < 1 || !isFainted()) | ||
| return UseItemReviveResponseOuterClass.UseItemReviveResponse.Result.ERROR_CANNOT_USE; | ||
|
|
||
| UseItemReviveMessageOuterClass.UseItemReviveMessage reqMsg = UseItemReviveMessageOuterClass.UseItemReviveMessage | ||
| .newBuilder() | ||
| .setItemId(itemId) | ||
| .setPokemonId(getId()) | ||
| .build(); | ||
|
|
||
| ServerRequest serverRequest = new ServerRequest(RequestType.USE_ITEM_REVIVE, reqMsg); | ||
| pgo.getRequestHandler().sendServerRequests(serverRequest); | ||
|
|
||
| UseItemReviveResponseOuterClass.UseItemReviveResponse response; | ||
| try { | ||
| response = UseItemReviveResponseOuterClass.UseItemReviveResponse.parseFrom(serverRequest.getData()); | ||
| if (response.getResult() == UseItemReviveResponseOuterClass.UseItemReviveResponse.Result.SUCCESS) { | ||
| setStamina(response.getStamina()); | ||
| } | ||
| return response.getResult(); | ||
| } catch (InvalidProtocolBufferException e) { | ||
| throw new RemoteServerException(e); | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing dot