Skip to content

Commit a2f3be9

Browse files
authored
Merge pull request #141 from mjmfighter/pokemon_null_pointer_fix
null pointer fix
2 parents 0763543 + ac3c15f commit a2f3be9

17 files changed

+276
-34
lines changed

.github/CONTRIBUTING.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Contributing
2+
Thank you for wanting to help with this project! Pull requests and bug reports are always welcome, there are just some small things you should review before submitting.
3+
4+
## Issues
5+
If you find any issue with the library, feel free to open an issue report. If it's just a small issue you might want to consider just asking on Slack (see below).
6+
7+
## Feature Requests
8+
You may open an issue also to request new features. Make sure you describe what you are missing in the library and add any pointers someone might need to implement it.
9+
10+
## Pull Requests
11+
If you consider submitting a pull request, please note the following:
12+
13+
1. All pull requests **must** be submitted to the `Development` branch. The `master` branch is exclusively mutable by release. PRs against `master` will not be merged.
14+
2. Pleae make sure you follow the projects code style. To make sure you did, you can use `./gradlew checkstyleMain`.
15+
3. The project is licensed under [GNU GPLv3](../LICENSE.txt) thus all code you submit will be subject to this license.
16+
17+
## Contact
18+
If you have any questions regarding the library you can ask those on the `#javaapi` channel on the [Pokemon GO Reverse Engineering Slack](https://pkre.slack.com/). You can [get your invite here](https://shielded-earth-81203.herokuapp.com/).

.github/ISSUE_TEMPLATE.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
**Description:**
2+
[Short description of the issue observed. If this ia feature request you can modify the template as required.]
3+
4+
**Steps to reproduce:**
5+
6+
1. [Step 1]
7+
2. [Step 2]
8+
9+
**Expected behavior:**
10+
[What should happen?]
11+
12+
**Actual behavior:**
13+
[What actually happens]
14+
15+
**Stacktrace (If it's a crash):**
16+
[Please use pastebin if it's too long]
17+
18+
**Version:**
19+
[The version of the lib you used]

.github/PULL_REQUEST_TEMPLATE.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### Prerequisites (Remove this section if you want)
2+
Make sure you...
3+
4+
* Follow the [contribution guidelines](CONTRIBUTING.md)
5+
* Follow the code style (run `./gradlew checkstyleMain`)
6+
* Submit this PR against the `Development` branch.
7+
8+
**Fixed issue:** [Reference the issue number here, or remove if not a fix]
9+
10+
**Changes made:**
11+
12+
* List your changes here
13+
* Change 2...

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ jdk:
66
branches:
77
only:
88
- master
9+
- Development
910

1011
before_cache:
1112
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock

build.gradle

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ checkstyleMain.doLast {
9494
}
9595

9696
dependencies {
97-
compile 'com.google.code.gson:gson:2.7'
97+
compile 'com.squareup.okio:okio:1.9.0'
98+
compile 'com.squareup.moshi:moshi:1.2.0'
9899
compile 'com.annimon:stream:1.1.1'
99100
compile 'com.squareup.okhttp3:okhttp:3.4.0-RC1'
100101
compile 'com.google.protobuf:protobuf-java:3.0.0-beta-3'

src/main/java/com/pokegoapi/api/inventory/Inventories.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void updateInventories(boolean forceUpdate) throws LoginFailedException,
125125

126126
// pokebank
127127
if (itemData.getPokemonData().getPokemonId() != PokemonId.MISSINGNO) {
128-
pokebank.addPokemon(new Pokemon(inventoryItem.getInventoryItemData().getPokemonData()));
128+
pokebank.addPokemon(new Pokemon(api, inventoryItem.getInventoryItemData().getPokemonData()));
129129
}
130130

131131
// items

src/main/java/com/pokegoapi/api/inventory/ItemBag.java

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.pokegoapi.exceptions.RemoteServerException;
3030
import com.pokegoapi.main.ServerRequest;
3131

32+
import java.util.Collection;
3233
import java.util.HashMap;
3334

3435
/**
@@ -102,4 +103,9 @@ public Item getItem(ItemId type) {
102103

103104
return items.get(type);
104105
}
106+
107+
108+
public Collection<Item> getItems() {
109+
return items.values();
110+
}
105111
}

src/main/java/com/pokegoapi/api/inventory/PokeBank.java

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public PokeBank(PokemonGo instance) {
4242
* @param pokemon Pokemon to add to the inventory
4343
*/
4444
public void addPokemon(final Pokemon pokemon) {
45-
pokemon.setPgo(instance);
4645
List<Pokemon> alreadyAdded = Stream.of(pokemons).filter(new Predicate<Pokemon>() {
4746
@Override
4847
public boolean test(Pokemon testPokemon) {
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

+126-1
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/api/map/pokemon/EvolutionResult.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@
1616
package com.pokegoapi.api.map.pokemon;
1717

1818
import POGOProtos.Networking.Responses.EvolvePokemonResponseOuterClass;
19+
import com.pokegoapi.api.PokemonGo;
1920
import com.pokegoapi.api.pokemon.Pokemon;
2021

2122
public class EvolutionResult {
2223

2324
private EvolvePokemonResponseOuterClass.EvolvePokemonResponse proto;
2425
private Pokemon pokemon;
2526

26-
public EvolutionResult(EvolvePokemonResponseOuterClass.EvolvePokemonResponse proto) {
27+
/**
28+
* The evolution result.
29+
* @param api PokemonGo api
30+
* @param proto Pokemon proto
31+
*/
32+
public EvolutionResult(PokemonGo api, EvolvePokemonResponseOuterClass.EvolvePokemonResponse proto) {
2733
this.proto = proto;
28-
this.pokemon = new Pokemon(proto.getEvolvedPokemonData());
34+
this.pokemon = new Pokemon(api, proto.getEvolvedPokemonData());
2935
}
3036

3137
public EvolvePokemonResponseOuterClass.EvolvePokemonResponse.Result getResult() {

0 commit comments

Comments
 (0)