Skip to content

Commit

Permalink
Merge pull request Grover-c13#141 from mjmfighter/pokemon_null_pointe…
Browse files Browse the repository at this point in the history
…r_fix

null pointer fix
  • Loading branch information
Grover-c13 authored Jul 24, 2016
2 parents 0763543 + ac3c15f commit a2f3be9
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 34 deletions.
18 changes: 18 additions & 0 deletions .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Contributing
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.

## Issues
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).

## Feature Requests
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.

## Pull Requests
If you consider submitting a pull request, please note the following:

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.
2. Pleae make sure you follow the projects code style. To make sure you did, you can use `./gradlew checkstyleMain`.
3. The project is licensed under [GNU GPLv3](../LICENSE.txt) thus all code you submit will be subject to this license.

## Contact
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/).
19 changes: 19 additions & 0 deletions .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
**Description:**
[Short description of the issue observed. If this ia feature request you can modify the template as required.]

**Steps to reproduce:**

1. [Step 1]
2. [Step 2]

**Expected behavior:**
[What should happen?]

**Actual behavior:**
[What actually happens]

**Stacktrace (If it's a crash):**
[Please use pastebin if it's too long]

**Version:**
[The version of the lib you used]
13 changes: 13 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### Prerequisites (Remove this section if you want)
Make sure you...

* Follow the [contribution guidelines](CONTRIBUTING.md)
* Follow the code style (run `./gradlew checkstyleMain`)
* Submit this PR against the `Development` branch.

**Fixed issue:** [Reference the issue number here, or remove if not a fix]

**Changes made:**

* List your changes here
* Change 2...
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ jdk:
branches:
only:
- master
- Development

before_cache:
- rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ checkstyleMain.doLast {
}

dependencies {
compile 'com.google.code.gson:gson:2.7'
compile 'com.squareup.okio:okio:1.9.0'
compile 'com.squareup.moshi:moshi:1.2.0'
compile 'com.annimon:stream:1.1.1'
compile 'com.squareup.okhttp3:okhttp:3.4.0-RC1'
compile 'com.google.protobuf:protobuf-java:3.0.0-beta-3'
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/pokegoapi/api/inventory/Inventories.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void updateInventories(boolean forceUpdate) throws LoginFailedException,

// pokebank
if (itemData.getPokemonData().getPokemonId() != PokemonId.MISSINGNO) {
pokebank.addPokemon(new Pokemon(inventoryItem.getInventoryItemData().getPokemonData()));
pokebank.addPokemon(new Pokemon(api, inventoryItem.getInventoryItemData().getPokemonData()));
}

// items
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/pokegoapi/api/inventory/ItemBag.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.main.ServerRequest;

import java.util.Collection;
import java.util.HashMap;

/**
Expand Down Expand Up @@ -102,4 +103,9 @@ public Item getItem(ItemId type) {

return items.get(type);
}


public Collection<Item> getItems() {
return items.values();
}
}
1 change: 0 additions & 1 deletion src/main/java/com/pokegoapi/api/inventory/PokeBank.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public PokeBank(PokemonGo instance) {
* @param pokemon Pokemon to add to the inventory
*/
public void addPokemon(final Pokemon pokemon) {
pokemon.setPgo(instance);
List<Pokemon> alreadyAdded = Stream.of(pokemons).filter(new Predicate<Pokemon>() {
@Override
public boolean test(Pokemon testPokemon) {
Expand Down
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
10 changes: 8 additions & 2 deletions src/main/java/com/pokegoapi/api/map/pokemon/EvolutionResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@
package com.pokegoapi.api.map.pokemon;

import POGOProtos.Networking.Responses.EvolvePokemonResponseOuterClass;
import com.pokegoapi.api.PokemonGo;
import com.pokegoapi.api.pokemon.Pokemon;

public class EvolutionResult {

private EvolvePokemonResponseOuterClass.EvolvePokemonResponse proto;
private Pokemon pokemon;

public EvolutionResult(EvolvePokemonResponseOuterClass.EvolvePokemonResponse proto) {
/**
* The evolution result.
* @param api PokemonGo api
* @param proto Pokemon proto
*/
public EvolutionResult(PokemonGo api, EvolvePokemonResponseOuterClass.EvolvePokemonResponse proto) {
this.proto = proto;
this.pokemon = new Pokemon(proto.getEvolvedPokemonData());
this.pokemon = new Pokemon(api, proto.getEvolvedPokemonData());
}

public EvolvePokemonResponseOuterClass.EvolvePokemonResponse.Result getResult() {
Expand Down
Loading

0 comments on commit a2f3be9

Please sign in to comment.