From 28d117efbd99d702096293ad66e3223ec87c690a Mon Sep 17 00:00:00 2001 From: Unknown Date: Sat, 13 Aug 2016 23:07:01 -0400 Subject: [PATCH] Fixed memory leak in navigate, added log support, small text changes --- src/main/java/dekk/pw/pokemate/PokeMate.java | 2 +- src/main/java/dekk/pw/pokemate/Walking.java | 22 +++++++----- .../dekk/pw/pokemate/tasks/CatchPokemon.java | 28 +++++++++------ .../dekk/pw/pokemate/tasks/DropItems.java | 15 +++++--- .../dekk/pw/pokemate/tasks/EvolvePokemon.java | 16 ++++++--- .../java/dekk/pw/pokemate/tasks/HatchEgg.java | 11 +++++- .../dekk/pw/pokemate/tasks/IncubateEgg.java | 16 +++++++-- .../java/dekk/pw/pokemate/tasks/Navigate.java | 36 +++++++++++-------- .../dekk/pw/pokemate/tasks/TagPokestop.java | 3 +- .../pw/pokemate/tasks/TaskController.java | 2 +- .../java/dekk/pw/pokemate/tasks/Update.java | 18 ++-------- .../pw/pokemate/tasks/UpdateInterface.java | 6 ++-- 12 files changed, 109 insertions(+), 66 deletions(-) diff --git a/src/main/java/dekk/pw/pokemate/PokeMate.java b/src/main/java/dekk/pw/pokemate/PokeMate.java index af33a50..df1699b 100644 --- a/src/main/java/dekk/pw/pokemate/PokeMate.java +++ b/src/main/java/dekk/pw/pokemate/PokeMate.java @@ -99,7 +99,7 @@ private PokeMate() throws LoginFailedException, RemoteServerException { context.setLng(lng); go.setLocation(context.getLat().get(), context.getLng().get(), 0); Time.sleep(5000); - new Update(context).runOnce(); + new Update(context).run(); if (Config.isShowUI()) { PokeMateUI.setPoke(this); diff --git a/src/main/java/dekk/pw/pokemate/Walking.java b/src/main/java/dekk/pw/pokemate/Walking.java index c6bae60..922d8ff 100644 --- a/src/main/java/dekk/pw/pokemate/Walking.java +++ b/src/main/java/dekk/pw/pokemate/Walking.java @@ -6,7 +6,11 @@ import com.google.maps.model.LatLng; import dekk.pw.pokemate.tasks.Navigate; import dekk.pw.pokemate.tasks.TagPokestop; +import dekk.pw.pokemate.util.Time; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import java.util.Random; import java.util.Timer; import java.util.TimerTask; /** @@ -14,6 +18,7 @@ */ public class Walking { + private static final Logger logger = LogManager.getLogger(Walking.class); private static final double VARIANCE = Config.getRange(); private static double getSmallRandom() { return Math.random() * 0.0001 - 0.00005; @@ -25,9 +30,9 @@ public static void setLocation(Context context) { private static void setLocation(boolean random, Context context) { if (random) { - context.getApi().setLocation(context.getLat().get() + getSmallRandom(), context.getLng().get() + getSmallRandom(), 0); + context.getApi().setLocation(context.getLat().get() + getSmallRandom(), context.getLng().get() + getSmallRandom(), new Random().nextInt(10)); } else { - context.getApi().setLocation(context.getLat().get(), context.getLng().get(), 0); + context.getApi().setLocation(context.getLat().get(), context.getLng().get(), new Random().nextInt(10)); } } @@ -41,16 +46,17 @@ public static void walk( final Context context, S2LatLng end) { // PokeStop Walk final AtomicDouble stepsRequired = new AtomicDouble(timeRequired / (timeout / 1000D)); double deltaLat = diff.latDegrees() / stepsRequired.get(); double deltaLng = diff.lngDegrees() / stepsRequired.get(); - + logger.debug("starting to walk"); //Schedule a timer to walk every 200 ms new Timer().scheduleAtFixedRate(new TimerTask() { @Override public void run() { - context.getApi().setLocation(context.getLat().addAndGet(deltaLat), context.getLng().addAndGet(deltaLng), 0); + context.getApi().setLocation(context.getLat().addAndGet(deltaLat), context.getLng().addAndGet(deltaLng), new Random().nextInt(10)); stepsRequired.getAndAdd(-1); if (stepsRequired.get() <= 0) { + logger.debug("Setting a new destination"); context.getWalking().set(false); context.addTask(new TagPokestop(context)); context.addTask(new Navigate(context, new LatLng(context.getLat().get() - VARIANCE, context.getLng().get() - VARIANCE), @@ -65,7 +71,7 @@ public static void walk(Context context, DirectionsStep[] steps) { // Streets W context.getWalking().set(true); if (steps != null) { for (DirectionsStep step : steps) { - //Log.i("WALKER", "Heading to: [" + step.endLocation.lat + ", " + step.endLocation.lng + "]"); + logger.debug("WALKER Heading to: [" + step.endLocation.lat + ", " + step.endLocation.lng + "]"); context.getApi().setLocation(step.startLocation.lat, step.startLocation.lng, 0); context.getLat().set(step.startLocation.lat); context.getLng().set(step.startLocation.lng); @@ -89,13 +95,13 @@ public static void walk(Context context, DirectionsStep[] steps) { // Streets W } catch (InterruptedException e) { e.printStackTrace(); } - // Log.i("WALKER", "Set location: [" + context.getLat().get() + ", " + context.getLng().get() + "]"); + logger.debug( "Set location: [" + context.getLat().get() + ", " + context.getLng().get() + "]"); remainingSteps--; } - //Log.i("WALKER", "Arrived at: [" + step.endLocation.lat + ", " + step.endLocation.lng + "]"); + logger.debug( "Arrived at: [" + step.endLocation.lat + ", " + step.endLocation.lng + "]"); } }else{ - System.out.println("WALKING ERROR"); + logger.error("WALKING ERROR"); } context.getWalking().set(false); diff --git a/src/main/java/dekk/pw/pokemate/tasks/CatchPokemon.java b/src/main/java/dekk/pw/pokemate/tasks/CatchPokemon.java index fe3eecd..b8d9c83 100644 --- a/src/main/java/dekk/pw/pokemate/tasks/CatchPokemon.java +++ b/src/main/java/dekk/pw/pokemate/tasks/CatchPokemon.java @@ -12,11 +12,10 @@ import com.pokegoapi.exceptions.LoginFailedException; import com.pokegoapi.exceptions.NoSuchItemException; import com.pokegoapi.exceptions.RemoteServerException; -import dekk.pw.pokemate.Config; -import dekk.pw.pokemate.Context; -import dekk.pw.pokemate.PokeMateUI; -import dekk.pw.pokemate.Walking; +import dekk.pw.pokemate.*; import dekk.pw.pokemate.util.StringConverter; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.text.SimpleDateFormat; import java.util.Date; @@ -30,6 +29,8 @@ */ class CatchPokemon extends Task implements Runnable { + private static final Logger logger = LogManager.getLogger(CatchPokemon.class); + CatchPokemon(final Context context) { super(context); } @@ -96,27 +97,34 @@ public void run() { log(output + " [IV: " + getIvRatio(p) + "%]"); } context.setConsoleString("CatchPokemon", String.format("%s [IV: %d%%]", output, getIvRatio(p))); - } catch (LoginFailedException | RemoteServerException e) { + } catch (LoginFailedException e) { + logger.error("Login Failed", e); + + } catch (RemoteServerException e) { context.setConsoleString("CatchPokemon", "Server Error."); + logger.error("Remote Server Exception", e); } }); - } catch (NullPointerException ex) { - ex.printStackTrace(); + } catch (NullPointerException e) { + logger.error("NullPointerException.", e); } } - } catch (LoginFailedException | RemoteServerException e) { - //e.printStackTrace(); + } catch (LoginFailedException e) { + logger.error("Login Failed", e); + + } catch (RemoteServerException e) { context.setConsoleString("CatchPokemon", "Server Error."); + logger.error("Remote Server Exception", e); } catch (NoSuchItemException e) { + logger.error("Out of Pokeballs", e); context.setConsoleString("CatchPokemon","Out of Pokeballs."); } finally { context.addTask(new CatchPokemon(context)); } } - private boolean shouldIgnore(final CatchablePokemon p) { return !Config.getIgnoreCatchingPokemon().contains(p.getPokemonId()); } diff --git a/src/main/java/dekk/pw/pokemate/tasks/DropItems.java b/src/main/java/dekk/pw/pokemate/tasks/DropItems.java index 219f88e..3f34d4b 100644 --- a/src/main/java/dekk/pw/pokemate/tasks/DropItems.java +++ b/src/main/java/dekk/pw/pokemate/tasks/DropItems.java @@ -4,9 +4,12 @@ import com.pokegoapi.exceptions.RemoteServerException; import dekk.pw.pokemate.Config; import dekk.pw.pokemate.Context; +import dekk.pw.pokemate.PokeMate; import dekk.pw.pokemate.PokeMateUI; import dekk.pw.pokemate.util.StringConverter; import dekk.pw.pokemate.util.Time; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.text.SimpleDateFormat; import java.util.Date; @@ -18,6 +21,8 @@ */ class DropItems extends Task implements Runnable { + private static final Logger logger = LogManager.getLogger(DropItems.class); + DropItems(final Context context) { super(context); } @@ -35,11 +40,13 @@ public void run() { String removedItem = "Removed " + StringConverter.titleCase(id.name()) + "(x" + countToDrop + ")"; removedItemsString.append(removedItem + " "); PokeMateUI.toast(removedItem, "Items removed!", "icons/items/" + id.getNumber() + ".png"); - } - } catch (RemoteServerException | LoginFailedException e) { - context.setConsoleString("DropItems", "Server Error"); - PokeMateUI.toast("Server Error", "DropItems", "icons/items/" + id.getNumber() + ".png"); + } catch (LoginFailedException e) { + logger.error("Login Failed", e); + context.setConsoleString("DropItems", "Login Failed."); + } catch (RemoteServerException e) { + context.setConsoleString("DropItems", "Server Error."); + logger.error("Remote Server Exception", e); } }); } finally { diff --git a/src/main/java/dekk/pw/pokemate/tasks/EvolvePokemon.java b/src/main/java/dekk/pw/pokemate/tasks/EvolvePokemon.java index d40fff0..e8908e6 100644 --- a/src/main/java/dekk/pw/pokemate/tasks/EvolvePokemon.java +++ b/src/main/java/dekk/pw/pokemate/tasks/EvolvePokemon.java @@ -9,6 +9,8 @@ import dekk.pw.pokemate.PokeMateUI; import dekk.pw.pokemate.util.StringConverter; import dekk.pw.pokemate.util.Time; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.io.DataInputStream; import java.text.SimpleDateFormat; @@ -20,6 +22,8 @@ * Created by TimD on 7/22/2016. */ class EvolvePokemon extends Task implements Runnable { + + private static final Logger logger = LogManager.getLogger(EvolvePokemon.class); private static final ConcurrentHashMap CANDY_AMOUNTS = new ConcurrentHashMap<>(); static { @@ -32,7 +36,7 @@ class EvolvePokemon extends Task implements Runnable { CANDY_AMOUNTS.put(dis.readInt(), dis.readInt()); } } catch (Exception e) { - e.printStackTrace(); + logger.debug("Error", e); } } @@ -42,7 +46,6 @@ class EvolvePokemon extends Task implements Runnable { @Override public void run() { - // System.out.println("[Evolve] Activating.."); try { CopyOnWriteArrayList pokeList = new CopyOnWriteArrayList<>(context.getInventories().getPokebank().getPokemons()); for (Pokemon pokemon : pokeList) @@ -61,9 +64,12 @@ public void run() { } } } - } catch (RemoteServerException | LoginFailedException e1) { - context.setConsoleString("EvolvePokemon", "Server Error"); - e1.printStackTrace(); + } catch (LoginFailedException e) { + logger.error("Login Failed", e); + + } catch (RemoteServerException e) { + context.setConsoleString("CatchPokemon", "Server Error."); + logger.debug("Remote Server Exception", e); } finally { context.addTask(new EvolvePokemon(context)); } diff --git a/src/main/java/dekk/pw/pokemate/tasks/HatchEgg.java b/src/main/java/dekk/pw/pokemate/tasks/HatchEgg.java index 7489887..cda51d4 100644 --- a/src/main/java/dekk/pw/pokemate/tasks/HatchEgg.java +++ b/src/main/java/dekk/pw/pokemate/tasks/HatchEgg.java @@ -6,10 +6,15 @@ import com.pokegoapi.exceptions.RemoteServerException; import dekk.pw.pokemate.Context; import dekk.pw.pokemate.PokeMateUI; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.List; class HatchEgg extends Task implements Runnable{ + + private static final Logger logger = LogManager.getLogger(HatchEgg.class); + HatchEgg(final Context context) { super(context); } @@ -32,8 +37,12 @@ public void run() { context.setConsoleString("HatchEgg", "Hatched " + hatchedPokemon.getPokemonId() + " with " + hatchedPokemon.getCp() + " CP " + " - " + details); } }); - } catch (LoginFailedException | RemoteServerException e) { + } catch (LoginFailedException e) { + logger.error("Login Failed", e); + + } catch (RemoteServerException e) { context.setConsoleString("HatchEgg", "Server Error"); + logger.error("Remote Server Exception", e); } finally { context.addTask(new HatchEgg(context)); } diff --git a/src/main/java/dekk/pw/pokemate/tasks/IncubateEgg.java b/src/main/java/dekk/pw/pokemate/tasks/IncubateEgg.java index 9016b88..4715e4a 100644 --- a/src/main/java/dekk/pw/pokemate/tasks/IncubateEgg.java +++ b/src/main/java/dekk/pw/pokemate/tasks/IncubateEgg.java @@ -3,8 +3,12 @@ import POGOProtos.Networking.Responses.UseItemEggIncubatorResponseOuterClass; import com.pokegoapi.api.inventory.EggIncubator; import com.pokegoapi.api.pokemon.EggPokemon; +import com.pokegoapi.exceptions.LoginFailedException; +import com.pokegoapi.exceptions.RemoteServerException; import dekk.pw.pokemate.Context; import dekk.pw.pokemate.PokeMateUI; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.List; @@ -15,6 +19,8 @@ */ class IncubateEgg extends Task implements Runnable{ + private static final Logger logger = LogManager.getLogger(IncubateEgg.class); + IncubateEgg(final Context context) { super(context); } @@ -26,7 +32,7 @@ public void run() { try { return !i.isInUse(); } catch (Exception e) { - e.printStackTrace(); + logger.error("Incubator Error", e); return false; } }).collect(Collectors.toList()); @@ -41,8 +47,12 @@ public void run() { context.setConsoleString("IncubateEgg", eggresult); } } - } catch (Exception e) { - e.printStackTrace(); + } catch (LoginFailedException e) { + logger.error("Login Failed", e); + context.setConsoleString("IncubateEgg", "Login Error."); + } catch (RemoteServerException e) { + context.setConsoleString("IncubateEgg", "Server Error."); + logger.error("Remote Server Exception", e); } finally { context.addTask(new IncubateEgg(context)); } diff --git a/src/main/java/dekk/pw/pokemate/tasks/Navigate.java b/src/main/java/dekk/pw/pokemate/tasks/Navigate.java index fc83e03..e92889d 100644 --- a/src/main/java/dekk/pw/pokemate/tasks/Navigate.java +++ b/src/main/java/dekk/pw/pokemate/tasks/Navigate.java @@ -11,6 +11,9 @@ import dekk.pw.pokemate.Config; import dekk.pw.pokemate.Context; import dekk.pw.pokemate.Walking; +import dekk.pw.pokemate.util.Time; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import java.util.ArrayList; @@ -29,10 +32,14 @@ public class Navigate extends Task implements Runnable { public static final double VARIANCE = Config.getRange(); private static final List routes = new ArrayList<>(); private static final List route = new ArrayList<>(); - public static boolean populated; + private static final Logger logger = LogManager.getLogger(Navigate.class); private static final Object lock = new Object(); - static final NavigationType navigationType = Config.getNavigationType(); + public static final NavigationType navigationType = Config.getNavigationType(); + public static boolean populated; + + private S2LatLng last; + private final List ids = new ArrayList<>(); public Navigate(final Context context, LatLng min, LatLng max) { super(context); @@ -41,10 +48,12 @@ public Navigate(final Context context, LatLng min, LatLng max) { switch (navigationType) { //Untested case POKESTOPS: - populateRoute(context); + if (context.getRoutesIndex() == 0) + populateRoute(context); break; default: - populateDirections(context); + if (context.getRoutesIndex() == 0) + populateDirections(context); break; } populated = true; @@ -58,15 +67,13 @@ public static List getRoute() { return route; } - private S2LatLng last; - private final List ids = new ArrayList<>(); - /** * Attempts to generate a route to all found pokestops... * * @param context */ private void populateRoute(Context context) { + Time.sleep(300); try { List stops = context.getMap().getMapObjects().getPokestops().stream().filter(a -> //only pokestops in our region @@ -76,7 +83,7 @@ private void populateRoute(Context context) { a.getLongitude() <= max.lng).collect(Collectors.toList()); int count = stops.size(); - //System.out.println("Stops found: " + count); + last = S2LatLng.fromDegrees(context.getLat().doubleValue(), context.getLng().doubleValue()); while (route.size() < count - 1) { List tempStops = stops.stream().filter(a -> !ids.contains(a.getId())).sorted((a, b) -> { @@ -88,7 +95,7 @@ private void populateRoute(Context context) { }).collect(Collectors.toList()); if (tempStops.size() == 0) { - //System.out.println("CRTICAL POKESTOP LOCATION ERROR BREAKING!"); + logger.fatal("Critical Pokestop Error"); break; } Pokestop first = tempStops.get(0); @@ -97,14 +104,14 @@ private void populateRoute(Context context) { } route.add(S2LatLng.fromDegrees(context.getLat().get(), context.getLng().get())); } catch (RemoteServerException e) { - System.out.println("[Navigate] Error - Hit Rate limiter."); + logger.error("Remote server error", e); //e.printStackTrace(); } catch (LoginFailedException e) { - System.out.println("[Navigate] Login Failed."); + logger.error("Login failed.", e); //e.printStackTrace(); } catch (NullPointerException e) { - System.out.println("[Navigate] Null Pointer Exception."); - e.printStackTrace(); + logger.error("Null pointer error.", e); + //e.printStackTrace(); } } @@ -115,7 +122,7 @@ public void run() { return; } else if (navigationType == (NavigationType.STREETS) && context.getRoutesIndex() >= getDirections().size()) { context.resetRoutesIndex(); - } else if (navigationType == (NavigationType.POKESTOPS) && context.getRoutesIndex() >= route.size()) { + } else if (navigationType == (NavigationType.POKESTOPS) && context.getRoutesIndex() >= getRoute().size()) { context.resetRoutesIndex(); } switch (navigationType) { @@ -162,6 +169,7 @@ private DirectionsStep[] queryDirections(LatLng start, LatLng end) { } } catch (Exception e) { e.printStackTrace(); + logger.error("Remote Server Exception", e); } return stepsToTake; } diff --git a/src/main/java/dekk/pw/pokemate/tasks/TagPokestop.java b/src/main/java/dekk/pw/pokemate/tasks/TagPokestop.java index 5d9c6f4..a13a12f 100644 --- a/src/main/java/dekk/pw/pokemate/tasks/TagPokestop.java +++ b/src/main/java/dekk/pw/pokemate/tasks/TagPokestop.java @@ -1,6 +1,5 @@ package dekk.pw.pokemate.tasks; -import com.pokegoapi.api.map.MapObjects; import com.pokegoapi.api.map.fort.Pokestop; import com.pokegoapi.api.map.fort.PokestopLootResult; import com.pokegoapi.exceptions.LoginFailedException; @@ -42,7 +41,7 @@ public void run() { PokeMateUI.toast(result, Config.POKE + "Stop interaction!", "icons/pokestop.png"); context.setConsoleString("TagPokestop", result); } catch (LoginFailedException | RemoteServerException e) { - context.setConsoleString("TagPokestop", "Server Error"); + context.setConsoleString("TagPokestop", "Server Error."); e.printStackTrace(); } }); diff --git a/src/main/java/dekk/pw/pokemate/tasks/TaskController.java b/src/main/java/dekk/pw/pokemate/tasks/TaskController.java index e606e53..dffb1b5 100644 --- a/src/main/java/dekk/pw/pokemate/tasks/TaskController.java +++ b/src/main/java/dekk/pw/pokemate/tasks/TaskController.java @@ -73,7 +73,7 @@ public void run() { e.printStackTrace(); } } - }, 0, 8000); + }, 0, 9000); tasks.forEach(context::addTask); diff --git a/src/main/java/dekk/pw/pokemate/tasks/Update.java b/src/main/java/dekk/pw/pokemate/tasks/Update.java index 9b628c8..41e9c68 100644 --- a/src/main/java/dekk/pw/pokemate/tasks/Update.java +++ b/src/main/java/dekk/pw/pokemate/tasks/Update.java @@ -4,6 +4,7 @@ import com.pokegoapi.exceptions.LoginFailedException; import com.pokegoapi.exceptions.RemoteServerException; import dekk.pw.pokemate.Context; +import dekk.pw.pokemate.util.Time; /** * Created by TimD on 7/22/2016. @@ -24,23 +25,10 @@ public void run() { } catch (LoginFailedException e) { context.setConsoleString("Update", "Login Failed, attempting to login again."); Context.Login(context.getHttp()); - } catch (RemoteServerException | AsyncPokemonGoException e) { - context.setConsoleString("Update", "Server Error"); - } finally { - //context.addTask(new Update(context)); - } - } - - public void runOnce() { - try { - context.refreshInventories(); - context.refreshMap(); - context.getProfile().updateProfile(); - } catch (LoginFailedException e) { - context.setConsoleString("Update", "Login Error, attempting to login again."); - Context.Login(context.getHttp()); } catch (RemoteServerException e) { context.setConsoleString("Update", "Server Error"); + } catch (AsyncPokemonGoException e) { + context.setConsoleString("Update", "API Async Error"); } } } diff --git a/src/main/java/dekk/pw/pokemate/tasks/UpdateInterface.java b/src/main/java/dekk/pw/pokemate/tasks/UpdateInterface.java index 87795a1..e38a68f 100644 --- a/src/main/java/dekk/pw/pokemate/tasks/UpdateInterface.java +++ b/src/main/java/dekk/pw/pokemate/tasks/UpdateInterface.java @@ -35,7 +35,7 @@ public class UpdateInterface extends Task implements Runnable { @Override public void run() { - // Clears old console output. (Probably won't work on windows) + // Clears old console output. System.out.print("\033[H\033[2J"); System.out.println(header()); context.getConsoleStrings().forEach( (key,value) -> { @@ -71,11 +71,13 @@ private String header() { lastLevel = curLevel; } - return String.format("Name: %-15s [%s] Level %d - %,.2fXP/H - Next Level in %,.0fXP - Runtime: %s", + return String.format("Name: %-15s [%s] Level %d - %,.2fXP/H (%,.0f/%,.0f) - Next Level in %,.0fXP - Runtime: %s", context.getProfile().getPlayerData().getUsername(), new SimpleDateFormat("HH:mm:ss").format(new Date()), curLevel, experienceGained / (runTime / 3.6E6), + curLevelXP, + nextXP, nextXP-curLevelXP, millisToTimeString(runTime)); } catch (LoginFailedException | RemoteServerException e) {