Skip to content

Commit 0c5e008

Browse files
CyperghostGrover-c13
authored andcommitted
Adding Sorting Map Objects (#396)
* Adding map util to get distance between two points * Adding sorting function for objects in the map * Adding the check if the user have enough poke ball´s * Adding the check if the user have enough poke ball´s * Adding license * Fixing example class * Fix code style * Updating code style * Updating code style * Fixing import
1 parent e076965 commit 0c5e008

File tree

7 files changed

+236
-67
lines changed

7 files changed

+236
-67
lines changed

src/main/java/com/pokegoapi/api/gym/Gym.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@
3030
import com.pokegoapi.exceptions.LoginFailedException;
3131
import com.pokegoapi.exceptions.RemoteServerException;
3232
import com.pokegoapi.main.ServerRequest;
33+
import com.pokegoapi.util.MapPoint;
3334

3435
import java.util.ArrayList;
3536
import java.util.List;
3637

37-
public class Gym {
38+
public class Gym implements MapPoint{
3839
private FortData proto;
3940
private GetGymDetailsResponse details;
4041
private PokemonGo api;

src/main/java/com/pokegoapi/api/map/Map.java

Lines changed: 61 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import com.pokegoapi.main.ServerRequest;
5555
import com.pokegoapi.util.DummyFuture;
5656
import com.pokegoapi.util.FutureWrapper;
57+
import com.pokegoapi.util.MapUtil;
5758
import com.pokegoapi.util.PokemonFuture;
5859

5960
import java.util.ArrayList;
@@ -122,6 +123,19 @@ public List<CatchablePokemon> getCatchablePokemon() throws LoginFailedException,
122123
return getCatchablePokemonAsync().toBlocking();
123124
}
124125

126+
/**
127+
* Gets catchable pokemon sort by distance.
128+
*
129+
* @return the catchable pokemon sort
130+
* @throws LoginFailedException the login failed exception
131+
* @throws RemoteServerException the remote server exception
132+
*/
133+
public java.util.Map<Double, CatchablePokemon> getCatchablePokemonSort()
134+
throws LoginFailedException, RemoteServerException {
135+
MapUtil<CatchablePokemon> util = new MapUtil<>();
136+
return util.sortItems(getCatchablePokemon(), api);
137+
}
138+
125139
/**
126140
* Returns a list of nearby pokemon (non-catchable).
127141
*
@@ -147,8 +161,8 @@ protected List<NearbyPokemon> handle(MapObjects result) throws RemoteServerExcep
147161
* Returns a list of nearby pokemon (non-catchable).
148162
*
149163
* @return a List of NearbyPokemon at your current location
150-
* @throws LoginFailedException if the login failed
151-
* @throws RemoteServerException When a buffer exception is thrown
164+
* @throws LoginFailedException if the login failed
165+
* @throws RemoteServerException When a buffer exception is thrown
152166
*/
153167
public List<NearbyPokemon> getNearbyPokemon() throws LoginFailedException, RemoteServerException {
154168
return getNearbyPokemonAsync().toBlocking();
@@ -178,8 +192,8 @@ protected List<Point> handle(MapObjects result) throws RemoteServerException {
178192
* Returns a list of spawn points.
179193
*
180194
* @return list of spawn points
181-
* @throws LoginFailedException if the login failed
182-
* @throws RemoteServerException When a buffer exception is thrown
195+
* @throws LoginFailedException if the login failed
196+
* @throws RemoteServerException When a buffer exception is thrown
183197
*/
184198
public List<Point> getSpawnPoints() throws LoginFailedException, RemoteServerException {
185199
return getSpawnPointsAsync().toBlocking();
@@ -214,6 +228,18 @@ public List<Gym> getGyms() throws LoginFailedException, RemoteServerException {
214228
return getGymsAsync().toBlocking();
215229
}
216230

231+
/**
232+
* Gets gym sort by distance.
233+
*
234+
* @return the gym sort
235+
* @throws LoginFailedException the login failed exception
236+
* @throws RemoteServerException the remote server exception
237+
*/
238+
public java.util.Map<Double, Gym> getGymSort() throws LoginFailedException, RemoteServerException {
239+
MapUtil<Gym> util = new MapUtil<>();
240+
return util.sortItems(getGyms(), api);
241+
}
242+
217243
/**
218244
* Returns a list of decimated spawn points at current location.
219245
*
@@ -237,13 +263,26 @@ protected List<Point> handle(MapObjects result) throws RemoteServerException {
237263
* Returns a list of decimated spawn points at current location.
238264
*
239265
* @return list of spawn points
240-
* @throws LoginFailedException if the login failed
241-
* @throws RemoteServerException When a buffer exception is thrown
266+
* @throws LoginFailedException if the login failed
267+
* @throws RemoteServerException When a buffer exception is thrown
242268
*/
243269
public List<Point> getDecimatedSpawnPoints() throws LoginFailedException, RemoteServerException {
244270
return getDecimatedSpawnPointsAsync().toBlocking();
245271
}
246272

273+
274+
/**
275+
* Gets decimated spawn points sort by distance.
276+
*
277+
* @return the decimated spawn points sort
278+
* @throws LoginFailedException the login failed exception
279+
* @throws RemoteServerException the remote server exception
280+
*/
281+
public java.util.Map<Double, Point> getDecimatedSpawnPointsSort() throws LoginFailedException, RemoteServerException {
282+
MapUtil<Point> util = new MapUtil<>();
283+
return util.sortItems(getDecimatedSpawnPoints(), api);
284+
}
285+
247286
/**
248287
* Returns MapObjects around your current location.
249288
*
@@ -271,7 +310,7 @@ public PokemonFuture<MapObjects> getMapObjectsAsync(int width) {
271310
*/
272311
public PokemonFuture<MapObjects> getMapObjectsAsync(List<Long> cellIds) {
273312

274-
if ( (api.currentTimeMillis() - lastMapUpdate) < RESEND_REQUEST ) {
313+
if ((api.currentTimeMillis() - lastMapUpdate) < RESEND_REQUEST) {
275314
return new DummyFuture<MapObjects>(cachedMapObjects);
276315
}
277316

@@ -322,7 +361,6 @@ public FortType apply(FortData fortData) {
322361
}
323362

324363

325-
326364
return result;
327365
}
328366
};
@@ -332,8 +370,8 @@ public FortType apply(FortData fortData) {
332370
* Returns MapObjects around your current location.
333371
*
334372
* @return MapObjects at your current location
335-
* @throws LoginFailedException if the login failed
336-
* @throws RemoteServerException When a buffer exception is thrown
373+
* @throws LoginFailedException if the login failed
374+
* @throws RemoteServerException When a buffer exception is thrown
337375
*/
338376
public MapObjects getMapObjects() throws LoginFailedException, RemoteServerException {
339377
return getMapObjectsAsync().toBlocking();
@@ -344,9 +382,8 @@ public MapObjects getMapObjects() throws LoginFailedException, RemoteServerExcep
344382
*
345383
* @param width width
346384
* @return MapObjects at your current location
347-
*
348-
* @throws LoginFailedException If login fails.
349-
* @throws RemoteServerException If request errors occurred.
385+
* @throws LoginFailedException If login fails.
386+
* @throws RemoteServerException If request errors occurred.
350387
*/
351388
public MapObjects getMapObjects(int width) throws LoginFailedException, RemoteServerException {
352389
return getMapObjectsAsync(width).toBlocking();
@@ -358,8 +395,8 @@ public MapObjects getMapObjects(int width) throws LoginFailedException, RemoteSe
358395
* @param latitude latitude
359396
* @param longitude longitude
360397
* @return MapObjects in the given cells
361-
* @throws LoginFailedException if the login failed
362-
* @throws RemoteServerException When a buffer exception is thrown
398+
* @throws LoginFailedException if the login failed
399+
* @throws RemoteServerException When a buffer exception is thrown
363400
*/
364401
@Deprecated
365402
public MapObjects getMapObjects(double latitude, double longitude)
@@ -374,8 +411,8 @@ public MapObjects getMapObjects(double latitude, double longitude)
374411
* @param latitude latitude
375412
* @param longitude longitude
376413
* @return MapObjects in the given cells
377-
* @throws LoginFailedException if the login failed
378-
* @throws RemoteServerException When a buffer exception is thrown
414+
* @throws LoginFailedException if the login failed
415+
* @throws RemoteServerException When a buffer exception is thrown
379416
*/
380417
@Deprecated
381418
public MapObjects getMapObjects(List<Long> cellIds, double latitude, double longitude)
@@ -390,8 +427,8 @@ public MapObjects getMapObjects(List<Long> cellIds, double latitude, double long
390427
* @param longitude longitude
391428
* @param width width
392429
* @return MapObjects in the given cells
393-
* @throws LoginFailedException if the login failed
394-
* @throws RemoteServerException When a buffer exception is thrown
430+
* @throws LoginFailedException if the login failed
431+
* @throws RemoteServerException When a buffer exception is thrown
395432
*/
396433
@Deprecated
397434
public MapObjects getMapObjects(double latitude, double longitude, int width)
@@ -405,10 +442,10 @@ public MapObjects getMapObjects(double latitude, double longitude, int width)
405442
* @param cellIds cellIds
406443
* @param latitude latitude
407444
* @param longitude longitude
408-
* @param altitude altitude
445+
* @param altitude altitude
409446
* @return MapObjects in the given cells
410-
* @throws LoginFailedException if the login failed
411-
* @throws RemoteServerException When a buffer exception is thrown
447+
* @throws LoginFailedException if the login failed
448+
* @throws RemoteServerException When a buffer exception is thrown
412449
*/
413450
@Deprecated
414451
public MapObjects getMapObjects(List<Long> cellIds, double latitude, double longitude, double altitude)
@@ -424,8 +461,8 @@ public MapObjects getMapObjects(List<Long> cellIds, double latitude, double long
424461
*
425462
* @param cellIds List of cellId
426463
* @return MapObjects in the given cells
427-
* @throws LoginFailedException if the login failed
428-
* @throws RemoteServerException When a buffer exception is thrown
464+
* @throws LoginFailedException if the login failed
465+
* @throws RemoteServerException When a buffer exception is thrown
429466
*/
430467
public MapObjects getMapObjects(List<Long> cellIds) throws LoginFailedException, RemoteServerException {
431468
return getMapObjectsAsync(cellIds).toBlocking();

src/main/java/com/pokegoapi/api/map/Point.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
package com.pokegoapi.api.map;
1717

1818
import POGOProtos.Map.SpawnPointOuterClass;
19+
import com.pokegoapi.util.MapPoint;
1920
import lombok.Getter;
2021
import lombok.Setter;
2122

22-
public class Point {
23+
public class Point implements MapPoint{
2324
@Getter
2425
@Setter
2526
private double longitude;

src/main/java/com/pokegoapi/api/map/pokemon/CatchablePokemon.java

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
import com.pokegoapi.api.inventory.ItemBag;
3636
import com.pokegoapi.api.inventory.Pokeball;
3737
import com.pokegoapi.exceptions.LoginFailedException;
38+
import com.pokegoapi.exceptions.NoSuchItemException;
3839
import com.pokegoapi.exceptions.RemoteServerException;
3940
import com.pokegoapi.main.AsyncServerRequest;
4041
import com.pokegoapi.util.FutureWrapper;
42+
import com.pokegoapi.util.MapPoint;
4143
import com.pokegoapi.util.Log;
4244
import com.pokegoapi.util.NestedFutureWrapper;
4345
import com.pokegoapi.util.PokemonFuture;
@@ -50,7 +52,7 @@
5052
* The type Catchable pokemon.
5153
*/
5254
@ToString
53-
public class CatchablePokemon {
55+
public class CatchablePokemon implements MapPoint{
5456
private static final String TAG = CatchablePokemon.class.getSimpleName();
5557
private final PokemonGo api;
5658

@@ -174,19 +176,10 @@ public EncounterResult encounterPokemon() throws LoginFailedException,
174176
* @throws RemoteServerException if the server failed to respond
175177
*/
176178
public PokemonFuture<CatchResult> catchPokemonWithRazzBerryAsync()
177-
throws LoginFailedException, RemoteServerException {
178-
final Pokeball pokeball;
179+
throws LoginFailedException, RemoteServerException, NoSuchItemException {
180+
final Pokeball pokeball = getItemBall();
181+
179182

180-
ItemBag bag = api.getInventories().getItemBag();
181-
if (bag.getItem(ItemId.ITEM_POKE_BALL).getCount() > 0) {
182-
pokeball = Pokeball.POKEBALL;
183-
} else if (bag.getItem(ItemId.ITEM_GREAT_BALL).getCount() > 0) {
184-
pokeball = Pokeball.GREATBALL;
185-
} else if (bag.getItem(ItemId.ITEM_ULTRA_BALL).getCount() > 0) {
186-
pokeball = Pokeball.ULTRABALL;
187-
} else {
188-
pokeball = Pokeball.MASTERBALL;
189-
}
190183
return new NestedFutureWrapper<CatchItemResult, CatchResult>(useItemAsync(ItemId.ITEM_RAZZ_BERRY)) {
191184
@Override
192185
protected Future<CatchResult> handleFuture(CatchItemResult result) {
@@ -199,27 +192,40 @@ protected Future<CatchResult> handleFuture(CatchItemResult result) {
199192
}
200193

201194
/**
202-
* Tries to catch a pokemon (will attempt to use a pokeball, if you have
203-
* none will use greatball etc) and uwill use a single razz berry if available.
195+
* Gets item ball to catch a pokemon
204196
*
205-
* @return CatchResult
206-
* @throws LoginFailedException if failed to login
207-
* @throws RemoteServerException if the server failed to respond
197+
* @return the item ball
198+
* @throws LoginFailedException the login failed exception
199+
* @throws RemoteServerException the remote server exception
200+
* @throws NoSuchItemException the no such item exception
208201
*/
209-
public CatchResult catchPokemonWithRazzBerry() throws LoginFailedException,
210-
RemoteServerException {
211-
Pokeball pokeball;
212-
202+
public Pokeball getItemBall() throws LoginFailedException,
203+
RemoteServerException, NoSuchItemException {
213204
ItemBag bag = api.getInventories().getItemBag();
214205
if (bag.getItem(ItemId.ITEM_POKE_BALL).getCount() > 0) {
215-
pokeball = Pokeball.POKEBALL;
206+
return Pokeball.POKEBALL;
216207
} else if (bag.getItem(ItemId.ITEM_GREAT_BALL).getCount() > 0) {
217-
pokeball = Pokeball.GREATBALL;
208+
return Pokeball.GREATBALL;
218209
} else if (bag.getItem(ItemId.ITEM_ULTRA_BALL).getCount() > 0) {
219-
pokeball = Pokeball.ULTRABALL;
210+
return Pokeball.ULTRABALL;
211+
} else if (bag.getItem(ItemId.ITEM_MASTER_BALL).getCount() > 0) {
212+
return Pokeball.MASTERBALL;
220213
} else {
221-
pokeball = Pokeball.MASTERBALL;
214+
throw new NoSuchItemException();
222215
}
216+
}
217+
218+
/**
219+
* Tries to catch a pokemon (will attempt to use a pokeball, if you have
220+
* none will use greatball etc) and uwill use a single razz berry if available.
221+
*
222+
* @return CatchResult
223+
* @throws LoginFailedException if failed to login
224+
* @throws RemoteServerException if the server failed to respond
225+
*/
226+
public CatchResult catchPokemonWithRazzBerry() throws LoginFailedException,
227+
RemoteServerException, NoSuchItemException {
228+
Pokeball pokeball = getItemBall();
223229

224230
useItem(ItemId.ITEM_RAZZ_BERRY);
225231
return catchPokemon(pokeball, -1, -1);
@@ -234,21 +240,9 @@ public CatchResult catchPokemonWithRazzBerry() throws LoginFailedException,
234240
* @throws RemoteServerException if the server failed to respond
235241
*/
236242
public CatchResult catchPokemon() throws LoginFailedException,
237-
RemoteServerException {
238-
Pokeball pokeball;
239-
240-
ItemBag bag = api.getInventories().getItemBag();
241-
if (bag.getItem(ItemId.ITEM_POKE_BALL).getCount() > 0) {
242-
pokeball = Pokeball.POKEBALL;
243-
} else if (bag.getItem(ItemId.ITEM_GREAT_BALL).getCount() > 0) {
244-
pokeball = Pokeball.GREATBALL;
245-
} else if (bag.getItem(ItemId.ITEM_ULTRA_BALL).getCount() > 0) {
246-
pokeball = Pokeball.ULTRABALL;
247-
} else {
248-
pokeball = Pokeball.MASTERBALL;
249-
}
243+
RemoteServerException, NoSuchItemException {
250244

251-
return catchPokemon(pokeball);
245+
return catchPokemon(getItemBall());
252246
}
253247

254248
/**

src/main/java/com/pokegoapi/examples/CatchPokemonAtAreaExample.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.pokegoapi.api.map.pokemon.EncounterResult;
3939
import com.pokegoapi.auth.PtcCredentialProvider;
4040
import com.pokegoapi.exceptions.LoginFailedException;
41+
import com.pokegoapi.exceptions.NoSuchItemException;
4142
import com.pokegoapi.exceptions.RemoteServerException;
4243
import com.pokegoapi.util.Log;
4344
import okhttp3.OkHttpClient;
@@ -78,7 +79,7 @@ public static void main(String[] args) {
7879

7980
}
8081

81-
} catch (LoginFailedException | RemoteServerException e) {
82+
} catch (LoginFailedException | NoSuchItemException | RemoteServerException e) {
8283
// failed to login, invalid credentials, auth issue or server issue.
8384
Log.e("Main", "Failed to login or server issue: ", e);
8485

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.util;
17+
18+
/**
19+
* @author Olaf Braun - Software Development
20+
* @version 1.0
21+
*/
22+
public interface MapPoint {
23+
/**
24+
* Gets latitude.
25+
*
26+
* @return the latitude
27+
*/
28+
double getLatitude();
29+
30+
/**
31+
* Gets longitude.
32+
*
33+
* @return the longitude
34+
*/
35+
double getLongitude();
36+
}

0 commit comments

Comments
 (0)