diff --git a/src/main/java/me/skiincraft/api/paladins/Paladins.java b/src/main/java/me/skiincraft/api/paladins/Paladins.java index d650554..ab1a207 100644 --- a/src/main/java/me/skiincraft/api/paladins/Paladins.java +++ b/src/main/java/me/skiincraft/api/paladins/Paladins.java @@ -100,7 +100,7 @@ public Boolean get() { HttpRequest request = HttpRequest.get(url); String body = request.body(); json = body; - bool = checkResponse(body); + bool = accessUtils.checkResponse(body); if (!bool) { throw new RequestException(body, body); } @@ -168,25 +168,5 @@ public AccessUtils getAccessUtils() { public List getSessions(){ return sessions; } - - private boolean checkResponse(String body) { - if (body.contains("Invalid Developer Id")) { - return false; - } - - if (body.contains("Invalid session id")) { - return false; - } - - if (body.contains("Exception while validating developer access.")) { - return false; - } - - if (body.contains("Error while comparing Server and Client timestamp")) { - return false; - } - - return !body.contains("Exception - Timestamp"); - } } diff --git a/src/main/java/me/skiincraft/api/paladins/cache/PaladinsCacheImpl.java b/src/main/java/me/skiincraft/api/paladins/cache/PaladinsCacheImpl.java index bdb9bdb..5707c62 100644 --- a/src/main/java/me/skiincraft/api/paladins/cache/PaladinsCacheImpl.java +++ b/src/main/java/me/skiincraft/api/paladins/cache/PaladinsCacheImpl.java @@ -54,7 +54,7 @@ public synchronized void addChampion(Champions champion) { public synchronized void addMatch(Match match) { RuntimeMemoryImpl impl = (RuntimeMemoryImpl) matchMemory; Match[] c = impl.item; - List cc = Arrays.asList(c); + List cc = new ArrayList<>(Arrays.asList(c)); cc.add(match); // Remove the last element cc.removeAll(cc.stream().filter(cham -> cham.getMatchId() == cham.getMatchId()).collect(Collectors.toList())); @@ -70,7 +70,7 @@ public synchronized void addCard(Cards cards) { } Cards[] c = impl.item; - List cc = Arrays.asList(c); + List cc = new ArrayList<>(Arrays.asList(c)); // Remove the last element cc.removeAll( cc.stream() diff --git a/src/main/java/me/skiincraft/api/paladins/exceptions/ChampionException.java b/src/main/java/me/skiincraft/api/paladins/exceptions/ChampionException.java new file mode 100644 index 0000000..aaccd31 --- /dev/null +++ b/src/main/java/me/skiincraft/api/paladins/exceptions/ChampionException.java @@ -0,0 +1,8 @@ +package me.skiincraft.api.paladins.exceptions; + +public class ChampionException extends RuntimeException { + + public ChampionException(String message) { + super(message); + } +} diff --git a/src/main/java/me/skiincraft/api/paladins/exceptions/MatchException.java b/src/main/java/me/skiincraft/api/paladins/exceptions/MatchException.java index 66de5c1..86c25ba 100644 --- a/src/main/java/me/skiincraft/api/paladins/exceptions/MatchException.java +++ b/src/main/java/me/skiincraft/api/paladins/exceptions/MatchException.java @@ -1,21 +1,9 @@ package me.skiincraft.api.paladins.exceptions; -public class MatchException extends Exception { - - /** - * - */ - private static final long serialVersionUID = -5758961246899999315L; - private String message; - +public class MatchException extends RuntimeException { public MatchException(String message) { - this.message = message; - } - - @Override - public String getMessage() { - return message; + super(message); } } diff --git a/src/main/java/me/skiincraft/api/paladins/exceptions/PlayerException.java b/src/main/java/me/skiincraft/api/paladins/exceptions/PlayerException.java index bfcbf53..d32a28f 100644 --- a/src/main/java/me/skiincraft/api/paladins/exceptions/PlayerException.java +++ b/src/main/java/me/skiincraft/api/paladins/exceptions/PlayerException.java @@ -1,22 +1,9 @@ package me.skiincraft.api.paladins.exceptions; -public class PlayerException extends Exception { +public class PlayerException extends RuntimeException { - - /** - * - */ - private static final long serialVersionUID = 1L; - private String message; - - public PlayerException(String message) { - this.message = message; - } - - @Override - public String getMessage() { - return message; + super(message); } } diff --git a/src/main/java/me/skiincraft/api/paladins/exceptions/RequestException.java b/src/main/java/me/skiincraft/api/paladins/exceptions/RequestException.java index 6d4dd46..74bc6c3 100644 --- a/src/main/java/me/skiincraft/api/paladins/exceptions/RequestException.java +++ b/src/main/java/me/skiincraft/api/paladins/exceptions/RequestException.java @@ -1,23 +1,20 @@ package me.skiincraft.api.paladins.exceptions; public class RequestException extends RuntimeException { - - /** - * - */ private static final long serialVersionUID = 1L; private String requestMessage; - - - + + public RequestException(String message) { + super(message); + } + public RequestException(String message, String retMsg) { super(message); this.requestMessage = retMsg; } - public String getRequestMessage() { return requestMessage; } diff --git a/src/main/java/me/skiincraft/api/paladins/exceptions/SearchException.java b/src/main/java/me/skiincraft/api/paladins/exceptions/SearchException.java new file mode 100644 index 0000000..e78bad2 --- /dev/null +++ b/src/main/java/me/skiincraft/api/paladins/exceptions/SearchException.java @@ -0,0 +1,9 @@ +package me.skiincraft.api.paladins.exceptions; + +public class SearchException extends RuntimeException { + + public SearchException(String message) { + super(message); + } + +} diff --git a/src/main/java/me/skiincraft/api/paladins/impl/EndpointImpl.java b/src/main/java/me/skiincraft/api/paladins/impl/EndpointImpl.java index 9cc6ea0..2487e54 100644 --- a/src/main/java/me/skiincraft/api/paladins/impl/EndpointImpl.java +++ b/src/main/java/me/skiincraft/api/paladins/impl/EndpointImpl.java @@ -35,6 +35,7 @@ import me.skiincraft.api.paladins.enums.PlayerStatus.Status; import me.skiincraft.api.paladins.enums.Queue; import me.skiincraft.api.paladins.enums.Tier; +import me.skiincraft.api.paladins.exceptions.*; import me.skiincraft.api.paladins.objects.Card; import me.skiincraft.api.paladins.objects.Place; import me.skiincraft.api.paladins.objects.SearchPlayer; @@ -87,6 +88,14 @@ public Player get() { json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new PlayerException("The requested Player does not exist, or has a private profile"); + } + play = new PlayerImpl(array.get(0).getAsJsonObject(), api); } return this.play; @@ -119,6 +128,14 @@ public SearchResults get() { json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new SearchException("No player was found."); + } + List searchPlayers = new ArrayList<>(); for (JsonElement element : array) { JsonObject object = element.getAsJsonObject(); @@ -155,7 +172,17 @@ public PlayerStatus get() { json = request.body(); - JsonObject object = new JsonParser().parse(json).getAsJsonArray().get(0).getAsJsonObject(); + JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new PlayerException("The requested Player does not exist, or has a private profile"); + } + + JsonObject object = array.get(0).getAsJsonObject(); playerStatus = new PlayerStatus(player, object.get("Match").getAsLong(), Status.getStatusById(object.get("status").getAsInt()), api); @@ -202,6 +229,14 @@ public Champions get() { HttpRequest request = HttpRequest.get(url); json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new ChampionException("The requested champions could not be found"); + } List champions = new ArrayList<>(); for (JsonElement element : array) { @@ -250,6 +285,9 @@ public Champion get() { if (!wasRequested()) { getChampions(language).getWithJson((c, j)->{ champion = c.getById(championId); + if (champion == null){ + throw new ChampionException("This requested champion does not exist."); + } json = j; }); return champion; @@ -298,6 +336,11 @@ public Champion get() { getChampions(language).getWithJson((c, j)->{ champion = c.getAsStream().filter(o -> o.getName().equalsIgnoreCase(championName)) .findAny().orElse(null); + + if (champion == null){ + throw new ChampionException("This requested champion does not exist."); + } + json = j; }); return champion; @@ -349,6 +392,14 @@ public Cards get() { json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new ChampionException("This requested champion does not exist."); + } + List cartas = new ArrayList<>(); for (JsonElement element : array) { JsonObject object = element.getAsJsonObject(); @@ -384,6 +435,15 @@ public Skins get() { json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new ChampionException("This requested champion does not exist."); + } + List skin = new ArrayList<>(); for (JsonElement element : array) { JsonObject object = element.getAsJsonObject(); @@ -424,13 +484,17 @@ public PlayerBatch get() { } }).collect(Collectors.toList()); - string.forEach(buffer::append); String url = makeUrl("getplayerbatch", new String[] { buffer.toString() }); HttpRequest request = HttpRequest.get(url); json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + List players = new ArrayList<>(); for (JsonElement element : array) { JsonObject object = element.getAsJsonObject(); @@ -448,7 +512,7 @@ public void getWithJson(BiConsumer biConsumer) { }; } - public Request getPlayerChampions(long user_id) { + public Request getPlayerChampions(long userId) { return new Request() { private PlayerChampions playerChampions; @@ -464,12 +528,20 @@ public void getWithJson(BiConsumer biConsumer) { public PlayerChampions get() { if (!wasRequested()) { - String url = makeUrl("getchampionranks", new String[] { String.valueOf(user_id) }); + String url = makeUrl("getchampionranks", new String[] { String.valueOf(userId) }); HttpRequest request = HttpRequest.get(url); json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new PlayerException("The requested Player does not exist, or has a private profile"); + } + List heeychamps = new ArrayList<>(); for (JsonElement element : array) { JsonObject object = element.getAsJsonObject(); @@ -499,6 +571,14 @@ public Friends get() { json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new SearchException("It was not possible to find the friends of the requested user, or he does not exist."); + } + List friendslist = new ArrayList<>(); for (JsonElement element : array) { JsonObject object = element.getAsJsonObject(); @@ -536,6 +616,13 @@ public Loadouts get() { json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new SearchException("It was not possible to find the loadouts of the requested user, or he does not exist."); + } List load = new ArrayList<>(); for (JsonElement element : array) { @@ -566,6 +653,14 @@ public Match get() { json = request.body(); JsonArray array = new JsonArray(); + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new MatchException("It was not possible to find this match."); + } + this.match = new MatchImpl(api, array); } return match; @@ -592,11 +687,8 @@ public List get() { StringBuilder buffer = new StringBuilder(); List string = matchbatch.stream().map(o -> { - if (ultimovalor != o) { - return o + ","; - } else { + if (ultimovalor != o) return o + ","; return o + ""; - } }).collect(Collectors.toList()); @@ -606,6 +698,14 @@ public List get() { json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new MatchException("It was not possible to find this match."); + } int num = 0; List partidas = new ArrayList<>(); @@ -643,6 +743,14 @@ public List get() { HttpRequest request = HttpRequest.get(url); json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new MatchException("It was not possible to find this match."); + } List partidas = new ArrayList<>(); for (JsonElement element : array) { @@ -677,6 +785,14 @@ public LeaderBoard get() { json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new SearchException("It was not possible to find this leaderboard."); + } + List place = new ArrayList<>(); for (JsonElement element : array) { JsonObject object = element.getAsJsonObject(); @@ -720,6 +836,14 @@ public LiveMatch get() { json = request.body(); JsonArray array = new JsonParser().parse(json).getAsJsonArray(); + if (!paladins.getAccessUtils().checkResponse(json)){ + throw new RequestException("Possibly the session is invalid. Check the session."); + } + + if (array.size() == 0){ + throw new MatchException("It was not possible to find this match."); + } + liveMatch = new LiveMatchImpl(array, api); } return liveMatch; diff --git a/src/main/java/me/skiincraft/api/paladins/utils/AccessUtils.java b/src/main/java/me/skiincraft/api/paladins/utils/AccessUtils.java index 9f36e9d..fc08726 100644 --- a/src/main/java/me/skiincraft/api/paladins/utils/AccessUtils.java +++ b/src/main/java/me/skiincraft/api/paladins/utils/AccessUtils.java @@ -16,6 +16,26 @@ public AccessUtils(int devId, String authkey) { this.devId = devId; this.authKey = authkey; } + + public boolean checkResponse(String body) { + if (body.contains("Invalid Developer Id")) { + return false; + } + + if (body.contains("Invalid session id")) { + return false; + } + + if (body.contains("Exception while validating developer access.")) { + return false; + } + + if (body.contains("Error while comparing Server and Client timestamp")) { + return false; + } + + return !body.contains("Exception - Timestamp"); + } public String getAuthKey() { return authKey;