Skip to content

Commit 27392b7

Browse files
committed
Added using capture items on CatchablePokemon, added overloaded methods for using razzberrys.
1 parent 01bf715 commit 27392b7

3 files changed

Lines changed: 183 additions & 2 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*/
15+
16+
package com.pokegoapi.api.map.pokemon;
17+
18+
import POGOProtos.Networking.Responses.UseItemCaptureResponseOuterClass;
19+
import POGOProtos.Networking.Responses.UseItemCaptureResponseOuterClass.UseItemCaptureResponse;
20+
21+
public class CatchItemResult {
22+
private UseItemCaptureResponse proto;
23+
24+
public CatchItemResult(UseItemCaptureResponse proto) {
25+
this.proto = proto;
26+
}
27+
28+
public boolean getSuccess() {
29+
return proto.getSuccess();
30+
}
31+
32+
public double getItemCaptureMult() {
33+
return proto.getItemCaptureMult();
34+
}
35+
36+
public double getItemFleeMult() {
37+
return proto.getItemFleeMult();
38+
}
39+
40+
public boolean getStopMovement() {
41+
return proto.getStopMovement();
42+
}
43+
44+
public boolean getStopAttack() {
45+
return proto.getStopAttack();
46+
}
47+
48+
public boolean getTargetMax() {
49+
return proto.getTargetMax();
50+
}
51+
52+
public boolean getTargetSlow() {
53+
return proto.getTargetSlow();
54+
}
55+
}

src/main/java/com/pokegoapi/api/map/pokemon/CatchablePokemon.java

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222
import POGOProtos.Map.Pokemon.WildPokemonOuterClass.WildPokemon;
2323
import POGOProtos.Networking.Requests.Messages.CatchPokemonMessageOuterClass.CatchPokemonMessage;
2424
import POGOProtos.Networking.Requests.Messages.EncounterMessageOuterClass;
25+
import POGOProtos.Networking.Requests.Messages.UseItemCaptureMessageOuterClass;
26+
import POGOProtos.Networking.Requests.Messages.UseItemCaptureMessageOuterClass.UseItemCaptureMessage;
2527
import POGOProtos.Networking.Requests.RequestTypeOuterClass;
2628
import POGOProtos.Networking.Responses.CatchPokemonResponseOuterClass.CatchPokemonResponse;
2729
import POGOProtos.Networking.Responses.EncounterResponseOuterClass;
2830
import POGOProtos.Networking.Responses.EncounterResponseOuterClass.EncounterResponse;
31+
import POGOProtos.Networking.Responses.UseItemCaptureResponseOuterClass;
32+
import POGOProtos.Networking.Responses.UseItemCaptureResponseOuterClass.UseItemCaptureResponse;
2933
import com.google.protobuf.InvalidProtocolBufferException;
3034
import com.pokegoapi.api.PokemonGo;
3135
import com.pokegoapi.api.inventory.ItemBag;
@@ -150,6 +154,36 @@ public EncounterResult encounterPokemon() throws LoginFailedException,
150154
return new EncounterResult(response);
151155
}
152156

157+
/**
158+
* Tries to catch a pokemon (will attempt to use a pokeball, if you have
159+
* none will use greatball etc) and uwill use a single razz berry if available.
160+
*
161+
* @return CatchResult
162+
* @throws LoginFailedException
163+
* if failed to login
164+
* @throws RemoteServerException
165+
* if the server failed to respond
166+
*/
167+
public CatchResult catchPokemonWithRazzBerry() throws LoginFailedException,
168+
RemoteServerException {
169+
Pokeball pokeball;
170+
171+
ItemBag bag = api.getInventories().getItemBag();
172+
if (bag.getItem(ItemId.ITEM_POKE_BALL).getCount() > 0) {
173+
pokeball = Pokeball.POKEBALL;
174+
} else if (bag.getItem(ItemId.ITEM_GREAT_BALL).getCount() > 0) {
175+
pokeball = Pokeball.GREATBALL;
176+
} else if (bag.getItem(ItemId.ITEM_ULTRA_BALL).getCount() > 0) {
177+
pokeball = Pokeball.ULTRABALL;
178+
} else {
179+
pokeball = Pokeball.MASTERBALL;
180+
}
181+
182+
useItem(ItemId.ITEM_RAZZ_BERRY);
183+
return catchPokemon(pokeball, -1, -1);
184+
}
185+
186+
153187
/**
154188
* Tries to catch a pokemon (will attempt to use a pokeball, if you have
155189
* none will use greatball etc).
@@ -178,6 +212,8 @@ public CatchResult catchPokemon() throws LoginFailedException,
178212
return catchPokemon(pokeball);
179213
}
180214

215+
216+
181217
/**
182218
* Tries to catch a pokeball with the given type.
183219
*
@@ -213,6 +249,54 @@ public CatchResult catchPokemon(Pokeball pokeball, int amount)
213249
0.85 + Math.random() * 0.15, pokeball, amount);
214250
}
215251

252+
/**
253+
* Tried to catch a pokemon with given pokeball and max number of pokeballs.
254+
*
255+
* @param pokeball
256+
* Type of pokeball
257+
* @param amount
258+
* Max number of pokeballs to use
259+
* @param razberryLimit
260+
* Max number of razberrys to use
261+
* @return CatchResult
262+
* @throws LoginFailedException
263+
* if failed to login
264+
* @throws RemoteServerException
265+
* if the server failed to respond
266+
*/
267+
public CatchResult catchPokemon(Pokeball pokeball, int amount, int razberryLimit)
268+
throws LoginFailedException, RemoteServerException {
269+
return catchPokemon(1.0, 1.95 + Math.random() * 0.05,
270+
0.85 + Math.random() * 0.15, pokeball, razberryLimit);
271+
}
272+
273+
/**
274+
* Tries to catch a pokemon.
275+
*
276+
* @param normalizedHitPosition
277+
* the normalized hit position
278+
* @param normalizedReticleSize
279+
* the normalized hit reticle
280+
* @param spinModifier
281+
* the spin modifier
282+
* @param type
283+
* Type of pokeball to throw
284+
* @param amount
285+
* Max number of Pokeballs to throw, negative number for
286+
* unlimited
287+
* @return CatchResult of resulted try to catch pokemon
288+
* @throws LoginFailedException
289+
* if failed to login
290+
* @throws RemoteServerException
291+
* if the server failed to respond
292+
*/
293+
public CatchResult catchPokemon(double normalizedHitPosition,
294+
double normalizedReticleSize, double spinModifier, Pokeball type,
295+
int amount) throws LoginFailedException, RemoteServerException {
296+
297+
return catchPokemon(normalizedHitPosition, normalizedReticleSize, spinModifier, type, amount, -1);
298+
}
299+
216300
/**
217301
* Tries to catch a pokemon.
218302
*
@@ -227,6 +311,8 @@ public CatchResult catchPokemon(Pokeball pokeball, int amount)
227311
* @param amount
228312
* Max number of Pokeballs to throw, negative number for
229313
* unlimited
314+
* @param razberriesLimit
315+
* The maximum amount of razberries to use, -1 for unlimited
230316
* @return CatchResult of resulted try to catch pokemon
231317
* @throws LoginFailedException
232318
* if failed to login
@@ -235,14 +321,21 @@ public CatchResult catchPokemon(Pokeball pokeball, int amount)
235321
*/
236322
public CatchResult catchPokemon(double normalizedHitPosition,
237323
double normalizedReticleSize, double spinModifier, Pokeball type,
238-
int amount) throws LoginFailedException, RemoteServerException {
324+
int amount, int razberriesLimit) throws LoginFailedException, RemoteServerException {
239325
if (!isEncountered()) {
240326
return new CatchResult();
241327
}
242328

329+
int razberries = 0;
243330
int numThrows = 0;
244331
CatchPokemonResponse response = null;
245332
do {
333+
334+
if (razberries < razberriesLimit || razberriesLimit == -1) {
335+
useItem(ItemId.ITEM_RAZZ_BERRY);
336+
razberries++;
337+
}
338+
246339
CatchPokemonMessage reqMsg = CatchPokemonMessage.newBuilder()
247340
.setEncounterId(getEncounterId()).setHitPokemon(true)
248341
.setNormalizedHitPosition(normalizedHitPosition)
@@ -274,6 +367,38 @@ public CatchResult catchPokemon(double normalizedHitPosition,
274367
return new CatchResult(response);
275368
}
276369

370+
/**
371+
* Tries to use an item on a catchable pokemon (ie razzberry).
372+
*
373+
* @param item
374+
* the item ID
375+
* @return CatchItemResult info about the new modifiers about the pokemon (can move, item capture multi) eg
376+
* @throws LoginFailedException
377+
* if failed to login
378+
* @throws RemoteServerException
379+
* if the server failed to respond
380+
*/
381+
public CatchItemResult useItem(ItemId item) throws LoginFailedException, RemoteServerException {
382+
383+
UseItemCaptureMessage reqMsg = UseItemCaptureMessage
384+
.newBuilder()
385+
.setEncounterId(this.getEncounterId())
386+
.setSpawnPointGuid(this.getSpawnPointId())
387+
.setItemId(item)
388+
.build();
389+
390+
ServerRequest serverRequest = new ServerRequest(
391+
RequestTypeOuterClass.RequestType.USE_ITEM_CAPTURE, reqMsg);
392+
api.getRequestHandler().sendServerRequests(serverRequest);
393+
UseItemCaptureResponse response = null;
394+
try {
395+
response = UseItemCaptureResponse.parseFrom(serverRequest.getData());
396+
} catch (InvalidProtocolBufferException e) {
397+
throw new RemoteServerException(e);
398+
}
399+
return new CatchItemResult(response);
400+
}
401+
277402
@Override
278403
public boolean equals(Object obj) {
279404
if (obj == this) {

src/main/java/com/pokegoapi/examples/CatchPokemonAtAreaExample.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.pokegoapi.api.map.pokemon.CatchResult;
3737
import com.pokegoapi.api.map.pokemon.CatchablePokemon;
3838
import com.pokegoapi.api.map.pokemon.EncounterResult;
39+
import com.pokegoapi.auth.GoogleLogin;
3940
import com.pokegoapi.auth.PtcLogin;
4041
import com.pokegoapi.exceptions.LoginFailedException;
4142
import com.pokegoapi.exceptions.RemoteServerException;
@@ -69,7 +70,7 @@ public static void main(String[] args) {
6970
// if encounter was succesful, catch
7071
if (encResult.wasSuccessful()) {
7172
System.out.println("Encounted:" + cp.getPokemonId());
72-
CatchResult result = cp.catchPokemon();
73+
CatchResult result = cp.catchPokemonWithRazzBerry();
7374
System.out.println("Attempt to catch:" + cp.getPokemonId() + " " + result.getStatus());
7475
}
7576

0 commit comments

Comments
 (0)