Skip to content

Commit

Permalink
Catching some exceptions when pokemonId is not recognized
Browse files Browse the repository at this point in the history
  • Loading branch information
igoticecream committed Oct 7, 2016
1 parent a4f2014 commit 71ab9ca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
18 changes: 14 additions & 4 deletions app/src/main/java/com/icecream/snorlax/module/Pokemons.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import static POGOProtos.Data.Capture.CaptureProbabilityOuterClass.CaptureProbability;
import static POGOProtos.Data.PokemonDataOuterClass.PokemonData;
import static POGOProtos.Enums.PokemonIdOuterClass.PokemonId;
import static POGOProtos.Enums.PokemonMoveOuterClass.PokemonMove;

@Singleton
Expand Down Expand Up @@ -123,19 +124,28 @@ public final class Pokemons {
mNames = resources.getStringArray(R.array.pokemon);
}

public Pokemons.Data with(PokemonData pokemonData) {
public Pokemons.Data with(PokemonData pokemonData) throws NullPointerException, IllegalArgumentException {
if (pokemonData == null) {
throw new NullPointerException("PokemonData cannot be null");
}
if (pokemonData.getPokemonId().equals(PokemonId.MISSINGNO) || pokemonData.getPokemonId().equals(PokemonId.UNRECOGNIZED)) {
throw new IllegalArgumentException("Unrecognized Pokemon Id");
}
return new Data(pokemonData);
}

public Probability with(CaptureProbability captureProbability) {
public Probability with(CaptureProbability captureProbability) throws NullPointerException {
if (captureProbability == null) {
throw new NullPointerException("CaptureProbability cannot be null");
}
return new Probability(captureProbability);
}

public final class Probability {

private final CaptureProbability mCaptureProbability;

Probability(CaptureProbability captureProbability) {
private Probability(CaptureProbability captureProbability) {
mCaptureProbability = captureProbability;
}

Expand Down Expand Up @@ -172,7 +182,7 @@ public final class Data {

private final PokemonData mPokemonData;

Data(PokemonData pokemonData) {
private Data(PokemonData pokemonData) {
mPokemonData = pokemonData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,11 @@ private void onWildEncounter(ByteString bytes) {
onEncounter(response.getWildPokemon().getPokemonData(), response.getCaptureProbability());
}
catch (InvalidProtocolBufferException | NullPointerException e) {
Log.d("EncounterResponse failed: %s" + e.getMessage());
Log.d("EncounterResponse failed: %s", e.getMessage());
Log.e(e);
}
catch (IllegalArgumentException e) {
Log.d("Cannot process IncenseEncounterResponse: %s", e.getMessage());
Log.e(e);
}
}
Expand All @@ -118,7 +122,11 @@ private void onDiskEncounter(ByteString bytes) {
onEncounter(response.getPokemonData(), response.getCaptureProbability());
}
catch (InvalidProtocolBufferException | NullPointerException e) {
Log.d("DiskEncounterResponse failed: %s" + e.getMessage());
Log.d("DiskEncounterResponse failed: %s", e.getMessage());
Log.e(e);
}
catch (IllegalArgumentException e) {
Log.d("Cannot process IncenseEncounterResponse: %s", e.getMessage());
Log.e(e);
}
}
Expand All @@ -129,12 +137,16 @@ private void onIncenseEncounter(ByteString bytes) {
onEncounter(response.getPokemonData(), response.getCaptureProbability());
}
catch (InvalidProtocolBufferException | NullPointerException e) {
Log.d("IncenseEncounterResponse failed: %s" + e.getMessage());
Log.d("IncenseEncounterResponse failed: %s", e.getMessage());
Log.e(e);
}
catch (IllegalArgumentException e) {
Log.d("Cannot process IncenseEncounterResponse: %s", e.getMessage());
Log.e(e);
}
}

private void onEncounter(PokemonData pokemonData, CaptureProbability captureProbability) {
private void onEncounter(PokemonData pokemonData, CaptureProbability captureProbability) throws NullPointerException, IllegalArgumentException {
Pokemons.Data data = mPokemons.with(pokemonData);
Pokemons.Probability probability = mPokemons.with(captureProbability);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.icecream.snorlax.module.feature.Feature;
import com.icecream.snorlax.module.feature.mitm.MitmListener;
import com.icecream.snorlax.module.feature.mitm.MitmProvider;
import com.icecream.snorlax.module.util.Log;

import static POGOProtos.Data.PokemonDataOuterClass.PokemonData;
import static POGOProtos.Enums.PokemonIdOuterClass.PokemonId;
Expand Down Expand Up @@ -103,19 +104,28 @@ private ByteString processInventory(GetInventoryResponse response) {
if (data.getPokemonData().getPokemonId() != PokemonId.MISSINGNO) {
PokemonData.Builder pokemon = data.getPokemonData().toBuilder();

//if (Strings.isEmpty(pokemon.getNickname())) {
try {
pokemon.setNickname(processNickname(data.getPokemonData()));
//}
data.setPokemonData(pokemon);
}
catch (NullPointerException e) {
Log.d("processNickname failed: %s", e.getMessage());
Log.e(e);
}
catch (IllegalArgumentException e) {
Log.d("Cannot process processNickname: %s", e.getMessage());
Log.e(e);
}
finally {
data.setPokemonData(pokemon);
}
}

item.setInventoryItemData(data);
delta.setInventoryItems(i, item);
}
return inventory.setInventoryDelta(delta).build().toByteString();
}

private String processNickname(PokemonData pokemonData) {
private String processNickname(PokemonData pokemonData) throws NullPointerException, IllegalArgumentException {
Pokemons.Data data = mPokemons.with(pokemonData);

DecimalFormat ivFormatter = new DecimalFormat("000.0");
Expand Down

0 comments on commit 71ab9ca

Please sign in to comment.