Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/com/pokegoapi/api/inventory/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,28 @@ public ItemId getItemId() {
public boolean isUnseen() {
return proto.getUnseen();
}

/**
* Check if the item it's a potion
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing dot

*
* @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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
;
}
}
170 changes: 155 additions & 15 deletions src/main/java/com/pokegoapi/api/pokemon/Pokemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -250,10 +259,6 @@ public int getCp() {
return proto.getCp();
}

public int getStamina() {
return proto.getStamina();
}

public int getMaxStamina() {
return proto.getStaminaMax();
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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);
}
}

}