diff --git a/app/src/main/java/com/icecream/snorlax/module/Pokemons.java b/app/src/main/java/com/icecream/snorlax/module/Pokemons.java index 0819dee..93e88e0 100644 --- a/app/src/main/java/com/icecream/snorlax/module/Pokemons.java +++ b/app/src/main/java/com/icecream/snorlax/module/Pokemons.java @@ -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 @@ -123,11 +124,20 @@ 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); } @@ -135,7 +145,7 @@ public final class Probability { private final CaptureProbability mCaptureProbability; - Probability(CaptureProbability captureProbability) { + private Probability(CaptureProbability captureProbability) { mCaptureProbability = captureProbability; } @@ -172,7 +182,7 @@ public final class Data { private final PokemonData mPokemonData; - Data(PokemonData pokemonData) { + private Data(PokemonData pokemonData) { mPokemonData = pokemonData; } diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/encounter/Encounter.java b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/Encounter.java index 42c70b0..ab9474a 100644 --- a/app/src/main/java/com/icecream/snorlax/module/feature/encounter/Encounter.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/Encounter.java @@ -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); } } @@ -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); } } @@ -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); diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/rename/Rename.java b/app/src/main/java/com/icecream/snorlax/module/feature/rename/Rename.java index afb5567..bab2437 100644 --- a/app/src/main/java/com/icecream/snorlax/module/feature/rename/Rename.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/rename/Rename.java @@ -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; @@ -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");