Skip to content

Commit

Permalink
Merge pull request Grover-c13#159 from Grover-c13/Gdev
Browse files Browse the repository at this point in the history
Gdev
  • Loading branch information
Grover-c13 authored Jul 23, 2016
2 parents 8bf8e2c + 27392b7 commit c1df2e1
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 2 deletions.
55 changes: 55 additions & 0 deletions src/main/java/com/pokegoapi/api/map/pokemon/CatchItemResult.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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();
}
}
127 changes: 126 additions & 1 deletion src/main/java/com/pokegoapi/api/map/pokemon/CatchablePokemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -178,6 +212,8 @@ public CatchResult catchPokemon() throws LoginFailedException,
return catchPokemon(pokeball);
}



/**
* Tries to catch a pokeball with the given type.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}

Expand Down

0 comments on commit c1df2e1

Please sign in to comment.